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

Unified Diff: net/http/ntlm_buffer_writer.cc

Issue 2879353002: Add a buffer reader/writer for NTLM. (Closed)
Patch Set: Add a buffer reader/writer for NTLM. Created 3 years, 7 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..dbe6358c964e77c7fb58cc3092d167b4d98ad1a3
--- /dev/null
+++ b/net/http/ntlm_buffer_writer.cc
@@ -0,0 +1,209 @@
+// Copyright (c) 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/sys_string_conversions.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() == nullptr)
+ return false;
+
+ DCHECK(cursor_ <= GetLength());
+
+ if (len == 0)
+ return true;
+
+ return (cursor_ + len > cursor_) && (cursor_ + len <= GetLength());
+}
+
+bool NtlmBufferWriter::SetCursor(size_t cursor) {
+ if ((GetBufferPtr() == nullptr) || (cursor > GetLength()))
+ return false;
+
+ cursor_ = cursor;
+ return true;
+}
+
+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);
+}
+
+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++) {
+ GetBufferPtr()[cursor_++] = static_cast<uint8_t>(value & 0xff);
+ value >>= 8;
+ }
+
+ return true;
+}
+
+bool NtlmBufferWriter::WriteBytes(const uint8_t* buffer, size_t len) {
+ if (!CanWrite(len))
+ return false;
+
+ memcpy(reinterpret_cast<void*>(GetBufferPtr() + cursor_),
+ reinterpret_cast<const void*>(buffer), len);
+
+ cursor_ += len;
+ return true;
+}
+
+bool NtlmBufferWriter::WriteSecurityBuffer(uint16_t length, uint32_t offset) {
+ if (!CanWrite(NtlmMessage::SECURITY_BUFFER_LEN))
+ return false;
+
+ if (WriteUInt16(length) && WriteUInt16(length) && WriteUInt32(offset))
+ return true;
+
+ NOTREACHED();
+ return false;
+}
+
+bool NtlmBufferWriter::WriteEmptySecurityBuffer() {
+ return WriteSecurityBuffer(0, 0);
+}
+
+bool NtlmBufferWriter::WriteNarrowString(const std::string& str) {
+ if (!CanWrite(str.length()))
+ return false;
+
+ memcpy(reinterpret_cast<void*>(GetBufferPtr() + cursor_), str.c_str(),
+ str.length());
+ cursor_ += str.length();
+ return true;
+}
+
+bool NtlmBufferWriter::WriteString16AsUTF8(const base::string16& str) {
+ // In practice this converts it to UTF8.
+ std::string utf8 = base::SysWideToNativeMB(base::UTF16ToWide(str));
+ return WriteNarrowString(utf8);
+}
+
+bool NtlmBufferWriter::WriteUnicodeString(const base::string16& str) {
+ size_t num_bytes = str.length() * 2;
+ if (!CanWrite(num_bytes))
+ return false;
+
+ if (!WriteUnicodeStringIntoBuffer(str, GetBufferPtr() + cursor_, num_bytes))
+ return false;
+
+ cursor_ += num_bytes;
+ return true;
+}
+
+bool NtlmBufferWriter::WriteNarrowStringAsString16(const std::string& str) {
+ base::string16 unicode;
+ unicode.assign(str.begin(), str.end());
+ return WriteUnicodeString(unicode);
+}
+
+bool NtlmBufferWriter::WriteUnicodeStringIntoBuffer(const base::string16& str,
+ uint8_t* buffer,
+ size_t buffer_len) {
+ // TODO: static??
+ size_t num_bytes = str.length() * 2;
+ if (num_bytes > buffer_len)
+ return false;
+
+#if IS_BIG_ENDIAN
+ uint8_t* ptr = reinterpret_cast<uint8_t*>(GetBufferPtr() + cursor_);
+
+ 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*>(GetBufferPtr() + cursor_), str.c_str(),
+ num_bytes);
+
+#endif
+ return true;
+}
+
+bool NtlmBufferWriter::WriteSignature() {
+ return WriteBytes(NtlmMessage::SIGNATURE, NtlmMessage::SIGNATURE_LEN);
+}
+
+bool NtlmBufferWriter::WriteZeros(size_t count) {
+ if (!CanWrite(count))
+ return false;
+
+ memset(GetBufferPtr() + cursor_, 0, count);
+ cursor_ += count;
+ return true;
+}
+
+bool NtlmBufferWriter::WriteMessageType(NtlmMessage::MessageType message_type) {
+ return WriteUInt32(message_type);
+}
+
+bool NtlmBufferWriter::WriteMessageHeader(
+ NtlmMessage::MessageType message_type) {
+ if (!CanWrite(NtlmMessage::SIGNATURE_LEN + sizeof(uint32_t)))
+ return false;
+
+ if (WriteSignature() && WriteMessageType(message_type))
+ return true;
+
+ NOTREACHED();
+ return false;
+}
+
+// static
+size_t NtlmBufferWriter::GetStringPayloadLength(const base::string16& str,
+ bool is_unicode) {
+ if (is_unicode)
+ return str.length() * 2;
+
+ // When |WriteString16AsUTF8| is called with
+ // a |base::string16| the string is effectively converted to
+ // UTF8 by |base::SysWideToNativeMB|. Do the conversion
+ // to ensure that the character count is correct.
+ return base::SysWideToNativeMB(base::UTF16ToWide(str)).length();
+}
+
+// static
+size_t NtlmBufferWriter::GetStringPayloadLength(const std::string& str,
+ bool is_unicode) {
+ // When |WriteUnicodePayload| is called with a |std::string|
+ // the string is assumed ASCII and the characters are zero-padded.
+ return is_unicode ? str.length() * 2 : str.length();
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698