-
Notifications
You must be signed in to change notification settings - Fork 12
/
example.c
125 lines (104 loc) · 3.34 KB
/
example.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
122
123
124
125
/* minor example usage of mmc.c
Copyright (C) Yann Collet 2018-present
GPL v2 License
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- public issue list : https://github.com/Cyan4973/mmc/issues
- website : http://fastcompression.blogspot.com/
*/
#include <stdlib.h> /* malloc, free */
#include <stdio.h> /* fopen, fread, fclose */
#include <assert.h>
#include "mmc.h"
#define KB *(1<<10)
/* --- safe variants --- */
static void* MALLOC(size_t s)
{
void* const buf = calloc(1, s);
assert(buf != NULL);
return buf;
}
#define FREE free /* cannot fail */
static FILE* FOPEN(const char* name, const char* mode)
{
FILE* f = fopen(name, mode);
assert(f != NULL);
return f;
}
static size_t FREAD(void* buf, size_t s, size_t n, FILE* f)
{
size_t const read = fread(buf, s, n, f);
assert(ferror(f) == 0);
assert(read <= s*n);
return read;
}
static void FCLOSE(FILE* f)
{
assert(fclose(f) == 0);
}
/* --- example --- */
char printableChar(char c)
{
if (32 <= c && c <= 126) return c;
return '.';
}
/* this function displays printable characters
* and replaces all other ones by `.` */
void printContent(const void* p, size_t l)
{
#define PRINT_CONTENT_LENGTH_MAX 80
const char* b = p;
size_t u;
if (l > PRINT_CONTENT_LENGTH_MAX) l = PRINT_CONTENT_LENGTH_MAX;
for (u=0; u<l; u++) {
printf("%c", printableChar(b[u]));
}
}
static void printMatches(const void* buffer, size_t size)
{
MMC_ctx* const mmc = MMC_create();
const char* const buf = (const char*)buffer;
size_t pos = 0;
MMC_init(mmc, buf);
for (pos=0; pos<size; pos++) {
const void* match;
size_t const length = MMC_insertAndFindBestMatch(mmc,
buf+pos, size-pos, &match);
if (length > 0) {
assert(match >= buffer);
printf("pos%6zu: found match of length%3zu at pos%6zi ( ",
pos, length, (const char*)match - buf);
printContent(buf + pos, length);
printf(" ) \n");
} }
MMC_free(mmc);
}
int main(int argc, const char** argv)
{
size_t const bufferSize = 64 KB;
void* const buffer = MALLOC(bufferSize);
const char* exename = argv[0];
const char* filename = argv[1];
assert(argc >= 1);
if (argc != 2) {
printf("usage : %s FILENAME \n", exename);
return 1; /* usage : exe Filename */
}
/* read up to 64 KB from input, then search matches into it */
{ FILE* const f = FOPEN(filename, "rb");
size_t const size = FREAD(buffer, 1, bufferSize, f);
FCLOSE(f);
printMatches(buffer, size);
}
FREE(buffer);
}