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

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

Powered by Google App Engine
This is Rietveld 408576698