-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckcrc.c
More file actions
164 lines (143 loc) · 3.45 KB
/
checkcrc.c
File metadata and controls
164 lines (143 loc) · 3.45 KB
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
/**
* checkcrc - crc32 checksum comparing of checksums found in file names
* Copyright (C) 2009 Daniel Triendl <daniel@pew.cc>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <inttypes.h>
#include <regex.h>
#include <unistd.h>
#include "checkcrc.h"
#include "crc32.h"
/* Print file usage */
void usage()
{
printf("Usage: %s [-cvh] [file ...]\n"
"\n"
"\t-c\tOnly calculate checksums, don't try to find a checksum string to compare\n"
"\t-v\tDisplay version number\n"
"\t-g\tDisplay this help\n", PACKAGE);
}
/* Try to find a CRC32-like hex number in a string */
int crc32_match(const char *string, char *match) {
int status;
regex_t re;
regmatch_t rm[1];
/* Try to complie the regex */
if(regcomp(&re, "[a-fA-F0-9]{8}", REG_EXTENDED) != 0) {
return 0;
}
/* Run the regex against the string */
status = regexec(&re, string, 1, rm, 0);
regfree(&re);
if(status != 0) {
return 0;
}
/* If we got a match, copy matching string to match */
strncpy(match, string + rm[0].rm_so, 8);
return 1;
}
/* Test if the file exists */
int file_exists(char *file)
{
FILE *f;
if((f = fopen(file, "r"))) {
fclose(f);
return 1;
}
return 0;
}
/* Gets the last component of a path *
char *basename(char *name)
{
char *np;
for(np = name; *name; name++) {
if(*name == '/') np = name + 1;
}
return (char *)np;
}*/
/* The main routine */
int main(int argc, char **argv)
{
int i, iexit = 0;
uint32_t crc_found, crc_calc;
char result[9] = "";
char ch, mode = 0;
if(argc <= 1) {
usage(argv[0]);
exit(1);
}
while ((ch = getopt(argc, argv, "chv")) != -1)
switch (ch) {
case 'h':
usage();
exit(0);
break;
case 'c':
mode = 1;
break;
case 'v':
printf("%s: Version %s\n", PACKAGE, VERSION);
exit(0);
break;
}
/* Generate the CRC32 table */
crc32_genTab();
/* Loop through the files */
for(i=optind; i<argc; i++) {
printf("%s: ", argv[i]);
if(file_exists(argv[i])) {
if(mode == 0) {
/* Searching for CRC32 checksum */
if (crc32_match(basename(argv[i]), result)) {
/*
* If we found something that looks like a CRC checksume
* calculate the CRC32 value of the file
*/
crc_found = strtoll(result, NULL, 16);
if (crc32(argv[i], &crc_calc)) {
printf("%08X | %08X: ", crc_found, crc_calc);
if (crc_found == crc_calc) {
printf("OK\n");
} else {
printf("CRC ERROR!\n");
iexit = 1;
}
} else {
printf( "Error calculating CRC32\n");
}
} else {
printf("CRC not found\n");
}
} else if(mode==1) {
if(crc32(argv[i], &crc_calc)) {
printf("%08X\n", crc_calc);
} else {
printf("CRC ERROR!\n");
iexit = 1;
}
}
} else {
printf("not found\n");
}
}
exit(iexit);
}