-
Notifications
You must be signed in to change notification settings - Fork 0
/
winhelp.c
2369 lines (2082 loc) · 70.2 KB
/
winhelp.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
/*
* winhelp.c a module to generate Windows .HLP files
*
* Documentation of the .HLP file format comes from the excellent
* HELPFILE.TXT, published alongside the Help decompiler HELPDECO
* by Manfred Winterhoff. This code would not have been possible
* without his efforts. Many thanks.
*/
/*
* Potential future features:
*
* - perhaps LZ77 compression? This appears to cause a phase order
* problem: it's hard to do the compression until the data to be
* compressed is finalised, and yet you can't finalise the data
* to be compressed until you know how much of it is going into
* which TOPICBLOCK in order to work out the offsets in the
* topic headers - for which you have to have already done the
* compression. Perhaps the thing to do is to implement an LZ77
* compressor that can guarantee to leave particular bytes in
* the stream as literals, and then go back and fix the offsets
* up later. Not pleasant.
*
* - It would be good to find out what relation (if any) the LCID
* record in the |SYSTEM section bears to the codepage used in
* the actual help text, so as to be able to vary that if the
* user needs it. For the moment I suspect we're stuck with
* Win1252.
*
* - tables might be nice.
*
* Unlikely future features:
*
* - Phrase compression sounds harder. It's reasonably easy
* (though space-costly) to analyse all the text in the file to
* determine the one key phrase which would save most space if
* replaced by a reference everywhere it appears; but finding
* the _1024_ most effective phrases seems much harder since a
* naive analysis might find lots of phrases that all overlap
* (so you wouldn't get the saving you expected, as after taking
* out the first phrase the rest would never crop up). In
* addition, MS hold US patent number 4955066 which may cover
* phrase compression, so perhaps it's best just to leave it.
*
* Cleanup work:
*
* - sort out begin_topic. Ideally we should have a separate
* topic_macro function that adds to the existing linkdata for
* the topic, because that's more flexible than a variadic
* function. This will be fiddly, though: if it's called before
* whlp_begin_topic then we must buffer macros, and if it's
* called afterwards then we must be able to go back and modify
* the linkdata2 of the topic start block. Foo.
*
* - find out what should happen if a single topiclink crosses
* _two_ topicblock boundaries.
*
* - What is the BlockSize in a topic header (first 4 bytes of
* LinkData1 in a type 2 record) supposed to mean? How on earth
* is it measured? The help file doesn't become perceptibly
* corrupt if I frob it randomly; and on some occasions taking a
* bit _out_ of the help file _increases_ that value. I have a
* feeling it's completely made up and/or vestigial, so for the
* moment I'm just making up a plausible value as I go along.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <stdarg.h>
#include "halibut.h"
#include "winhelp.h"
#include "tree234.h"
#ifdef WINHELP_TESTMODE
/*
* This lot is useful for testing. Something like it will also be
* needed to use this module standalone.
*/
#define smalloc malloc
#define srealloc realloc
#define sfree free
#define snew(type) ( (type *) smalloc (sizeof (type)) )
#define snewn(number, type) ( (type *) smalloc ((number) * sizeof (type)) )
#define sresize(array, len, type) \
( (type *) srealloc ((array), (len) * sizeof (type)) )
#define lenof(array) ( sizeof(array) / sizeof(*(array)) )
char *dupstr(char *s) {
char *r = snewn(1+strlen(s), char); strcpy(r,s); return r;
}
#endif
#define UNUSEDARG(x) ( (x) = (x) )
#define GET_32BIT_LSB_FIRST(cp) \
(((unsigned long)(unsigned char)(cp)[0]) | \
((unsigned long)(unsigned char)(cp)[1] << 8) | \
((unsigned long)(unsigned char)(cp)[2] << 16) | \
((unsigned long)(unsigned char)(cp)[3] << 24))
#define PUT_32BIT_LSB_FIRST(cp, value) do { \
(cp)[0] = 0xFF & (value); \
(cp)[1] = 0xFF & ((value) >> 8); \
(cp)[2] = 0xFF & ((value) >> 16); \
(cp)[3] = 0xFF & ((value) >> 24); } while (0)
#define GET_16BIT_LSB_FIRST(cp) \
(((unsigned long)(unsigned char)(cp)[0]) | \
((unsigned long)(unsigned char)(cp)[1] << 8))
#define PUT_16BIT_LSB_FIRST(cp, value) do { \
(cp)[0] = 0xFF & (value); \
(cp)[1] = 0xFF & ((value) >> 8); } while (0)
#define MAX_PAGE_SIZE 0x800 /* max page size in any B-tree */
#define TOPIC_BLKSIZE 4096 /* implied by version/flags combo */
typedef struct WHLP_TOPIC_tag context;
struct file {
char *name; /* file name, will need freeing */
unsigned char *data; /* file data, will need freeing */
int pos; /* position for adding data */
int len; /* # of meaningful bytes in data */
int size; /* # of allocated bytes in data */
int fileoffset; /* offset in the real .HLP file */
};
struct indexrec {
char *term; /* index term, will need freeing */
context *topic; /* topic it links to */
int count, offset; /* used when building |KWDATA */
};
struct topiclink {
int topicoffset, topicpos; /* for referencing from elsewhere */
int recordtype;
int len1, len2;
unsigned char *data1, *data2;
context *context;
struct topiclink *nonscroll, *scroll, *nexttopic;
int block_size; /* for the topic header - *boggle* */
};
struct WHLP_TOPIC_tag {
char *name; /* needs freeing */
unsigned long hash;
struct topiclink *link; /* this provides TOPICOFFSET */
context *browse_next, *browse_prev;
char *title; /* needs freeing */
int index; /* arbitrary number */
};
struct fontdesc {
char *font;
int family, rendition, halfpoints;
int r, g, b;
};
struct WHLP_tag {
tree234 *files; /* stores `struct file' */
tree234 *pre_contexts; /* stores `context' */
tree234 *contexts; /* also stores `context' */
tree234 *titles; /* _also_ stores `context' */
tree234 *text; /* stores `struct topiclink' */
tree234 *index; /* stores `struct indexrec' */
tree234 *tabstops; /* stores `int' */
tree234 *fontnames; /* stores `char *' */
tree234 *fontdescs; /* stores `struct fontdesc' */
struct file *systemfile; /* the |SYSTEM internal file */
context *ptopic; /* primary topic */
struct topiclink *prevtopic; /* to link type-2 records together */
struct topiclink *link; /* while building a topiclink */
unsigned char linkdata1[TOPIC_BLKSIZE]; /* while building a topiclink */
unsigned char linkdata2[TOPIC_BLKSIZE]; /* while building a topiclink */
int topicblock_remaining; /* while building |TOPIC section */
int lasttopiclink; /* while building |TOPIC section */
int firsttopiclink_offset; /* while building |TOPIC section */
int lasttopicstart; /* while building |TOPIC section */
int para_flags;
int para_attrs[7];
int ncontexts;
int picture_index;
};
/* Functions to return the index and leaf data for B-tree contents. */
typedef int (*bt_index_fn)(const void *item, unsigned char *outbuf);
typedef int (*bt_leaf_fn)(const void *item, unsigned char *outbuf);
/* Forward references. */
static void whlp_para_reset(WHLP h);
static struct file *whlp_new_file(WHLP h, char *name);
static void whlp_file_add(struct file *f, const void *data, int len);
static void whlp_file_add_char(struct file *f, int data);
static void whlp_file_add_short(struct file *f, int data);
static void whlp_file_add_long(struct file *f, int data);
static void whlp_file_add_cushort(struct file *f, int data);
#if 0 /* currently unused */
static void whlp_file_add_csshort(struct file *f, int data);
#endif
static void whlp_file_add_culong(struct file *f, int data);
#if 0 /* currently unused */
static void whlp_file_add_cslong(struct file *f, int data);
#endif
static void whlp_file_fill(struct file *f, int len);
static void whlp_file_seek(struct file *f, int pos, int whence);
static int whlp_file_offset(struct file *f);
/* ----------------------------------------------------------------------
* Fiddly little functions: B-tree compare, index and leaf functions.
*/
/* The master index maps file names to help-file offsets. */
static int filecmp(const void *av, const void *bv, void *cmpctx)
{
const struct file *a = (const struct file *)av;
const struct file *b = (const struct file *)bv;
return strcmp(a->name, b->name);
}
static int fileindex(const void *av, unsigned char *outbuf)
{
const struct file *a = (const struct file *)av;
int len = 1+strlen(a->name);
memcpy(outbuf, a->name, len);
return len;
}
static int fileleaf(const void *av, unsigned char *outbuf)
{
const struct file *a = (const struct file *)av;
int len = 1+strlen(a->name);
memcpy(outbuf, a->name, len);
PUT_32BIT_LSB_FIRST(outbuf+len, a->fileoffset);
return len+4;
}
/* The |CONTEXT internal file maps help context hashes to TOPICOFFSETs. */
static int ctxcmp(const void *av, const void *bv, void *cmpctx)
{
const context *a = (const context *)av;
const context *b = (const context *)bv;
if ((signed long)a->hash < (signed long)b->hash)
return -1;
if ((signed long)a->hash > (signed long)b->hash)
return +1;
return 0;
}
static int ctxindex(const void *av, unsigned char *outbuf)
{
const context *a = (const context *)av;
PUT_32BIT_LSB_FIRST(outbuf, a->hash);
return 4;
}
static int ctxleaf(const void *av, unsigned char *outbuf)
{
const context *a = (const context *)av;
PUT_32BIT_LSB_FIRST(outbuf, a->hash);
PUT_32BIT_LSB_FIRST(outbuf+4, a->link->topicoffset);
return 8;
}
/* The |TTLBTREE internal file maps TOPICOFFSETs to title strings. */
static int ttlcmp(const void *av, const void *bv, void *cmpctx)
{
const context *a = (const context *)av;
const context *b = (const context *)bv;
if (a->link->topicoffset < b->link->topicoffset)
return -1;
if (a->link->topicoffset > b->link->topicoffset)
return +1;
return 0;
}
static int ttlindex(const void *av, unsigned char *outbuf)
{
const context *a = (const context *)av;
PUT_32BIT_LSB_FIRST(outbuf, a->link->topicoffset);
return 4;
}
static int ttlleaf(const void *av, unsigned char *outbuf)
{
const context *a = (const context *)av;
int slen;
PUT_32BIT_LSB_FIRST(outbuf, a->link->topicoffset);
slen = 1+strlen(a->title);
memcpy(outbuf+4, a->title, slen);
return 4+slen;
}
/* The |KWBTREE internal file maps index strings to TOPICOFFSETs. */
static int idxcmp(const void *av, const void *bv, void *cmpctx)
{
const struct indexrec *a = (const struct indexrec *)av;
const struct indexrec *b = (const struct indexrec *)bv;
int cmp;
if ( (cmp = strcmp(a->term, b->term)) != 0)
return cmp;
/* Now sort on the index field of the topics. */
if (a->topic->index < b->topic->index)
return -1;
if (a->topic->index > b->topic->index)
return +1;
return 0;
}
static int idxindex(const void *av, unsigned char *outbuf)
{
const struct indexrec *a = (const struct indexrec *)av;
int len = 1+strlen(a->term);
memcpy(outbuf, a->term, len);
return len;
}
static int idxleaf(const void *av, unsigned char *outbuf)
{
const struct indexrec *a = (const struct indexrec *)av;
int len = 1+strlen(a->term);
memcpy(outbuf, a->term, len);
PUT_16BIT_LSB_FIRST(outbuf+len, a->count);
PUT_32BIT_LSB_FIRST(outbuf+len+2, a->offset);
return len+6;
}
/*
* The internal `tabstops' B-tree stores pointers-to-int. Sorting
* is by the low 16 bits of the number (above that is flags).
*/
static int tabcmp(const void *av, const void *bv, void *cmpctx)
{
const int *a = (const int *)av;
const int *b = (const int *)bv;
if ((*a & 0xFFFF) < (*b & 0xFFFF))
return -1;
if ((*a & 0xFFFF) > (*b & 0xFFFF))
return +1;
return 0;
}
/* The internal `fontnames' B-tree stores strings. */
static int fontcmp(const void *av, const void *bv, void *cmpctx)
{
const char *a = (const char *)av;
const char *b = (const char *)bv;
return strcmp(a,b);
}
/* ----------------------------------------------------------------------
* Manage help contexts and topics.
*/
/*
* This is the code to compute the hash of a context name. Copied
* straight from Winterhoff's documentation.
*/
static unsigned long context_hash(char *context)
{
signed char bytemapping[256] =
"\x00\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF"
"\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF"
"\xF0\x0B\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\x0C\xFF"
"\x0A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x0B\x0C\x0D\x0E\x0D"
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F"
"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F"
"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F"
"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F"
"\x80\x81\x82\x83\x0B\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
"\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
"\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF"
"\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF";
unsigned long hash;
/*
* The hash algorithm starts the hash at 0 and updates it with
* each character. Therefore, logically, the hash of an empty
* string should be 0 (it starts at 0 and is never updated);
* but Winterhoff says it is in fact 1. Shouldn't matter, since
* I never plan to use empty context names, but I'll stick the
* special case in here anyway.
*/
if (!*context)
return 1;
/*
* Now compute the hash in the normal way.
*/
hash = 0;
while (*context) {
/*
* Be careful of overflowing `unsigned long', for maximum
* portability.
*/
/*
* 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 val = bytemapping[(unsigned char)*context];
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;
}
context++;
}
return hash;
}
WHLP_TOPIC whlp_register_topic(WHLP h, char *context_name, char **clash)
{
context *ctx = snew(context);
context *otherctx;
/*
* Index contexts in order of creation, just so there's some
* sort of non-arbitrary ordering in the index B-tree. Call me
* fussy, but I don't like indexing on pointer values because I
* prefer the code to be deterministic when run under different
* C libraries.
*/
ctx->index = h->ncontexts++;
ctx->browse_prev = ctx->browse_next = NULL;
if (context_name) {
/*
* We have a context name, which means we can put this
* context straight into the `contexts' tree.
*/
ctx->name = dupstr(context_name);
ctx->hash = context_hash(context_name);
otherctx = add234(h->contexts, ctx);
if (otherctx != ctx) {
/*
* Hash clash. Destroy the new context and return NULL,
* providing the clashing string.
*/
sfree(ctx->name);
sfree(ctx);
if (clash) *clash = otherctx->name;
return NULL;
}
} else {
/*
* We have no context name yet. Enter this into the
* pre_contexts tree of anonymous topics, which we will go
* through later and allocate unique context names and hash
* values.
*/
ctx->name = NULL;
addpos234(h->pre_contexts, ctx, count234(h->pre_contexts));
}
return ctx;
}
void whlp_prepare(WHLP h)
{
/*
* We must go through pre_contexts and allocate a context ID to
* each anonymous context, making sure it doesn't clash with
* the existing contexts.
*
* Our own context IDs will just be of the form `t00000001',
* and we'll increment the number each time and skip over any
* IDs that clash with existing context names.
*/
int ctx_num = 0;
context *ctx, *otherctx;
while ( (ctx = index234(h->pre_contexts, 0)) != NULL ) {
delpos234(h->pre_contexts, 0);
ctx->name = snewn(20, char);
do {
sprintf(ctx->name, "t%08d", ctx_num++);
ctx->hash = context_hash(ctx->name);
otherctx = add234(h->contexts, ctx);
} while (otherctx != ctx);
}
/*
* Ensure paragraph attributes are clear for the start of text
* output.
*/
whlp_para_reset(h);
}
char *whlp_topic_id(WHLP_TOPIC topic)
{
return topic->name;
}
void whlp_begin_topic(WHLP h, WHLP_TOPIC topic, char *title, ...)
{
struct topiclink *link = snew(struct topiclink);
int len, slen;
char *macro;
va_list ap;
link->nexttopic = NULL;
if (h->prevtopic)
h->prevtopic->nexttopic = link;
h->prevtopic = link;
link->nonscroll = link->scroll = NULL;
link->context = topic;
link->block_size = 0;
link->recordtype = 2; /* topic header */
link->len1 = 4*7; /* standard linkdata1 size */
link->data1 = snewn(link->len1, unsigned char);
slen = strlen(title);
assert(slen+1 <= TOPIC_BLKSIZE);
memcpy(h->linkdata2, title, slen+1);
len = slen+1;
va_start(ap, title);
while ( (macro = va_arg(ap, char *)) != NULL) {
slen = strlen(macro);
assert(len+slen+1 <= TOPIC_BLKSIZE);
memcpy(h->linkdata2+len, macro, slen+1);
len += slen+1;
}
va_end(ap);
len--; /* lose the last \0 on the last macro */
link->len2 = len;
link->data2 = snewn(link->len2, unsigned char);
memcpy(link->data2, h->linkdata2, link->len2);
topic->title = dupstr(title);
topic->link = link;
addpos234(h->text, link, count234(h->text));
}
void whlp_browse_link(WHLP h, WHLP_TOPIC before, WHLP_TOPIC after)
{
UNUSEDARG(h);
/*
* See if the `before' topic is already linked to another one,
* and break the link to that if so. Likewise the `after'
* topic.
*/
if (before->browse_next)
before->browse_next->browse_prev = NULL;
if (after->browse_prev)
after->browse_prev->browse_next = NULL;
before->browse_next = after;
after->browse_prev = before;
}
/* ----------------------------------------------------------------------
* Manage the actual generation of paragraph and text records.
*/
static void whlp_linkdata(WHLP h, int which, int c)
{
int *len = (which == 1 ? &h->link->len1 : &h->link->len2);
unsigned char *data = (which == 1 ? h->linkdata1 : h->linkdata2);
assert(*len < TOPIC_BLKSIZE);
data[(*len)++] = c;
}
static void whlp_linkdata_short(WHLP h, int which, int data)
{
whlp_linkdata(h, which, data & 0xFF);
whlp_linkdata(h, which, (data >> 8) & 0xFF);
}
static void whlp_linkdata_long(WHLP h, int which, int data)
{
whlp_linkdata(h, which, data & 0xFF);
whlp_linkdata(h, which, (data >> 8) & 0xFF);
whlp_linkdata(h, which, (data >> 16) & 0xFF);
whlp_linkdata(h, which, (data >> 24) & 0xFF);
}
static void whlp_linkdata_cushort(WHLP h, int which, int data)
{
if (data <= 0x7F) {
whlp_linkdata(h, which, data*2);
} else {
whlp_linkdata(h, which, 1 + (data%128 * 2));
whlp_linkdata(h, which, data/128);
}
}
static void whlp_linkdata_csshort(WHLP h, int which, int data)
{
if (data >= -0x40 && data <= 0x3F)
whlp_linkdata_cushort(h, which, data+64);
else
whlp_linkdata_cushort(h, which, data+16384);
}
static void whlp_linkdata_culong(WHLP h, int which, int data)
{
if (data <= 0x7FFF) {
whlp_linkdata_short(h, which, data*2);
} else {
whlp_linkdata_short(h, which, 1 + (data%32768 * 2));
whlp_linkdata_short(h, which, data/32768);
}
}
static void whlp_linkdata_cslong(WHLP h, int which, int data)
{
if (data >= -0x4000 && data <= 0x3FFF)
whlp_linkdata_culong(h, which, data+16384);
else
whlp_linkdata_culong(h, which, data+67108864);
}
static void whlp_para_reset(WHLP h)
{
int *p;
h->para_flags = 0;
while ( (p = index234(h->tabstops, 0)) != NULL) {
delpos234(h->tabstops, 0);
sfree(p);
}
}
void whlp_para_attr(WHLP h, int attr_id, int attr_param)
{
if (attr_id >= WHLP_PARA_SPACEABOVE &&
attr_id <= WHLP_PARA_FIRSTLINEINDENT) {
h->para_flags |= 1 << attr_id;
h->para_attrs[attr_id] = attr_param;
} else if (attr_id == WHLP_PARA_ALIGNMENT) {
h->para_flags &= ~0xC00;
if (attr_param == WHLP_ALIGN_RIGHT)
h->para_flags |= 0x400;
else if (attr_param == WHLP_ALIGN_CENTRE)
h->para_flags |= 0x800;
}
}
void whlp_set_tabstop(WHLP h, int tabstop, int alignment)
{
int *p;
if (alignment == WHLP_ALIGN_CENTRE)
tabstop |= 0x20000;
if (alignment == WHLP_ALIGN_RIGHT)
tabstop |= 0x10000;
p = snew(int);
*p = tabstop;
add234(h->tabstops, p);
h->para_flags |= 0x0200;
}
void whlp_begin_para(WHLP h, int para_type)
{
struct topiclink *link = snew(struct topiclink);
int i;
/*
* Clear these to NULL out of paranoia, although in records
* that aren't type 2 they should never actually be needed.
*/
link->nexttopic = NULL;
link->context = NULL;
link->nonscroll = link->scroll = NULL;
link->recordtype = 32; /* text record */
h->link = link;
link->len1 = link->len2 = 0;
link->data1 = h->linkdata1;
link->data2 = h->linkdata2;
if (para_type == WHLP_PARA_NONSCROLL && h->prevtopic &&
!h->prevtopic->nonscroll)
h->prevtopic->nonscroll = link;
if (para_type == WHLP_PARA_SCROLL && h->prevtopic &&
!h->prevtopic->scroll)
h->prevtopic->scroll = link;
/*
* Now we're ready to start accumulating stuff in linkdata1 and
* linkdata2. Next we build up the paragraph info. Note that
* the TopicSize (cslong: size of LinkData1 minus the topicsize
* and topiclength fields) and TopicLength (cushort: size of
* LinkData2) fields are missing; we will put those on when we
* end the paragraph.
*/
whlp_linkdata(h, 1, 0); /* must-be-0x00 */
whlp_linkdata(h, 1, 0x80); /* must-be-0x80 */
whlp_linkdata_short(h, 1, 0); /* Winterhoff says `id'; always 0 AFAICT */
whlp_linkdata_short(h, 1, h->para_flags);
for (i = WHLP_PARA_SPACEABOVE; i <= WHLP_PARA_FIRSTLINEINDENT; i++) {
if (h->para_flags & (1<<i))
whlp_linkdata_csshort(h, 1, h->para_attrs[i]);
}
if (h->para_flags & 0x0200) {
int ntabs;
/*
* Write out tab stop data.
*/
ntabs = count234(h->tabstops);
whlp_linkdata_csshort(h, 1, ntabs);
for (i = 0; i < ntabs; i++) {
int tab, *tabp;
tabp = index234(h->tabstops, i);
tab = *tabp;
if (tab & 0x30000)
tab |= 0x4000;
whlp_linkdata_cushort(h, 1, tab & 0xFFFF);
if (tab & 0x4000)
whlp_linkdata_cushort(h, 1, tab >> 16);
}
}
/*
* Fine. Now we're ready to start writing actual text and
* formatting commands.
*/
}
void whlp_set_font(WHLP h, int font_id)
{
/*
* Write a NUL into linkdata2 to cause the reader to flip over
* to linkdata1 to see the formatting command.
*/
whlp_linkdata(h, 2, 0);
/*
* Now the formatting command is 0x80 followed by a short.
*/
whlp_linkdata(h, 1, 0x80);
whlp_linkdata_short(h, 1, font_id);
}
void whlp_start_hyperlink(WHLP h, WHLP_TOPIC target)
{
/*
* Write a NUL into linkdata2.
*/
whlp_linkdata(h, 2, 0);
/*
* Now the formatting command is 0xE3 followed by the context
* hash.
*/
whlp_linkdata(h, 1, 0xE3);
whlp_linkdata_long(h, 1, target->hash);
}
void whlp_end_hyperlink(WHLP h)
{
/*
* Write a NUL into linkdata2.
*/
whlp_linkdata(h, 2, 0);
/*
* Now the formatting command is 0x89.
*/
whlp_linkdata(h, 1, 0x89);
}
void whlp_tab(WHLP h)
{
/*
* Write a NUL into linkdata2.
*/
whlp_linkdata(h, 2, 0);
/*
* Now the formatting command is 0x83.
*/
whlp_linkdata(h, 1, 0x83);
}
int whlp_add_picture(WHLP h, int wd, int ht, const void *vpicdata,
const unsigned long *palette)
{
struct file *f;
char filename[80];
const unsigned char *picdata = (const unsigned char *)vpicdata;
int picstart, picoff, imgoff, imgstart;
int palettelen;
int i, index;
int wdrounded;
/*
* Determine the limit of the colour palette.
*/
palettelen = -1;
for (i = 0; i < wd*ht; i++)
if (palettelen < picdata[i])
palettelen = picdata[i];
palettelen++;
/*
* Round up the width to the next multiple of 4.
*/
wdrounded = (wd + 3) & ~3;
index = h->picture_index++;
sprintf(filename, "bm%d", index);
f = whlp_new_file(h, filename);
whlp_file_add_short(f, 0x706C); /* magic number */
whlp_file_add_short(f, 1); /* number of pictures */
picoff = whlp_file_offset(f);
whlp_file_add_long(f, 0); /* offset of first (only) picture */
picstart = whlp_file_offset(f);
whlp_file_add_char(f, 6); /* DIB */
whlp_file_add_char(f, 0); /* no packing */
whlp_file_add_culong(f, 100); /* xdpi */
whlp_file_add_culong(f, 100); /* ydpi */
whlp_file_add_cushort(f, 1); /* planes (?) */
whlp_file_add_cushort(f, 8); /* bitcount */
whlp_file_add_culong(f, wd); /* width */
whlp_file_add_culong(f, ht); /* height */
whlp_file_add_culong(f, palettelen);/* colours used */
whlp_file_add_culong(f, palettelen);/* colours important */
whlp_file_add_culong(f, wdrounded*ht); /* `compressed' data size */
whlp_file_add_culong(f, 0); /* hotspot size (no hotspots) */
imgoff = whlp_file_offset(f);
whlp_file_add_long(f, 0); /* offset of `compressed' data */
whlp_file_add_long(f, 0); /* offset of hotspot data (none) */
for (i = 0; i < palettelen; i++)
whlp_file_add_long(f, palette[i]);
imgstart = whlp_file_offset(f);
/*
* Windows Help files, like BMP, start from the bottom scanline.
*/
for (i = ht; i-- > 0 ;) {
whlp_file_add(f, picdata + i*wd, wd);
if (wd < wdrounded)
whlp_file_add(f, "\0\0\0", wdrounded - wd);
}
/* Now go back and fix up internal offsets */
whlp_file_seek(f, picoff, 0);
whlp_file_add_long(f, picstart);
whlp_file_seek(f, imgoff, 0);
whlp_file_add_long(f, imgstart - picstart);
whlp_file_seek(f, 0, 2);
return index;
}
void whlp_ref_picture(WHLP h, int picid)
{
/*
* Write a NUL into linkdata2.
*/
whlp_linkdata(h, 2, 0);
/*
* Write the formatting command and its followup data to
* specify a picture in a separate file.
*/
whlp_linkdata(h, 1, 0x86);
whlp_linkdata(h, 1, 3); /* type (picture without hotspots) */
whlp_linkdata_cslong(h, 1, 4);
whlp_linkdata_short(h, 1, 0);
whlp_linkdata_short(h, 1, picid);
}
void whlp_text(WHLP h, char *text)
{
while (*text) {
whlp_linkdata(h, 2, *text++);
}
}
void whlp_end_para(WHLP h)
{
int data1cut;
/*
* Round off the paragraph with 0x82 and 0xFF formatting
* commands. Each requires a NUL in linkdata2.
*/
whlp_linkdata(h, 2, 0);
whlp_linkdata(h, 1, 0x82);
whlp_linkdata(h, 2, 0);
whlp_linkdata(h, 1, 0xFF);
/*
* Now finish up: create the header of linkdata1 (TopicLength
* and TopicSize fields), allocate the real linkdata1 and
* linkdata2 fields, and copy them out of the buffers in h.
* Then insert the finished topiclink into the `text' tree, and
* clean up.
*/
data1cut = h->link->len1;
whlp_linkdata_cslong(h, 1, data1cut);
whlp_linkdata_cushort(h, 1, h->link->len2);
h->link->data1 = snewn(h->link->len1, unsigned char);
memcpy(h->link->data1, h->linkdata1 + data1cut, h->link->len1 - data1cut);
memcpy(h->link->data1 + h->link->len1 - data1cut, h->linkdata1, data1cut);
h->link->data2 = snewn(h->link->len2, unsigned char);
memcpy(h->link->data2, h->linkdata2, h->link->len2);
addpos234(h->text, h->link, count234(h->text));
/* Hack: accumulate the `blocksize' parameter in the topic header. */
if (h->prevtopic)
h->prevtopic->block_size += 21 + h->link->len1 + h->link->len2;
h->link = NULL; /* this is now in the tree */
whlp_para_reset(h);
}
/* ----------------------------------------------------------------------
* Manage the layout and generation of the |TOPIC section.
*/
static void whlp_topicsect_write(WHLP h, struct file *f, void *data, int len,
int can_break)
{
unsigned char *p = (unsigned char *)data;
if (h->topicblock_remaining <= 0 ||
h->topicblock_remaining < can_break) {
/*
* Start a new block.
*/
if (h->topicblock_remaining > 0)
whlp_file_fill(f, h->topicblock_remaining);
whlp_file_add_long(f, h->lasttopiclink);
h->firsttopiclink_offset = whlp_file_offset(f);
whlp_file_add_long(f, -1L); /* this will be filled in later */
whlp_file_add_long(f, h->lasttopicstart);
h->topicblock_remaining = TOPIC_BLKSIZE - 12;
}
while (len > 0) {
int thislen = (h->topicblock_remaining < len ?
h->topicblock_remaining : len);
whlp_file_add(f, p, thislen);
p += thislen;
len -= thislen;
h->topicblock_remaining -= thislen;
if (len > 0 && h->topicblock_remaining <= 0) {
/*
* Start a new block.
*/
whlp_file_add_long(f, h->lasttopiclink);
h->firsttopiclink_offset = whlp_file_offset(f);
whlp_file_add_long(f, -1L); /* this will be filled in later */
whlp_file_add_long(f, h->lasttopicstart);
h->topicblock_remaining = TOPIC_BLKSIZE - 12;
}
}
}
static void whlp_topic_layout(WHLP h)
{
int block, offset, pos;
int i, nlinks, size;
int topicnum;
struct topiclink *link;
struct file *f;
/*
* Create a final TOPICLINK containing no usable data.
*/
link = snew(struct topiclink);
link->nexttopic = NULL;
if (h->prevtopic)