Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(997)

Side by Side Diff: base/big_endian.h

Issue 145873006: ui/base/resource: Roll our own version of ReadBigEndian() function. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: base/big_endian Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/base.gypi ('k') | base/big_endian.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef NET_BASE_BIG_ENDIAN_H_ 5 #ifndef BASE_BIG_ENDIAN_H_
6 #define NET_BASE_BIG_ENDIAN_H_ 6 #define BASE_BIG_ENDIAN_H_
7 7
8 #include "base/base_export.h"
8 #include "base/basictypes.h" 9 #include "base/basictypes.h"
9 #include "base/strings/string_piece.h" 10 #include "base/strings/string_piece.h"
10 #include "net/base/net_export.h"
11 11
12 namespace net { 12 namespace base {
13 13
14 // Read an integer (signed or unsigned) from |buf| in Big Endian order. 14 // Read an integer (signed or unsigned) from |buf| in Big Endian order.
15 // Note: this loop is unrolled with -O1 and above. 15 // Note: this loop is unrolled with -O1 and above.
16 // NOTE(szym): glibc dns-canon.c and SpdyFrameBuilder use 16 // NOTE(szym): glibc dns-canon.c and SpdyFrameBuilder use
17 // ntohs(*(uint16_t*)ptr) which is potentially unaligned. 17 // ntohs(*(uint16_t*)ptr) which is potentially unaligned.
18 // This would cause SIGBUS on ARMv5 or earlier and ARMv6-M. 18 // This would cause SIGBUS on ARMv5 or earlier and ARMv6-M.
19 template<typename T> 19 template<typename T>
20 inline void ReadBigEndian(const char buf[], T* out) { 20 inline void ReadBigEndian(const char buf[], T* out) {
21 *out = buf[0]; 21 *out = buf[0];
22 for (size_t i = 1; i < sizeof(T); ++i) { 22 for (size_t i = 1; i < sizeof(T); ++i) {
(...skipping 19 matching lines...) Expand all
42 *out = buf[0]; 42 *out = buf[0];
43 } 43 }
44 44
45 template<> 45 template<>
46 inline void WriteBigEndian<uint8>(char buf[], uint8 val) { 46 inline void WriteBigEndian<uint8>(char buf[], uint8 val) {
47 buf[0] = static_cast<char>(val); 47 buf[0] = static_cast<char>(val);
48 } 48 }
49 49
50 // Allows reading integers in network order (big endian) while iterating over 50 // Allows reading integers in network order (big endian) while iterating over
51 // an underlying buffer. All the reading functions advance the internal pointer. 51 // an underlying buffer. All the reading functions advance the internal pointer.
52 class NET_EXPORT BigEndianReader { 52 class BASE_EXPORT BigEndianReader {
53 public: 53 public:
54 BigEndianReader(const void* buf, size_t len); 54 BigEndianReader(const void* buf, size_t len);
Mark Mentovai 2014/02/18 17:14:30 I would be more comfortable if buf were typed (con
tfarina 2014/02/23 03:45:01 Done.
55 55
56 const char* ptr() const { return ptr_; } 56 const char* ptr() const { return ptr_; }
57 int remaining() const { return end_ - ptr_; } 57 int remaining() const { return end_ - ptr_; }
58 58
59 bool Skip(size_t len); 59 bool Skip(size_t len);
60 bool ReadBytes(void* out, size_t len); 60 bool ReadBytes(void* out, size_t len);
61 // Creates a StringPiece in |out| that points to the underlying buffer. 61 // Creates a StringPiece in |out| that points to the underlying buffer.
62 bool ReadPiece(base::StringPiece* out, size_t len); 62 bool ReadPiece(base::StringPiece* out, size_t len);
63 bool ReadU8(uint8* value); 63 bool ReadU8(uint8* value);
64 bool ReadU16(uint16* value); 64 bool ReadU16(uint16* value);
65 bool ReadU32(uint32* value); 65 bool ReadU32(uint32* value);
66 66
67 private: 67 private:
68 // Hidden to promote type safety. 68 // Hidden to promote type safety.
69 template<typename T> 69 template<typename T>
70 bool Read(T* v); 70 bool Read(T* v);
71 71
72 const char* ptr_; 72 const char* ptr_;
73 const char* end_; 73 const char* end_;
74 }; 74 };
75 75
76 // Allows writing integers in network order (big endian) while iterating over 76 // Allows writing integers in network order (big endian) while iterating over
77 // an underlying buffer. All the writing functions advance the internal pointer. 77 // an underlying buffer. All the writing functions advance the internal pointer.
78 class NET_EXPORT BigEndianWriter { 78 class BASE_EXPORT BigEndianWriter {
79 public: 79 public:
80 BigEndianWriter(void* buf, size_t len); 80 BigEndianWriter(void* buf, size_t len);
81 81
82 char* ptr() const { return ptr_; } 82 char* ptr() const { return ptr_; }
83 int remaining() const { return end_ - ptr_; } 83 int remaining() const { return end_ - ptr_; }
84 84
85 bool Skip(size_t len); 85 bool Skip(size_t len);
86 bool WriteBytes(const void* buf, size_t len); 86 bool WriteBytes(const void* buf, size_t len);
87 bool WriteU8(uint8 value); 87 bool WriteU8(uint8 value);
88 bool WriteU16(uint16 value); 88 bool WriteU16(uint16 value);
89 bool WriteU32(uint32 value); 89 bool WriteU32(uint32 value);
90 90
91 private: 91 private:
92 // Hidden to promote type safety. 92 // Hidden to promote type safety.
93 template<typename T> 93 template<typename T>
94 bool Write(T v); 94 bool Write(T v);
95 95
96 char* ptr_; 96 char* ptr_;
97 char* end_; 97 char* end_;
98 }; 98 };
99 99
100 } // namespace net 100 } // namespace base
101 101
102 #endif // NET_BASE_BIG_ENDIAN_H_ 102 #endif // BASE_BIG_ENDIAN_H_
103
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/big_endian.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698