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_reader.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/logging.h" | |
10 | |
11 namespace net { | |
12 namespace ntlm { | |
13 | |
14 NtlmBufferReader::NtlmBufferReader(base::StringPiece buffer) | |
15 : buffer_(buffer), cursor_(0) { | |
16 DCHECK(buffer.data()); | |
17 } | |
18 | |
19 NtlmBufferReader::NtlmBufferReader(const uint8_t* ptr, size_t len) | |
20 : NtlmBufferReader( | |
21 base::StringPiece(reinterpret_cast<const char*>(ptr), len)) {} | |
22 | |
23 NtlmBufferReader::~NtlmBufferReader() {} | |
24 | |
25 bool NtlmBufferReader::CanRead(size_t len) const { | |
26 return CanReadFrom(GetCursor(), len); | |
27 } | |
28 | |
29 bool NtlmBufferReader::CanReadFrom(size_t offset, size_t len) const { | |
30 if (len == 0) | |
31 return true; | |
32 | |
33 return (len <= GetLength() && offset <= GetLength() - len); | |
34 } | |
35 | |
36 void NtlmBufferReader::SetCursor(size_t cursor) { | |
37 DCHECK_LE(cursor, GetLength()); | |
38 | |
39 cursor_ = cursor; | |
40 } | |
41 | |
42 bool NtlmBufferReader::ReadUInt16(uint16_t* value) { | |
43 return ReadUInt<uint16_t>(value); | |
44 } | |
45 | |
46 bool NtlmBufferReader::ReadUInt32(uint32_t* value) { | |
47 return ReadUInt<uint32_t>(value); | |
48 } | |
49 | |
50 bool NtlmBufferReader::ReadUInt64(uint64_t* value) { | |
51 return ReadUInt<uint64_t>(value); | |
52 } | |
53 | |
54 bool NtlmBufferReader::ReadFlags(NegotiateFlags* flags) { | |
55 uint32_t raw; | |
56 if (!ReadUInt32(&raw)) | |
57 return false; | |
58 | |
59 *flags = static_cast<NegotiateFlags>(raw); | |
60 return true; | |
61 } | |
62 | |
63 template <typename T> | |
64 bool NtlmBufferReader::ReadUInt(T* value) { | |
Ryan Sleevi
2017/07/10 15:07:56
Please keep the order of declarations and definiti
zentaro
2017/07/11 14:00:29
Done. Here and the writer.
| |
65 size_t int_size = sizeof(T); | |
66 if (!CanRead(int_size)) | |
67 return false; | |
68 | |
69 *value = 0; | |
70 for (size_t i = 0; i < int_size; i++) { | |
71 *value += static_cast<T>(GetByteAtCursor()) << (i * 8); | |
72 AdvanceCursor(1); | |
73 } | |
74 | |
75 return true; | |
76 } | |
77 | |
78 bool NtlmBufferReader::ReadBytes(uint8_t* buffer, size_t len) { | |
79 if (!CanRead(len)) | |
80 return false; | |
81 | |
82 memcpy(reinterpret_cast<void*>(buffer), | |
83 reinterpret_cast<const void*>(GetBufferAtCursor()), len); | |
84 | |
85 AdvanceCursor(len); | |
86 return true; | |
87 } | |
88 | |
89 bool NtlmBufferReader::ReadBytesFrom(const SecurityBuffer& sec_buf, | |
90 uint8_t* buffer) { | |
91 if (!CanReadFrom(sec_buf)) | |
92 return false; | |
93 | |
94 memcpy(reinterpret_cast<void*>(buffer), | |
95 reinterpret_cast<const void*>(GetBufferPtr() + sec_buf.offset), | |
96 sec_buf.length); | |
97 | |
98 return true; | |
99 } | |
100 | |
101 bool NtlmBufferReader::ReadSecurityBuffer(SecurityBuffer* sec_buf) { | |
102 return ReadUInt16(&sec_buf->length) && SkipBytes(sizeof(uint16_t)) && | |
103 ReadUInt32(&sec_buf->offset); | |
104 } | |
105 | |
106 bool NtlmBufferReader::ReadMessageType(MessageType* message_type) { | |
107 uint32_t raw_message_type; | |
108 if (!ReadUInt32(&raw_message_type)) | |
109 return false; | |
110 | |
111 *message_type = static_cast<MessageType>(raw_message_type); | |
112 | |
113 if (*message_type != MessageType::NEGOTIATE && | |
114 *message_type != MessageType::CHALLENGE && | |
115 *message_type != MessageType::AUTHENTICATE) | |
116 return false; | |
117 | |
118 return true; | |
119 } | |
120 | |
121 bool NtlmBufferReader::SkipSecurityBuffer() { | |
122 return SkipBytes(SECURITY_BUFFER_LEN); | |
123 } | |
124 | |
125 bool NtlmBufferReader::SkipSecurityBufferWithValidation() { | |
126 SecurityBuffer sec_buf; | |
127 return ReadSecurityBuffer(&sec_buf) && CanReadFrom(sec_buf); | |
128 } | |
129 | |
130 bool NtlmBufferReader::SkipBytes(size_t count) { | |
131 if (!CanRead(count)) | |
132 return false; | |
133 | |
134 AdvanceCursor(count); | |
135 return true; | |
136 } | |
137 | |
138 bool NtlmBufferReader::MatchSignature() { | |
139 if (!CanRead(SIGNATURE_LEN)) | |
140 return false; | |
141 | |
142 if (memcmp(SIGNATURE, GetBufferAtCursor(), SIGNATURE_LEN) != 0) | |
143 return false; | |
144 | |
145 AdvanceCursor(SIGNATURE_LEN); | |
146 return true; | |
147 } | |
148 | |
149 bool NtlmBufferReader::MatchMessageType(MessageType message_type) { | |
150 MessageType actual_message_type; | |
151 return ReadMessageType(&actual_message_type) && | |
152 (actual_message_type == message_type); | |
153 } | |
154 | |
155 bool NtlmBufferReader::MatchMessageHeader(MessageType message_type) { | |
156 return MatchSignature() && MatchMessageType(message_type); | |
157 } | |
158 | |
159 bool NtlmBufferReader::MatchZeros(size_t count) { | |
160 if (!CanRead(count)) | |
161 return false; | |
162 | |
163 for (size_t i = 0; i < count; i++) { | |
164 if (GetBufferAtCursor()[i] != 0) | |
165 return false; | |
166 } | |
167 | |
168 AdvanceCursor(count); | |
169 return true; | |
170 } | |
171 | |
172 bool NtlmBufferReader::MatchEmptySecurityBuffer() { | |
173 SecurityBuffer sec_buf; | |
174 return ReadSecurityBuffer(&sec_buf) && (sec_buf.offset <= GetLength()) && | |
175 (sec_buf.length == 0); | |
176 } | |
177 | |
178 } // namespace ntlm | |
179 } // namespace net | |
OLD | NEW |