]>
Commit | Line | Data |
---|---|---|
528b2c61 | 1 | /* |
77b1029d | 2 | * Copyright (C) 1996-2020 The Squid Software Foundation and contributors |
528b2c61 | 3 | * |
bbc27441 AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
528b2c61 | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 20 Storage Manager Swapfile Unpacker */ |
10 | ||
582c2af2 | 11 | #include "squid.h" |
4c2f8b72 | 12 | #include "base/TextException.h" |
582c2af2 | 13 | #include "Debug.h" |
602d9612 | 14 | #include "defines.h" |
528b2c61 | 15 | #include "StoreMeta.h" |
602d9612 | 16 | #include "StoreMetaUnpacker.h" |
528b2c61 | 17 | |
57d55dfa | 18 | int const StoreMetaUnpacker::MinimumBufferLength = sizeof(char) + sizeof(int); |
528b2c61 | 19 | |
e2851fe7 AR |
20 | /// useful for meta stored in pre-initialized (with zeros) db files |
21 | bool | |
22 | StoreMetaUnpacker::isBufferZero() | |
23 | { | |
24 | // We could memcmp the entire buffer, but it is probably safe enough | |
25 | // to test a few bytes because if we do not detect a corrupted entry | |
26 | // it is not a big deal. Empty entries are not isBufferSane anyway. | |
27 | const int depth = 10; | |
28 | if (buflen < depth) | |
29 | return false; // cannot be sure enough | |
30 | ||
31 | for (int i = 0; i < depth; ++i) { | |
32 | if (buf[i]) | |
33 | return false; | |
34 | } | |
35 | return true; | |
36 | } | |
37 | ||
4c2f8b72 EB |
38 | void |
39 | StoreMetaUnpacker::checkBuffer() | |
528b2c61 | 40 | { |
4c2f8b72 EB |
41 | assert(buf); // paranoid; already checked in the constructor |
42 | if (buf[0] != static_cast<char>(STORE_META_OK)) | |
43 | throw TexcHere("store entry metadata is corrupted"); | |
528b2c61 | 44 | /* |
45 | * sanity check on 'buflen' value. It should be at least big | |
46 | * enough to hold one type and one length. | |
47 | */ | |
48 | getBufferLength(); | |
7c209882 | 49 | if (*hdr_len < MinimumBufferLength) |
4c2f8b72 | 50 | throw TexcHere("store entry metadata is too small"); |
528b2c61 | 51 | if (*hdr_len > buflen) |
4c2f8b72 | 52 | throw TexcHere("store entry metadata is too big"); |
528b2c61 | 53 | } |
54 | ||
55 | void | |
56 | StoreMetaUnpacker::getBufferLength() | |
57 | { | |
41d00cd3 | 58 | memcpy(hdr_len, &buf[1], sizeof(int)); |
528b2c61 | 59 | } |
60 | ||
23541b3e | 61 | StoreMetaUnpacker::StoreMetaUnpacker(char const *aBuffer, ssize_t aLen, int *anInt) : |
f53969cc SM |
62 | buf(aBuffer), |
63 | buflen(aLen), | |
64 | hdr_len(anInt), | |
65 | position(1 + sizeof(int)), | |
66 | type('\0'), | |
67 | length(0), | |
68 | tail(NULL) | |
528b2c61 | 69 | { |
23541b3e | 70 | assert(aBuffer != NULL); |
528b2c61 | 71 | } |
72 | ||
73 | void | |
74 | StoreMetaUnpacker::getType() | |
75 | { | |
f412b2d6 FC |
76 | type = buf[position]; |
77 | ++position; | |
528b2c61 | 78 | } |
79 | ||
80 | void | |
81 | StoreMetaUnpacker::getLength() | |
82 | { | |
41d00cd3 | 83 | memcpy(&length, &buf[position], sizeof(int)); |
528b2c61 | 84 | position += sizeof(int); |
85 | } | |
86 | ||
87 | bool | |
88 | StoreMetaUnpacker::doOneEntry() | |
89 | { | |
90 | getType(); | |
91 | getLength(); | |
62e76326 | 92 | |
528b2c61 | 93 | if (position + length > *hdr_len) { |
fa84c01d FC |
94 | debugs(20, DBG_CRITICAL, "storeSwapMetaUnpack: overflow!"); |
95 | debugs(20, DBG_CRITICAL, "\ttype=" << type << ", length=" << length << ", *hdr_len=" << *hdr_len << ", offset=" << position); | |
528b2c61 | 96 | return false; |
97 | } | |
62e76326 | 98 | |
528b2c61 | 99 | StoreMeta *newNode = StoreMeta::Factory(type, length, &buf[position]); |
62e76326 | 100 | |
5b8ddb2a | 101 | if (newNode) |
26ac0430 | 102 | tail = StoreMeta::Add (tail, newNode); |
62e76326 | 103 | |
528b2c61 | 104 | position += length; |
62e76326 | 105 | |
528b2c61 | 106 | return true; |
107 | } | |
108 | ||
109 | bool | |
110 | StoreMetaUnpacker::moreToProcess() const | |
111 | { | |
7c209882 | 112 | return *hdr_len - position - MinimumBufferLength >= 0; |
528b2c61 | 113 | } |
114 | ||
115 | StoreMeta * | |
116 | StoreMetaUnpacker::createStoreMeta () | |
117 | { | |
118 | tlv *TLV = NULL; | |
119 | tail = &TLV; | |
120 | assert(hdr_len != NULL); | |
62e76326 | 121 | |
4c2f8b72 | 122 | checkBuffer(); |
62e76326 | 123 | |
528b2c61 | 124 | getBufferLength(); |
62e76326 | 125 | |
528b2c61 | 126 | assert (position == 1 + sizeof(int)); |
62e76326 | 127 | |
528b2c61 | 128 | while (moreToProcess()) { |
62e76326 | 129 | if (!doOneEntry()) |
130 | break; | |
528b2c61 | 131 | } |
62e76326 | 132 | |
4c2f8b72 | 133 | if (!TLV) |
a038725f | 134 | throw TexcHere("store entry metadata is empty"); |
4c2f8b72 EB |
135 | |
136 | assert(TLV); | |
528b2c61 | 137 | return TLV; |
138 | } | |
f53969cc | 139 |