-
Notifications
You must be signed in to change notification settings - Fork 13
/
synochecksum.c
384 lines (270 loc) · 8.42 KB
/
synochecksum.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
$Id: synochecksum.c,v 1.7 2008/06/03 20:06:05 flip Exp flip $
synochecksum.c -- calculates a checksum.syno file
Written 2008 by flipflip and Emmanuel
CRC32 routines based on:
efone - Distributed internet phone system.
(c) 1999,2000 Krzysztof Dabrowski
(c) 1999,2000 ElysiuM deeZine
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.
All trademarks mentioned in this file or any other file or program
contained in this package are the property of their respective owners.
Background:
-----------
Synology's (http://www.synology.com) NAS devices (DiskStation, CubeStation
and RackStation series) use tar(1) for "firmware patch (.pat) files". See
http://oinkzwurgl.org/diskstation_patchfiles for details. The patch file must
contain a checksum.syno file with some checksums.
The checksum.syno file has the following format. Each file in the .pat
archive is listed on a separate line which consists of five fields. They are:
crc2 (decimal), filesize (in bytes), filename, x and y. While the first three
fields are trivial to determine, the numbers a and b are a bit more
complicated (see synock() routine below).
This programme outputs a valid checksum.syno file on the standard output for
all files given on the command line (see -h for usage instructions).
*/
/* we need some funky functions */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <err.h>
#include <stdint.h>
#include <dirent.h>
/* programme meta data */
#define PROG_NAME "synochecksum"
#define PROG_INFO "Calculates checksum.syno files."
#define PROG_VERSION "1.0"
#define PROG_AUTHOR "Written 2008 by flipflip and Emmanuel"
#define PROG_REV "\
$Id: synochecksum.c,v 1.7 2008/06/03 20:06:05 flip Exp flip $"
/* constants */
#define BUFSIZE 0x7FFF /* 32k file read buffer (configurable) */
#define CRC32INIT 0xFFFFFFFFL /* for crc32() */
#define CRC32POLY 0xEDB88320L /* for crc32gentab() */
#define RAND1INIT 1804289383 /* result of random() with prior srandom(1) */
/* lookup table for the CRC32 routine */
u_int32_t crc_tab[256];
/* routine to initialise the CRC32 table */
void crc32gentab()
{
int i, j;
for (i = 0; i < 256; i++)
{
crc_tab[i] = i;
for (j = 8; j > 0; j--)
{
if (crc_tab[i] & 1)
{
crc_tab[i] = (crc_tab[i] >> 1) ^ CRC32POLY;
}
else
{
crc_tab[i] >>= 1;
}
}
}
}
/* routine to calculate the CRC32 for a block of a file */
u_int32_t crc32(int fd)
{
unsigned char buf[BUFSIZE], *tmp;
u_int32_t crc = CRC32INIT;
ssize_t bytes_read;
while ((bytes_read = read(fd, buf, BUFSIZE)) > 0)
{
tmp = buf;
while (bytes_read--)
{
crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *tmp++) & 0xFF];
}
}
return crc ^ CRC32INIT;
}
/* routine to get the size of a file */
size_t filesize(int fd)
{
struct stat buf;
if ((fstat(fd, &buf)) == -1)
err(EXIT_FAILURE, "Cannot determine file size.");
return buf.st_size;
}
/* routine to determine Syno's magic "checksums" of a file*/
void synock(int fd, size_t filesize, int *x, int *y)
{
#ifndef SYNOCKVARIANT
#define SYNOCKVARIANT 1
#endif
#if SYNOCKVARIANT == 1
#warning "Using flipflip's synock() variant #1"
off_t offset;
unsigned char ybuf[4];
/* determine y (offset of the magic bytes and sum them up) */
offset = RAND1INIT % filesize;
if (filesize - offset < 4) offset = filesize - 5;
if (offset == 0) offset = 1;
if (offset < 0) offset = 0;
lseek(fd, offset, SEEK_SET);
bzero(ybuf, sizeof(ybuf));
read(fd, ybuf, sizeof(ybuf));
*y = ybuf[0] + ybuf[1] + ybuf[2] + ybuf[3];
/* the x magic number */
*x = offset + filesize;
#elif SYNOCKVARIANT == 2
#warning "Using Emmanuel's synock() variant #1"
off_t offset;
unsigned char ybuf[4];
int r, bytes_to_read;
srand(1);
r = random();
offset = r % filesize;
if (offset + 4 > filesize)
offset = (filesize > 3) ? filesize - 4 : 0;
lseek(fd, offset, SEEK_SET);
*x = offset + filesize;
bzero(ybuf, 4);
bytes_to_read = filesize > 3 ? 4 : filesize;
read(fd, ybuf, bytes_to_read);
*y = ybuf[0] + ybuf[1] + ybuf[2] + ybuf[3];
#else
#error "Illegal value of SYNOCKVARIANT"
#endif
}
/* returns 1 for filtered files, 2 for directories */
int filter(char *fn)
{
DIR *d;
int l;
l = strlen(fn);
if (strcmp(fn, "checksum.syno") == 0) return 1;
if (fn[l-1] == '~') return 1;
if (fn[l-4] == '.' && fn[l-3] == 'p' && fn[l-2] == 'a' && fn[l-1] == 't')
return 1;
if ((d = opendir(fn)) == NULL) return 0;
closedir(d);
return 2;
}
/* prints usage information */
void usage()
{
fprintf(stderr, "\n%s %s -- %s\n\n%s\n\n",
PROG_NAME, PROG_VERSION, PROG_INFO, PROG_AUTHOR);
fprintf(stderr, "\
Usage: %s [-q] <file1> [<file2> ...]\n\
\n", PROG_NAME);
fprintf(stderr, "\
Computes the checksums of the files given on the command line and\n\
outputs a checksum.syno file on stdout.\n\n\
Files matching \"checksum.syno\", \"*.pat\" and \"*~\", as well as\n\
directories, are ignored.\n\n\
Therefore, '%s * > checksum.syno' creates and updates\n\
the checksum file.\n\n\
The -q flag (as the first argument) enables quiet operation.\n\n\
The programme returns %i on success and %i on errors.\n",
PROG_NAME, EXIT_SUCCESS, EXIT_FAILURE);
fprintf(stderr, "\n\
The CRC32 routines used in this programme are based on:\n\
efone - Distributed internet phone system.\n\
(c) 1999,2000 Krzysztof Dabrowski and ElysiuM deeZine\n\
\n\
This program is free software; you can redistribute it and/or\n\
modify it under the terms of the GNU General Public License\n\
as published by the Free Software Foundation; either version\n\
2 of the License, or (at your option) any later version.\n\n");
exit(EXIT_FAILURE);
}
/* main programme */
int main(int argc, char **argv)
{
char *fn; /* file name */
u_int32_t crc; /* CRC32 of the file */
size_t fs; /* size of the file */
int x, y; /* Syno's "checksums" */
size_t sumfs; /* [fs] */
int sumx, sumy; /* [x], [y] */
int fd; /* file descriptor */
int quiet; /* -q flag */
int argp; /* position of first file name argument */
int nfiles; /* number of files processed */
// int r;
/* check command line arguments */
if (argc > 1 && strcmp(argv[1], "-q"))
{
quiet = 0; argp = 1;
}
else
{
quiet = 1; argp = 2;
}
if (argc > argp && strcmp(argv[argp], "-h") == 0)
{
usage();
}
if (argc < argp + 1)
{
fprintf(stderr, "%s %s -- %s\n",
PROG_NAME, PROG_VERSION, PROG_AUTHOR);
fprintf(stderr, "Try '%s -h'.\n", PROG_NAME);
exit(EXIT_FAILURE);
}
/* say hello */
if (!quiet)
fprintf(stderr, "%s %s -- %s\n",
PROG_NAME, PROG_VERSION, PROG_AUTHOR);
/* initialise CRC32 table */
crc32gentab();
/* process all files */
sumfs = 0; sumx = 0; sumy = 0; nfiles = 0;
while (argp < argc)
{
/* next file name */
fn = argv[argp++];
if (filter(fn))
{
if (!quiet)
fprintf(stderr, "Ignoring %s.\n", fn);
continue;
}
if (!quiet)
fprintf(stderr, "Processing %s ..\n", fn);
/* open file */
if ((fd = open(fn, O_RDONLY)) == -1)
err(EXIT_FAILURE, "%s", fn);
/* calculate checksums */
fs = filesize(fd);
if (fs == 0)
{
/* special case for filesize == 0 */
crc = 0;
x = 0;
y = 2767;
}
else
{
crc = crc32(fd);
synock(fd, fs, &x, &y);
}
printf("%ju %ju %s %i %i\n", (intmax_t)crc, (intmax_t)fs, fn, x, y);
/* sum up */
sumfs += fs;
sumx += x;
sumy += y;
nfiles++;
/* close file */
if ((fd = close(fd)) == -1)
warn("%s", fn);
}
/* print sums */
printf("#Synocksum %ju %i %i\n", (intmax_t)sumfs, sumx, sumy);
/* goodbye */
if (!quiet)
fprintf(stderr, "%i files processed.\n", nfiles);
exit(EXIT_SUCCESS);
}
/* eof */