OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #include "net/ntlm/ntlm_buffer_writer.h" |
| 6 |
| 7 #include <string.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "build/build_config.h" |
| 12 |
| 13 namespace net { |
| 14 namespace ntlm { |
| 15 |
| 16 NtlmBufferWriter::NtlmBufferWriter(size_t buffer_len) |
| 17 : buffer_len_(buffer_len), cursor_(0) { |
| 18 buffer_.reset(new uint8_t[buffer_len]()); |
| 19 } |
| 20 |
| 21 NtlmBufferWriter::~NtlmBufferWriter() {} |
| 22 |
| 23 bool NtlmBufferWriter::CanWrite(size_t len) const { |
| 24 if (!GetBufferPtr()) |
| 25 return false; |
| 26 |
| 27 DCHECK_LE(GetCursor(), GetLength()); |
| 28 |
| 29 if (len == 0) |
| 30 return true; |
| 31 |
| 32 return (len <= GetLength()) && (GetCursor() <= GetLength() - len); |
| 33 } |
| 34 |
| 35 void NtlmBufferWriter::SetCursor(size_t cursor) { |
| 36 DCHECK(GetBufferPtr() && cursor <= GetLength()); |
| 37 |
| 38 cursor_ = cursor; |
| 39 } |
| 40 |
| 41 bool NtlmBufferWriter::WriteUInt64(uint64_t value) { |
| 42 return WriteUInt<uint64_t>(value); |
| 43 } |
| 44 |
| 45 bool NtlmBufferWriter::WriteUInt32(uint32_t value) { |
| 46 return WriteUInt<uint32_t>(value); |
| 47 } |
| 48 |
| 49 bool NtlmBufferWriter::WriteUInt16(uint16_t value) { |
| 50 return WriteUInt<uint16_t>(value); |
| 51 } |
| 52 |
| 53 bool NtlmBufferWriter::WriteFlags(NegotiateFlags flags) { |
| 54 return WriteUInt32(static_cast<uint32_t>(flags)); |
| 55 } |
| 56 |
| 57 template <typename T> |
| 58 bool NtlmBufferWriter::WriteUInt(T value) { |
| 59 size_t int_size = sizeof(T); |
| 60 if (!CanWrite(int_size)) |
| 61 return false; |
| 62 |
| 63 for (size_t i = 0; i < int_size; i++) { |
| 64 GetBufferPtrAtCursor()[i] = static_cast<uint8_t>(value & 0xff); |
| 65 value >>= 8; |
| 66 } |
| 67 |
| 68 AdvanceCursor(int_size); |
| 69 return true; |
| 70 } |
| 71 |
| 72 bool NtlmBufferWriter::WriteBytes(const uint8_t* buffer, size_t len) { |
| 73 if (!CanWrite(len)) |
| 74 return false; |
| 75 |
| 76 memcpy(reinterpret_cast<void*>(GetBufferPtrAtCursor()), |
| 77 reinterpret_cast<const void*>(buffer), len); |
| 78 |
| 79 AdvanceCursor(len); |
| 80 return true; |
| 81 } |
| 82 |
| 83 bool NtlmBufferWriter::WriteBytes(base::StringPiece bytes) { |
| 84 return WriteBytes(reinterpret_cast<const uint8_t*>(bytes.data()), |
| 85 bytes.length()); |
| 86 } |
| 87 |
| 88 bool NtlmBufferWriter::WriteSecurityBuffer(SecurityBuffer sec_buf) { |
| 89 return WriteUInt16(sec_buf.length) && WriteUInt16(sec_buf.length) && |
| 90 WriteUInt32(sec_buf.offset); |
| 91 } |
| 92 |
| 93 bool NtlmBufferWriter::WriteUtf8String(const std::string& str) { |
| 94 return WriteBytes(reinterpret_cast<const uint8_t*>(str.c_str()), |
| 95 str.length()); |
| 96 } |
| 97 |
| 98 bool NtlmBufferWriter::WriteUtf16AsUtf8String(const base::string16& str) { |
| 99 std::string utf8 = base::UTF16ToUTF8(str); |
| 100 return WriteUtf8String(utf8); |
| 101 } |
| 102 |
| 103 bool NtlmBufferWriter::WriteUtf16String(const base::string16& str) { |
| 104 size_t num_bytes = str.length() * 2; |
| 105 if (!CanWrite(num_bytes)) |
| 106 return false; |
| 107 |
| 108 #if defined(ARCH_CPU_BIG_ENDIAN) |
| 109 uint8_t* ptr = reinterpret_cast<uint8_t*>(GetBufferPtrAtCursor()); |
| 110 |
| 111 for (int i = 0; i < num_bytes; i += 2) { |
| 112 ptr[i] = str[i / 2] & 0xff; |
| 113 ptr[i + 1] = str[i / 2] >> 8; |
| 114 } |
| 115 #else |
| 116 memcpy(reinterpret_cast<void*>(GetBufferPtrAtCursor()), str.c_str(), |
| 117 num_bytes); |
| 118 |
| 119 #endif |
| 120 |
| 121 AdvanceCursor(num_bytes); |
| 122 return true; |
| 123 } |
| 124 |
| 125 bool NtlmBufferWriter::WriteUtf8AsUtf16String(const std::string& str) { |
| 126 base::string16 unicode = base::UTF8ToUTF16(str); |
| 127 return WriteUtf16String(unicode); |
| 128 } |
| 129 |
| 130 bool NtlmBufferWriter::WriteSignature() { |
| 131 return WriteBytes(SIGNATURE, SIGNATURE_LEN); |
| 132 } |
| 133 |
| 134 bool NtlmBufferWriter::WriteZeros(size_t count) { |
| 135 if (!CanWrite(count)) |
| 136 return false; |
| 137 |
| 138 memset(GetBufferPtrAtCursor(), 0, count); |
| 139 AdvanceCursor(count); |
| 140 return true; |
| 141 } |
| 142 |
| 143 bool NtlmBufferWriter::WriteMessageType(MessageType message_type) { |
| 144 return WriteUInt32(static_cast<uint32_t>(message_type)); |
| 145 } |
| 146 |
| 147 bool NtlmBufferWriter::WriteMessageHeader(MessageType message_type) { |
| 148 return WriteSignature() && WriteMessageType(message_type); |
| 149 } |
| 150 |
| 151 } // namespace ntlm |
| 152 } // namespace net |
OLD | NEW |