forked from cmuratori/meow_hash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meow_example.cpp
239 lines (205 loc) · 6.61 KB
/
meow_example.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* ========================================================================
meow_example.cpp - basic usage example of the Meow hash
(C) Copyright 2018 by Molly Rocket, Inc. (https://mollyrocket.com)
See https://mollyrocket.com/meowhash for details.
======================================================================== */
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
//
// NOTE(casey): Step 1 - include an intrinsics header, then include meow_hash.h
//
// Meow relies on definitions for non-standard types (meow_u128, etc.) and
// intrinsics for various platforms. You can either include the supplied meow_intrinsics.h
// file that will define these for you with its best guesses for your platform, or for
// more control, you can define them all yourself to map to your own stuff.
//
#include "meow_intrinsics.h" // NOTE(casey): Platform prerequisites for the Meow hash code (replace with your own, if you want)
#include "meow_hash.h" // NOTE(casey): The Meow hash code itself
//
// NOTE(casey): Step 2 - use the Meow hash in a variety of ways!
//
// Example functions below:
// PrintHash - how to print a Meow hash to stdout, from highest-order 32-bits to lowest
// HashTestBuffer - how to have Meow hash a buffer of data
// HashOneFile - have Meow hash the contents of a file
// CompareTwoFiles - have Meow hash the contents of two files, and check for equivalence
//
//
// NOTE(casey): entire_file / ReadEntireFile / FreeEntireFile are simple helpers
// for loading a file into memory. They are defined at the end of this file.
//
struct entire_file
{
size_t Size;
void *Contents;
};
static entire_file ReadEntireFile(char *Filename);
static void FreeEntireFile(entire_file *File);
static void
PrintHash(meow_hash Hash)
{
printf(" %08X-%08X-%08X-%08X\n",
MeowU32From(Hash, 3),
MeowU32From(Hash, 2),
MeowU32From(Hash, 1),
MeowU32From(Hash, 0));
}
static void
HashTestBuffer(void)
{
// NOTE(casey): Make a buffer with repeating numbers.
int Size = 16000;
char *Buffer = (char *)malloc(Size);
for(int Index = 0;
Index < Size;
++Index)
{
Buffer[Index] = (char)Index;
}
// NOTE(casey): Ask Meow for the hash
meow_hash Hash = MeowHash_Accelerated(0, Size, Buffer);
// NOTE(casey): Extract example smaller hash sizes you might want:
long long unsigned Hash64 = MeowU64From(Hash, 0);
int unsigned Hash32 = MeowU32From(Hash, 0);
// NOTE(casey): Print the hash
printf(" Hash of a test buffer:\n");
PrintHash(Hash);
free(Buffer);
}
static void
HashOneFile(char *FilenameA)
{
// NOTE(casey): Load the file
entire_file A = ReadEntireFile(FilenameA);
if(A.Contents)
{
// NOTE(casey): Ask Meow for the hash
meow_hash HashA = MeowHash_Accelerated(0, A.Size, A.Contents);
// NOTE(casey): Print the hash
printf(" Hash of \"%s\":\n", FilenameA);
PrintHash(HashA);
}
FreeEntireFile(&A);
}
static void
CompareTwoFiles(char *FilenameA, char *FilenameB)
{
// NOTE(casey): Load both files
entire_file A = ReadEntireFile(FilenameA);
entire_file B = ReadEntireFile(FilenameB);
if(A.Contents && B.Contents)
{
// NOTE(casey): Hash both files
meow_hash HashA = MeowHash_Accelerated(0, A.Size, A.Contents);
meow_hash HashB = MeowHash_Accelerated(0, B.Size, B.Contents);
// NOTE(casey): Check for match
int HashesMatch = MeowHashesAreEqual(HashA, HashB);
int FilesMatch = ((A.Size == B.Size) && (memcmp(A.Contents, B.Contents, A.Size) == 0));
// NOTE(casey): Print the result
if(HashesMatch && FilesMatch)
{
printf("Files \"%s\" and \"%s\" are the same:\n", FilenameA, FilenameB);
PrintHash(HashA);
}
else if(FilesMatch)
{
printf("MEOW HASH FAILURE: Files match but hashes don't!\n");
printf(" Hash of \"%s\":\n", FilenameA);
PrintHash(HashA);
printf(" Hash of \"%s\":\n", FilenameB);
PrintHash(HashB);
}
else if(HashesMatch)
{
printf("MEOW HASH FAILURE: Hashes match but files don't!\n");
printf(" Hash of both \"%s\" and \"%s\":\n", FilenameA, FilenameB);
PrintHash(HashA);
}
else
{
printf("Files \"%s\" and \"%s\" are different:\n", FilenameA, FilenameB);
printf(" Hash of \"%s\":\n", FilenameA);
PrintHash(HashA);
printf(" Hash of \"%s\":\n", FilenameB);
PrintHash(HashB);
}
}
FreeEntireFile(&A);
FreeEntireFile(&B);
}
//
// NOTE(casey): That's it! Everything else below here is just boilerplate for starting up
// and loading files with the C runtime library.
//
int
main(int ArgCount, char **Args)
{
// NOTE(casey): Print the banner
printf("meow_example %s - basic usage example of the Meow hash\n", MEOW_HASH_VERSION_NAME);
printf("(C) Copyright 2018 by Molly Rocket, Inc. (https://mollyrocket.com)\n");
printf("See https://mollyrocket.com/meowhash for details.\n");
printf("\n");
// NOTE(casey): Look at our arguments to decide which example to run
if(ArgCount < 2)
{
HashTestBuffer();
}
else if(ArgCount == 2)
{
HashOneFile(Args[1]);
}
else if(ArgCount == 3)
{
CompareTwoFiles(Args[1], Args[2]);
}
else
{
printf("Usage:\n");
printf("%s - hash a test buffer\n", Args[0]);
printf("%s [filename] - hash the contents of [filename]\n", Args[0]);
printf("%s [filename0] [filename1] - hash the contents of [filename0] and [filename1] and compare them\n", Args[0]);
}
return(0);
}
static entire_file
ReadEntireFile(char *Filename)
{
entire_file Result = {};
FILE *File = fopen(Filename, "rb");
if(File)
{
fseek(File, 0, SEEK_END);
Result.Size = ftell(File);
fseek(File, 0, SEEK_SET);
Result.Contents = malloc(Result.Size);
if(Result.Contents)
{
if(Result.Size)
{
fread(Result.Contents, Result.Size, 1, File);
}
}
else
{
Result.Contents = 0;
Result.Size = 0;
}
fclose(File);
}
else
{
printf("ERROR: Unable to load \"%s\"\n", Filename);
}
return(Result);
}
static void
FreeEntireFile(entire_file *File)
{
if(File->Contents)
{
free(File->Contents);
File->Contents = 0;
}
File->Size = 0;
}