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

Unified Diff: net/http/ntlm_buffer_writer.cc

Issue 2879353002: Add a buffer reader/writer for NTLM. (Closed)
Patch Set: Rebase Created 3 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: net/http/ntlm_buffer_writer.cc
diff --git a/net/http/ntlm_buffer_writer.cc b/net/http/ntlm_buffer_writer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2f4b0fbd29231940dc8b8bba845daff801c308c2
--- /dev/null
+++ b/net/http/ntlm_buffer_writer.cc
@@ -0,0 +1,176 @@
+// Copyright 2017 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.
+
+#include "net/http/ntlm_buffer_writer.h"
+
+#include <string.h>
+
+#include "base/logging.h"
+#include "base/strings/utf_string_conversions.h"
+
+#if defined(ARCH_CPU_LITTLE_ENDIAN)
+#define IS_LITTLE_ENDIAN 1
+#undef IS_BIG_ENDIAN
+#elif defined(ARCH_CPU_BIG_ENDIAN)
+#define IS_BIG_ENDIAN 1
+#undef IS_LITTLE_ENDIAN
+#else
+#error "Unknown endianness"
+#endif
+
+namespace net {
+
+NtlmBufferWriter::NtlmBufferWriter(size_t buffer_len)
+ : buffer_len_(buffer_len), cursor_(0) {
+ buffer_.reset(new uint8_t[buffer_len]);
+}
+
+NtlmBufferWriter::~NtlmBufferWriter() {}
+
+bool NtlmBufferWriter::CanWrite(size_t len) const {
+ if (!GetBufferPtr())
+ return false;
+
+ DCHECK_LE(GetCursor(), GetLength());
+
+ if (len == 0)
+ return true;
+
+ return (len <= GetLength()) && (GetCursor() <= GetLength() - len);
+}
+
+void NtlmBufferWriter::SetCursor(size_t cursor) {
+ DCHECK(GetBufferPtr() && cursor <= GetLength());
+
+ cursor_ = cursor;
+}
+
+bool NtlmBufferWriter::WriteUInt64(uint64_t value) {
+ return WriteUInt<uint64_t>(value);
+}
+
+bool NtlmBufferWriter::WriteUInt32(uint32_t value) {
+ return WriteUInt<uint32_t>(value);
+}
+
+bool NtlmBufferWriter::WriteUInt16(uint16_t value) {
+ return WriteUInt<uint16_t>(value);
+}
+
+bool NtlmBufferWriter::WriteFlags(ntlm::NegotiateFlags flags) {
+ return WriteUInt32(static_cast<uint32_t>(flags));
+}
+
+template <typename T>
+bool NtlmBufferWriter::WriteUInt(T value) {
+ size_t int_size = sizeof(T);
+ if (!CanWrite(int_size))
+ return false;
+
+ for (size_t i = 0; i < int_size; i++) {
+ GetBufferPtrAtCursor()[i] = static_cast<uint8_t>(value & 0xff);
+ value >>= 8;
+ }
+
+ AdvanceCursor(int_size);
+ return true;
+}
+
+bool NtlmBufferWriter::WriteBytes(const uint8_t* buffer, size_t len) {
+ if (!CanWrite(len))
+ return false;
+
+ memcpy(reinterpret_cast<void*>(GetBufferPtrAtCursor()),
+ reinterpret_cast<const void*>(buffer), len);
+
+ AdvanceCursor(len);
+ return true;
+}
+
+bool NtlmBufferWriter::WriteBytes(base::StringPiece bytes) {
+ return WriteBytes(reinterpret_cast<const uint8_t*>(bytes.data()),
+ bytes.length());
+}
+
+bool NtlmBufferWriter::WriteSecurityBuffer(ntlm::SecurityBuffer sec_buf) {
+ if (!CanWrite(ntlm::SECURITY_BUFFER_LEN))
+ return false;
+
+ bool result = WriteUInt16(sec_buf.length) && WriteUInt16(sec_buf.length) &&
+ WriteUInt32(sec_buf.offset);
+
+ DCHECK(result);
+ return result;
+}
+
+bool NtlmBufferWriter::WriteEmptySecurityBuffer() {
+ return WriteSecurityBuffer(ntlm::SecurityBuffer());
+}
+
+bool NtlmBufferWriter::WriteUtf8String(const std::string& str) {
+ return WriteBytes(reinterpret_cast<const uint8_t*>(str.c_str()),
+ str.length());
+}
+
+bool NtlmBufferWriter::WriteUtf16AsUtf8String(const base::string16& str) {
+ std::string utf8 = base::UTF16ToUTF8(str);
+ return WriteUtf8String(utf8);
+}
+
+bool NtlmBufferWriter::WriteUtf16String(const base::string16& str) {
+ size_t num_bytes = str.length() * 2;
+ if (!CanWrite(num_bytes))
+ return false;
+
+#if IS_BIG_ENDIAN
+ uint8_t* ptr = reinterpret_cast<uint8_t*>(GetBufferPtrAtCursor());
+
+ for (int i = 0; i < num_bytes; i += 2) {
+ ptr[i] = str[i / 2] % 0xff;
+ ptr[i + 1] = str[i / 2] >> 8;
+ }
+#else
+ memcpy(reinterpret_cast<void*>(GetBufferPtrAtCursor()), str.c_str(),
+ num_bytes);
+
+#endif
+
+ AdvanceCursor(num_bytes);
+ return true;
+}
+
+bool NtlmBufferWriter::WriteUtf8AsUtf16String(const std::string& str) {
+ base::string16 unicode = base::UTF8ToUTF16(str);
+ return WriteUtf16String(unicode);
+}
+
+bool NtlmBufferWriter::WriteSignature() {
+ return WriteBytes(ntlm::SIGNATURE, ntlm::SIGNATURE_LEN);
+}
+
+bool NtlmBufferWriter::WriteZeros(size_t count) {
+ if (!CanWrite(count))
+ return false;
+
+ memset(GetBufferPtrAtCursor(), 0, count);
+ AdvanceCursor(count);
+ return true;
+}
+
+bool NtlmBufferWriter::WriteMessageType(ntlm::MessageType message_type) {
+ return WriteUInt32(static_cast<uint32_t>(message_type));
+}
+
+bool NtlmBufferWriter::WriteMessageHeader(ntlm::MessageType message_type) {
+ if (!CanWrite(ntlm::SIGNATURE_LEN + sizeof(uint32_t)))
+ return false;
+
+ if (WriteSignature() && WriteMessageType(message_type))
+ return true;
+
+ NOTREACHED();
+ return false;
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698