1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include "checksum.h"
#include <string.h>
#include <stdio.h>
#include "../simdebug.h"
checksum_t::checksum_t()
{
sha = NULL;
reset();
}
checksum_t& checksum_t::operator=(const checksum_t& other)
{
assert(other.valid);
valid = true;
delete sha;
sha = NULL;
for(uint8 i=0; i<20; i++) {
message_digest[i] = other.message_digest[i];
}
return *this;
}
checksum_t::checksum_t(const checksum_t &other)
{
assert(other.valid);
valid = true;
sha = NULL;
for(uint8 i=0; i<20; i++) {
message_digest[i] = other.message_digest[i];
}
}
checksum_t::~checksum_t()
{
delete sha;
}
void checksum_t::reset()
{
if (sha == NULL) {
sha = new SHA1();
}
else {
sha->Reset();
}
valid = false;
}
bool checksum_t::operator== (checksum_t &other)
{
if (!valid) {
finish();
}
if (!other.valid) {
other.finish();
}
for(uint8 i=0; i<20; i++) {
if (message_digest[i] != other.message_digest[i]) {
return false;
}
}
return true;
}
bool checksum_t::operator== (const checksum_t &other) const
{
assert(valid && other.valid);
for(uint8 i=0; i<20; i++) {
if (message_digest[i] != other.message_digest[i]) {
return false;
}
}
return true;
}
void checksum_t::input(bool data)
{
assert(sha);
// save bool as (uint8)1
uint8 bool1 = data ? 1 : 0;
input(bool1);
}
void checksum_t::input(uint8 data)
{
assert(sha);
sha->Input((const char*)&data, sizeof(uint8));
}
void checksum_t::input(sint8 data)
{
input((uint8)data);
}
void checksum_t::input(uint16 data)
{
uint16 little_endian = endian(data);
assert(sha);
sha->Input((const char*)&little_endian, sizeof(uint16));
}
void checksum_t::input(sint16 data)
{
input((uint16)data);
}
void checksum_t::input(uint32 data)
{
uint32 little_endian = endian(data);
assert(sha);
sha->Input((const char*)&little_endian, sizeof(uint32));
}
void checksum_t::input(sint32 data)
{
input((uint32)data);
}
void checksum_t::input(const char *data)
{
if (data==NULL) {
data = "null";
}
assert(sha);
sha->Input(data, strlen(data));
}
const char* checksum_t::get_str(const int maxlen) const
{
static char buf[41];
for(uint8 i=0; i<min(maxlen,20); i++) {
sprintf(buf + 2*i, "%2.2X", message_digest[i]);
}
return buf;
}
void checksum_t::calc_checksum(checksum_t *chk) const
{
for(uint8 i=0; i<20; i++) {
chk->input(message_digest[i]);
}
}
void checksum_t::finish()
{
if (!valid) {
assert(sha);
sha->Result(message_digest);
valid = true;
delete sha;
sha = NULL;
}
}
|