~ubuntu-branches/ubuntu/oneiric/isomd5sum/oneiric

« back to all changes in this revision

Viewing changes to libcheckisomd5.c

  • Committer: Bazaar Package Importer
  • Author(s): Ryan Finnie
  • Date: 2008-02-12 20:50:55 UTC
  • mfrom: (1.1.3 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080212205055-ok7t538if9lctyws
Tags: 1:1.0.4-1
* New upstream version.
* Epoch added, upstream re-worked into standalone package
* debian/control: new homepage/git
* debian/control: clarified description
* Removed sgml documentation (manpages moved upstream)
* Added watch file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright 2001 Red Hat, Inc.                                    */
2
 
/* Michael Fulbright msf@redhat.com                                */
 
1
/*
 
2
 * Copyright (C) 2001-2007 Red Hat, Inc.
 
3
 *
 
4
 * Michael Fulbright <msf@redhat.com>
 
5
 * Dustin Kirkland  <dustin.dirkland@gmail.com>
 
6
 *      Added support for checkpoint fragment sums;                
 
7
 *      Exits media check as soon as bad fragment md5sum'ed        
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU General Public License
 
11
 * as published by the Free Software Foundation; either version 2
 
12
 * of the License, or (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
22
 */
3
23
 
4
 
/*   4/2005     Dustin Kirkland (dustin.kirkland@gmail.com)        */
5
 
/*      Added support for checkpoint fragment sums;                */
6
 
/*      Exits media check as soon as bad fragment md5sum'ed        */
7
24
 
8
25
#include <stdio.h>
9
26
#include <stdlib.h>
158
175
/* returns -1 if no checksum encoded in media, 0 if no match, 1 if match */
159
176
/* mediasum is the sum encoded in media, computedsum is one we compute   */
160
177
/* both strings must be pre-allocated at least 33 chars in length        */
161
 
static int checkmd5sum(int isofd, char *mediasum, char *computedsum, int flags) {
 
178
static int checkmd5sum(int isofd, char *mediasum, char *computedsum, checkCallback cb, void *cbdata) {
162
179
    int nread;
163
180
    int i, j;
164
181
    int appdata_start_offset, appdata_end_offset;
167
184
    int supported;
168
185
    int current_fragment = 0;
169
186
    int previous_fragment = 0;
170
 
    int printed_frag_status = 0;
171
187
    unsigned int bufsize = 32768;
172
188
    unsigned char md5sum[16];
173
189
    unsigned char fragmd5sum[16];
178
194
    char thisfragsum[FRAGMENT_SUM_LENGTH];
179
195
    long long fragmentcount = 0;
180
196
    MD5_CTX md5ctx, fragmd5ctx;
181
 
    int quiet;
182
 
    int gauge;
183
 
    int gaugeat = -1;
184
 
    int gaugeval;
185
 
 
186
 
    quiet = (((flags & 1) == 1) ? 1 : 0); /* bit 1: quiet */
187
 
    gauge = (((flags & 2) == 2) ? 1 : 0); /* bit 2: gauge */
188
197
 
189
198
    if ((pvd_offset = parsepvd(isofd, mediasum, &skipsectors, &isosize, &supported, fragmentsums, &fragmentcount)) < 0)
190
199
        return -1;
200
209
    apoff = pvd_offset + APPDATA_OFFSET;
201
210
 
202
211
    buf = malloc(bufsize * sizeof(unsigned char));
203
 
    if (!quiet) {
204
 
        printf("Percent complete: %05.1f%%", (100.0*offset)/(isosize-skipsectors*2048.0));
205
 
        fflush(stdout);
206
 
    }
 
212
    if (cb)
 
213
        cb(cbdata, 0, isosize - skipsectors*2048);
207
214
 
208
215
    while (offset < isosize - skipsectors*2048) {
209
216
        nattempt = MIN(isosize - skipsectors*2048 - offset, bufsize);
254
261
                    thisfragsum[i] = fragmentsums[j++];
255
262
                }
256
263
                thisfragsum[j] = '\0';
257
 
                if (!quiet) {
258
 
                    printf("   Fragment[%02i/%02lld] -> OK", previous_fragment+1, fragmentcount);
259
 
                    printed_frag_status = 1;
260
 
                    fflush(stdout);
261
 
                }
262
264
                previous_fragment = current_fragment;
263
265
                /* Exit immediately if current fragment sum is incorrect */
264
266
                if (strcmp(thisfragsum, computedsum) != 0) {
265
 
                    if (!quiet) {
266
 
                        printf("\nFragment %02i of %02lld is BAD!\n", previous_fragment+1, fragmentcount);
267
 
                    }
268
 
                    free(buf);
269
267
                    return 0;
270
268
                }
271
269
            }
272
270
        }
273
271
        offset = offset + nread;
274
 
        
275
 
        if (!quiet) {
276
 
            if (printed_frag_status) {
277
 
                printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
278
 
                printed_frag_status = 0;
279
 
            }
280
 
            printf("\b\b\b\b\b\b%05.1f%%", (100.0*offset)/(isosize-skipsectors*2048.0));
281
 
            fflush(stdout);
282
 
        }
283
 
        if (gauge) {
284
 
            gaugeval = (100.0*offset)/(isosize-skipsectors*2048.0);
285
 
            if (gaugeval != gaugeat) {
286
 
                printf("%d\n", gaugeval);
287
 
                fflush(stdout);
288
 
                gaugeat = gaugeval;
289
 
            }
290
 
        }
 
272
        if (cb)
 
273
            cb(cbdata, offset, isosize - skipsectors*2048);
291
274
    }
292
275
 
293
 
    if (!quiet) {
294
 
        printf("\b\b\b\b\b\b\n%05.1f\n", (100.0*offset)/(isosize-skipsectors*2048.0));
295
 
    }
 
276
    if (cb)
 
277
        cb(cbdata, isosize, isosize - skipsectors*2048);
296
278
 
297
279
    sleep(1);
298
280
 
316
298
}
317
299
 
318
300
 
319
 
#if 0
320
 
static void readCB(void *co, long long pos) {
321
 
    struct progressCBdata *data = co;
322
 
    static int tick = 0;
323
 
    char tickmark[2] = "-";
324
 
    char * ticks = "-\\|/";
325
 
 
326
 
    newtScaleSet(data->scale, pos);
327
 
    tick++;
328
 
    if (tick > 399) tick = 0;
329
 
    *tickmark = ticks[tick / 100];
330
 
 
331
 
    newtLabelSetText(data->label, tickmark);
332
 
    newtRefresh();
333
 
}
334
 
#endif
335
 
 
336
 
static int doMediaCheck(int isofd, char *mediasum, char *computedsum, long long *isosize, int *supported, int flags) {
 
301
static int doMediaCheck(int isofd, char *mediasum, char *computedsum, long long *isosize, int *supported, checkCallback cb, void *cbdata) {
337
302
    int rc;
338
303
    int skipsectors;
339
304
    long long fragmentcount = 0;
347
312
        return -1;
348
313
    }
349
314
 
350
 
    rc = checkmd5sum(isofd, mediasum, computedsum, flags);
 
315
    rc = checkmd5sum(isofd, mediasum, computedsum, cb, cbdata);
351
316
 
352
317
    return rc;
353
318
}
354
319
 
355
 
int mediaCheckFile(char *file, int flags) {
 
320
int mediaCheckFile(char *file, checkCallback cb, void *cbdata) {
356
321
    int isofd;
357
322
    int rc;
358
 
    char *result;
359
323
    char mediasum[33], computedsum[33];
360
324
    long long isosize;
361
325
    int supported;
362
 
    int quiet;
363
 
 
364
 
    quiet = (((flags & 1) == 1) ? 1 : 0); /* bit 1: quiet */
365
326
 
366
327
    isofd = open(file, O_RDONLY);
367
328
 
370
331
        return -1;
371
332
    }
372
333
 
373
 
    rc = doMediaCheck(isofd, mediasum, computedsum, &isosize, &supported, flags);
 
334
    rc = doMediaCheck(isofd, mediasum, computedsum, &isosize, &supported, cb, cbdata);
374
335
 
375
336
    close(isofd);
376
337
 
377
338
    /*    printf("isosize = %lld\n", isosize); 
378
339
          printf("%s\n%s\n", mediasum, computedsum);*/
379
340
 
380
 
    /*    if (!quiet)
381
 
          fprintf(stderr, "The supported flag value is %d\n", supported);*/
382
 
 
383
 
    if (rc == 0)
384
 
        result = "FAIL.\n\nIt is not recommended to use this media.";
385
 
    else if (rc > 0)
386
 
        result = "PASS.\n\nIt is OK to use this media.";
387
 
    else
388
 
        result = "NA.\n\nNo checksum information available, unable to verify media.";
389
 
 
390
 
    if (!quiet)
391
 
        fprintf(stderr, "The media check is complete, the "
392
 
                "result is: %s\n", result);
393
 
 
394
341
    return rc;
395
342
}
396
343