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