-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
295 lines (278 loc) · 9.62 KB
/
main.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
// main.c
// high-speed lossless tiny data compression for 1 to 512 bytes based on td512
//
// file-based test bed outputs .td512 file with encoded values
// then reads in that file and generates .td512d file with
// original values.
//
// Created by L. Stevan Leonard on 10/31/21.
// Copyright © 2021-2022 L. Stevan Leonard. All rights reserved.
/*
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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include "td512.h" // td512 functions
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define BENCHMARK_LOOP_COUNT // special loop count for benchmarking
#define EXTERNAL_LOOP_COUNT_MAX 2000
//#define TEST_TD512 // invokes test_td512_1to512
#ifdef TD512_TEST_MODE
extern uint32_t gExtendedStringCnt;
extern uint32_t gExtendedTextCnt;
extern uint32_t gtd64Cnt;
#endif
int32_t test_td512_1to512(void)
{
// generate data then run through compress and decompress and compare for 1 to 512 values
unsigned char textData[512]={"it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT- POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it positive"};
unsigned char textOut[515];
unsigned char textOrig[512];
uint32_t bytesProcessed;
int32_t retVal;
int i;
int j;
RUN_512:
for (i=1; i<=512; i++)
{
retVal = td512(textData, textOut, i);
if (retVal < 0)
return i;
retVal = td512d(textOut, textOrig, &bytesProcessed);
if (retVal < 0)
return -i;
for (j=0; j<i; j++)
{
if (textData[j] != textOrig[j])
return 1000+j;
textOut[j] = '0';
textOrig[j] = '1';
}
}
if (textData[0] == 'i')
{
// set all values to same value and run again
memset(textData, 0x91, sizeof(textData));
goto RUN_512;
}
return 0;
}
int main(int argc, char* argv[])
{
FILE *ifile, *ofile;
char ofileName[FILENAME_MAX];
unsigned char *src, *dst;
size_t len, len2, len3;
clock_t begin, end;
double timeSpent;
double minTimeSpent=600;
int32_t nBytesRemaining;
int32_t nCompressedBytes;
uint32_t totalCompressedBytes;
uint32_t bytesProcessed;
uint32_t totalOutBytes;
uint32_t srcBlockOffset;
uint32_t dstBlockOffset;
int loopNum;
int loopCnt; // argv[4] option: default is 1
uint32_t blockSize=512; // block size to use when iterating through file
printf("tiny data compression td512 %s\n", TD512_VERSION);
#ifdef TEST_TD512
int32_t retVal;
if ((retVal=test_td512_1to512()) != 0) // do check of 1 to 512 values
{
printf("error from test_td512_1to512=%d\n", retVal);
return -83;
}
printf("TEST_TD512 passed\n");
#endif
if (argc < 2)
{
printf("td512 error: input file must be specified\n");
return 14;
}
ifile = fopen(argv[1], "rb");
if (!ifile)
{
printf("td512 error: file not found: %s\n", argv[1]);
return 9;
}
printf(" file=%s\n", argv[1]);
strcpy(ofileName, argv[1]);
ofile = fopen(strcat(ofileName, ".td512"), "wb");
// allocate source buffer and read file
fseek(ifile, 0, SEEK_END);
len = (size_t)ftell(ifile);
fseek(ifile, 0, SEEK_SET);
src = (unsigned char*) malloc(len);
fread(src, 1, len, ifile);
fclose(ifile);
// allocate "uncompressed size" + 3 bytes per block for the destination buffer
dst = (unsigned char*) malloc(len + 4 * (len / blockSize + 1));
if (argc >= 3)
{
sscanf(argv[2], "%d", &loopCnt);
if (loopCnt < 1)
loopCnt = 1;
}
else
{
loopCnt = 1;
}
#ifdef BENCHMARK_LOOP_COUNT // special loop count for benchmarking
loopCnt = 100000000 / len;
loopCnt = (loopCnt < 20) ? 20 : loopCnt;
#endif
loopNum = 0;
uint32_t internalLoop;
internalLoop = 1;
uint32_t savedInternalLoopCnt = 1;
if (loopCnt > EXTERNAL_LOOP_COUNT_MAX)
{
savedInternalLoopCnt = loopCnt/EXTERNAL_LOOP_COUNT_MAX;
loopCnt = EXTERNAL_LOOP_COUNT_MAX;
}
printf(" block size= %d loop count= %d*%d\n", blockSize, loopCnt, savedInternalLoopCnt);
COMPRESS_LOOP:
internalLoop = savedInternalLoopCnt;
nBytesRemaining=(int32_t)len;
totalCompressedBytes=0;
srcBlockOffset=0;
dstBlockOffset=0;
// compress and write result
begin = clock();
while (internalLoop > 0)
{
while (nBytesRemaining > 0)
{
uint32_t nBlockBytes=(uint32_t)nBytesRemaining>=blockSize ? blockSize : (uint32_t)nBytesRemaining;
nCompressedBytes = td512(src+srcBlockOffset, dst+dstBlockOffset, nBlockBytes);
if (nCompressedBytes < 0)
exit(nCompressedBytes); // error occurred
nBytesRemaining -= nBlockBytes;
totalCompressedBytes += (uint32_t)nCompressedBytes;
srcBlockOffset += nBlockBytes;
dstBlockOffset += (uint32_t)nCompressedBytes;
}
if (--internalLoop > 0)
{
// perform some loops within the timing structure
totalCompressedBytes = 0;
nBytesRemaining=(int32_t)len;
srcBlockOffset=0;
dstBlockOffset=0;
}
}
end = clock();
timeSpent = (double)(end - begin) / (double)CLOCKS_PER_SEC;
if (timeSpent < minTimeSpent && timeSpent > 1.e-10)
minTimeSpent = timeSpent;
if (++loopNum < loopCnt)
{
usleep(10); // sleep 10 us
goto COMPRESS_LOOP;
}
timeSpent = minTimeSpent / savedInternalLoopCnt;
printf("compression=%.02f%% %.00f bytes per second inbytes=%lu outbytes=%u\n", (float)100*(1.0-((float)totalCompressedBytes/(float)len)), (float)len/(float)timeSpent, len, totalCompressedBytes);
#ifdef TD512_TEST_MODE
double totalBlocks=gExtendedTextCnt+gExtendedStringCnt+gtd64Cnt;
printf("TD512_TEST_MODE\n Extended text mode=%.01f%% Extended string mode= %.01f%% td64 =%.01f%%\n", (float)gExtendedTextCnt/totalBlocks*100, (float)gExtendedStringCnt/totalBlocks*100, (float)gtd64Cnt/totalBlocks*100);
#endif
fwrite(dst, totalCompressedBytes, 1, ofile);
fclose(ofile);
free(src);
free(dst);
// **********************
// decompress
ifile = fopen(ofileName, "rb");
strcpy(ofileName, argv[1]);
ofile = fopen(strcat(ofileName, ".td512d"), "wb");
// allocate source buffer
fseek(ifile, 0, SEEK_END);
len3 = (size_t)ftell(ifile);
fseek(ifile, 0, SEEK_SET);
src = (unsigned char*) malloc(len3);
// read file and allocate destination buffer
fread(src, 1, len3, ifile);
len2 = len; // output==input
dst = (unsigned char*) malloc(len2);
fclose(ifile);
minTimeSpent=600;
loopNum = 0;
DECOMPRESS_LOOP:
// decompress and write result
internalLoop = savedInternalLoopCnt;
totalOutBytes = 0;
nBytesRemaining = (int32_t)len3;
srcBlockOffset = 0;
dstBlockOffset = 0;
begin = clock();
while (internalLoop > 0)
{
while (nBytesRemaining > 0)
{
int32_t nRetBytes;
nRetBytes = td512d(src+srcBlockOffset, dst+dstBlockOffset, &bytesProcessed);
if (nRetBytes < 0)
return nRetBytes;
assert(nBytesRemaining>=blockSize?nRetBytes==blockSize:1);
nBytesRemaining -= bytesProcessed;
totalOutBytes += (uint32_t)nRetBytes;
srcBlockOffset += bytesProcessed;
dstBlockOffset += (uint32_t)nRetBytes;
}
if (--internalLoop > 0)
{
totalOutBytes = 0;
nBytesRemaining = (int32_t)len3;
srcBlockOffset = 0;
dstBlockOffset = 0;
}
}
end = clock();
timeSpent = (double)(end - begin) / (double)CLOCKS_PER_SEC;
if (timeSpent < minTimeSpent && timeSpent > 1.e-10)
minTimeSpent = timeSpent;
if (++loopNum < loopCnt)
{
usleep(10); // sleep 10 us
goto DECOMPRESS_LOOP;
}
timeSpent = minTimeSpent / savedInternalLoopCnt;
printf("decompression=%.00f bytes per second inbytes=%lu outbytes=%u\n", (float)len/(float)timeSpent, len3, totalOutBytes);
fwrite(dst, len, 1, ofile);
fclose(ofile);
free(src);
// verify original input file with decompressed output
ifile = fopen(argv[1], "rb");
if (!ifile)
{
printf("td512 error: file not found to verify with decompressed output file: %s\n", argv[1]);
return 9;
}
// allocate source buffer and read file
src = (unsigned char*) malloc(len);
fread(src, 1, len, ifile);
fclose(ifile);
if ((memcmp(src, dst, len)) != 0)
{
printf("td512 error: decompressed file differs from original input file\n");
free(src);
free(dst);
return 31;
}
free(src);
free(dst);
return 0;
}