OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/browser/indexed_db/indexed_db_leveldb_coding.h" | 5 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 into->push_back(c); | 256 into->push_back(c); |
257 } while (n); | 257 } while (n); |
258 } | 258 } |
259 | 259 |
260 void EncodeString(const base::string16& value, std::string* into) { | 260 void EncodeString(const base::string16& value, std::string* into) { |
261 if (value.empty()) | 261 if (value.empty()) |
262 return; | 262 return; |
263 // Backing store is UTF-16BE, convert from host endianness. | 263 // Backing store is UTF-16BE, convert from host endianness. |
264 size_t length = value.length(); | 264 size_t length = value.length(); |
265 size_t current = into->size(); | 265 size_t current = into->size(); |
266 into->resize(into->size() + length * sizeof(char16)); | 266 into->resize(into->size() + length * sizeof(base::char16)); |
267 | 267 |
268 const char16* src = value.c_str(); | 268 const base::char16* src = value.c_str(); |
269 char16* dst = reinterpret_cast<char16*>(&*into->begin() + current); | 269 base::char16* dst = |
| 270 reinterpret_cast<base::char16*>(&*into->begin() + current); |
270 for (unsigned i = 0; i < length; ++i) | 271 for (unsigned i = 0; i < length; ++i) |
271 *dst++ = htons(*src++); | 272 *dst++ = htons(*src++); |
272 } | 273 } |
273 | 274 |
274 void EncodeBinary(const std::string& value, std::string* into) { | 275 void EncodeBinary(const std::string& value, std::string* into) { |
275 EncodeVarInt(value.length(), into); | 276 EncodeVarInt(value.length(), into); |
276 into->append(value.begin(), value.end()); | 277 into->append(value.begin(), value.end()); |
277 DCHECK(into->size() >= value.size()); | 278 DCHECK(into->size() >= value.size()); |
278 } | 279 } |
279 | 280 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 return true; | 422 return true; |
422 } | 423 } |
423 | 424 |
424 bool DecodeString(StringPiece* slice, base::string16* value) { | 425 bool DecodeString(StringPiece* slice, base::string16* value) { |
425 if (slice->empty()) { | 426 if (slice->empty()) { |
426 value->clear(); | 427 value->clear(); |
427 return true; | 428 return true; |
428 } | 429 } |
429 | 430 |
430 // Backing store is UTF-16BE, convert to host endianness. | 431 // Backing store is UTF-16BE, convert to host endianness. |
431 DCHECK(!(slice->size() % sizeof(char16))); | 432 DCHECK(!(slice->size() % sizeof(base::char16))); |
432 size_t length = slice->size() / sizeof(char16); | 433 size_t length = slice->size() / sizeof(base::char16); |
433 base::string16 decoded; | 434 base::string16 decoded; |
434 decoded.reserve(length); | 435 decoded.reserve(length); |
435 const char16* encoded = reinterpret_cast<const char16*>(slice->begin()); | 436 const base::char16* encoded = |
| 437 reinterpret_cast<const base::char16*>(slice->begin()); |
436 for (unsigned i = 0; i < length; ++i) | 438 for (unsigned i = 0; i < length; ++i) |
437 decoded.push_back(ntohs(*encoded++)); | 439 decoded.push_back(ntohs(*encoded++)); |
438 | 440 |
439 *value = decoded; | 441 *value = decoded; |
440 slice->remove_prefix(length * sizeof(char16)); | 442 slice->remove_prefix(length * sizeof(base::char16)); |
441 return true; | 443 return true; |
442 } | 444 } |
443 | 445 |
444 bool DecodeStringWithLength(StringPiece* slice, base::string16* value) { | 446 bool DecodeStringWithLength(StringPiece* slice, base::string16* value) { |
445 if (slice->empty()) | 447 if (slice->empty()) |
446 return false; | 448 return false; |
447 | 449 |
448 int64 length = 0; | 450 int64 length = 0; |
449 if (!DecodeVarInt(slice, &length) || length < 0) | 451 if (!DecodeVarInt(slice, &length) || length < 0) |
450 return false; | 452 return false; |
451 size_t bytes = length * sizeof(char16); | 453 size_t bytes = length * sizeof(base::char16); |
452 if (slice->size() < bytes) | 454 if (slice->size() < bytes) |
453 return false; | 455 return false; |
454 | 456 |
455 StringPiece subpiece(slice->begin(), bytes); | 457 StringPiece subpiece(slice->begin(), bytes); |
456 slice->remove_prefix(bytes); | 458 slice->remove_prefix(bytes); |
457 if (!DecodeString(&subpiece, value)) | 459 if (!DecodeString(&subpiece, value)) |
458 return false; | 460 return false; |
459 | 461 |
460 return true; | 462 return true; |
461 } | 463 } |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 return false; | 623 return false; |
622 if (slice->size() < static_cast<size_t>(length)) | 624 if (slice->size() < static_cast<size_t>(length)) |
623 return false; | 625 return false; |
624 slice->remove_prefix(length); | 626 slice->remove_prefix(length); |
625 return true; | 627 return true; |
626 } | 628 } |
627 case kIndexedDBKeyStringTypeByte: { | 629 case kIndexedDBKeyStringTypeByte: { |
628 int64 length = 0; | 630 int64 length = 0; |
629 if (!DecodeVarInt(slice, &length) || length < 0) | 631 if (!DecodeVarInt(slice, &length) || length < 0) |
630 return false; | 632 return false; |
631 if (slice->size() < static_cast<size_t>(length) * sizeof(char16)) | 633 if (slice->size() < static_cast<size_t>(length) * sizeof(base::char16)) |
632 return false; | 634 return false; |
633 slice->remove_prefix(length * sizeof(char16)); | 635 slice->remove_prefix(length * sizeof(base::char16)); |
634 return true; | 636 return true; |
635 } | 637 } |
636 case kIndexedDBKeyDateTypeByte: | 638 case kIndexedDBKeyDateTypeByte: |
637 case kIndexedDBKeyNumberTypeByte: | 639 case kIndexedDBKeyNumberTypeByte: |
638 if (slice->size() < sizeof(double)) | 640 if (slice->size() < sizeof(double)) |
639 return false; | 641 return false; |
640 slice->remove_prefix(sizeof(double)); | 642 slice->remove_prefix(sizeof(double)); |
641 return true; | 643 return true; |
642 } | 644 } |
643 NOTREACHED(); | 645 NOTREACHED(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 if (!DecodeVarInt(slice1, &len1) || !DecodeVarInt(slice2, &len2)) { | 685 if (!DecodeVarInt(slice1, &len1) || !DecodeVarInt(slice2, &len2)) { |
684 *ok = false; | 686 *ok = false; |
685 return 0; | 687 return 0; |
686 } | 688 } |
687 DCHECK_GE(len1, 0); | 689 DCHECK_GE(len1, 0); |
688 DCHECK_GE(len2, 0); | 690 DCHECK_GE(len2, 0); |
689 if (len1 < 0 || len2 < 0) { | 691 if (len1 < 0 || len2 < 0) { |
690 *ok = false; | 692 *ok = false; |
691 return 0; | 693 return 0; |
692 } | 694 } |
693 DCHECK_GE(slice1->size(), len1 * sizeof(char16)); | 695 DCHECK_GE(slice1->size(), len1 * sizeof(base::char16)); |
694 DCHECK_GE(slice2->size(), len2 * sizeof(char16)); | 696 DCHECK_GE(slice2->size(), len2 * sizeof(base::char16)); |
695 if (slice1->size() < len1 * sizeof(char16) || | 697 if (slice1->size() < len1 * sizeof(base::char16) || |
696 slice2->size() < len2 * sizeof(char16)) { | 698 slice2->size() < len2 * sizeof(base::char16)) { |
697 *ok = false; | 699 *ok = false; |
698 return 0; | 700 return 0; |
699 } | 701 } |
700 | 702 |
701 // Extract the string data, and advance the passed slices. | 703 // Extract the string data, and advance the passed slices. |
702 StringPiece string1(slice1->begin(), len1 * sizeof(char16)); | 704 StringPiece string1(slice1->begin(), len1 * sizeof(base::char16)); |
703 StringPiece string2(slice2->begin(), len2 * sizeof(char16)); | 705 StringPiece string2(slice2->begin(), len2 * sizeof(base::char16)); |
704 slice1->remove_prefix(len1 * sizeof(char16)); | 706 slice1->remove_prefix(len1 * sizeof(base::char16)); |
705 slice2->remove_prefix(len2 * sizeof(char16)); | 707 slice2->remove_prefix(len2 * sizeof(base::char16)); |
706 | 708 |
707 *ok = true; | 709 *ok = true; |
708 // Strings are UTF-16BE encoded, so a simple memcmp is sufficient. | 710 // Strings are UTF-16BE encoded, so a simple memcmp is sufficient. |
709 return string1.compare(string2); | 711 return string1.compare(string2); |
710 } | 712 } |
711 | 713 |
712 int CompareEncodedBinary(StringPiece* slice1, | 714 int CompareEncodedBinary(StringPiece* slice1, |
713 StringPiece* slice2, | 715 StringPiece* slice2, |
714 bool* ok) { | 716 bool* ok) { |
715 int64 len1, len2; | 717 int64 len1, len2; |
(...skipping 1155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1871 scoped_ptr<IndexedDBKey> IndexDataKey::primary_key() const { | 1873 scoped_ptr<IndexedDBKey> IndexDataKey::primary_key() const { |
1872 scoped_ptr<IndexedDBKey> key; | 1874 scoped_ptr<IndexedDBKey> key; |
1873 StringPiece slice(encoded_primary_key_); | 1875 StringPiece slice(encoded_primary_key_); |
1874 if (!DecodeIDBKey(&slice, &key)) { | 1876 if (!DecodeIDBKey(&slice, &key)) { |
1875 // TODO(jsbell): Return error. | 1877 // TODO(jsbell): Return error. |
1876 } | 1878 } |
1877 return key.Pass(); | 1879 return key.Pass(); |
1878 } | 1880 } |
1879 | 1881 |
1880 } // namespace content | 1882 } // namespace content |
OLD | NEW |