diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-26 18:14:57 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-26 18:14:57 +0000 |
commit | d9806a97728e34d79a34d2d7a04dfa6048ad048c (patch) | |
tree | 8c71ff87111e177731fd02101f05bdd354cb336d /base/big_endian.h | |
parent | 9ea8ec9f63db0b2473b4a84648cebce4e0bfcf93 (diff) | |
download | chromium_src-d9806a97728e34d79a34d2d7a04dfa6048ad048c.zip chromium_src-d9806a97728e34d79a34d2d7a04dfa6048ad048c.tar.gz chromium_src-d9806a97728e34d79a34d2d7a04dfa6048ad048c.tar.bz2 |
ui/base/resource: Remove dependency on net's big_endian implementation.
To remove this dependency we ended up moving big_endian* to base/, since besides ui/,
big_endian is also used by other top-level modules: chrome/utility/, cloud_print, media/cast
This way we removed one more net dependency from ui/base/.
BUG=299841
TEST=ui_unittests
R=tony@chromium.org,mark@chromium.org,hclam@chromium.org,vitalybuka@chromium.org
TBR=ben@chromium.org
Review URL: https://codereview.chromium.org/145873006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@253510 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/big_endian.h')
-rw-r--r-- | base/big_endian.h | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/base/big_endian.h b/base/big_endian.h new file mode 100644 index 0000000..35df5e7 --- /dev/null +++ b/base/big_endian.h @@ -0,0 +1,102 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_BIG_ENDIAN_H_ +#define BASE_BIG_ENDIAN_H_ + +#include "base/base_export.h" +#include "base/basictypes.h" +#include "base/strings/string_piece.h" + +namespace base { + +// Read an integer (signed or unsigned) from |buf| in Big Endian order. +// Note: this loop is unrolled with -O1 and above. +// NOTE(szym): glibc dns-canon.c and SpdyFrameBuilder use +// ntohs(*(uint16_t*)ptr) which is potentially unaligned. +// This would cause SIGBUS on ARMv5 or earlier and ARMv6-M. +template<typename T> +inline void ReadBigEndian(const char buf[], T* out) { + *out = buf[0]; + for (size_t i = 1; i < sizeof(T); ++i) { + *out <<= 8; + // Must cast to uint8 to avoid clobbering by sign extension. + *out |= static_cast<uint8>(buf[i]); + } +} + +// Write an integer (signed or unsigned) |val| to |buf| in Big Endian order. +// Note: this loop is unrolled with -O1 and above. +template<typename T> +inline void WriteBigEndian(char buf[], T val) { + for (size_t i = 0; i < sizeof(T); ++i) { + buf[sizeof(T)-i-1] = static_cast<char>(val & 0xFF); + val >>= 8; + } +} + +// Specializations to make clang happy about the (dead code) shifts above. +template<> +inline void ReadBigEndian<uint8>(const char buf[], uint8* out) { + *out = buf[0]; +} + +template<> +inline void WriteBigEndian<uint8>(char buf[], uint8 val) { + buf[0] = static_cast<char>(val); +} + +// Allows reading integers in network order (big endian) while iterating over +// an underlying buffer. All the reading functions advance the internal pointer. +class BASE_EXPORT BigEndianReader { + public: + BigEndianReader(const char* buf, size_t len); + + const char* ptr() const { return ptr_; } + int remaining() const { return end_ - ptr_; } + + bool Skip(size_t len); + bool ReadBytes(void* out, size_t len); + // Creates a StringPiece in |out| that points to the underlying buffer. + bool ReadPiece(base::StringPiece* out, size_t len); + bool ReadU8(uint8* value); + bool ReadU16(uint16* value); + bool ReadU32(uint32* value); + + private: + // Hidden to promote type safety. + template<typename T> + bool Read(T* v); + + const char* ptr_; + const char* end_; +}; + +// Allows writing integers in network order (big endian) while iterating over +// an underlying buffer. All the writing functions advance the internal pointer. +class BASE_EXPORT BigEndianWriter { + public: + BigEndianWriter(char* buf, size_t len); + + char* ptr() const { return ptr_; } + int remaining() const { return end_ - ptr_; } + + bool Skip(size_t len); + bool WriteBytes(const void* buf, size_t len); + bool WriteU8(uint8 value); + bool WriteU16(uint16 value); + bool WriteU32(uint32 value); + + private: + // Hidden to promote type safety. + template<typename T> + bool Write(T v); + + char* ptr_; + char* end_; +}; + +} // namespace base + +#endif // BASE_BIG_ENDIAN_H_ |