cd6b0c5296d7e07c699e6a8852cdbc2500af0757
[dvdrip] / chkdvdimg.c
1 #include "lib.h"
2
3 static int status = 0;
4 static unsigned flags = 0;
5 #define F_FIX 1u
6
7 static void set_status(int st)
8 { if (st > status) status = st; }
9 static void vgripe_syserr(int st, int err, const char *fmt, va_list ap)
10 { vmoan_syserr(err, fmt, ap); set_status(st); }
11 static void vgripe(int st, const char *fmt, va_list ap)
12 { vgripe_syserr(st, 0, fmt, ap); }
13 PRINTF_LIKE(3, 4)
14 static void gripe_syserr(int st, int err, const char *fmt, ...)
15 {
16 va_list ap;
17 va_start(ap, fmt); vgripe_syserr(st, err, fmt, ap); va_end(ap);
18 }
19 PRINTF_LIKE(2, 3) static void gripe(int st, const char *fmt, ...)
20 { va_list ap; va_start(ap, fmt); vgripe(st, fmt, ap); va_end(ap); }
21
22 static int carefully_read(int fd, off_t off,
23 void *p, size_t sz, const char *file)
24 {
25 ssize_t n;
26 char *q = p;
27
28 if (lseek(fd, off, SEEK_SET) < 0)
29 { gripe_syserr(2, errno, "failed to seek in `%s'", file); return (-1); }
30
31 while (sz) {
32 n = read(fd, q, sz);
33 if (n < 0)
34 { gripe_syserr(2, errno, "failed to read `%s'", file); return (-1); }
35 else if (!n)
36 { gripe(2, "unexpected end-of-file reading `%s'", file); return (-1); }
37 q += n; sz -= n;
38 }
39 return (0);
40 }
41
42 typedef uint_least32_t u32;
43 #define Pu32 PRIuLEAST32
44
45 static u32 load32(const unsigned char *p)
46 {
47 return (((u32)p[0] << 0) | ((u32)p[1] << 8) |
48 ((u32)p[2] << 16) | ((u32)p[3] << 24));
49 }
50
51 #define CAHF_GRIPE 1u
52 #define CAHF_FULL 1u
53 static int check_anchor_header(const unsigned char *b, secaddr addr,
54 unsigned f, const char *file)
55 {
56 unsigned i, t;
57 secaddr n;
58
59 if (b[0] != 2) {
60 if (f&CAHF_GRIPE)
61 gripe(1, "anchor descriptor not found at `%s' sector %"PRIuSEC"",
62 file, addr);
63 return (-1);
64 }
65
66 for (i = 0, t = 0; i < 16; i++) if (i != 4) t += b[i];
67 t %= 256;
68 if (b[4] != t) {
69 if (f&CAHF_GRIPE)
70 gripe(1, "bad anchor descriptor header checksum (%u /= %u) "
71 "at `%s' sector %"PRIuSEC"", (unsigned)b[4], t, file, addr);
72 return (-1);
73 }
74
75 if (f&CAHF_FULL) {
76 n = load32(b + 12);
77 if (n != addr) {
78 if (f&CAHF_GRIPE)
79 gripe(1, "bad sector number in anchor descriptor "
80 "(%"PRIuSEC" /= %"PRIuSEC") in `%s'",
81 n, addr, file);
82 return (-1);
83 }
84 }
85
86 return (0);
87 }
88
89 static int all_zero_p(const unsigned char *p, size_t sz)
90 {
91 while (sz) {
92 if (*p) return (0);
93 p++; sz--;
94 }
95 return (1);
96 }
97
98 static void check_img(const char *file)
99 {
100 int fd = -1;
101 unsigned char b[SECTORSZ], bb[SECTORSZ];
102 off_t volsz;
103 int blksz;
104 secaddr end;
105 unsigned i, j;
106
107 open_dvd(file, (flags&F_FIX) ? O_RDWR : O_RDONLY, &fd, 0);
108 blksz = SECTORSZ; volsz = device_size(fd, file, &blksz);
109 if (SECTORSZ != 2048)
110 { gripe(2, "device sector size %d /= 2048", blksz); goto end; }
111 if (volsz%SECTORSZ) {
112 gripe(2, "bad length for `%s' -- not whole number of sectors", file);
113 goto end;
114 }
115 end = volsz/SECTORSZ;
116
117 if (carefully_read(fd, 256*SECTORSZ, b, SECTORSZ, file) ||
118 check_anchor_header(b, 256, CAHF_GRIPE | CAHF_FULL, file))
119 goto end;
120
121 for (i = 1; i < 32768; i++) {
122 if (carefully_read(fd, (off_t)(end - i)*SECTORSZ, bb, SECTORSZ, file))
123 goto end;
124 if (bb[0] || !all_zero_p(bb, SECTORSZ)) goto nonzero;
125 }
126 gripe(1, "too many trailing zero sectors: "
127 "couldn't find backup anchor descriptor in `%s'", file);
128 goto end;
129 nonzero:
130 j = i;
131 for (;;) {
132 if (bb[0] == 2 && !check_anchor_header(bb, end - j, 0, file))
133 break;
134 j++;
135 if (j > i + 256) {
136 gripe(1, "failed to find backup anchor descriptor in `%s'", file);
137 goto end;
138 }
139 if (carefully_read(fd, (off_t)(end - j)*SECTORSZ, bb, SECTORSZ, file))
140 goto end;
141 }
142 if (j > i) {
143 gripe(1, "found backup anchor descriptor at sector %"PRIuSEC" "
144 " = %"PRIuSEC" from end, = %"PRIuSEC" from trailing zeros",
145 end - j, j, j - i + 1);
146 i = j;
147 }
148
149 if (check_anchor_header(bb, end - i, CAHF_GRIPE | CAHF_FULL, file))
150 goto end;
151
152 if (MEMCMP(b + 16, != , bb + 16, SECTORSZ - 16)) {
153 gripe(1, "backup anchor descriptor in sector %"PRIuSEC" "
154 "doesn't match primary in sector 256 of `%s'",
155 end - i, file);
156 goto end;
157 }
158
159 if (i > 1) {
160 if (!(flags&F_FIX))
161 gripe(1, "%u trailing zero sectors in `%s'", i - 1, file);
162 else {
163 if (ftruncate(fd, (off_t)(end - i + 1)*SECTORSZ))
164 gripe_syserr(2, errno,
165 "failed to truncate `%s' to %"PRIuSEC" sectors",
166 file, end - i + 1);
167 else
168 moan("removed %u trailing zero sectors from `%s'", i - 1, file);
169 }
170 }
171
172 end:
173 if (fd != -1) close(fd);
174 }
175
176 static void usage(FILE *fp)
177 { fprintf(fp, "usage: %s [-x] IMG ...\n", prog); }
178
179 int main(int argc, char *argv[])
180 {
181 int i, opt;
182 unsigned f = 0;
183 #define f_bogus 1u
184
185 set_prog(argv[0]);
186 for (;;) {
187 opt = getopt(argc, argv, "hx"); if (opt < 0) break;
188 switch (opt) {
189 case 'h': usage(stdout); exit(0);
190 case 'x': flags |= F_FIX; break;
191 default: f |= f_bogus; break;
192 }
193 }
194 if (optind >= argc) f |= f_bogus;
195 if (f&f_bogus) { usage(stderr); exit(2); }
196 setlocale(LC_ALL, "");
197 progress_init(&progress);
198 for (i = optind; i < argc; i++) check_img(argv[i]);
199 progress_free(&progress);
200 return (status);
201 }