memory stream API?

42 views
Skip to first unread message

Alpha (Hin-Chung) Lam

unread,
Aug 10, 2011, 8:57:13 AM8/10/11
to Chromium-dev, Satish Sampath, Allan Wojciechowski
Hello Chromium people,

I'm reviewing a piece of code and wonder if we have any base code that can take a memory pointer and parse it into various data types, e.g.

class MemoryInputStream {
  MemoryInputStream(const StringPiece& buffer);

  void Read(int bytes, uint8* output);
  void ReadUInt8(uint8* output);
  void ReadInt8(int8* output);

  void ReadUInt16BE(uint16* output);
  void ReadInt16BE(int16* output);

  // ... etc.. you know what I mean.
};

or write various data types to a string buffer, e.g.

class MemoryOutputStream {
  MemoryOutputStream(std::string* buffer);

  void Write(int bytes, uint8* data);
  void WriteUInt16BE(uint16* output);
  // ... yeah many of them ...
};

I've written some of these little endian / big endian code it seems to me there's more places that duplicates these functions now, e.g. media, network, crypto and even now in the speech code too.

Any suggestions here?

Alpha

Denis Lagno

unread,
Aug 10, 2011, 8:59:03 AM8/10/11
to hc...@google.com, Chromium-dev, Satish Sampath, Allan Wojciechowski
base/pickle*?

> --
> Chromium Developers mailing list: chromi...@chromium.org
> View archives, change email options, or unsubscribe:
> http://groups.google.com/a/chromium.org/group/chromium-dev
>

Alpha (Hin-Chung) Lam

unread,
Aug 10, 2011, 9:04:30 AM8/10/11
to Denis Lagno, Chromium-dev, Satish Sampath, Allan Wojciechowski
I mean size is not know..

2011年8月10日14:04 Alpha (Hin-Chung) Lam <hc...@google.com>:
There's several limitations of this class:
1. Doesn't support endianess.
2. Doesn't append to the stream if the size of now known.

Alpha

2011年8月10日13:59 Denis Lagno <dil...@chromium.org>:

Alpha (Hin-Chung) Lam

unread,
Aug 10, 2011, 9:04:12 AM8/10/11
to Denis Lagno, Chromium-dev, Satish Sampath, Allan Wojciechowski
There's several limitations of this class:
1. Doesn't support endianess.
2. Doesn't append to the stream if the size of now known.

Alpha
2011年8月10日13:59 Denis Lagno <dil...@chromium.org>:
base/pickle*?

Jonathan Dixon

unread,
Aug 10, 2011, 10:45:36 AM8/10/11
to hc...@google.com, Denis Lagno, Chromium-dev, Satish Sampath, Allan Wojciechowski
I thought I'd seen something like this in net/ but beats me. Certainly http_auth_handler_ntlm_portable.cc seems to just roll its own (for example).

Brett Wilson

unread,
Aug 10, 2011, 11:21:45 AM8/10/11
to hc...@google.com, Denis Lagno, Chromium-dev, Satish Sampath, Allan Wojciechowski
On Wed, Aug 10, 2011 at 6:04 AM, Alpha (Hin-Chung) Lam <hc...@google.com> wrote:
> There's several limitations of this class:
> 1. Doesn't support endianess.

Why do we need to support endianness in this case?

Chrome has never compiled on any big endian platform and likely never
will. We've explicitly not handled endianness. If you have code that
you know will be sensitive to endianness, write a compiler assert or
something so if we ever port, people will know to write big endian
versions of your functions.

Brett

Alpha (Hin-Chung) Lam

unread,
Aug 10, 2011, 11:24:02 AM8/10/11
to Brett Wilson, Denis Lagno, Chromium-dev, Satish Sampath, Allan Wojciechowski
What I mean here is write a be16 or be32 to a buffer or reading a be16 or be32. This is quite common in network or crypto code.

Alpha

2011年8月10日16:21 Brett Wilson <bre...@chromium.org>:

Denis Lagno

unread,
Aug 10, 2011, 11:24:24 AM8/10/11
to Brett Wilson, hc...@google.com, Chromium-dev, Satish Sampath, Allan Wojciechowski
For example, we speak websocket protocol, and it uses network order of
bytes (big endian).
The only alleviation that it is done in WebKit, not chromium itself.

Wez

unread,
Aug 10, 2011, 1:22:32 PM8/10/11
to hc...@google.com, Brett Wilson, Denis Lagno, Chromium-dev, Satish Sampath, Allan Wojciechowski
Don't most platforms provide htons() and htonl(), which take care of endian-swapping to big-endian, and will continue to work even if Chrome ends up on a big-endian platform?

Wez


Denis Lagno

unread,
Aug 10, 2011, 1:28:31 PM8/10/11
to Wez, hc...@google.com, Brett Wilson, Chromium-dev, Satish Sampath, Allan Wojciechowski
They do. Following satisfies all trybots:

#if defined(OS_WIN)
#include <winsock2.h> // for htonl
#else
#include <arpa/inet.h>
#endif

However htonl is still 32-bit, not 64-bit.

Reply all
Reply to author
Forward
0 new messages