windows help file format

windows help file format / annotation file format / shg and mrb file format
this documentation describes the file format parsed by helpdeco, because
microsoft did not publish the file formats used by winhelp and multimedia
viewers, and created by hc30, hc31, hcp, hcrtf, hcw, mvc, mmvc and wmvc.
this way it is not an official reference, but the result of many weekends
of work dumping 500+ help files and trying to understand what all the bytes
may mean.
i would like to thank pete davis, who first tried to describe 'the windows
help file format' in dr. dobbs journal, sep/oct 1993, and holger haase, who
did a lot of work on picture file formats and bent lynggaard for the infor-
mation on free lists in help files and unused bytes in b+ trees.
revision 1: fixed hash value calculation and |font, minor additions
revision 2: transparent bitmaps, {button}, and {mci} commands
revision 3: unknown in paragraphinfo changed, minor additions
revision 4: ctxomap corrected, bitmap dimensions dpi - not pelspermeter
revision 5: macrodata in hotspotinfo added, annotation file format added
revision 6: [macros] section / internal file |rose added, mvb font structure
revision 7: [groups] section *.grp and [chartab] section *.tbl file format
revision 8: free list, clarified topicpos/topicoffset
revision 9: b+ tree unused bytes and what i found out about gid files
a help file starts with a header, the only structure at a fixed place
long magic		   0x00035f3f
long directorystart	   offset of fileheader of internal directory
long firstfreeblock	   offset of freeheader or -1l if no free list
long entirefilesize	   size of entire help file in bytes
----
char helpfilecontent[entirefilesize-16]   the remainder of the help file
at offset directorystart the fileheader of the internal directory is located
long reservedspace	     size reserved including fileheader
long usedspace		     size of internal file in bytes
unsigned char fileflags      normally 4
----
char filecontent[usedspace]  the bytes contained in the internal file
char freespace[reservedspace-usedspace-9]
the fileheader of the internal directory is followed by usedspace bytes
containing the internal directory which is used to associate filenames and
fileoffsets. the directory is structured as a b+ tree.
a b+ tree is made from leaf-pages and index-pages of fixed size, one of which
is the root-page. all entries are contained in leaf-pages. if more entries
are required than fit into a single leaf-page, index-pages are used to locate
the leaf-page which contains the required entry.
a b+ tree starts with a btreeheader telling you the size of the b+ tree pages,
the root-page, the number of levels, and the number of all entries in this
b+ tree. you must follow (nlevels-1) index-pages before you reach a leaf-page.
unsigned short magic		0x293b
unsigned short flags		bit 0x0002 always 1, bit 0x0400 1 if directory
unsigned short pagesize 	0x0400=1k if directory, 0x0800=2k else, or 4k
char structure[16]		string describing format of data
'l' = long (indexed)
'f' = nul-terminated string (indexed)
'i' = nul-terminated string (indexed)
'2' = short
'4' = long
'z' = nul-terminated string
'!' = long count value, count/8 * record
long filenumber
long topicoffset
short mustbezero		0
short pagesplits		number of page splits b+ tree has suffered
short rootpage			page number of b+ tree root page
short mustbenegone		0xffff
short totalpages		number of b+ tree pages
short nlevels			number of levels of b+ tree
long totalbtreeentries		number of entries in b+ tree
----
char page[totalpages][pagesize] the pages the b+ tree is made of
if nlevel is greater than 1, rootpage is the page number of an index-page.
index-pages start with a btreeindexheader and are followed by an array of
btreeindex structures, in case of the internal directory containing pairs
of filenames and pagenumbers.
(stringz is a nul-terminated string, sizeof(stringz) is strlen(string)+1).
pagenumber gets you to the next page containing entries lexically starting
at filename, but less than the next filename. previouspage gets you to the
next page if the desired filename is lexically before the first filename.
unsigned short unused	 number of free bytes at end of this page
short nentries		 number of entries in this index-page
short previouspage	 page number of previous page
----
struct			 and this is the structure of directory index-pages
{
stringz filename	 varying length nul-terminated string
short pagenumber	 page number of page dealing with filename and above
}
directoryindexentry[nentries]
after nlevels-1 of index-pages you will reach a leaf-page starting with a
btreenodeheader followed by an array of btreeleaf structures, in case of the
internal directory containing pairs of filenames and fileoffsets.
you may follow the previouspage entry in all nlevels-1 index-pages to reach
the first leaf-page, then iterate thru all entries and use nextpage to
follow the double linked list of leaf-pages until nextpage is -1 to retrieve
a sorted list of all totalbtreeentries entries contained in the b+ tree.
unsigned short unused	 number of free bytes at end of this page
short nentries		 number of entries in this leaf-page
short previouspage	 page number of previous leaf-page or -1 if first
short nextpage		 page number of next leaf-page or -1 if last
----
struct			 and this is the structure of directory leaf-pages
{
stringz filename	 varying length nul-terminated string
long fileoffset	 offset of fileheader of internal file filename
relative to beginning of help file
}
directoryleafentry[nentries]
at offset freelistblock the first freeheader is located. it contains
long freespace		 number of bytes unused, including this header
long nextfreeblock	 offset of next freeheader or -1l if end of list
----
char unused[freespace-8] unused bytes
all unused portions of the help file are linked together using freeheaders.
now that you are able to locate the position of an internal file in the
help file, let's describe what they contain. remember that each fileoffset
first takes you to the fileheader of the internal file. the structures
described next are located just behind this fileheader.
|system
the first one to start with is the |system file. this is the systemheader,
the structure of the first bytes of this internal file:
short magic		 0x036c
short minor		 help file format version number
15 = hc30 windows 3.0 help file
21 = hc31 windows 3.1 help file
27 = wmvc/mmvc media view file
33 = mvc or hcw 4.00 windows 95
short major		 1
time_t gendate		 help file created seconds after 1.1.1980, or 0
unsigned short flags	 see below
use minor and flags to find out how the help file was compressed:
minor <= 16		 not compressed, topicblocksize 2k
minor > 16		 flags=0: not compressed,  topicblocksize 4k
flags=4: lz77 compressed, topicblocksize 4k
flags=8: lz77 compressed, topicblocksize 2k
additionally the help file may use phrase compression (oldstyle or hall).
if minor is 16 or less, the help file title follows the systemheader:
stringz helpfiletitle
if minor is above 16, one or more systemrec records follow instead up to the
internal end of the |system file:
struct
{
unsigned short recordtype	       type of data in record
unsigned short datasize	       size of data
----
char data[datasize] 	       dependent on recordtype
}
systemrec[]
there are different recordtypes defined, each storing different data.
they mainly contain what was specified in the help project file.
recordtype  data
1 title     stringz title	       help file title
2 copyright stringz copyright	       copyright notice shown in aboutbox
3 contents  topicoffset contents       topic offset of starting topic
4 config    stringz macro	       all macros executed on opening
5 icon	    windows *.ico file	       see win31wh on icon file format
6 window    struct		       windows defined in the hpj-file
{
struct
{
unsigned short typeisvalid:1
unsigned short nameisvalid:1
unsigned short captionisvalid:1
unsigned short xisvalid:1
unsigned short yisvalid:1
unsigned short withisvalid:1
unsigned short heigthisvalid:1
unsigned short maximizewindow:1
unsigned short rgbisvalid:1
unsigned short rgbnsrisvalid:1
unsigned short windowsalwaysontop:1
unsigned short autosizeheight:1
}
flags
char type[10]	       type of window
char name[9]	       window name
char caption[51]       caption of window
short x 	       x coordinate of window (0..1000)
short y 	       y coordinate of window (0..1000)
short width	       width of window (0..1000)
short height	       height of window (0..1000)
short maximize	       maximize flag and window styles
colorref rgb	       color of scrollable region
colorref rgbnsr        color of non scrollable region
}
window
6 window    typedef struct	       viewer 2.0 windows defined in mvp-file
{
unsigned short flags
char type[10]		 /* type of window */
char name[9]		 /* window name */
char caption[51]	 /* caption for window */
unsigned char moreflags
short x 		 /* x coordinate of window (0..1000) */
short y 		 /* y coordinate of window (0..1000) */
short width		 /* width of window (0..1000) */
short height		 /* height of window (0..1000) */
short maximize		 /* maximize flag and window styles */
colorref rgb1
char unknown
colorreg rgb2
colorref rgb3
short x2
short y2
short width2
short height2
short x3
short y3
}
window;
8 citation  stringz citation	       the citation printed
9 lcid	    short lcid[4]	       language id, windows 95 (hcw 4.00)
10 cnt	    stringz contentfilename    cnt file name, windows 95 (hcw 4.00)
11 charset  unsigned short charset     charset, windows 95 (hcw 4.00)
12 deffont  struct		       default dialog font, windows 95 (hcw 4.00)
{
unsigned char heightinpoints
unsigned char charset
stringz fontname
}
deffont
12 ftindex  stringz dtype	       multimedia help files dtypes
13 groups   stringz group	       defined groups, multimedia help file
14 index_s. stringz indexseparators    separators, windows 95 (hcw 4.00)
14 keyindex struct		       multimedia help files
{
char btreename[10];    btreename[1] is footnote character
char mapname[10];
char dataname[10];
char title[80];
}
keyindex
18 language stringz language	       defined language, multimedia help files
19 dllmaps  struct		       defined dllmaps, multimedia help files
{
stringz win16retaildll
stringz win16debugdll
stringz win32retaildll
stringz win32debugdll
}
dllnames
|phrase
if the help file is phrase compressed, it contains an internal file named
|phrases. windows 3.0 help files generated with hc30 use the following
uncompressed structure to store phrases. a phrase is not nul-terminated,
instead use the next phraseoffset to locate the end of the phrase string
(there is one more phrase offset stored than phrases are defined to allow
for this).
unsigned short numphrases	 number of phrases in table
unsigned short onehundred	 0x0100
unsigned short phraseoffset[numphrases+1] phraseoffset[0]==2*(numphrases+1)
char phrase[numphrases][phraseoffset[phrasenum+1]-phraseoffset[phrasenum]]
windows 3.1 help files generated using hc31 and later always lz77 compress
the phrase character array. read numphrases, onehundred, decompressedsize,
and numphrases+1 phraseoffset values. allocate decompressedsize bytes for
the phrase character array and decompress the usedspace-2*numphrases-10
remaining bytes into the allocated space to retrieve the phrase strings.
unsigned short numphrases	 number of phrases in table
unsigned short onehundred	 0x0100
long decompressedsize
unsigned short phraseoffset[numphrases+1] phraseoffset[0]==2*(numphrases+1)
----				 the remaining part is lz77 compressed
char phrase[numphrases][phraseoffset[phrasenum+1]-phraseoffset[phrasenum]]
the lz77 decompression algorithm can best be described like this:
take the next byte
start at the least significant bit
if the bit is cleared
copy 1 byte from source to destination
else
get the next word into the struct { unsigned pos:12; unsigned len:4; }
copy len+3 bytes from destination-pos-1 to destination
loop until all bits are done
loop until all bytes are consumed
see end of this file for a detailed algorithm.
some mvbs use a slightly different layout of internal |phrases file:
unsigned short eighthundred	 0x0800
unsigned short numphrases	 number of phrases in table
unsigned short onehundred	 0x0100
long decompressedsize
char unused[30]
unsigned short phraseoffset[numphrases+1] phraseoffset[0]==2*(numphrases+1)
----				 the remaining part is lz77 compressed
char phrase[numphrases][phraseoffset[phrasenum+1]-phraseoffset[phrasenum]]
|phrindex
windows 95 (hcw 4.00) may use hall compression and the internal files
|phrindex and |phrimage to store phrases. both must be used to build a
table of phrases and phraseoffsets. |phrindex starts with this header:
long magic			 1l
long nentries
long compressedsize
long phrimagesize
long phrimagecompressedsize
long always0			 0l
unsigned short bitcount:4
unsigned short unknownbits:12
unsigned short always4a00	 not really always
the remaining data is bitcompressed. use this algorithm to build a table
of phraseoffsets:
short n,i; long mask=0,*ptr=(long *)(&always4a00+1);
int getbit(void)
{
ptr+=(mask<0);
mask=mask*2+(mask<=0);
return (*ptr&mask)!=0;
}
phaseoffset[0]=0;
for(i=0;i<nentries;i++)
{
for(n=1;getbit();n+=1<<bitcount) ;
if(getbit()) n+=1;
if(bitcount>1) if(getbit()) n+=2;
if(bitcount>2) if(getbit()) n+=4;
if(bitcount>3) if(getbit()) n+=8;
if(bitcount>4) if(getbit()) n+=16;
phraseoffset[i+1]=phraseoffset[i]+n;
}
just behind the bitcompressed phrase length information (on a 32-bit boundary,
that's why getbit consumed longs) follow numphrases bits (one bit for each
phrase). it is assumed that this information is used for the full text search
capability to exclude certain phrases.
|phrimage
the |phrimage file stores the phrases. a phrase is not nul-terminated. use
phraseoffset[numphrase] and phraseoffset[numphrase+1] to locate beginning
and end of the phrase string. we generated one more phraseoffset to allow
for this. |phrimage is lz77 compressed if phrimagecompressedsize is not
equal to phrimagesize. otherwise you may take it as stored.
|font
the next internal file described is the |font file, which uses this header:
unsigned short numfacenames	      number of face names
unsigned short numdescriptors	      number of font descriptors
unsigned short facenamesoffset	      start of array of face names
relative to &numfacenames
unsigned short descriptorsoffset      start of array of font descriptors
relative to &numfacenames
---				      only if facenamesoffset >= 12
unsigned short numstyles	      number of style descriptors
unsigned short styleoffset	      start of array of style descriptors
relative to &numfacenames
---				      only if facenamesoffset >= 16
unsigned short numcharmaptables       number of character mapping tables
unsigned short charmaptableoffset     start of array of character mapping
table names relative to &numfacenames
the face name array is located at facenamesoffset and contains strings, which
are windows font names or in case of multimedia files a windows font name
concatenated with ',' and the character mapping table number. short strings
are nul-terminated, but a string may use all bytes for characters.
char facename[numfacenames][(descriptorsoffset-facenamesoffset)/numfacenames]
at descriptorsoffset is an array located describing all fonts used in the help
file. if this kind of descriptor appears in a help file, any metric value is
given in halfpoints.
struct oldfont
{
struct
{
unsigned char bold:1
unsigned char italic:1
unsigned char underline:1
unsigned char strikeout:1
unsigned char doubleunderline:1
unsigned char smallcaps:1
}
attributes
unsigned char halfpoints		      pointsize * 2
unsigned char fontfamily		      font family. see values below
unsigned short facenameindex	      index into facename array
unsigned char fgrgb[3]		      rgb values of foreground
unsigned char bgrgb[3]		      unused background../jpg/di1.JPGb values
}
fontdescriptor[numdescriptors]
#define fam_modern 0x01 		      this is a different order than
#define fam_roman  0x02 		      ff_roman, ff_swiss, etc. of
#define fam_swiss  0x03 		      windows !
#define fam_tech   0x03
#define fam_nil    0x03
#define fam_script 0x04
#define fam_decor  0x05
multimedia mvb files use different structures to store font descriptors.
assume this structure for descriptors if facenamesoffset is at least 12.
if this kind of descriptor is used, any metric is given in twips.
struct newfont
{
unsigned char unknown1
short facenameindex
unsigned char fgrgb[3]
unsigned char bgrgb[3]
unsigned char unknown5
unsigned char unknown6
unsigned char unknown7
unsigned char unknown8
unsigned char unknown9
long height
unsigned char mostlyzero[12]
short weight
unsigned char unknown10
unsigned char unknown11
unsigned char italic
unsigned char underline
unsigned char strikeout
unsigned char doubleunderline
unsigned char smallcaps
unsigned char unknown17
unsigned char unknown18
unsigned char pitchandfamily      same values as windows logfont
}
fontdescriptor[numdescriptors]
assume this structure for descriptors if facenamesoffset is at least 16.
if this kind of descriptor is used, any metric is given in twips.
struct mvbfont
{
short facenameindex 	      index into facename array
short stylenumber		      0 if not used
unsigned char unknown3
unsigned char unknown4
unsigned char fgrgb[3]
unsigned char bgrgb[3]
long height 		      negative (incl. external leading)
unsigned char mostlyzero[12]
short weight
unsigned char unknown10
unsigned char unknown11
unsigned char italic
unsigned char underline
unsigned char strikeout
unsigned char doubleunderline
unsigned char smallcaps
unsigned char unknown17
unsigned char unknown18
unsigned char pitchandfamily      same values as windows logfont
unsigned char unknown20
unsigned char unknown21
}
fontdescriptor[numdescriptors]
if facenamesoffset is at least 12, the |font file supports character styles.
stylenumber-1 of the fontdescriptor indexes into this array located at
styleoffset in |font.
struct
{
short stylenum
short basedonstylenum	       0 if not used
struct font 		       struct newfont or struct mvbfont
char unknown[35]
char stylename[65]
}
style[numstyles]
if facenamesoffset is at least 16, the |font file supports character mapping
tables.
the array of character mapping table file names is located in |font at
charmaptableoffset and contains strings of the internal filename of the
character mapping table concatenated with ',' and the character mapping table
number. the entries are not sorted by character mapping table numbers. short
strings are nul-terminated, but a string may use up all bytes.
char charmaptablename[numcharmaptables][32]
|tomap
windows 3.0 (hc30) uses topic numbers that start at 16 for the first topic
to identify topics. to retrieve the location of the topiclink for the topic-
header of a certain topic (in |topic explained later), use the |tomap file.
it contains an array of topic positions. index with topicnumber (do not
subtract 16). topicpos[0] points to the topic specified as index in the help
project.
topicpos topicpos[usedspace/4]
|context
windows 3.1 (hc31) uses hash values of context names to identify topics.
to get the location of the topic, search the b+ tree of the internal file
|context:
structure of |context index-page entries:
struct
{
long hashvalue
short pagenumber
}
contextindexentry[nentries]
structure of |context leaf-page entries:
struct
{
long hashvalue	       hash value of context id
topicoffset topicoffset    position
}
contextleafentry[nentries]
to calculate the hashvalue hash from a context id ptr do this:
signed char table[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'
}
for(hash=0l;*ptr;ptr++) hash=(hash*43)+table[(unsigned char)*ptr];
remember that only 0-9, a-z, a-z, _ and . are legal characters for context ids
in win 3.1 (hc31). only windows 95 (hcrtf) allows nearly all characters.
the hash value for an empty string is 1.
|ctxomap
if your help project file had a [map] section, the internal file |ctxomap
contains an array to assign map ids to topic offsets.
short nentries
struct
{
long mapid
topicoffset topicoffset
}
ctxomapenrty[nentries]
|xwbtree, |xwdata, |xwmap, |xkwbtree, |xkwdata, |xkwmap
to locate a keyword assigned using a x-footnote (x may be a-z, a-z), use the
|xwdata, |xwbtree and |xwmap internal files. |xwbtree tells you how often a
certain keyword is defined in the help file.
structure of |xwbtree index page entries:
struct
{
stringz keyword
short pagenumber
}
xwbtreeindexentry[nentries]
structure of |xwbtree leaf page entries:
struct
{
stringz keyword
short count 	    number of times keyword is referenced
long kwdataoffset	    this is the offset into |xwdata
}
xwbtreeleafentry[nentries]
kwbtree files in winhlp32 gid files are structured differently (they have
a different description in the structure field of the btreeheader) and pack
former kwbtree and kwdata files into one:
structure of |xwbtree leaf page entries in win95 gid files:
struct
{
stringz keyword
long size		    size of following record
struct
{
long filenumber     ?
long topicoffset    this is the offset into |xwdata
}
record[size/8]
}
xwbtreeleafentry[nentries]
the |xwdata contains an array of topic offsets. the kwdataoffset from the
|xwbtree tells you where to seek to in the |xwdata file to read count topic
offsets.
topicoffset keywordtopicoffset[usedspace/4]
and the topic offset retrieved tells you which location the keyword was
assigned to. it is -1l if the keyword is assigned to a macro using the [macros]
section of hcrtf 4.0 (see description of |rose file).
the |xwmap contains an array that tells you where to find the n-th keyword in
the |xwbtree. you don't need to use this file but it allows for faster
scrolling lists of alphabetically ordered keywords. (winhelp search dialog).
struct
{
long keywordnumber	      number of first keyword on leaf-page
unsigned short pagenum    b+ tree page number
}
xwmap[usedspace/6]
similarily |xkwbtree b+ tree and |xkwdata, |xkwmap files (where x may be 0-9,
a-z, a-z) are built from k-x:footnotes and [keyindex] declarations of multi
media files.
|ttlbtree
if you want to know the topic title assigned using the $-footnote, take a look
into the |ttlbtree internal file, which contains topic titles ordered by topic
offsets in a b+ tree. (it is used by winhelp to display the topic titles in
the search dialog).
structure of |ttlbtree index page entries:
struct
{
topicoffset topicoffset
short pagenumber
}
ttlbtreeindexentry[nentries]
structure of |ttlbtree leaf page entries:
struct
{
topicoffset topicoffset
stringz topictitle
}
ttlbtreeleafentry[nentries]
|cfn
the |cfn (where n is integer) internal file lists the macros defined in
[config:n] sections of the help project file (hcw 4.00). the file contains as
many macro strings as were specified one after another:
stringz macro[]
|rose
the |rose internal file contains all definitions from the [macros] section of a
windows 95 (hcw 4.00) help project file. it is build using a b+ tree. keywords
only appear using hash values but are listed in the |kwbtree with a topicpos in
the associated |kwdata array of -1l.
structure of |rose index page entries:
struct
{
long keywordhash
short pagenumber
}
roseindexentry[nentries]
structure of |rose leaf page entries:
struct
{
long keywordhash
stringz macro
stringz topictitle		 not a real topic title but the string
displayed in the search dialog where
normally topic titles are listed
}
roseleafentry[nentries]
|topicid
the |topicid internal file lists the contextname assigned to a specific topic
offset if the help file was created using the /a option of hcrtf and is build
using a b+ tree.
structure of |topicid index-page entries:
struct
{
topicoffset topicoffset
short pagenumber
}
topicidindexentry[nentries]
structure of |topicid leaf-page entries:
struct
{
topicoffset topicoffset
stringz contextname
}
topicidleafentry[nentries]
|petra
the |petra internal file contains a b+ tree mentioning the names of the rtf
source files the help file was build from for each topic if the help file was
created using the /a option of hcrtf.
structure of |petra index-page entries:
struct
{
topicoffset topicoffset
short pagenumber
}
petraindexentry[nentries]
structure of |petra leaf-page entries:
struct
{
topicoffset topicoffset
stringz rtfsourcefilename
}
petraleafentry[nentries]
|viola
the |viola internal file contains a b+ tree specifying the default windows
assigned to topics using the > footnote available in hcrtf 4.00.
structure of |viola index-page entries:
struct
{
topicoffset topicoffset
short pagenumber
}
violaindexentry[nentries]
structure of |viola leaf-page entries:
struct
{
topicoffset topicoffset
long defaultwindownumber
}
violaleafentry[nentries]
*.gid
i have not investigated gid files, as they are created by winhlp32 and are not
needed for help file reconstruction. but they are based on the same file format
as windows help files, so helpdeco may be used to display their content. notice
the difference between |xwbtree files stored in *.gid files and regular files.
|winpos
this file has been seen in winhlp32 gid files, but always contained an empty
btree (with an unknown 'a' in the btreeheader structure).
|pete
this file has been seen in winhlp32 gid files but is currently not understood.
|flags
this file has been seen in winhlp32 gid files but is currently not understood.
|cntjump
this b+ tree stored in winhlp32 gid files contains the jump references of
the *.cnt file.
|cnttext
this b+ tree stored in winhlp32 gid files contains the topic titles of the
jumps from the *.cnt file.
*.grp
mediaview compilers create *.grp internal files from group + footnotes
assigned to topics. all *.grp files follow this structure:
struct
{
unsigned long magic      /* 0x000a3333 */
unsigned long bitmapsize /* max. 64000 equalling 512000 topics */
unsigned long lasttopic  /* first topic in help file has topic number 0 */
unsigned long firsttopic /* first topic in help file has topic number 0 */
unsigned long topicsused /* in this group */
unsigned long topiccount /* in whole help file */
unsigned long grouptype  /* 1 or 2, see below */
unsigned long unknown[3]
unsigned char bitmap[bitmapsize] /* only if grouptype equals 2 */
}
group
starting with the first topic of the help file using topicnumber 0, a topic is
included in a group if topicnumber is in the range of firsttopic to lasttopic.
if grouptype equals 2 it is additionally required that the corresponding bit
starting with lsb of bitmap[0] is set in the bitmap.
(bitmap[topicnumber>>3]&(1<<(topicnumber&7))!=0).
*.tbl
mediaview compilers store character mapping tables listed in the [chartab]
section in internal *.tbl files using the following binary structure:
struct
{
unsigned short magic /* 0x5555 */
unsigned short size
unsigned short unknown1[2]
unsigned short entries
unsigned short ligatures
unsigned short liglen
unsigned short unknown2[13]
struct
{
unsigned short class
unsigned short order
unsigned char normal
unsigned char clipboard
unsigned char mac
unsigned char macclipboard
unsigned short unused
}
charentry[entries]
unsigned char ligature[ligatures][liglen]
}
chartab
a character mapping table is assigned to a font by appending ,x (where x is a
decimal number) to the font name and the same ,x to the character mapping table
name (in the charmap section of the internal |font file).
|topic
and now to the interesting part, the internal file named |topic. it's divided
into blocks of topicblocksize bytes, each beginning with a topicblockheader:
topicpos lasttopiclink	  points to last topic link in previous block or -1l
topicpos firsttopiclink   points to first topic link in this block
topicpos lasttopicheader  points to topic link of last topic header or 0l, -1l
----
char plainorcompresseddata[topicblocksize-12]
read the first 12 bytes into a topicblockheader structure. the remaining
topicblocksize-12 bytes of each topic block may be compressed using the lz77
algorithm described above.
decompress them into a buffer of decompresssize bytes size if the flags value
contained in the internal |system file is 4 or 8 and minor is greater than 16
(decompresssize is 16k this way), else they are not compressed and you should
copy them as delivered (decompresssize=topicblocksize-12).
do not decompress to more than decompresssize bytes. as this would cause
ambiguos values for topicpos, the help compilers will not compress more, but
fill the remaining topic block with 0es. data will continue in the next
topic block.
topicpos
a topicpos is used to locate the position of topiclinks in |topic and contains
the topicblocknumber in it's higher bits and an offset into the decompression
buffer in it's lower bits.
how many bits are used for topicblocknumber and topicblockoffset depends on
the compression method used and the topicblocksize:
(topicpos-sizeof(topicblockheader))%decompresssize = topicblockoffset
(topicpos-sizeof(topicblockheader))/decompresssize = topicblocknumber
a topicpos below sizeof(topicblockheader) is invalid.
topiclink
a topiclink (located inside the buffer after decompression, the first of it
pointed to by topicblockheaders firsttopiclink field) looks like this:
long blocksize		  size of topiclink + linkdata1 + compressed linkdata2
long datalen2		  length of decompressed linkdata2
topicpos prevblock	  windows 3.0 (hc30): number of bytes previous
topiclink is located before this topiclinc,
including eventually skipped topicblockheader and
unused bytes.
windows 3.1 (hc31): topicpos of previous topiclink
topicpos nextblock	  windows 3.0 (hc30): number of bytes next topiclink
is located behind this topiclink, incl. eventually
skipped topicblockheader and unused bytes.
windows 3.1 (hc31): topicpos of next topiclink
long datalen1		  includes size of topiclink
unsigned char recordtype  see below
----
char linkdata1[datalen1-11]
char linkdata2[blocksize-datalen1]
linkdata2 may be compressed using phrase compression. if you find
datalen2>blocksize-datalen1 use the following algorithm to decompress
if your help file contains a |phrases internal file:
  take the next character. if it's value is 0 or above 15 emit it. else
multiply it with 256, subtract 256 and add the value of the next character.
divide by 2 to get the phrase number. emit the phrase from the |phrase file
and append a space if the division had a remainder (the number was odd).
if the help file doesn't contain a |phrases file but instead a |phrindex
and |phrimage, it uses hall compression and the decompression of linkdata2
is a bit more difficult:
  take the next character (ch). if ch is even emit the phrase number ch/2.
else if the least two bits are 01 multiply by 64, add 64 and the value of
the next character. emit the phrase using this number. if the least three
bits are 011 copy the next ch/8+1 characters. if the least four bits are
0111 emit ch/16+1 spaces. if the least four bits are 1111 emit ch/16+1 nul's.
if datalen2<=blocksize-datalen1 the datalen2 bytes of linkdata2 are stored
uncompressed (makes a difference for hall compression only).
if datalen2<blocksize-datalen1 the remaining blocksize-datalen1-datalen2 bytes
are unused, but must be read from the |topic file (this can only happen in hall
compressed help files).
now that you know how to decompress the topic data, let's see what you get.
if the topiclink recordtype is 2 you got a topic header in linkdata1.
in windows 3.0 (hc30) the topicheader is structured like this:
long blocksize		  size of topic, including internal topic links
long prevtopicnumber	  -1l or 0xffff at the beginning of a browse sequence
long nexttopicnumber	  -1l or 0xffff at the end of a browse sequence
in windows version 3.1 (hc31) and later it looks like this:
long blocksize		  size of topic, including internal topic links
topicoffset browsebck	  topic offset for prev topic in browse sequence
topicoffset browsefor	  topic offset for next topic in browse sequence
long topicnum		  topic number
topicpos nonscroll	  start of non-scrolling region (topic offset) or -1l
topicpos scroll 	  start of scrolling region (topic offset)
topicpos nexttopic	  start of next type 2 record
the linkdata2 of topic recordtype 2 contains nul terminated strings. the
first string is the topic title, the next strings contain all macros to be
executed on opening this topic (specified using the ! footnote).
if the topiclink recordtype is 1, you have a windows 3.0 displayable text
record, a recordtype of 0x20 is windows 3.1 displayable text and 0x23 is
a windows 3.1 table record. a displayable text record may contain multiple
paragraphs, but all have the same paragraph formatting. a table record
stores all rows and columns of a table and may contain multiple paragraphs
of different formatting.
data inside linkdata1 is sometimes stored as compressed shorts or longs:
a compressed unsigned short is made of a single byte. divide by two to get
the value if it's even. divide by two and add 128 times the value of the
next byte if it's odd.
a compressed signed short is made of a single byte. divide by two and sub-
tract 64 to get the value if it's even. divide by two, add 128 times the
value of the next byte and subtract 16384 if it's odd.
a compressed unsigned long is made of a 2 byte value. divide by two to get
it's value if it's even. divide by two and add 32768 times the value of the
next 2 bytes if it's odd.
a compressed signed long is made of a 2 byte value. divide by two and sub-
tract 16384 to get it's value if it's even. divide by two, add 32768 times
the value of the next 2 bytes and subtract 67108864 if it's odd.
the structure of linkdata1 in recordtype 1, 0x20, and 0x23 is difficult to
describe, as some values are only stored if a certain condition is met and
is therefore of variable size. i try to describe them as a c-structure and
note which fields are not present under certain circumstances. don't
declare this structure. write a parser which reads a value only if it's
condition is met.
the metric used (gapwidth, leftindent, etc.) is dependend upon the font-
descriptor used (see |font file). it may be halfpoints or twips.
compressed long topicsize
struct					only in records type 0x20 and 0x23
{
compressed unsigned short topiclength
struct				only in records type 0x23
{
unsigned char numberofcolumns
unsigned char tabletype 	0,2=variable width, 1,3=normal
struct				only for tabletype 0 and 2
{
short mintablewidth
}
fortabletype0or2only
struct
{
short gapwidth		leftmargin if first column
short colwidth		relative in variable width tables
sum of all gapwidth/colwidth values
is 32767 in variable width tables
}
column[numberofcolumns]
}
recordtype0x23only
}
recordtype0x20or0x23only
struct
{
struct				only in recordtype 0x23
{
short column			-1 if end of topic, don't continue
short unknown
char always0
}
recordtype0x23only
unsigned char unknownunsignedchar
char unknownbiasedchar
unsigned short id
struct
{
unsigned short unknownfollows:1
unsigned short spacingabovefollows:1
unsigned short spacingbelowfollows:1
unsigned short spacinglinesfollows:1
unsigned short leftindentfollows:1
unsigned short rightindentfollows:1
unsigned short firstlineindentfollows:1
unsigned short unused:1
unsigned short borderinfofollows:1
unsigned short tabinfofollows:1
unsigned short rightalignedparagraph:1
unsigned short centeralignedparagraph:1
}
bits
compressed long  unknown		only if unknownfollows set
compressed short spacingabove	only if spacingabovefollows set
compressed short spacingbelow	only if spacingbelowfollows set
compressed short spacinglines	only if spacinglinesfollows set
compressed short leftindent 	only if leftindentfollows set
compressed short rightindent	only if rightindentfollows set
compressed short firstlineindent	only if firstlineindentfollows set
struct				only if borderinfofollows set
{
unsigned char borderbox:1
unsigned char bordertop:1
unsigned char borderleft:1
unsigned char borderbottom:1
unsigned char borderright:1
unsigned char borderthick:1
unsigned char borderdouble:1
unsigned char borderunknown:1
short borderwidth
}
borderinfo
struct				only if tabinfofollows set
{
compressed short numberoftabstops
struct
{
compressed unsigned short tabstop  position is lower 14 bits
struct			       only if tabstop bit 0x4000 set
{
compressed unsigned short tabtype	  1=right, 2=center
}
onlyiftabstopbit0x4000set
}
tab[numberoftabstops]
}
tabinfo
}
paragraphinfo
behind this structure linkdata1 contains character formatting information.
always output the next string (nul terminated) from linkdata2 (use phrase
decompression if required), than read the next formatting command, set up
the required font, color or position before displaying the next string.
sometimes the string is of zero length, as multiple formatting commands are
required before output.
0xff: end of character formatting. proceed with next paragraphinfo if
recordtype is 0x23, else you are done.
0x20: long vfldnumber	  0 = {vfld}   n = {vfld n}
0x21: short dtypenumber   0 = {dtype}  n = {dtype n}
0x80: short fontnumber	  index into descriptor array of internal |font file
0x81: line break	  no firstlineindent/spacingabove on next paragraph
0x82: end of paragraph	  next paragraph has same paragraphinfo as this one
0x83: tab		  jump to next tab stop
0x86: ewc or bmc or bmcwd or bmct or button or mci
0x87: ewl or bml or bmlwd or bmlt or button or mci_left
0x88: ewr or bmr or bmrwd or bmrt or button or mci_right
unsigned char type		5=embedded, 3 or 0x22=picture
compressed long picturesize	size of union
struct				only if type = 0x22
{
compressed word numberofhotspots	add to topicpos if counting
}
onlyiftypeis0x22
union
{
struct
{
short pictureisembedded	0=bmc/bmr/bml or 1=bmcwd/bmlwd/bmrwd
short picturenumber	only if pictureisembedded = 0
char embeddedpicture[picturesize-4]
only if pictureisembedded = 1
see 'format of pictures' section
}
type3or0x22
struct
{
short unknown1
short unknown2
short unknown3
stringz embedded		format of string depends on statement
dllname,windowclass,param     if ewc/ewr/ewl
!label,macro		    if button
*n,m,[helpfilename+]filename  if mci/mci_left/mci_right
n=0x8400
n+=2 if noplaybar specified
n+=8 if nomenu specified
m=0
m+=1 if play specified
n+=2 if repeat specified
[helpfilename+] if not external
}
type5only
}
picturedata			size of union is picturesize
0x89: end of hotspot	  switch back from underlined green
0x8b: non-break-space	  the blank does not appear in linkdata2
0x8c: non-break-hyphen	  the hyphen itself is stored in linkdata2
0xc8: macro		  start with underlined green
0xcc: macro without font change
short length
char macrostring[length-3]
0xe0: popup jump	  start with underlined green
0xe1: topic jump	  start with underlined green
topicoffset topicoffset
0xe2: popup jump	  start with underlined green
0xe3: topic jump	  start with underlined green
0xe3: topic jump	  start with underlined green
0xe6: popup jump without font change
0xe7: topic jump without font change
topicoffset topicoffset
0xea: popup jump into external file			   start with underlined green
0xeb: popup jump into external file without font change
0xee: topic jump into external file / secondary window	   start with underlined green
0xef: topic jump into external file / secondary window without font change
short sizeoffollowingstruct
struct
{
unsigned char type		0, 1, 4 or 6
topicoffset topicoffset
unsigned char windownumber	only if type = 1
stringz nameofexternalfile	only if type = 4 or 6
stringz windowname		only if type = 6
}
continue outputting strings from linkdata2 and parsing formatting commands
from linkdata1 until the 'end of character formatting' command is found.
topicoffset
a topicoffset is used since winhelp 3.1 to locate a cursor-like position, even
in the middle of a topic. the position must be unique for hotspots (tabbing).
and it needs to be unique for every scrollable position (going 'back' to a
topic that was scrolled). and it needs to quickly give you the topic block
to read from the help file.
like a topicpos, a topicoffset is divided into a topicblocknumber in it's
17 higher bits (topicpos/32768) and a charactercount in it's 15 lower bits
(topicpos%32768) counting all characters and the number of hotspots in
pictures appearing in all topiclinks in the topic block before this position.
if you got a topicoffset, seek to the topicblock in |topic as told by the
topicblocknumber, read in and decompress the whole block. use firsttopiclink
to locate the first topiclink in this decompressed block (charactercount is
0 at this place) and follow the list of topiclinks up to the desired
position, adding topiclength of every recordtype 0x20 and 0x23 you come
across, until adding topiclength would exceed the desired characterposition.
your position is located in this tl_display or tl_table topiclink. expand
linkdata2 if phrase compressed and follow the formatting procedure described
above incrementing charactercount on every character (and nul-terminator)
passed. add the numberofhotspots if a picture is included.
if a topiclink crosses a topic block, this has no effect on the topicblock-
number for this topiclink (i.e. a topicoffset pointing into the second part
has the topicblocknumber of the beginning of the topiclink).
if you didn't come across a topicheader (topiclink recordtype 2) in this
process, the beginning of the topic is located in a previous block. the
lasttopicheader field of the topicblockheader of the current block tells
you where to find it.
walking topics
to follow all topics contained in the help file, set the current topicpos
to 12 (that's firsttopiclink of the first topicblockheader at offset 0 in
|topic) and load it's topicblock ((12-12)/decompresssize = 0) and decompress.
the topiclink is located at topicblockoffset ((12-12)%decompresssize = 0)
in the decompression buffer. the first topiclink contains the topicheader
of the first topic.
in windows 3.0 (hc30) help files you move from one topiclink to the next
by adding nextblock to the current topicpos. if the next topiclink is
located in the next topic block, the value of nextblock handles the jump
over the intervening topicblockheader and possibly unused bytes nicely.
in windows 3.1 (hc31) and later you move from one topiclink to the next
by setting the current position to nextblock, which also handles the jump
from one topic block to the other nicely.
the last topiclink has nextblock set to 0 or -1l. the last topiclink does
not contain any usable data.
format of pictures
inside help files bitmaps and metafiles are stored in lp- or lp-format. this
is the format of shg/mrb files that shed/mrbc produce and may contain multiple
pictures at different resolutions, each with optional additional hotspot data.
pictures may be embedded in linkdata2 of |topic or appear as |bm<x> files
(or bm<x> in case of windows 3.0 hc30). each picture starts with this header
data. the pictureoffset tells you where to look for the desired picture.
short magic				  0x506c (shg,lp) or 0x706c (mrb,lp)
short numberofpictures			  >1 if multi-resolution-bitmap
long pictureoffset[numberofpictures]	  relative to &magic
you shouldn't depend on magic lp/lp upon reading, as there are some mrbs
flagged like shg, but please write correct values.
seek to pictureoffset and you will find this:
char picturetype	   5=ddb 6=dib 8=metafile
char packingmethod	   0=uncompressed 1=runlen 2=lz77 3=both
if picturetype is 5 or 6 the picture is a bitmap described by:
compressed unsigned long xdpi		    resolution in dpi, not pelspermeter
compressed unsigned long ydpi		    resolution in dpi, not pelspermeter
compressed unsigned short planes
compressed unsigned short bitcount
compressed unsigned long width
compressed unsigned long height
compressed unsigned long colorsused
compressed unsigned long colorsimportant    1 if bitmap is transparent
compressed unsigned long compressedsize
compressed unsigned long hotspotsize	    0 if none are defined
unsigned long compressedoffset		    relative to &picturetype
unsigned long hotspotoffset		    relative to &picturetype
if picturetype is 6 a color palette follows immediatly
colorref palette[colorsused]		    or 1<<bitcount if colorsused=0
if packingmethod is 0 copy compressedsize bytes starting at compressedoffset
to retrieve the bitmap data. if packingmethod is 1 seek to compressedoffset,
and decode compressedsize bytes using the runlen algorithm:
n=getc(f); if(n&0x80) copy n&0x7f bytes, else copy next byte n times.
if packingmethod is 2 use the lz77 algorithm described above and if packing-
method is 3 first use lz77, then runlen to decompress.
if picturetype is 8 the picture is a metafile described by:
compressed unsigned short mappingmode
unsigned short width
unsigned short height
compressed unsigned long decompressedsize   can be used to allocate buffer
compressed unsigned long compressedsize
compressed unsigned long hotspotsize	    0 if none are defined
unsigned long compressedoffset		    relative to &picturetype
unsigned long hotspotoffset		    relative to &picturetype
seek to compressedoffset and decompress compressedsize bytes as described
above to retrieve metafile data.
if hotspotsize or hotspotoffset is 0, no hotspots are defined. otherwise
seek to hotspotoffset and retrieve hotspotsize bytes of hotspot definition
as declared below. each macro hotspot contributes data to macrodata in a
way not fully understood at this moment.
unsigned char always1
unsigned short numberofhotspots
unsigned long sizeofmacrodata
struct
{
unsigned char id0,id1,id2;
unsigned short x,y,w,h;
unsigned long hash;
}
hotspot[numberofhotspots]
char macrodata[sizeofmacrodata]       if sizeofmacrodata>0 the first byte
of macrodata is always 2.
struct
{
stringz hotspotname
stringz contextnameormacro
}
stringdata[numberofhotspots]
possible values of id0,id1,id2 are:
0xc8 0x00 0x00	macro visible
0xcc 0x04 0x00	macro invisible
0xe2 0x00 0x00	popup jump visible
0xe3 0x00 0x00	topic jump visible
0xe6 0x04 0x00	popup jump invisible
0xe7 0x04 0x00	topic jump invisible
0xea 0x00 0x00	popup jump into external file visible
0xeb 0x00 0x00	topic jump into external file / secondary window visible
0xee 0x04 0x00	popup jump into external file invisible
0xef 0x04 0x00	topic jump into external file / secondary window invisible
the hash field is only used if id0 = 0xe2, 0xe3, 0xe6, 0xe7. it is 1 if
id0 = 0xc8 or 0xcc.
the contextnameormacro contains a macro if id0 = 0xc8 or 0xcc, otherwise
it contains a contextname (id0 = 0xe2, 0xe3, 0xe6, 0xe7) or the complete
reference contextname>window@file (id0 = 0xea, 0xeb, 0xee, 0xef) (@file
may be missing if target is in same file).
annotation file format
an annotation file created by winhelp uses the same basic file format as
a windows help file. the first 16 bytes contain the same header as a help
file, with same magic. directorystart points to a fileheader of an internal
directory formatted the same way as a help file internal directory. there
are just internal files of different name and format used to collect the
annotations.
@version
the first internal file described contains (after the usual fileheader) 6
bytes of version info:
0x08 0x62 0x6d 0x66 0x01 0x00	       (i've never seen other values)
@link
the @link internal file contains (after the usual fileheader) the number of
annotations and the topicoffset of every annotation. the topicoffset separates
into a topicblocknumber in it's upper bits and topicblockoffset pointing into
the decompression buffer in it's lower bits as explained above in the
description of the |topic format and points the the first topiclink following
the topicheader of the topic where the annotation belongs to.
unsigned short numberofannotations
struct
{
unsigned long topicoffset
unsigned long unknown1	  // always 0
unsigned long unknown2	  // always 0
}
annotationtopicref[numberofannotations]
n!0
for each annotation the ann file also carrys an internal file with a name like
12345!0, where 12345 is the decimal representation of the topicoffset (as
listed in the @link array) where the annotation belongs to. these files
contain the annotation text as unformatted, uncompressed plain ansi characters,
and are not nul terminated.
that's all what i've seen in an annotation file.
*.cac, *.aux
multimedia files using extensions *.cac or *.aux are formatted like helpfiles,
but contain only auxillary files, no |system or |topic.
investigate them yourself. helpdeco may be used to display or extract files
contained in them.
lz77
you want to handle lz77 compressed data in hlps, mrbs, and shgs yourself ?
here is an algorithm to do it:
// lz77 compression / decompression algorithm
// this is the compression microsoft used in windows *.hlp and *.mrb files
// so it works like microsoft compress.exe/expand.exe/lzexpand.dll
//#define msexpand
#include <stdio.h>
#include <stdlib.h>
#define n 4096
#define f 16
#define threshold 3
#define dad (node+1)
#define lson (node+1+n)
#define rson (node+1+n+n)
#define root (node+1+n+n+n)
#define nil -1
char *buffer;
int *node;
int pos;
int insert(int i,int run)
{
int c,j,k,l,n,match;
int *p;
    k=l=1;
match=threshold-1;
p=&root[(unsigned char)buffer[i]];
lson[i]=rson[i]=nil;
while((j=*p)!=nil)
{
for(n=min(k,l);n<run&&(c=(buffer[j+n]-buffer[i+n]))==0;n++) ;
if(n>match)
{
match=n;
pos=j;
}
if(c<0)
{
p=&lson[j];
k=n;
}
else if(c>0)
{
p=&rson[j];
l=n;
}
else
{
dad[j]=nil;
dad[lson[j]]=lson+i-node;
dad[rson[j]]=rson+i-node;
lson[i]=lson[j];
rson[i]=rson[j];
break;
}
}
dad[i]=p-node;
*p=i;
return match;
}
void delete(int z)
{
int j;
    if(dad[z]!=nil)
{
if(rson[z]==nil)
{
j=lson[z];
}
else if(lson[z]==nil)
{
j=rson[z];
}
else
{
j=lson[z];
if(rson[j]!=nil)
{
do
{
j=rson[j];
}
while(rson[j]!=nil);
node[dad[j]]=lson[j];
dad[lson[j]]=dad[j];
lson[j]=lson[z];
dad[lson[z]]=lson+j-node;
}
rson[j]=rson[z];
dad[rson[z]]=rson+j-node;
}
dad[j]=dad[z];
node[dad[z]]=j;
dad[z]=nil;
}
}
void compress(file *f,file *out)
{
int ch,i,run,len,match,size,mask;
char buf[17];
    buffer=malloc(n+f+(n+1+n+n+256)*sizeof(int)); // 28.5 k !
if(buffer)
{
#ifdef msexpand
struct { long magic, magic2; int magic3; long filesize; } header;
	header.magic=0x44445a53l; // szdd
header.magic2=0x3327f088l;
header.magic3=0x0041;
header.filesize=filelength(fileno(f));
fwrite(&header,sizeof(header),1,out);
#endif
node=(int *)(buffer+n+f);
for(i=0;i<256;i++) root[i]=nil;
for(i=nil;i<n;i++) dad[i]=nil;
size=mask=1;
buf[0]=0;
i=n-f-f;
for(len=0;len<f&&(ch=getc(f))!=-1;len++)
{
buffer[i+f]=ch;
i=(i+1)&(n-1);
}
run=len;
do
{
ch=getc(f);
if(i>=n-f)
{
delete(i+f-n);
buffer[i+f]=buffer[i+f-n]=ch;
}
else
{
delete(i+f);
buffer[i+f]=ch;
}
match=insert(i,run);
if(ch==-1)
{
run--;
len--;
}
if(len++>=run)
{
if(match>=threshold)
{
#ifdef msexpand
buf[size++]=pos;
buf[size++]=((pos>>4)&0xf0)+(match-3);
#else
buf[0]|=mask;
*(int *)(buf+size)=((match-3)<<12)|((i-pos-1)&(n-1));
size+=2;
#endif
len-=match;
}
else
{
#ifdef msexpand
buf[0]|=mask;
#endif
buf[size++]=buffer[i];
len--;
}
if(!((mask+=mask)&0xff))
{
fwrite(buf,size,1,out);
size=mask=1;
buf[0]=0;
}
}
i=(i+1)&(n-1);
}
while(len>0);
if(size>1) fwrite(buf,size,1,out);
free(buffer);
}
}
void expand(file *f,file *out)
{
int bits,ch,i,j,len,mask;
char *buffer;
#ifdef msexpand
struct { long magic, magic2; int magic3; long filesize; } header;
    i=fread(&header,1,sizeof(header),f);
if(i!=sizeof(header)||header.magic!=0x44445a53l
||header.magic2!=0x3327f088l||header.magic3!=0x0041)
{
fwrite(&header,1,i,out);
while((ch=getc(f))!=-1) putc(ch,out);
return;
}
#endif
buffer=malloc(n);
if(buffer)
{
i=n-f;
while((bits=getc(f))!=-1)
{
for(mask=0x01;mask&0xff;mask<<=1)
{
#ifdef msexpand
if(!(bits&mask))
{
j=getc(f);
if(j==-1) break;
len=getc(f);
j+=(len&0xf0)<<4;
len=(len&15)+3;
#else
if(bits&mask)
{
j=getw(f);
len=((j>>12)&15)+3;
j=(i-j-1)&(n-1);
#endif
while(len--)
{
putc(buffer[i]=buffer[j],out);
j=(j+1)&(n-1);
i=(i+1)&(n-1);
}
}
else
{
ch=getc(f);
#ifndef msexpand
if(ch==-1) break;
#endif
putc(buffer[i]=ch,out);
i=(i+1)&(n-1);
}
}
}
free(buffer);
}
}
that's all i can tell you about the format of windows 3.x/95 help files.
if you found out more, please let me know.
m. winterhoff
100326.2776@compuserve.com