simple_util.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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
  3. // found in the LICENSE file.
  4. #include "net/disk_cache/simple/simple_util.h"
  5. #include <limits>
  6. #include "base/check_op.h"
  7. #include "base/files/file_util.h"
  8. #include "base/format_macros.h"
  9. #include "base/hash/sha1.h"
  10. #include "base/numerics/safe_conversions.h"
  11. #include "base/strings/string_number_conversions.h"
  12. #include "base/strings/stringprintf.h"
  13. #include "base/threading/thread_restrictions.h"
  14. #include "base/time/time.h"
  15. #include "net/disk_cache/simple/simple_entry_format.h"
  16. #include "third_party/zlib/zlib.h"
  17. namespace {
  18. // Size of the uint64_t hash_key number in Hex format in a string.
  19. const size_t kEntryHashKeyAsHexStringSize = 2 * sizeof(uint64_t);
  20. } // namespace
  21. namespace disk_cache::simple_util {
  22. std::string ConvertEntryHashKeyToHexString(uint64_t hash_key) {
  23. const std::string hash_key_str = base::StringPrintf("%016" PRIx64, hash_key);
  24. DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size());
  25. return hash_key_str;
  26. }
  27. std::string GetEntryHashKeyAsHexString(const std::string& key) {
  28. std::string hash_key_str =
  29. ConvertEntryHashKeyToHexString(GetEntryHashKey(key));
  30. DCHECK_EQ(kEntryHashKeyAsHexStringSize, hash_key_str.size());
  31. return hash_key_str;
  32. }
  33. bool GetEntryHashKeyFromHexString(const base::StringPiece& hash_key,
  34. uint64_t* hash_key_out) {
  35. if (hash_key.size() != kEntryHashKeyAsHexStringSize) {
  36. return false;
  37. }
  38. return base::HexStringToUInt64(hash_key, hash_key_out);
  39. }
  40. uint64_t GetEntryHashKey(const std::string& key) {
  41. union {
  42. unsigned char sha_hash[base::kSHA1Length];
  43. uint64_t key_hash;
  44. } u;
  45. base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(key.data()),
  46. key.size(), u.sha_hash);
  47. return u.key_hash;
  48. }
  49. std::string GetFilenameFromEntryFileKeyAndFileIndex(
  50. const SimpleFileTracker::EntryFileKey& key,
  51. int file_index) {
  52. if (key.doom_generation == 0)
  53. return base::StringPrintf("%016" PRIx64 "_%1d", key.entry_hash, file_index);
  54. else
  55. return base::StringPrintf("todelete_%016" PRIx64 "_%1d_%" PRIu64,
  56. key.entry_hash, file_index, key.doom_generation);
  57. }
  58. std::string GetSparseFilenameFromEntryFileKey(
  59. const SimpleFileTracker::EntryFileKey& key) {
  60. if (key.doom_generation == 0)
  61. return base::StringPrintf("%016" PRIx64 "_s", key.entry_hash);
  62. else
  63. return base::StringPrintf("todelete_%016" PRIx64 "_s_%" PRIu64,
  64. key.entry_hash, key.doom_generation);
  65. }
  66. std::string GetFilenameFromKeyAndFileIndex(const std::string& key,
  67. int file_index) {
  68. return GetEntryHashKeyAsHexString(key) +
  69. base::StringPrintf("_%1d", file_index);
  70. }
  71. size_t GetHeaderSize(size_t key_length) {
  72. return sizeof(SimpleFileHeader) + key_length;
  73. }
  74. int32_t GetDataSizeFromFileSize(size_t key_length, int64_t file_size) {
  75. int64_t data_size =
  76. file_size - key_length - sizeof(SimpleFileHeader) - sizeof(SimpleFileEOF);
  77. return base::checked_cast<int32_t>(data_size);
  78. }
  79. int64_t GetFileSizeFromDataSize(size_t key_length, int32_t data_size) {
  80. return data_size + key_length + sizeof(SimpleFileHeader) +
  81. sizeof(SimpleFileEOF);
  82. }
  83. int GetFileIndexFromStreamIndex(int stream_index) {
  84. return (stream_index == 2) ? 1 : 0;
  85. }
  86. uint32_t Crc32(const char* data, int length) {
  87. uint32_t empty_crc = crc32(0, Z_NULL, 0);
  88. if (length == 0)
  89. return empty_crc;
  90. return crc32(empty_crc, reinterpret_cast<const Bytef*>(data), length);
  91. }
  92. uint32_t IncrementalCrc32(uint32_t previous_crc, const char* data, int length) {
  93. return crc32(previous_crc, reinterpret_cast<const Bytef*>(data), length);
  94. }
  95. } // namespace disk_cache::simple_util