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

Unified Diff: net/base/bigendian.h

Issue 8762001: Isolates generic DnsClient from AsyncHostResolver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix out-of-bounds access; more sensible error codes Created 9 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | net/base/bigendian.cc » ('j') | net/dns/dns_protocol.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/bigendian.h
diff --git a/net/base/bigendian.h b/net/base/bigendian.h
new file mode 100644
index 0000000000000000000000000000000000000000..a834c4fee0448ca605141149c382a2b054a519b0
--- /dev/null
+++ b/net/base/bigendian.h
@@ -0,0 +1,97 @@
+// Copyright (c) 2011 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 NET_BASE_BIGENDIAN_H_
mmenke 2011/12/02 00:53:56 nit: This should be named big_endian.h
szym 2011/12/05 23:06:28 Done.
+#define NET_BASE_BIGENDIAN_H_
+#pragma once
+
+#include "base/string_piece.h"
+#include "net/base/net_export.h"
+
+namespace net {
+
+// 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.
+class NET_EXPORT BigEndianReader {
+ public:
+ BigEndianReader(const void* buf, size_t len);
+ const char* ptr() const { return ptr_; }
+ int remaining() const { return end_ - ptr_; }
+ bool Skip(size_t len);
+ bool Copy(void* out, size_t len);
+ // Creates a StringPiece in |out| that points to the underlying buffer.
+ bool Piece(base::StringPiece* out, size_t len);
+ bool U8(uint8* v);
+ bool U16(uint16* v);
+ bool U32(uint32* v);
+ 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.
+class NET_EXPORT BigEndianWriter {
+ public:
+ BigEndianWriter(void* buf, size_t len);
+ char* ptr() const { return ptr_; }
+ int remaining() const { return end_ - ptr_; }
+ bool Skip(size_t len);
+ bool Copy(const void* buf, size_t len);
+ bool U8(uint8 v);
+ bool U16(uint16 v);
+ bool U32(uint32 v);
+ private:
+ // Hidden to promote type safety.
+ template<typename T>
+ bool Write(T v);
+
+ char* ptr_;
+ char* end_;
+};
+
+} // namespace net
+
+#endif // NET_BASE_BIGENDIAN_H_
+
« no previous file with comments | « no previous file | net/base/bigendian.cc » ('j') | net/dns/dns_protocol.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698