-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cfile.c
118 lines (102 loc) · 3.29 KB
/
test_cfile.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
// simple test script for cfile
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "cfile.h"
const char filename[] = "./test/test.tif";
const char filenamegz[] = "./test/test.tif.gz";
const char filenamebz[] = "./test/test.tif.bz2";
const char filenamexz[] = "./test/test.tif.xz";
const char filenamezstd[] = "./test/test.tif.zstd";
int main(void) {
// read in the raw file using standard read calls
FILE* fid;
if (fopen_s(&fid, filename, "rb") != 0) {
printf_s("Error opening file using fopen_s\n");
return 1;
}
// get file stats
struct _stat64 fstats;
if (_fstat64(_fileno(fid), &fstats) != 0) {
printf_s("Error getting file stats\n");
return 2;
}
size_t filesize = fstats.st_size;
// initialize buffers for orignal file and for cfile test
char* buf1 = malloc(filesize);
char* buf2 = malloc(filesize);
// read raw file
size_t n1 = fread_s(buf1, filesize, 1, filesize, fid);
fclose(fid);
if (n1 != filesize) {
printf_s("Error reading file with fread_s\n");
return 3;
}
// get a crc32 checksum
uint32_t crc1 = crc32(0L, Z_NULL, 0);
crc1 = crc32(crc1, (Bytef*) buf1, (uInt) filesize);
printf_s("CRC32: %32s: %X\n", "Raw file with native calls", crc1);
// now read the raw file with cfile
cfid_s* cfid = calloc(1, sizeof(cfid_s));
if (cfopen_s(cfid, filename, "rb") != 0) {
printf_s("Error opening raw file with cfopen_s\n");
return 4;
}
// read the file
size_t n2 = (*(cfid->cfread_s))(buf2, filesize, 1, filesize, cfid);
if (n2 != filesize) {
printf_s("Error reading raw file with cfread_s\n");
return 5;
}
(*(cfid->cfclose))(cfid);
uint32_t crc2 = crc32(0L, Z_NULL, 0);
crc2 = crc32(crc2, (Bytef*) buf2, (uInt) filesize);
printf_s("CRC32: %32s: %X\n", "Raw file with cfile", crc2);
// verify files are equal
if (memcmp(buf1, buf2, filesize) != 0) {
printf("Error: native raw file doesn't match raw file with cfile\n");
}
// read the gz file
cfid = memset(cfid, 0, sizeof(cfid));
if (cfopen_s(cfid, filenamegz, "rb") != 0) {
printf_s("Error opening gz file with cfopen_s\n");
return 4;
}
n2 = (*(cfid->cfread_s))(buf2, filesize, 1, filesize, cfid);
if (n2 != filesize) {
printf_s("Error reading gz file with cfread_s\n");
return 5;
}
(*(cfid->cfclose))(cfid);
crc2 = crc32(0L, Z_NULL, 0);
crc2 = crc32(crc2, (Bytef*) buf2, (uInt) filesize);
printf_s("CRC32: %32s: %X\n", "GZ file with cfile", crc2);
// verify files are equal
if (memcmp(buf1, buf2, filesize) != 0) {
printf("Error: native raw file doesn't match gz file with cfile\n");
return 6;
}
// read the bz file
cfid = memset(cfid, 0, sizeof(cfid));
if (cfopen_s(cfid, filenamebz, "rb") != 0) {
printf_s("Error opening bz file with cfopen_s\n");
return 4;
}
n2 = (*(cfid->cfread_s))(buf2, filesize, 1, filesize, cfid);
if (n2 != filesize) {
printf_s("Error reading bz file with cfread_s\n");
return 5;
}
(*(cfid->cfclose))(cfid);
crc2 = crc32(0L, Z_NULL, 0);
crc2 = crc32(crc2, (Bytef*) buf2, (uInt) filesize);
printf_s("CRC32: %32s: %X\n", "BZ file with cfile", crc2);
// verify files are equal
if (memcmp(buf1, buf2, filesize) != 0) {
printf("Error: native raw file doesn't match bz file with cfile\n");
return 6;
}
free(buf1);
free(buf2);
return 0;
}