OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/webcrypto/webcrypto_util.h" | 5 #include "content/child/webcrypto/webcrypto_util.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "content/child/webcrypto/status.h" | 10 #include "content/child/webcrypto/status.h" |
11 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 11 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
13 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 13 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
17 namespace webcrypto { | 17 namespace webcrypto { |
18 | 18 |
19 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { | 19 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { |
20 if (data.empty()) | 20 if (data.empty()) |
21 return NULL; | 21 return NULL; |
22 return &data[0]; | 22 return &data[0]; |
23 } | 23 } |
24 | 24 |
25 uint8* Uint8VectorStart(std::vector<uint8>* data) { | 25 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned int new_size) { |
26 if (data->empty()) | 26 DCHECK_LE(new_size, buffer->byteLength()); |
27 return NULL; | 27 |
28 return &(*data)[0]; | 28 if (new_size == buffer->byteLength()) |
| 29 return; |
| 30 |
| 31 blink::WebArrayBuffer new_buffer = blink::WebArrayBuffer::create(new_size, 1); |
| 32 DCHECK(!new_buffer.isNull()); |
| 33 memcpy(new_buffer.data(), buffer->data(), new_size); |
| 34 *buffer = new_buffer; |
| 35 } |
| 36 |
| 37 blink::WebArrayBuffer CreateArrayBuffer(const uint8* data, |
| 38 unsigned int data_size) { |
| 39 blink::WebArrayBuffer buffer = blink::WebArrayBuffer::create(data_size, 1); |
| 40 DCHECK(!buffer.isNull()); |
| 41 if (data_size) // data_size == 0 might mean the data pointer is invalid |
| 42 memcpy(buffer.data(), data, data_size); |
| 43 return buffer; |
29 } | 44 } |
30 | 45 |
31 // This function decodes unpadded 'base64url' encoded data, as described in | 46 // This function decodes unpadded 'base64url' encoded data, as described in |
32 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. To do this, first | 47 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. To do this, first |
33 // change the incoming data to 'base64' encoding by applying the appropriate | 48 // change the incoming data to 'base64' encoding by applying the appropriate |
34 // transformation including adding padding if required, and then call a base64 | 49 // transformation including adding padding if required, and then call a base64 |
35 // decoder. | 50 // decoder. |
36 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { | 51 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { |
37 std::string base64EncodedText(input); | 52 std::string base64EncodedText(input); |
38 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); | 53 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 algorithm.id(), keylen_bytes * 8); | 197 algorithm.id(), keylen_bytes * 8); |
183 return true; | 198 return true; |
184 default: | 199 default: |
185 return false; | 200 return false; |
186 } | 201 } |
187 } | 202 } |
188 | 203 |
189 } // namespace webcrypto | 204 } // namespace webcrypto |
190 | 205 |
191 } // namespace content | 206 } // namespace content |
OLD | NEW |