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

Side by Side Diff: net/ntlm/ntlm_buffer_writer.cc

Issue 2879353002: Add a buffer reader/writer for NTLM. (Closed)
Patch Set: Remove ReleaseBufferPtr Created 3 years, 5 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
« no previous file with comments | « net/ntlm/ntlm_buffer_writer.h ('k') | net/ntlm/ntlm_buffer_writer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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 bool NtlmBufferWriter::WriteUInt16(uint16_t value) {
36 return WriteUInt<uint16_t>(value);
37 }
38
39 bool NtlmBufferWriter::WriteUInt32(uint32_t value) {
40 return WriteUInt<uint32_t>(value);
41 }
42
43 bool NtlmBufferWriter::WriteUInt64(uint64_t value) {
44 return WriteUInt<uint64_t>(value);
45 }
46
47 bool NtlmBufferWriter::WriteFlags(NegotiateFlags flags) {
48 return WriteUInt32(static_cast<uint32_t>(flags));
49 }
50
51 bool NtlmBufferWriter::WriteBytes(const uint8_t* buffer, size_t len) {
52 if (!CanWrite(len))
53 return false;
54
55 memcpy(reinterpret_cast<void*>(GetBufferPtrAtCursor()),
56 reinterpret_cast<const void*>(buffer), len);
57
58 AdvanceCursor(len);
59 return true;
60 }
61
62 bool NtlmBufferWriter::WriteBytes(base::StringPiece bytes) {
63 return WriteBytes(reinterpret_cast<const uint8_t*>(bytes.data()),
64 bytes.length());
65 }
66
67 bool NtlmBufferWriter::WriteZeros(size_t count) {
68 if (!CanWrite(count))
69 return false;
70
71 memset(GetBufferPtrAtCursor(), 0, count);
72 AdvanceCursor(count);
73 return true;
74 }
75
76 bool NtlmBufferWriter::WriteSecurityBuffer(SecurityBuffer sec_buf) {
77 return WriteUInt16(sec_buf.length) && WriteUInt16(sec_buf.length) &&
78 WriteUInt32(sec_buf.offset);
79 }
80
81 bool NtlmBufferWriter::WriteUtf8String(const std::string& str) {
82 return WriteBytes(reinterpret_cast<const uint8_t*>(str.c_str()),
83 str.length());
84 }
85
86 bool NtlmBufferWriter::WriteUtf16AsUtf8String(const base::string16& str) {
87 std::string utf8 = base::UTF16ToUTF8(str);
88 return WriteUtf8String(utf8);
89 }
90
91 bool NtlmBufferWriter::WriteUtf8AsUtf16String(const std::string& str) {
92 base::string16 unicode = base::UTF8ToUTF16(str);
93 return WriteUtf16String(unicode);
94 }
95
96 bool NtlmBufferWriter::WriteUtf16String(const base::string16& str) {
97 size_t num_bytes = str.length() * 2;
98 if (!CanWrite(num_bytes))
99 return false;
100
101 #if defined(ARCH_CPU_BIG_ENDIAN)
102 uint8_t* ptr = reinterpret_cast<uint8_t*>(GetBufferPtrAtCursor());
103
104 for (int i = 0; i < num_bytes; i += 2) {
105 ptr[i] = str[i / 2] & 0xff;
106 ptr[i + 1] = str[i / 2] >> 8;
107 }
108 #else
109 memcpy(reinterpret_cast<void*>(GetBufferPtrAtCursor()), str.c_str(),
110 num_bytes);
111
112 #endif
113
114 AdvanceCursor(num_bytes);
115 return true;
116 }
117
118 bool NtlmBufferWriter::WriteSignature() {
119 return WriteBytes(kSignature, kSignatureLen);
120 }
121
122 bool NtlmBufferWriter::WriteMessageType(MessageType message_type) {
123 return WriteUInt32(static_cast<uint32_t>(message_type));
124 }
125
126 bool NtlmBufferWriter::WriteMessageHeader(MessageType message_type) {
127 return WriteSignature() && WriteMessageType(message_type);
128 }
129
130 template <typename T>
131 bool NtlmBufferWriter::WriteUInt(T value) {
132 size_t int_size = sizeof(T);
133 if (!CanWrite(int_size))
134 return false;
135
136 for (size_t i = 0; i < int_size; i++) {
137 GetBufferPtrAtCursor()[i] = static_cast<uint8_t>(value & 0xff);
138 value >>= 8;
139 }
140
141 AdvanceCursor(int_size);
142 return true;
143 }
144
145 void NtlmBufferWriter::SetCursor(size_t cursor) {
146 DCHECK(GetBufferPtr() && cursor <= GetLength());
147
148 cursor_ = cursor;
149 }
150
151 } // namespace ntlm
152 } // namespace net
OLDNEW
« no previous file with comments | « net/ntlm/ntlm_buffer_writer.h ('k') | net/ntlm/ntlm_buffer_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698