forked from openslide/openslide
-
Notifications
You must be signed in to change notification settings - Fork 4
/
openslide-hash.c
121 lines (100 loc) · 3.18 KB
/
openslide-hash.c
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
/*
* OpenSlide, a library for reading whole slide image files
*
* Copyright (c) 2007-2010 Carnegie Mellon University
* All rights reserved.
*
* OpenSlide is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 2.1.
*
* OpenSlide is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with OpenSlide. If not, see
* <https://www.gnu.org/licenses/>.
*
*/
#include "openslide-private.h"
#include "openslide-hash.h"
#include <stdio.h>
#include <string.h>
#include <glib.h>
struct _openslide_hash {
GChecksum *checksum;
bool enabled;
};
struct _openslide_hash *_openslide_hash_quickhash1_create(void) {
struct _openslide_hash *hash = g_new(struct _openslide_hash, 1);
hash->checksum = g_checksum_new(G_CHECKSUM_SHA256);
hash->enabled = true;
return hash;
}
void _openslide_hash_data(struct _openslide_hash *hash, const void *data,
int32_t datalen) {
if (hash && hash->enabled && data && datalen) {
g_checksum_update(hash->checksum, data, datalen);
}
}
void _openslide_hash_string(struct _openslide_hash *hash, const char *str) {
const char *str_to_hash = str ? str : "";
_openslide_hash_data(hash, str_to_hash, strlen(str_to_hash) + 1);
}
bool _openslide_hash_file(struct _openslide_hash *hash, const char *filename,
GError **err) {
return _openslide_hash_file_part(hash, filename, 0, -1, err);
}
bool _openslide_hash_file_part(struct _openslide_hash *hash,
const char *filename,
int64_t offset, int64_t size,
GError **err) {
g_autoptr(_openslide_file) f = _openslide_fopen(filename, err);
if (f == NULL) {
return false;
}
if (size == -1) {
// hash to end of file
int64_t len = _openslide_fsize(f, err);
if (len == -1) {
return false;
}
size = len - offset;
}
uint8_t buf[4096];
if (offset != 0) {
if (!_openslide_fseek(f, offset, SEEK_SET, err)) {
return false;
}
}
int64_t bytes_left = size;
while (bytes_left > 0) {
int64_t bytes_to_read = MIN((int64_t) sizeof buf, bytes_left);
if (!_openslide_fread_exact(f, buf, bytes_to_read, err)) {
return false;
}
// g_debug("hash '%s' %"PRId64" %d", filename, offset + (size - bytes_left), bytes_to_read);
bytes_left -= bytes_to_read;
_openslide_hash_data(hash, buf, bytes_to_read);
}
return true;
}
// Invalidate this hash. Use if this slide is unhashable for some reason.
void _openslide_hash_disable(struct _openslide_hash *hash) {
if (hash) {
hash->enabled = false;
}
}
const char *_openslide_hash_get_string(struct _openslide_hash *hash) {
if (hash->enabled) {
return g_checksum_get_string(hash->checksum);
} else {
return NULL;
}
}
void _openslide_hash_destroy(struct _openslide_hash *hash) {
g_checksum_free(hash->checksum);
g_free(hash);
}