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

Side by Side Diff: net/base/bigendian.h

Issue 8762001: Isolates generic DnsClient from AsyncHostResolver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years 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 | « no previous file | net/base/bigendian.cc » ('j') | net/base/bigendian.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef NET_BASE_BIGENDIAN_H_
6 #define NET_BASE_BIGENDIAN_H_
7 #pragma once
8
9 #include "base/string_piece.h"
10 #include "net/base/net_export.h"
11
12 namespace net {
13
14 // Read an integer (signed or unsigned) from |buf| in Big Endian order.
15 // Note: this loop is unrolled with -O1 and above.
16 // NOTE(szym): glibc dns-canon.c and SpdyFrameBuilder use
17 // ntohs(*(uint16_t*)ptr) which is potentially unaligned.
18 // This would cause SIGBUS on ARMv5 or earlier and ARMv6-M.
19 template<typename T>
20 inline void ReadBigEndian(const char buf[], T* out) {
21 *out = buf[0];
22 for (size_t i = 1; i < sizeof(T); ++i) {
23 *out <<= 8;
24 // Must cast to uint8 to avoid clobbering by sign extension.
25 *out |= static_cast<uint8>(buf[i]);
26 }
27 }
28
29 // Write an integer (signed or unsigned) |val| to |buf| in Big Endian order.
30 // Note: this loop is unrolled with -O1 and above.
31 template<typename T>
32 inline void WriteBigEndian(char buf[], T val) {
33 for (size_t i = 0; i < sizeof(T); ++i) {
34 buf[sizeof(T)-i-1] = static_cast<char>(val & 0xFF);
35 val >>= 8;
36 }
37 }
38
39 // Specializations to make clang happy about the (dead code) shifts above.
40 template<>
41 inline void ReadBigEndian<uint8>(const char buf[], uint8* out) {
42 *out = buf[0];
43 }
44
45 template<>
46 inline void WriteBigEndian<uint8>(char buf[], uint8 val) {
47 buf[0] = static_cast<char>(val);
48 }
49
50 // Allows reading integers in network order (big endian) while iterating over
51 // an underlying buffer.
52 class NET_EXPORT BigEndianReader {
53 public:
54 BigEndianReader(const void* buf, size_t len);
55 const char* ptr() const { return ptr_; }
56 int remaining() const { return end_ - ptr_; }
mmenke 2011/12/02 00:53:55 Could you add a couple linebreaks here, to increas
mmenke 2011/12/02 01:19:57 Oops. That should be above Copy.
szym 2011/12/05 23:06:28 Done.
57 bool Skip(size_t len);
58 bool Copy(void* out, size_t len);
mmenke 2011/12/02 00:53:55 I believe this can be const.
mmenke 2011/12/02 00:53:55 Think a comment that states that all of these func
szym 2011/12/05 23:06:28 Done.
59 // Creates a StringPiece in |out| that points to the underlying buffer.
60 bool Piece(base::StringPiece* out, size_t len);
mmenke 2011/12/02 00:53:55 Google style is to use verbs for function names.
szym 2011/12/05 23:06:28 Done.
61 bool U8(uint8* v);
mmenke 2011/12/02 00:53:55 Google style discourages overly abbreviated variab
szym 2011/12/05 23:06:28 Done.
62 bool U16(uint16* v);
63 bool U32(uint32* v);
64 private:
mmenke 2011/12/02 00:53:55 nit: Google style is to put a blank line before t
szym 2011/12/05 23:06:28 Done.
65 // Hidden to promote type safety.
66 template<typename T>
67 bool Read(T* v);
68
69 const char* ptr_;
70 const char* end_;
mmenke 2011/12/02 00:53:55 Not vital, but could make both |end_| pointers cou
szym 2011/12/05 23:06:28 Left it non-const to allow the compiler to generat
71 };
72
73 // Allows writing integers in network order (big endian) while iterating over
74 // an underlying buffer.
75 class NET_EXPORT BigEndianWriter {
76 public:
77 BigEndianWriter(void* buf, size_t len);
78 char* ptr() const { return ptr_; }
79 int remaining() const { return end_ - ptr_; }
80 bool Skip(size_t len);
81 bool Copy(const void* buf, size_t len);
mmenke 2011/12/02 00:53:55 Think the name of this function is a little odd.
szym 2011/12/05 23:06:28 Done. I like WriteBytes, although it's meant to wr
82 bool U8(uint8 v);
83 bool U16(uint16 v);
84 bool U32(uint32 v);
85 private:
mmenke 2011/12/02 00:53:55 nit: Missing blank line.
szym 2011/12/05 23:06:28 Done.
86 // Hidden to promote type safety.
87 template<typename T>
88 bool Write(T v);
89
90 char* ptr_;
91 char* end_;
92 };
93
94 } // namespace net
95
96 #endif // NET_BASE_BIGENDIAN_H_
97
OLDNEW
« no previous file with comments | « no previous file | net/base/bigendian.cc » ('j') | net/base/bigendian.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698