]> git.ipfire.org Git - thirdparty/squid.git/blame - src/StoreSwapLogData.cc
Boilerplate: update copyright blurbs on src/
[thirdparty/squid.git] / src / StoreSwapLogData.cc
CommitLineData
51ee7c82 1/*
bbc27441 2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
51ee7c82 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.
51ee7c82 7 */
8
bbc27441
AJ
9/* DEBUG: section 47 Store Directory Routines */
10
f7f3304a 11#include "squid.h"
51ee7c82 12#include "StoreSwapLogData.h"
4b981814 13#include "swap_log_op.h"
51ee7c82 14
786f6516
AR
15// Based on Internet Checksum (RFC 1071) algorithm but takes three 32bit ints.
16// TODO: Consider Fletcher's checksum algorithm as a higher quality alternative
17void
18SwapChecksum24::set(uint32_t f1, uint32_t f2, uint32_t f3)
51ee7c82 19{
786f6516
AR
20 uint64_t sum = f1;
21 sum += f2;
22 sum += f3;
23
24 while (const uint64_t higherBits = sum >> 24)
d178daf7 25 sum = (sum & 0xFFFFFF) + higherBits;
786f6516
AR
26
27 sum = ~sum;
28
29 raw[0] = static_cast<uint8_t>(sum);
30 raw[1] = static_cast<uint8_t>(sum >> 8);
31 raw[2] = static_cast<uint8_t>(sum >> 16);
32}
33
34/// Same as 3-argument SwapChecksum24::set() but for int32_t and uint64_t
35void
36SwapChecksum24::set(int32_t f1, uint64_t f2)
37{
38 // split the second 64bit word into two 32bit words
39 set(static_cast<uint32_t>(f1),
40 static_cast<uint32_t>(f2 >> 32),
41 static_cast<uint32_t>(f2 & 0xFFFFFFFF));
42}
43
44std::ostream &
45SwapChecksum24::print(std::ostream &os) const
46{
47 return os << raw[0] << '-' << raw[1] << '-' << raw[2];
48}
49
50StoreSwapLogData::StoreSwapLogData()
51{
52 memset(this, 0, sizeof(*this));
51ee7c82 53}
47f6e231 54
3f9d4dc2
AR
55bool
56StoreSwapLogData::sane() const
57{
786f6516
AR
58 SwapChecksum24 actualSum;
59 actualSum.set(swap_filen, swap_file_sz);
60 if (checksum != actualSum)
61 return false;
3f9d4dc2 62
68992b63
AR
63 const time_t minTime = -2; // -1 is common; expires sometimes uses -2
64
3f9d4dc2
AR
65 // Check what we safely can; for some fields any value might be valid
66 return SWAP_LOG_NOP < op && op < SWAP_LOG_MAX &&
4db8126a
A
67 swap_filen >= 0 &&
68 timestamp >= minTime &&
69 lastref >= minTime &&
70 expires >= minTime &&
71 lastmod >= minTime &&
72 swap_file_sz > 0; // because swap headers ought to consume space
3f9d4dc2
AR
73}
74
786f6516
AR
75void
76StoreSwapLogData::finalize()
77{
78 checksum.set(swap_filen, swap_file_sz);
79}
80
81StoreSwapLogHeader::StoreSwapLogHeader(): op(SWAP_LOG_VERSION), version(2),
d178daf7 82 record_size(sizeof(StoreSwapLogData))
786f6516
AR
83{
84 checksum.set(version, record_size, 0);
85}
86
87bool
88StoreSwapLogHeader::sane() const
89{
90 SwapChecksum24 actualSum;
91 actualSum.set(version, record_size, 0);
92 if (checksum != actualSum)
93 return false;
94
95 return op == SWAP_LOG_VERSION && version >= 2 && record_size > 0;
96}
97
98size_t
99StoreSwapLogHeader::gapSize() const
47f6e231 100{
786f6516
AR
101 assert(record_size > 0);
102 assert(static_cast<size_t>(record_size) > sizeof(*this));
103 return static_cast<size_t>(record_size) - sizeof(*this);
47f6e231 104}