-
Notifications
You must be signed in to change notification settings - Fork 0
/
winchm.c
1434 lines (1259 loc) · 54.7 KB
/
winchm.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* winchm.c: direct output of .CHM files.
*/
#include <assert.h>
#include <stdio.h>
#include "halibut.h"
#include "tree234.h"
#include "lzx.h"
#define PUT_32BIT_LSB_FIRST(cp, value) do { \
((unsigned char *)cp)[0] = 0xFF & (value); \
((unsigned char *)cp)[1] = 0xFF & ((value) >> 8); \
((unsigned char *)cp)[2] = 0xFF & ((value) >> 16); \
((unsigned char *)cp)[3] = 0xFF & ((value) >> 24); } while (0)
#define PUT_32BIT_MSB_FIRST(cp, value) do { \
((unsigned char *)cp)[3] = 0xFF & (value); \
((unsigned char *)cp)[2] = 0xFF & ((value) >> 8); \
((unsigned char *)cp)[1] = 0xFF & ((value) >> 16); \
((unsigned char *)cp)[0] = 0xFF & ((value) >> 24); } while (0)
#define PUT_16BIT_LSB_FIRST(cp, value) do { \
((unsigned char *)cp)[0] = 0xFF & (value); \
((unsigned char *)cp)[1] = 0xFF & ((value) >> 8); } while (0)
#define RDADD_32BIT_LSB_FIRST(rs, value) do { \
unsigned char out[4]; \
PUT_32BIT_LSB_FIRST(out, value); \
rdaddsn(rs, (void *)out, sizeof(out)); \
} while (0)
#define RDADD_32BIT_MSB_FIRST(rs, value) do { \
unsigned char out[4]; \
PUT_32BIT_MSB_FIRST(out, value); \
rdaddsn(rs, (void *)out, sizeof(out)); \
} while (0)
#define RDADD_16BIT_LSB_FIRST(rs, value) do { \
unsigned char out[2]; \
PUT_16BIT_LSB_FIRST(out, value); \
rdaddsn(rs, (void *)out, sizeof(out)); \
} while (0)
static void guid(rdstringc *rs, unsigned long w0,
unsigned short h0, unsigned short h1,
unsigned char b0, unsigned char b1,
unsigned char b2, unsigned char b3,
unsigned char b4, unsigned char b5,
unsigned char b6, unsigned char b7)
{
RDADD_32BIT_LSB_FIRST(rs, w0);
RDADD_16BIT_LSB_FIRST(rs, h0);
RDADD_16BIT_LSB_FIRST(rs, h1);
rdaddc(rs, b0);
rdaddc(rs, b1);
rdaddc(rs, b2);
rdaddc(rs, b3);
rdaddc(rs, b4);
rdaddc(rs, b5);
rdaddc(rs, b6);
rdaddc(rs, b7);
}
static void itsf(rdstringc *rs,
const rdstringc *directory, const rdstringc *content0)
{
int headersize_field;
int headersect_off, headersect_off_field, headersect_size_field;
int directory_off_field, content0_off_field, filesize_field;
/* Main file header */
rdaddsc(rs, "ITSF"); /* main file magic number */
RDADD_32BIT_LSB_FIRST(rs, 3); /* file format version */
headersize_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* size of main header; fill in later */
RDADD_32BIT_LSB_FIRST(rs, 1); /* unknown, always observed to be 1 */
RDADD_32BIT_MSB_FIRST(rs, 0x12345678); /* timestamp (FIXME) */
RDADD_32BIT_LSB_FIRST(rs, 0x809); /* language code (FIXME: configurable) */
guid(rs,0x7C01FD10,0x7BAA,0x11D0,0x9E,0x0C,0x00,0xA0,0xC9,0x22,0xE6,0xEC);
guid(rs,0x7C01FD11,0x7BAA,0x11D0,0x9E,0x0C,0x00,0xA0,0xC9,0x22,0xE6,0xEC);
headersect_off_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* header section offset; fill in later */
RDADD_32BIT_LSB_FIRST(rs, 0); /* MSW of 64-bit field */
headersect_size_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* header section size; fill in later */
RDADD_32BIT_LSB_FIRST(rs, 0); /* MSW of 64-bit field */
directory_off_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* directory offset; fill in later */
RDADD_32BIT_LSB_FIRST(rs, 0); /* MSW of 64-bit field */
RDADD_32BIT_LSB_FIRST(rs, directory->pos);
RDADD_32BIT_LSB_FIRST(rs, 0); /* MSW of 64-bit field */
content0_off_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* content section 0 offset; fill in later */
RDADD_32BIT_LSB_FIRST(rs, 0); /* MSW of 64-bit field */
PUT_32BIT_LSB_FIRST(rs->text + headersize_field, rs->pos);
/* 'Header section' */
headersect_off = rs->pos;
PUT_32BIT_LSB_FIRST(rs->text + headersect_off_field, rs->pos);
RDADD_32BIT_LSB_FIRST(rs, 0x1FE); /* magic number */
RDADD_32BIT_LSB_FIRST(rs, 0); /* unknown, always observed to be 0 */
filesize_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* file size; fill in later */
RDADD_32BIT_LSB_FIRST(rs, 0); /* MSW of 64-bit field */
RDADD_32BIT_LSB_FIRST(rs, 0); /* unknown, always observed to be 0 */
RDADD_32BIT_LSB_FIRST(rs, 0); /* unknown, always observed to be 0 */
PUT_32BIT_LSB_FIRST(rs->text + headersect_size_field,
rs->pos - headersect_off);
PUT_32BIT_LSB_FIRST(rs->text + directory_off_field, rs->pos);
rdaddsn(rs, directory->text, directory->pos);
PUT_32BIT_LSB_FIRST(rs->text + content0_off_field, rs->pos);
rdaddsn(rs, content0->text, content0->pos);
PUT_32BIT_LSB_FIRST(rs->text + filesize_field, rs->pos);
}
static void encint(rdstringc *rs, unsigned val)
{
int i, j, topbit;
/* ENCINT in the CHM format is big-endian, but it's easier to
* write little-endian and byte-reverse afterwards. */
i = rs->pos; /* first byte index */
topbit = 0;
while (val >= 0x80) {
rdaddc(rs, (val & 0x7F) | topbit);
val >>= 7;
topbit = 0x80;
}
j = rs->pos; /* last byte index */
rdaddc(rs, val | topbit);
while (j > i) {
char tmp = rs->text[i];
rs->text[i] = rs->text[j];
rs->text[j] = tmp;
i++;
j--;
}
}
struct chm_directory_entry {
char *filename; /* free this when done */
int which_content_section;
int offset_in_content_section;
int file_size;
};
static int strcmp_chm(const char *a, const char *b)
{
/*
* CHM directory sorting criterion appears to be case-insensitive,
* and based on sorting the _lowercased_ text. (Hence, in
* particular, '_' sorts before any alphabetic character.)
*/
while (*a || *b) {
char ac = *a, bc = *b;
if (ac >= 'A' && ac <= 'Z') ac += 'a'-'A';
if (bc >= 'A' && bc <= 'Z') bc += 'a'-'A';
if (ac != bc)
return ac < bc ? -1 : +1;
a++;
b++;
}
return 0;
}
int chm_directory_entry_cmp(const void *av, const void *bv, void *cmpctx)
{
const struct chm_directory_entry
*a = (const struct chm_directory_entry *)av,
*b = (const struct chm_directory_entry *)bv;
return strcmp_chm(a->filename, b->filename);
}
int chm_directory_entry_find(const void *av, const void *bv, void *cmpctx)
{
const char *a = (const char *)av;
const struct chm_directory_entry
*b = (const struct chm_directory_entry *)bv;
return strcmp_chm(a, b->filename);
}
struct chm_index_entry {
char *first_filename; /* shared pointer with some chm_directory_entry */
int chunk_index;
};
static void directory(rdstringc *rs, tree234 *files)
{
const int chunksize = 4096;
const int encoded_density = 2;
const int useful_density = 1 + (1 << encoded_density);
int dirhdr_size_field, dirhdr_size2_field, dirhdr_depth_field;
int dirhdr_root_field, dirhdr_tail_field, dirhdr_nchunks_field;
int curr_chunk, depth, filename_index;
tree234 *index;
assert(rs->pos == 0);
assert(count234(files) > 0);
/* Directory header */
rdaddsc(rs, "ITSP"); /* directory header magic number */
RDADD_32BIT_LSB_FIRST(rs, 1); /* format version */
dirhdr_size_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* directory header size; fill in later */
RDADD_32BIT_LSB_FIRST(rs, 10); /* unknown; observed to be 10 */
RDADD_32BIT_LSB_FIRST(rs, chunksize);
RDADD_32BIT_LSB_FIRST(rs, encoded_density);
dirhdr_depth_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* B-tree depth; fill in later */
dirhdr_root_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* root chunk index; fill in later */
RDADD_32BIT_LSB_FIRST(rs, 0); /* head of PMGL chunk list; always 0 here */
dirhdr_tail_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* tail of PMGL chunk list; fill in later */
RDADD_32BIT_LSB_FIRST(rs, 0xFFFFFFFFU); /* unknown; observed to be -1 */
dirhdr_nchunks_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* total number of chunks; fill in later */
RDADD_32BIT_LSB_FIRST(rs, 0x409); /* language (FIXME) */
guid(rs,0x5D02926A,0x212E,0x11D0,0x9D,0xF9,0x00,0xA0,0xC9,0x22,0xE6,0xEC);
dirhdr_size2_field = rs->pos;
RDADD_32BIT_LSB_FIRST(rs, 0); /* directory header size; fill in later */
RDADD_32BIT_LSB_FIRST(rs, 0xFFFFFFFFU); /* unknown; observed to be -1 */
RDADD_32BIT_LSB_FIRST(rs, 0xFFFFFFFFU); /* unknown; observed to be -1 */
RDADD_32BIT_LSB_FIRST(rs, 0xFFFFFFFFU); /* unknown; observed to be -1 */
PUT_32BIT_LSB_FIRST(rs->text + dirhdr_size_field, rs->pos);
PUT_32BIT_LSB_FIRST(rs->text + dirhdr_size2_field, rs->pos);
index = newtree234(NULL, NULL);
curr_chunk = 0;
depth = 1;
/* Write out lowest-level PMGL chunks full of actual directory entries */
filename_index = 0;
while (filename_index < count234(files)) {
rdstringc chunk = {0, 0, NULL};
rdstringc reversed_quickref = {0, 0, NULL};
int chunk_endlen_field, chunk_nextptr_field;
int n_entries, offset_of_first_entry;
int saved_pos, saved_rq_pos, i;
rdaddsc(&chunk, "PMGL");
chunk_endlen_field = chunk.pos;
RDADD_32BIT_LSB_FIRST(&chunk, 0); /* space at end; fill in later */
RDADD_32BIT_LSB_FIRST(&chunk, 0); /* unknown; observed to be 0 */
if (curr_chunk == 0) {
RDADD_32BIT_LSB_FIRST(&chunk, 0xFFFFFFFF); /* 'null' prev ptr */
} else {
RDADD_32BIT_LSB_FIRST(&chunk, curr_chunk - 1);
}
chunk_nextptr_field = chunk.pos; /* may overwrite 'next' ptr later */
RDADD_32BIT_LSB_FIRST(&chunk, curr_chunk + 1);
/* Enter this chunk in our index for the next level of the
* B-tree (if we end up needing one). */
{
struct chm_directory_entry *ent = (struct chm_directory_entry *)
index234(files, filename_index);
struct chm_index_entry *ient = snew(struct chm_index_entry);
assert(ent);
ient->first_filename = ent->filename;
ient->chunk_index = curr_chunk;
addpos234(index, ient, count234(index));
}
/* Start accumulating the quick-reference index at the end of this
* chunk. We'll build it up backwards, and reverse it halfwordwise
* when we copy it into the end of our output chunk. */
RDADD_16BIT_LSB_FIRST(&reversed_quickref, 0);
offset_of_first_entry = chunk.pos;
n_entries = 0;
/* Write filenames into this chunk until it's full, or until
* we run out of filenames. */
while (1) {
struct chm_directory_entry *ent = (struct chm_directory_entry *)
index234(files, filename_index++);
if (!ent) {
/* Run out of filenames, so this is the last PMGL chunk.
* Reset its 'next' pointer to the 'null' -1 value. */
PUT_32BIT_LSB_FIRST(chunk.text + chunk_nextptr_field,
0xFFFFFFFFU);
/* And point the directory header's tail pointer at
* this chunk. */
PUT_32BIT_LSB_FIRST(rs->text + dirhdr_tail_field, curr_chunk);
break;
}
/* Save the sizes of stuff in this chunk, so we can put
* them back if this entry turns out to overflow. */
saved_pos = chunk.pos;
saved_rq_pos = reversed_quickref.pos;
if (n_entries > 0 && n_entries % useful_density == 0) {
/* Add a quick-reference index pointer. */
RDADD_16BIT_LSB_FIRST(&reversed_quickref, chunk.pos -
offset_of_first_entry);
}
encint(&chunk, strlen(ent->filename));
rdaddsc(&chunk, ent->filename);
encint(&chunk, ent->which_content_section);
encint(&chunk, ent->offset_in_content_section);
encint(&chunk, ent->file_size);
if (chunk.pos + reversed_quickref.pos > chunksize) {
filename_index--;
chunk.pos = saved_pos;
reversed_quickref.pos = saved_rq_pos;
break;
}
/* If we didn't overflow, then commit to this entry and
* loop round for the next one. */
n_entries++;
}
/* Finalise the chunk. */
assert(chunk.pos + reversed_quickref.pos <= chunksize);
PUT_32BIT_LSB_FIRST(chunk.text + chunk_endlen_field,
chunksize - chunk.pos);
PUT_16BIT_LSB_FIRST(reversed_quickref.text, n_entries);
rdaddc_rep(&chunk, 0, chunksize - chunk.pos - reversed_quickref.pos);
for (i = reversed_quickref.pos - 2; i >= 0; i -= 2)
rdaddsn(&chunk, reversed_quickref.text+i, 2);
assert(chunk.pos == chunksize);
rdaddsn(rs, chunk.text, chunk.pos);
sfree(chunk.text);
sfree(reversed_quickref.text);
curr_chunk++;
}
/* Write out as many layers of PMGI index chunks as it takes to
* reduce the total number of chunks at the current level to 1. */
while (count234(index) > 1) {
tree234 *prev_index;
int index_index = 0;
prev_index = index;
index = newtree234(NULL, NULL);
depth++;
while (index_index < count234(prev_index)) {
rdstringc chunk = {0, 0, NULL};
rdstringc reversed_quickref = {0, 0, NULL};
int chunk_endlen_field;
int n_entries, offset_of_first_entry;
int saved_pos, saved_rq_pos, i;
rdaddsc(&chunk, "PMGI");
chunk_endlen_field = chunk.pos;
RDADD_32BIT_LSB_FIRST(&chunk, 0); /* space at end; fill in later */
/* Enter this chunk in our index for the next level of the
* B-tree (if we end up needing one). */
{
struct chm_index_entry *ent = (struct chm_index_entry *)
index234(prev_index, index_index);
struct chm_index_entry *ient = snew(struct chm_index_entry);
assert(ent);
ient->first_filename = ent->first_filename;
ient->chunk_index = curr_chunk;
addpos234(index, ient, count234(index));
}
/* Start accumulating the quick-reference index at the end
* of this chunk, as above. */
RDADD_16BIT_LSB_FIRST(&reversed_quickref, 0);
offset_of_first_entry = chunk.pos;
n_entries = 0;
/* Write index entries into this chunk until it's full, or
* until we run out of chunks at the previous level. */
while (1) {
struct chm_index_entry *ent = (struct chm_index_entry *)
index234(prev_index, index_index++);
if (!ent)
break;
/* Save the sizes of stuff in this chunk, so we can put
* them back if this entry turns out to overflow. */
saved_pos = chunk.pos;
saved_rq_pos = reversed_quickref.pos;
if (n_entries > 0 && n_entries % useful_density == 0) {
/* Add a quick-reference index pointer. */
RDADD_16BIT_LSB_FIRST(&reversed_quickref, chunk.pos -
offset_of_first_entry);
}
encint(&chunk, strlen(ent->first_filename));
rdaddsc(&chunk, ent->first_filename);
encint(&chunk, ent->chunk_index);
if (chunk.pos + reversed_quickref.pos > chunksize) {
index_index--;
chunk.pos = saved_pos;
reversed_quickref.pos = saved_rq_pos;
break;
}
/* If we didn't overflow, then commit to this entry and
* loop round for the next one. */
n_entries++;
}
/* Finalise the chunk. */
assert(chunk.pos + reversed_quickref.pos <= chunksize);
PUT_32BIT_LSB_FIRST(chunk.text + chunk_endlen_field,
chunksize - chunk.pos);
PUT_16BIT_LSB_FIRST(reversed_quickref.text, n_entries);
rdaddc_rep(&chunk, 0,
chunksize - chunk.pos - reversed_quickref.pos);
for (i = reversed_quickref.pos - 2; i >= 0; i -= 2)
rdaddsn(&chunk, reversed_quickref.text+i, 2);
assert(chunk.pos == chunksize);
rdaddsn(rs, chunk.text, chunk.pos);
sfree(chunk.text);
sfree(reversed_quickref.text);
curr_chunk++;
}
/*
* Now free the old index.
*/
while (1) {
struct chm_index_entry *ent = (struct chm_index_entry *)
delpos234(prev_index, 0);
if (!ent)
break;
sfree(ent);
}
freetree234(prev_index);
}
/*
* Finished! We've reduced to a single chunk. Free the remaining
* index (which must have size 1).
*/
assert(count234(index) == 1);
sfree(delpos234(index, 0));
freetree234(index);
/* Fill in the deferred fields in the main header. */
PUT_32BIT_LSB_FIRST(rs->text + dirhdr_depth_field, depth);
PUT_32BIT_LSB_FIRST(rs->text + dirhdr_root_field, curr_chunk-1);
PUT_32BIT_LSB_FIRST(rs->text + dirhdr_nchunks_field, curr_chunk);
}
static int sys_start(rdstringc *rs, int code)
{
int toret = rs->pos;
RDADD_16BIT_LSB_FIRST(rs, code);
RDADD_16BIT_LSB_FIRST(rs, 0); /* length; overwrite later */
return toret;
}
static void sys_end(rdstringc *rs, int recstart)
{
PUT_16BIT_LSB_FIRST(rs->text + recstart+2, rs->pos - (recstart+4));
}
struct chm_window {
char *name;
char *title;
char *contentsfile;
char *indexfile;
char *rootfile;
int navpaneflags;
int toolbarflags;
};
struct chm {
tree234 *files;
tree234 *windows;
tree234 *stringtab;
rdstringc content0; /* outer uncompressed container */
rdstringc content1; /* compressed subfile */
rdstringc outfile;
rdstringc stringsfile;
char *title, *contents_filename, *index_filename, *default_topic;
char *default_window;
struct chm_section *rootsecthead, *rootsecttail;
struct chm_section *allsecthead, *allsecttail;
};
struct chm_section {
/* Logical links within the section tree structure */
struct chm_section *firstchild, *lastchild, *nextsibling, *parent;
/* Link all chm_sections together into one big list, in a
* topological order (i.e. every section comes after its
* parent) */
struct chm_section *next;
char *title, *url;
int tocidx_offset_1, tocidx_offset_2;
int topic_index, urltbl_offset, urlstr_offset;
};
struct chm_stringtab_entry {
struct chm *chm;
int strtab_offset;
};
static int chm_stringtab_cmp(const void *av, const void *bv, void *cmpctx)
{
const struct chm_stringtab_entry
*a = (const struct chm_stringtab_entry *)av,
*b = (const struct chm_stringtab_entry *)bv;
return strcmp(a->chm->stringsfile.text + a->strtab_offset,
b->chm->stringsfile.text + b->strtab_offset);
}
static int chm_stringtab_find(const void *av, const void *bv, void *cmpctx)
{
const char *a = (const char *)av;
const struct chm_stringtab_entry
*b = (const struct chm_stringtab_entry *)bv;
return strcmp(a, b->chm->stringsfile.text + b->strtab_offset);
}
int chm_intern_string(struct chm *chm, const char *string)
{
struct chm_stringtab_entry *ent;
int size;
if (!string)
return 0;
if ((ent = (struct chm_stringtab_entry *)findcmp234(
chm->stringtab, (void *)string, chm_stringtab_find, NULL)) ==
NULL) {
ent = snew(struct chm_stringtab_entry);
ent->chm = chm;
/* Pad to ensure the string doesn't cross a page boundary. */
size = strlen(string) + 1; /* include the NUL terminator */
assert(size < 0x1000); /* avoid really serious trouble */
if ((chm->stringsfile.pos ^ (chm->stringsfile.pos + size-1)) >> 12)
rdaddc_rep(&chm->stringsfile, 0, 0xFFF & -chm->stringsfile.pos);
ent->strtab_offset = chm->stringsfile.pos;
rdaddsc(&chm->stringsfile, string);
rdaddc(&chm->stringsfile, '\0');
add234(chm->stringtab, ent);
}
return ent->strtab_offset;
}
struct chm *chm_new(void)
{
struct chm *chm = snew(struct chm);
chm->files = newtree234(chm_directory_entry_cmp, NULL);
chm->windows = newtree234(NULL, NULL);
chm->stringtab = newtree234(chm_stringtab_cmp, NULL);
chm->content0 = empty_rdstringc;
chm->content1 = empty_rdstringc;
chm->outfile = empty_rdstringc;
chm->stringsfile = empty_rdstringc;
chm->title = NULL;
chm->contents_filename = NULL;
chm->index_filename = NULL;
chm->default_topic = NULL;
chm->default_window = NULL;
chm->rootsecthead = chm->rootsecttail = NULL;
chm->allsecthead = chm->allsecttail = NULL;
chm_intern_string(chm, ""); /* preinitialise the strings table */
return chm;
}
void chm_free(struct chm *chm)
{
struct chm_directory_entry *ent;
struct chm_window *win;
struct chm_stringtab_entry *str;
struct chm_section *sect;
while ((ent = delpos234(chm->files, 0)) != NULL) {
sfree(ent->filename);
sfree(ent);
}
freetree234(chm->files);
while ((win = delpos234(chm->windows, 0)) != NULL) {
sfree(win->name);
sfree(win->title);
sfree(win->contentsfile);
sfree(win->indexfile);
sfree(win->rootfile);
sfree(win);
}
freetree234(chm->windows);
while ((str = delpos234(chm->stringtab, 0)) != NULL) {
sfree(str);
}
freetree234(chm->stringtab);
for (sect = chm->allsecthead; sect ;) {
struct chm_section *tmp = sect->next;
sfree(sect->title);
sfree(sect->url);
sfree(sect);
sect = tmp;
}
sfree(chm->content0.text);
sfree(chm->content1.text);
sfree(chm->outfile.text);
sfree(chm->stringsfile.text);
sfree(chm->title);
sfree(chm->contents_filename);
sfree(chm->index_filename);
sfree(chm->default_topic);
sfree(chm->default_window);
sfree(chm);
}
static void chm_add_file_internal(struct chm *chm, const char *name,
const char *data, int len,
rdstringc *sect, int which_sect)
{
struct chm_directory_entry *ent = snew(struct chm_directory_entry);
ent->filename = dupstr(name);
ent->which_content_section = which_sect;
ent->offset_in_content_section = sect->pos;
ent->file_size = len;
add234(chm->files, ent);
rdaddsn(sect, data, len);
}
static struct chm_directory_entry *chm_find_file(
struct chm *chm, const char *name)
{
return findcmp234(chm->files, (const void *)name,
chm_directory_entry_find, NULL);
}
static char *add_leading_slash(const char *str)
{
char *toret = snewn(2 + strlen(str), char);
toret[0] = '/';
strcpy(toret+1, str);
return toret;
}
void chm_add_file(struct chm *chm, const char *name, const char *data, int len)
{
char *name_with_slash = add_leading_slash(name);
chm_add_file_internal(chm, name_with_slash, data, len, &chm->content1, 1);
sfree(name_with_slash);
}
void chm_title(struct chm *chm, const char *title)
{
chm->title = dupstr(title);
}
void chm_contents_filename(struct chm *chm, const char *name)
{
chm->contents_filename = dupstr(name);
}
void chm_index_filename(struct chm *chm, const char *name)
{
chm->index_filename = dupstr(name);
}
void chm_default_topic(struct chm *chm, const char *name)
{
chm->default_topic = dupstr(name);
}
void chm_default_window(struct chm *chm, const char *name)
{
chm->default_window = dupstr(name);
}
void chm_add_window(struct chm *chm, const char *winname, const char *title,
const char *contentsfile, const char *indexfile,
const char *rootfile, int navpaneflags, int toolbarflags)
{
struct chm_window *win = snew(struct chm_window);
win->name = dupstr(winname);
win->title = dupstr(title);
win->contentsfile = contentsfile ? dupstr(contentsfile) : NULL;
win->indexfile = indexfile ? dupstr(indexfile) : NULL;
win->rootfile = dupstr(rootfile);
win->navpaneflags = navpaneflags;
win->toolbarflags = toolbarflags;
addpos234(chm->windows, win, count234(chm->windows));
}
struct chm_section *chm_add_section(struct chm *chm,
struct chm_section *parent,
const char *title, const char *url)
{
struct chm_section *sect = snew(struct chm_section);
sect->title = dupstr(title);
sect->url = dupstr(url);
sect->firstchild = sect->lastchild = sect->nextsibling = sect->next = NULL;
if (parent) {
sect->parent = parent;
if (parent->lastchild) {
parent->lastchild->nextsibling = sect;
} else {
parent->firstchild = sect;
}
parent->lastchild = sect;
} else {
sect->parent = NULL;
if (chm->rootsecttail) {
chm->rootsecttail->nextsibling = sect;
} else {
chm->rootsecthead = sect;
}
chm->rootsecttail = sect;
}
if (chm->allsecttail) {
chm->allsecttail->next = sect;
} else {
chm->allsecthead = sect;
}
chm->allsecttail = sect;
return sect;
}
struct chm_urltbl_entry {
/*
* Records of #URLTBL, before their order is finalised.
*
* The first word of this record is listed as 'unknown, perhaps
* some kind of unique ID' in chmspec. But my observation in HTML
* Help Workshop's output is that it's actually a hash of the
* target URL, and the file is sorted by them. chm_url_hash()
* below implements the hash algorithm.
*/
unsigned long hash;
int topic_index;
int urlstr_pos;
int topics_offset_to_update;
};
int chm_urltbl_entry_cmp(const void *av, const void *bv, void *cmpctx)
{
const struct chm_urltbl_entry
*a = (const struct chm_urltbl_entry *)av,
*b = (const struct chm_urltbl_entry *)bv;
if (a->hash < b->hash) return -1;
if (a->hash > b->hash) return +1;
if (a->topic_index < b->topic_index) return -1;
if (a->topic_index > b->topic_index) return -1;
return 0;
}
static unsigned long chm_url_hash(const char *str)
{
const char *p;
unsigned long hash;
hash = 0;
for (p = str; *p; p++) {
/*
* Multiply `hash' by 43.
*/
{
unsigned long bottom, top;
bottom = (hash & 0xFFFFUL) * 43;
top = ((hash >> 16) & 0xFFFFUL) * 43;
top += (bottom >> 16);
bottom &= 0xFFFFUL;
top &= 0xFFFFUL;
hash = (top << 16) | bottom;
}
/*
* Add the mapping value for this byte to `hash'.
*/
{
int c = (signed char)*p;
/*
* Translation rule determined by getting hhc.exe to hash
* a lot of strings and analysing the results. I was able
* to confirm this mapping rule for all byte values except
* for NUL, CR, LF, ^Z and backslash: the first four of
* those I couldn't find any way to get hhc to insert into
* a URL, and the last one is automatically translated
* into '/', presumably for reasons of Windows vs URI path
* syntax normalisation.
*/
int val = (c == '/' ? 0x2c : c <= 'Z' ? c-0x30 : c-0x50);
if (val > 0 && hash > (0xFFFFFFFFUL - val)) {
hash -= (0xFFFFFFFFUL - val) + 1;
} else if (val < 0 && hash < (unsigned long)-val) {
hash += (0xFFFFFFFFUL + val) + 1;
} else
hash += val;
}
}
/*
* Special case: an output hash of 0 is turned into 1, which I
* conjecture is so that in some context or other 0 can be
* reserved to mean something like 'null' or 'no hash value
* available'.
*/
if (hash == 0)
hash = 1;
return hash;
}
const char *chm_build(struct chm *chm, int *outlen)
{
rdstringc dir = {0, 0, NULL};
rdstringc sysfile = {0, 0, NULL};
struct LZXEncodedFile *ef;
int rec;
chm_add_file_internal(chm, "/", "", 0, &chm->content0, 0);
RDADD_32BIT_LSB_FIRST(&sysfile, 3); /* #SYSTEM file version */
rec = sys_start(&sysfile, 9); /* identify CHM-producing tool */
rdaddsc(&sysfile, "Halibut, ");
rdaddsc(&sysfile, version);
rdaddc(&sysfile, '\0');
sys_end(&sysfile, rec);
rec = sys_start(&sysfile, 12); /* number of 'information types' */
RDADD_32BIT_LSB_FIRST(&sysfile, 0);
sys_end(&sysfile, rec);
rec = sys_start(&sysfile, 15); /* checksum of 'information types' */
RDADD_32BIT_LSB_FIRST(&sysfile, 0);
sys_end(&sysfile, rec);
/* actual section of 'information types', whatever those might be */
chm_add_file_internal(chm, "/#ITBITS", "", 0, &chm->content0, 0);
if (chm->title) {
rec = sys_start(&sysfile, 3); /* document title */
rdaddsc(&sysfile, chm->title);
rdaddc(&sysfile, '\0');
sys_end(&sysfile, rec);
}
if (chm->default_topic) {
rec = sys_start(&sysfile, 2);
rdaddsc(&sysfile, chm->default_topic);
rdaddc(&sysfile, '\0');
sys_end(&sysfile, rec);
}
if (chm->contents_filename) {
rec = sys_start(&sysfile, 0);
rdaddsc(&sysfile, chm->contents_filename);
rdaddc(&sysfile, '\0');
sys_end(&sysfile, rec);
}
if (chm->index_filename) {
rec = sys_start(&sysfile, 1);
rdaddsc(&sysfile, chm->index_filename);
rdaddc(&sysfile, '\0');
sys_end(&sysfile, rec);
}
if (chm->default_window) {
rec = sys_start(&sysfile, 5);
rdaddsc(&sysfile, chm->default_window);
rdaddc(&sysfile, '\0');
sys_end(&sysfile, rec);
}
rec = sys_start(&sysfile, 4);
RDADD_32BIT_LSB_FIRST(&sysfile, 0x809); /* language again (FIXME) */
RDADD_32BIT_LSB_FIRST(&sysfile, 0); /* DBCS: off */
RDADD_32BIT_LSB_FIRST(&sysfile, 1); /* full-text search: on */
RDADD_32BIT_LSB_FIRST(&sysfile, 0); /* no KLinks (whatever they are) */
RDADD_32BIT_LSB_FIRST(&sysfile, 0); /* no ALinks (whatever they are) */
RDADD_32BIT_LSB_FIRST(&sysfile, 0x11223344); /* timestamp LSW (FIXME) */
RDADD_32BIT_LSB_FIRST(&sysfile, 0x55667788); /* timestamp MSW (FIXME) */
RDADD_32BIT_LSB_FIRST(&sysfile, 0); /* unknown */
RDADD_32BIT_LSB_FIRST(&sysfile, 0); /* unknown */
sys_end(&sysfile, rec);
{
rdstringc winfile = {0, 0, NULL};
int i, s;
struct chm_window *win;
RDADD_32BIT_LSB_FIRST(&winfile, count234(chm->windows));
RDADD_32BIT_LSB_FIRST(&winfile, 196); /* size of each entry */
for (i = 0;
(win = (struct chm_window *)index234(chm->windows, i)) != NULL;
i++) {
RDADD_32BIT_LSB_FIRST(&winfile, 196); /* size of entry */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* not Unicode */
s = chm_intern_string(chm, win->name);
RDADD_32BIT_LSB_FIRST(&winfile, s);
/* Bitmap of which fields are used: 2 means nav pane
* style, 0x200 means whether nav pane is initially
* closed, 0x400 means tab position */
RDADD_32BIT_LSB_FIRST(&winfile, 0x502);
/* Nav pane styles:
* 0x40000 = user can control window size/pos
* 0x20000 = advanced full-text search UI
* 0x00400 = include a search tab
* 0x00100 = keep contents/index in sync with current topic
* 0x00020 = three-pane window */
RDADD_32BIT_LSB_FIRST(&winfile, win->navpaneflags);
s = chm_intern_string(chm, win->title);
RDADD_32BIT_LSB_FIRST(&winfile, s);
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* window styles */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* window ex styles */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* window rect.left */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* window rect.top */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* window rect.right */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* window rect.bottom */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* window show state */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* only used at runtime */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* only used at runtime */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* only used at runtime */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* only used at runtime */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* only used at runtime */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* only used at runtime */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* nav pane width */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* topic rect.left */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* topic rect.top */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* topic rect.right */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* topic rect.bottom */
s = chm_intern_string(chm, win->contentsfile);
RDADD_32BIT_LSB_FIRST(&winfile, s);
s = chm_intern_string(chm, win->indexfile);
RDADD_32BIT_LSB_FIRST(&winfile, s);
s = chm_intern_string(chm, win->rootfile);
RDADD_32BIT_LSB_FIRST(&winfile, s);
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* no Home button target */
RDADD_32BIT_LSB_FIRST(&winfile, win->toolbarflags);
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* nav pane initially open */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* default nav pane = TOC */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* nav pane tabs at top */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* WM_NOTIFY id */
rdaddc_rep(&winfile, 0, 20); /* tab order block */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* history to keep */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* no Jump 1 button target */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* no Jump 2 button target */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* no Jump 1 button text */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* no Jump 2 button text */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* window min rect.left */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* window min rect.top */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* window min rect.right */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* window min rect.bottom */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* no information types */
RDADD_32BIT_LSB_FIRST(&winfile, 0); /* no custom tabs */
}
assert(winfile.pos == 8 + 196 * count234(chm->windows));
chm_add_file_internal(chm, "/#WINDOWS", winfile.text, winfile.pos,
&chm->content1, 1);
sfree(winfile.text);
}
{
struct chm_section *sect;
rdstringc tocidx = {0, 0, NULL};
rdstringc topics = {0, 0, NULL};
rdstringc urltbl = {0, 0, NULL};
rdstringc urlstr = {0, 0, NULL};
int index, s, n_tocidx_3;
struct chm_directory_entry *contentsfile = NULL, *indexfile = NULL;
tree234 *urltbl_pre;
struct chm_urltbl_entry *urltbl_entry;
urltbl_pre = newtree234(chm_urltbl_entry_cmp, NULL);
rdaddc_rep(&tocidx, 0, 0x1000);
/* Write a header of one zero byte at the start of #URLSTR.
* chmspec says this doesn't always appear, and is unclear on
* what this is for, but I suspect it serves the same purpose
* as the zero byte at the start of #STRINGS, namely that it
* arranges that an absent string in the following records can
* be represented by an offset of zero which will
* automatically point to this byte and hence indicate the
* empty string. */
rdaddc(&urlstr, 0);
if (chm->contents_filename) {
char *withslash = add_leading_slash(chm->contents_filename);
contentsfile = chm_find_file(chm, withslash);