1 /* chmd_find: tests fast-find functionality
2 */
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <mspack.h>
11
12 #include <error.h>
13 #include <system.h>
14
find(struct mschm_decompressor * chmd,struct mschmd_header * chm,char * archive,char * filename,struct mschmd_file * compare)15 void find(struct mschm_decompressor *chmd, struct mschmd_header *chm,
16 char *archive, char *filename, struct mschmd_file *compare)
17 {
18 struct mschmd_file result;
19 if (chmd->fast_find(chmd, chm, filename, &result, sizeof(result))) {
20 fprintf(stderr, "%s: find error on \"%s\": %s\n",
21 archive, filename, ERROR(chmd));
22 }
23 else if (!result.section) {
24 if (compare) {
25 fprintf(stderr, "%s: file \"%s\" not found\n", archive, filename);
26 }
27 else {
28 printf("%s: file \"%s\" not found\n", archive, filename);
29 }
30 }
31 else {
32 printf("%s\n", filename);
33 printf(" section: %d\n", result.section->id);
34 printf(" offset: %" LD "\n", result.offset);
35 printf(" length: %" LD "\n", result.length);
36 if (compare) {
37 if (result.section->id != compare->section->id) {
38 fprintf(stderr, "%s: found file \"%s\" section is wrong "
39 "(%d vs %d)\n", archive, filename,
40 result.section->id, compare->section->id);
41 }
42
43 if (result.offset != compare->offset) {
44 fprintf(stderr, "%s: found file \"%s\" offset is wrong "
45 "(%" LD " vs %" LD ")\n", archive, filename,
46 result.offset, compare->offset);
47 }
48
49 if (result.length != compare->length) {
50 fprintf(stderr, "%s: found file \"%s\" length is wrong "
51 "(%" LD " vs %" LD ")\n", archive, filename,
52 result.length, compare->length);
53 }
54 }
55 }
56 }
57
main(int argc,char * argv[])58 int main(int argc, char *argv[]) {
59 struct mschm_decompressor *chmd;
60 struct mschmd_header *chm, *chm2;
61 unsigned int i;
62
63 if (argc < 2 || argc > 3) {
64 printf("Usage: %s <file.chm> [filename to find]\n", argv[0]);
65 return 1;
66 }
67
68 MSPACK_SYS_SELFTEST(i);
69 if (i) return 0;
70
71 if ((chmd = mspack_create_chm_decompressor(NULL))) {
72 if ((chm = chmd->fast_open(chmd, argv[1]))) {
73 if (argv[2]) {
74 find(chmd, chm, argv[1], argv[2], NULL);
75 }
76 else {
77 if ((chm2 = chmd->open(chmd, argv[1]))) {
78 struct mschmd_file *file;
79 for (file = chm2->files; file; file = file->next) {
80 find(chmd, chm, argv[1], file->filename, file);
81 }
82 }
83 else {
84 printf("%s: can't open -- %s\n", argv[1], ERROR(chmd));
85 }
86 }
87 chmd->close(chmd, chm);
88 }
89 else {
90 printf("%s: can't open -- %s\n", argv[1], ERROR(chmd));
91 }
92 mspack_destroy_chm_decompressor(chmd);
93 }
94 return 0;
95 }
96