1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <mspack.h>
9 
10 #include <md5_fh.h>
11 #include <error.h>
12 
sortfunc(const void * a,const void * b)13 static int sortfunc(const void *a, const void *b) {
14   off_t diff =
15     ((* ((struct mschmd_file **) a))->offset) -
16     ((* ((struct mschmd_file **) b))->offset);
17   return (diff < 0) ? -1 : ((diff > 0) ? 1 : 0);
18 }
19 
main(int argc,char * argv[])20 int main(int argc, char *argv[]) {
21   struct mschm_decompressor *chmd;
22   struct mschmd_header *chm;
23   struct mschmd_file *file, **f;
24   unsigned int numf, i;
25   int err;
26 
27   setbuf(stdout, NULL);
28   setbuf(stderr, NULL);
29 
30   MSPACK_SYS_SELFTEST(err);
31   if (err) return 0;
32 
33   if ((chmd = mspack_create_chm_decompressor(&read_files_write_md5))) {
34     for (argv++; *argv; argv++) {
35       printf("*** %s\n", *argv);
36       if ((chm = chmd->open(chmd, *argv))) {
37 
38         /* extract in order of the offset into content section - faster */
39         for (numf=0, file=chm->files; file; file = file->next) numf++;
40         if ((f = (struct mschmd_file **) calloc(numf, sizeof(struct mschmd_file *)))) {
41           for (i=0, file=chm->files; file; file = file->next) f[i++] = file;
42           qsort(f, numf, sizeof(struct mschmd_file *), &sortfunc);
43           for (i = 0; i < numf; i++) {
44             if (chmd->extract(chmd, f[i], NULL)) {
45               fprintf(stderr, "%s: extract error on \"%s\": %s\n",
46                       *argv, f[i]->filename, ERROR(chmd));
47             }
48             else {
49               printf("%s %s\n", md5_string, f[i]->filename);
50             }
51           }
52           free(f);
53         }
54 
55         chmd->close(chmd, chm);
56       }
57       else {
58         fprintf(stderr, "%s: can't open -- %s\n", *argv, ERROR(chmd));
59       }
60     }
61     mspack_destroy_chm_decompressor(chmd);
62   }
63   else {
64     fprintf(stderr, "%s: can't make CHM decompressor\n", *argv);
65   }
66   return 0;
67 }
68