]> git.ipfire.org Git - thirdparty/squid.git/blame - src/StoreSwapLogData.cc
Prep for 3.3.12 and 3.4.4
[thirdparty/squid.git] / src / StoreSwapLogData.cc
CommitLineData
51ee7c82 1/*
51ee7c82 2 * DEBUG: section 47 Store Directory Routines
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
26ac0430 21 *
51ee7c82 22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26ac0430 26 *
51ee7c82 27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
f7f3304a 33#include "squid.h"
51ee7c82 34#include "StoreSwapLogData.h"
4b981814 35#include "swap_log_op.h"
51ee7c82 36
786f6516
AR
37// Based on Internet Checksum (RFC 1071) algorithm but takes three 32bit ints.
38// TODO: Consider Fletcher's checksum algorithm as a higher quality alternative
39void
40SwapChecksum24::set(uint32_t f1, uint32_t f2, uint32_t f3)
51ee7c82 41{
786f6516
AR
42 uint64_t sum = f1;
43 sum += f2;
44 sum += f3;
45
46 while (const uint64_t higherBits = sum >> 24)
d178daf7 47 sum = (sum & 0xFFFFFF) + higherBits;
786f6516
AR
48
49 sum = ~sum;
50
51 raw[0] = static_cast<uint8_t>(sum);
52 raw[1] = static_cast<uint8_t>(sum >> 8);
53 raw[2] = static_cast<uint8_t>(sum >> 16);
54}
55
56/// Same as 3-argument SwapChecksum24::set() but for int32_t and uint64_t
57void
58SwapChecksum24::set(int32_t f1, uint64_t f2)
59{
60 // split the second 64bit word into two 32bit words
61 set(static_cast<uint32_t>(f1),
62 static_cast<uint32_t>(f2 >> 32),
63 static_cast<uint32_t>(f2 & 0xFFFFFFFF));
64}
65
66std::ostream &
67SwapChecksum24::print(std::ostream &os) const
68{
69 return os << raw[0] << '-' << raw[1] << '-' << raw[2];
70}
71
72StoreSwapLogData::StoreSwapLogData()
73{
74 memset(this, 0, sizeof(*this));
51ee7c82 75}
47f6e231 76
3f9d4dc2
AR
77bool
78StoreSwapLogData::sane() const
79{
786f6516
AR
80 SwapChecksum24 actualSum;
81 actualSum.set(swap_filen, swap_file_sz);
82 if (checksum != actualSum)
83 return false;
3f9d4dc2 84
68992b63
AR
85 const time_t minTime = -2; // -1 is common; expires sometimes uses -2
86
3f9d4dc2
AR
87 // Check what we safely can; for some fields any value might be valid
88 return SWAP_LOG_NOP < op && op < SWAP_LOG_MAX &&
4db8126a
A
89 swap_filen >= 0 &&
90 timestamp >= minTime &&
91 lastref >= minTime &&
92 expires >= minTime &&
93 lastmod >= minTime &&
94 swap_file_sz > 0; // because swap headers ought to consume space
3f9d4dc2
AR
95}
96
786f6516
AR
97void
98StoreSwapLogData::finalize()
99{
100 checksum.set(swap_filen, swap_file_sz);
101}
102
103StoreSwapLogHeader::StoreSwapLogHeader(): op(SWAP_LOG_VERSION), version(2),
d178daf7 104 record_size(sizeof(StoreSwapLogData))
786f6516
AR
105{
106 checksum.set(version, record_size, 0);
107}
108
109bool
110StoreSwapLogHeader::sane() const
111{
112 SwapChecksum24 actualSum;
113 actualSum.set(version, record_size, 0);
114 if (checksum != actualSum)
115 return false;
116
117 return op == SWAP_LOG_VERSION && version >= 2 && record_size > 0;
118}
119
120size_t
121StoreSwapLogHeader::gapSize() const
47f6e231 122{
786f6516
AR
123 assert(record_size > 0);
124 assert(static_cast<size_t>(record_size) > sizeof(*this));
125 return static_cast<size_t>(record_size) - sizeof(*this);
47f6e231 126}