4 * Copyright 1996 Ulrich Schmid
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winhelp
);
36 static inline unsigned short GET_USHORT(const BYTE
* buffer
, unsigned i
)
38 return (BYTE
)buffer
[i
] + 0x100 * (BYTE
)buffer
[i
+ 1];
41 static inline short GET_SHORT(const BYTE
* buffer
, unsigned i
)
43 return (BYTE
)buffer
[i
] + 0x100 * (signed char)buffer
[i
+1];
46 static inline unsigned GET_UINT(const BYTE
* buffer
, unsigned i
)
48 return GET_USHORT(buffer
, i
) + 0x10000 * GET_USHORT(buffer
, i
+ 2);
51 static HLPFILE
*first_hlpfile
= 0;
52 static BYTE
*file_buffer
;
77 static BOOL
HLPFILE_DoReadHlpFile(HLPFILE
*, LPCSTR
);
78 static BOOL
HLPFILE_ReadFileToBuffer(HFILE
);
79 static BOOL
HLPFILE_FindSubFile(LPCSTR name
, BYTE
**, BYTE
**);
80 static BOOL
HLPFILE_SystemCommands(HLPFILE
*);
81 static INT
HLPFILE_UncompressedLZ77_Size(BYTE
*ptr
, BYTE
*end
);
82 static BYTE
* HLPFILE_UncompressLZ77(BYTE
*ptr
, BYTE
*end
, BYTE
*newptr
);
83 static BOOL
HLPFILE_UncompressLZ77_Phrases(HLPFILE
*);
84 static BOOL
HLPFILE_Uncompress_Phrases40(HLPFILE
*);
85 static BOOL
HLPFILE_Uncompress_Topic(HLPFILE
*);
86 static BOOL
HLPFILE_GetContext(HLPFILE
*);
87 static BOOL
HLPFILE_GetMap(HLPFILE
*);
88 static BOOL
HLPFILE_AddPage(HLPFILE
*, BYTE
*, BYTE
*, unsigned);
89 static BOOL
HLPFILE_AddParagraph(HLPFILE
*, BYTE
*, BYTE
*, unsigned*);
90 static void HLPFILE_Uncompress2(const BYTE
*, const BYTE
*, BYTE
*, const BYTE
*);
91 static BOOL
HLPFILE_Uncompress3(char*, const char*, const BYTE
*, const BYTE
*);
92 static void HLPFILE_UncompressRLE(const BYTE
* src
, const BYTE
* end
, BYTE
** dst
, unsigned dstsz
);
93 static BOOL
HLPFILE_ReadFont(HLPFILE
* hlpfile
);
96 /***********************************************************************
98 * HLPFILE_PageByNumber
100 static HLPFILE_PAGE
*HLPFILE_PageByNumber(LPCSTR lpszPath
, UINT wNum
)
103 HLPFILE
*hlpfile
= HLPFILE_ReadHlpFile(lpszPath
);
105 if (!hlpfile
) return 0;
107 WINE_TRACE("[%s/%u]\n", lpszPath
, wNum
);
109 for (page
= hlpfile
->first_page
; page
&& wNum
; page
= page
->next
) wNum
--;
111 /* HLPFILE_FreeHlpFile(lpszPath); */
118 * this finds the page containing the offset. The offset can either
119 * refer to the top of the page (offset == page->offset), or
120 * to some paragraph inside the page...
121 * As of today, we only return the page... we should also return
122 * a paragraph, and then, while opening a new page, compute the
123 * y-offset of the paragraph to be shown and scroll the window
126 /******************************************************************
127 * HLPFILE_PageByOffset
131 HLPFILE_PAGE
*HLPFILE_PageByOffset(HLPFILE
* hlpfile
, LONG offset
)
136 if (!hlpfile
) return 0;
138 WINE_TRACE("<%s>[%x]\n", hlpfile
->lpszPath
, offset
);
140 if (offset
== 0xFFFFFFFF) return NULL
;
143 for (found
= NULL
, page
= hlpfile
->first_page
; page
; page
= page
->next
)
145 if (page
->offset
<= offset
&& (!found
|| found
->offset
< page
->offset
))
149 WINE_ERR("Page of offset %u not found in file %s\n",
150 offset
, hlpfile
->lpszPath
);
154 /***********************************************************************
156 * HLPFILE_HlpFilePageByHash
158 HLPFILE_PAGE
*HLPFILE_PageByHash(HLPFILE
* hlpfile
, LONG lHash
)
162 if (!hlpfile
) return 0;
164 WINE_TRACE("<%s>[%x]\n", hlpfile
->lpszPath
, lHash
);
166 for (i
= 0; i
< hlpfile
->wContextLen
; i
++)
168 if (hlpfile
->Context
[i
].lHash
== lHash
)
169 return HLPFILE_PageByOffset(hlpfile
, hlpfile
->Context
[i
].offset
);
172 WINE_ERR("Page of hash %x not found in file %s\n", lHash
, hlpfile
->lpszPath
);
176 /***********************************************************************
180 HLPFILE_PAGE
*HLPFILE_PageByMap(HLPFILE
* hlpfile
, LONG lMap
)
184 if (!hlpfile
) return 0;
186 WINE_TRACE("<%s>[%x]\n", hlpfile
->lpszPath
, lMap
);
188 for (i
= 0; i
< hlpfile
->wMapLen
; i
++)
190 if (hlpfile
->Map
[i
].lMap
== lMap
)
191 return HLPFILE_PageByOffset(hlpfile
, hlpfile
->Map
[i
].offset
);
194 WINE_ERR("Page of Map %x not found in file %s\n", lMap
, hlpfile
->lpszPath
);
198 /***********************************************************************
202 HLPFILE_PAGE
* HLPFILE_Contents(HLPFILE
*hlpfile
)
204 HLPFILE_PAGE
* page
= NULL
;
206 if (!hlpfile
) return NULL
;
208 page
= HLPFILE_PageByOffset(hlpfile
, hlpfile
->contents_start
);
209 if (!page
) page
= hlpfile
->first_page
;
213 /***********************************************************************
217 LONG
HLPFILE_Hash(LPCSTR lpszContext
)
222 while ((c
= *lpszContext
++))
225 if (c
>= 'A' && c
<= 'Z') x
= c
- 'A' + 17;
226 if (c
>= 'a' && c
<= 'z') x
= c
- 'a' + 17;
227 if (c
>= '1' && c
<= '9') x
= c
- '0';
228 if (c
== '0') x
= 10;
229 if (c
== '.') x
= 12;
230 if (c
== '_') x
= 13;
231 if (x
) lHash
= lHash
* 43 + x
;
236 /***********************************************************************
238 * HLPFILE_ReadHlpFile
240 HLPFILE
*HLPFILE_ReadHlpFile(LPCSTR lpszPath
)
244 for (hlpfile
= first_hlpfile
; hlpfile
; hlpfile
= hlpfile
->next
)
246 if (!strcmp(lpszPath
, hlpfile
->lpszPath
))
248 hlpfile
->wRefCount
++;
253 hlpfile
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE
) + lstrlen(lpszPath
) + 1);
254 if (!hlpfile
) return 0;
256 hlpfile
->lpszPath
= (char*)hlpfile
+ sizeof(HLPFILE
);
257 hlpfile
->lpszTitle
= NULL
;
258 hlpfile
->lpszCopyright
= NULL
;
259 hlpfile
->first_page
= NULL
;
260 hlpfile
->first_macro
= NULL
;
261 hlpfile
->wContextLen
= 0;
262 hlpfile
->Context
= NULL
;
263 hlpfile
->wMapLen
= 0;
265 hlpfile
->contents_start
= 0xFFFFFFFF;
266 hlpfile
->prev
= NULL
;
267 hlpfile
->next
= first_hlpfile
;
268 hlpfile
->wRefCount
= 1;
270 hlpfile
->numBmps
= 0;
271 hlpfile
->bmps
= NULL
;
273 hlpfile
->numFonts
= 0;
274 hlpfile
->fonts
= NULL
;
276 hlpfile
->numWindows
= 0;
277 hlpfile
->windows
= NULL
;
279 strcpy(hlpfile
->lpszPath
, lpszPath
);
281 first_hlpfile
= hlpfile
;
282 if (hlpfile
->next
) hlpfile
->next
->prev
= hlpfile
;
284 phrases
.offsets
= NULL
;
285 phrases
.buffer
= NULL
;
290 if (!HLPFILE_DoReadHlpFile(hlpfile
, lpszPath
))
292 HLPFILE_FreeHlpFile(hlpfile
);
296 HeapFree(GetProcessHeap(), 0, phrases
.offsets
);
297 HeapFree(GetProcessHeap(), 0, phrases
.buffer
);
298 HeapFree(GetProcessHeap(), 0, topic
.map
);
299 HeapFree(GetProcessHeap(), 0, file_buffer
);
304 /***********************************************************************
306 * HLPFILE_DoReadHlpFile
308 static BOOL
HLPFILE_DoReadHlpFile(HLPFILE
*hlpfile
, LPCSTR lpszPath
)
315 unsigned index
, old_index
, offset
, len
, offs
;
317 hFile
= OpenFile(lpszPath
, &ofs
, OF_READ
);
318 if (hFile
== HFILE_ERROR
) return FALSE
;
320 ret
= HLPFILE_ReadFileToBuffer(hFile
);
322 if (!ret
) return FALSE
;
324 if (!HLPFILE_SystemCommands(hlpfile
)) return FALSE
;
326 /* load phrases support */
327 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile
))
328 HLPFILE_Uncompress_Phrases40(hlpfile
);
330 if (!HLPFILE_Uncompress_Topic(hlpfile
)) return FALSE
;
331 if (!HLPFILE_ReadFont(hlpfile
)) return FALSE
;
340 /* FIXME this depends on the blocksize, can be 2k in some cases */
341 index
= (ref
- 0x0C) >> 14;
342 offset
= (ref
- 0x0C) & 0x3fff;
344 WINE_TRACE("ref=%08x => [%u/%u]\n", ref
, index
, offset
);
346 if (index
>= topic
.wMapLen
) {WINE_WARN("maplen\n"); break;}
347 buf
= topic
.map
[index
] + offset
;
348 if (buf
+ 0x15 >= topic
.end
) {WINE_WARN("extra\n"); break;}
349 end
= min(buf
+ GET_UINT(buf
, 0), topic
.end
);
350 if (index
!= old_index
) {offs
= 0; old_index
= index
;}
355 if (!HLPFILE_AddPage(hlpfile
, buf
, end
, index
* 0x8000L
+ offs
)) return FALSE
;
359 if (!HLPFILE_AddParagraph(hlpfile
, buf
, end
, &len
)) return FALSE
;
364 if (!HLPFILE_AddParagraph(hlpfile
, buf
, end
, &len
)) return FALSE
;
369 WINE_ERR("buf[0x14] = %x\n", buf
[0x14]);
372 ref
= GET_UINT(buf
, 0xc);
373 } while (ref
!= 0xffffffff);
375 HLPFILE_GetMap(hlpfile
);
376 return HLPFILE_GetContext(hlpfile
);
379 /***********************************************************************
383 static BOOL
HLPFILE_AddPage(HLPFILE
*hlpfile
, BYTE
*buf
, BYTE
*end
, unsigned offset
)
391 if (buf
+ 0x31 > end
) {WINE_WARN("page1\n"); return FALSE
;};
392 title
= buf
+ GET_UINT(buf
, 0x10);
393 if (title
> end
) {WINE_WARN("page2\n"); return FALSE
;};
395 titlesize
= GET_UINT(buf
, 4);
396 page
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE
) + titlesize
+ 1);
397 if (!page
) return FALSE
;
398 page
->lpszTitle
= (char*)page
+ sizeof(HLPFILE_PAGE
);
400 if (hlpfile
->hasPhrases
)
402 HLPFILE_Uncompress2(title
, end
, (BYTE
*)page
->lpszTitle
, (BYTE
*)page
->lpszTitle
+ titlesize
);
406 if (GET_UINT(buf
, 0x4) > GET_UINT(buf
, 0) - GET_UINT(buf
, 0x10))
408 /* need to decompress */
409 HLPFILE_Uncompress3(page
->lpszTitle
, page
->lpszTitle
+ titlesize
,
414 memcpy(page
->lpszTitle
, title
, titlesize
);
418 page
->lpszTitle
[titlesize
] = '\0';
420 if (hlpfile
->first_page
)
424 for (p
= hlpfile
->first_page
; p
->next
; p
= p
->next
);
430 hlpfile
->first_page
= page
;
434 page
->file
= hlpfile
;
436 page
->first_paragraph
= NULL
;
437 page
->first_macro
= NULL
;
438 page
->wNumber
= GET_UINT(buf
, 0x21);
439 page
->offset
= offset
;
441 page
->browse_bwd
= GET_UINT(buf
, 0x19);
442 page
->browse_fwd
= GET_UINT(buf
, 0x1D);
444 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
445 page
->wNumber
, page
->lpszTitle
,
446 page
->browse_bwd
, page
->offset
, page
->browse_fwd
);
448 memset(&attributes
, 0, sizeof(attributes
));
450 /* now load macros */
451 ptr
= page
->lpszTitle
+ strlen(page
->lpszTitle
) + 1;
452 while (ptr
< page
->lpszTitle
+ titlesize
)
454 unsigned len
= strlen(ptr
);
457 WINE_TRACE("macro: %s\n", ptr
);
458 macro
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO
) + len
+ 1);
459 macro
->lpszMacro
= macro_str
= (char*)(macro
+ 1);
460 memcpy(macro_str
, ptr
, len
+ 1);
461 /* FIXME: shall we really link macro in reverse order ??
462 * may produce strange results when played at page opening
464 macro
->next
= page
->first_macro
;
465 page
->first_macro
= macro
;
472 static long fetch_long(BYTE
** ptr
)
478 ret
= (*(unsigned long*)(*ptr
) - 0x80000000L
) / 2;
483 ret
= (*(unsigned short*)(*ptr
) - 0x8000) / 2;
490 static unsigned long fetch_ulong(BYTE
** ptr
)
496 ret
= *(unsigned long*)(*ptr
) / 2;
501 ret
= *(unsigned short*)(*ptr
) / 2;
507 static short fetch_short(BYTE
** ptr
)
513 ret
= (*(unsigned short*)(*ptr
) - 0x8000) / 2;
518 ret
= (*(unsigned char*)(*ptr
) - 0x80) / 2;
524 static unsigned short fetch_ushort(BYTE
** ptr
)
530 ret
= *(unsigned short*)(*ptr
) / 2;
535 ret
= *(unsigned char*)(*ptr
) / 2;
541 /******************************************************************
542 * HLPFILE_DecompressGfx
544 * Decompress the data part of a bitmap or a metafile
546 static BYTE
* HLPFILE_DecompressGfx(BYTE
* src
, unsigned csz
, unsigned sz
, BYTE packing
)
553 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing
, csz
, sz
);
557 case 0: /* uncompressed */
559 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz
, csz
);
563 tmp
= dst
= HeapAlloc(GetProcessHeap(), 0, sz
);
564 if (!dst
) return NULL
;
565 HLPFILE_UncompressRLE(src
, src
+ csz
, &tmp
, sz
);
567 WINE_WARN("Bogus gfx sizes (RunLen): %u/%u\n", tmp
- dst
, sz
);
570 sz77
= HLPFILE_UncompressedLZ77_Size(src
, src
+ csz
);
571 dst
= HeapAlloc(GetProcessHeap(), 0, sz77
);
572 if (!dst
) return NULL
;
573 HLPFILE_UncompressLZ77(src
, src
+ csz
, dst
);
575 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77
, sz
);
577 case 3: /* LZ77 then RLE */
578 sz77
= HLPFILE_UncompressedLZ77_Size(src
, src
+ csz
);
579 tmp
= HeapAlloc(GetProcessHeap(), 0, sz77
);
580 if (!tmp
) return FALSE
;
581 HLPFILE_UncompressLZ77(src
, src
+ csz
, tmp
);
582 dst
= tmp2
= HeapAlloc(GetProcessHeap(), 0, sz
);
583 if (!dst
) return FALSE
;
584 HLPFILE_UncompressRLE(tmp
, tmp
+ sz77
, &tmp2
, sz
);
585 if (tmp2
- dst
!= sz
)
586 WINE_WARN("Bogus gfx sizes (LZ77+RunLen): %u / %u\n", tmp2
- dst
, sz
);
587 HeapFree(GetProcessHeap(), 0, tmp
);
590 WINE_FIXME("Unsupported packing %u\n", packing
);
596 /******************************************************************
601 static BOOL
HLPFILE_LoadBitmap(BYTE
* beg
, BYTE type
, BYTE pack
,
602 HLPFILE_PARAGRAPH
* paragraph
)
607 unsigned long off
, csz
;
610 bi
= HeapAlloc(GetProcessHeap(), 0, sizeof(*bi
));
611 if (!bi
) return FALSE
;
613 ptr
= beg
+ 2; /* for type and pack */
615 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
616 bi
->bmiHeader
.biXPelsPerMeter
= fetch_ulong(&ptr
);
617 bi
->bmiHeader
.biYPelsPerMeter
= fetch_ulong(&ptr
);
618 bi
->bmiHeader
.biPlanes
= fetch_ushort(&ptr
);
619 bi
->bmiHeader
.biBitCount
= fetch_ushort(&ptr
);
620 bi
->bmiHeader
.biWidth
= fetch_ulong(&ptr
);
621 bi
->bmiHeader
.biHeight
= fetch_ulong(&ptr
);
622 bi
->bmiHeader
.biClrUsed
= fetch_ulong(&ptr
);
623 bi
->bmiHeader
.biClrImportant
= fetch_ulong(&ptr
);
624 bi
->bmiHeader
.biCompression
= BI_RGB
;
625 if (bi
->bmiHeader
.biBitCount
> 32) WINE_FIXME("Unknown bit count %u\n", bi
->bmiHeader
.biBitCount
);
626 if (bi
->bmiHeader
.biPlanes
!= 1) WINE_FIXME("Unsupported planes %u\n", bi
->bmiHeader
.biPlanes
);
627 bi
->bmiHeader
.biSizeImage
= (((bi
->bmiHeader
.biWidth
* bi
->bmiHeader
.biBitCount
+ 31) & ~31) / 8) * bi
->bmiHeader
.biHeight
;
628 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
629 bi
->bmiHeader
.biPlanes
, bi
->bmiHeader
.biBitCount
,
630 bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
);
632 csz
= fetch_ulong(&ptr
);
633 fetch_ulong(&ptr
); /* hotspot size */
635 off
= GET_UINT(ptr
, 0); ptr
+= 4;
636 /* GET_UINT(ptr, 0); hotspot offset */ ptr
+= 4;
638 /* now read palette info */
641 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
644 /* not quite right, especially for bitfields type of compression */
645 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
646 nc
= 1 << bi
->bmiHeader
.biBitCount
;
648 bi
= HeapReAlloc(GetProcessHeap(), 0, bi
, sizeof(*bi
) + nc
* sizeof(RGBQUAD
));
649 if (!bi
) return FALSE
;
650 for (i
= 0; i
< nc
; i
++)
652 bi
->bmiColors
[i
].rgbBlue
= ptr
[0];
653 bi
->bmiColors
[i
].rgbGreen
= ptr
[1];
654 bi
->bmiColors
[i
].rgbRed
= ptr
[2];
655 bi
->bmiColors
[i
].rgbReserved
= 0;
659 pict_beg
= HLPFILE_DecompressGfx(beg
+ off
, csz
, bi
->bmiHeader
.biSizeImage
, pack
);
661 paragraph
->u
.gfx
.u
.bmp
.hBitmap
= CreateDIBitmap(hdc
= GetDC(0), &bi
->bmiHeader
,
665 if (!paragraph
->u
.gfx
.u
.bmp
.hBitmap
)
666 WINE_ERR("Couldn't create bitmap\n");
668 HeapFree(GetProcessHeap(), 0, bi
);
669 if (pict_beg
!= beg
+ off
) HeapFree(GetProcessHeap(), 0, pict_beg
);
674 /******************************************************************
675 * HLPFILE_LoadMetaFile
679 static BOOL
HLPFILE_LoadMetaFile(BYTE
* beg
, BYTE pack
, HLPFILE_PARAGRAPH
* paragraph
)
682 unsigned long size
, csize
;
683 unsigned long off
, hsoff
;
685 LPMETAFILEPICT lpmfp
;
687 WINE_TRACE("Loading metafile\n");
689 ptr
= beg
+ 2; /* for type and pack */
691 lpmfp
= ¶graph
->u
.gfx
.u
.mfp
;
692 lpmfp
->mm
= fetch_ushort(&ptr
); /* mapping mode */
694 lpmfp
->xExt
= GET_USHORT(ptr
, 0);
695 lpmfp
->yExt
= GET_USHORT(ptr
, 2);
698 size
= fetch_ulong(&ptr
); /* decompressed size */
699 csize
= fetch_ulong(&ptr
); /* compressed size */
700 fetch_ulong(&ptr
); /* hotspot size */
701 off
= GET_UINT(ptr
, 0);
702 hsoff
= GET_UINT(ptr
, 4);
705 WINE_TRACE("sz=%lu csz=%lu (%d,%d) offs=%lu/%u,%lu\n",
706 size
, csize
, lpmfp
->xExt
, lpmfp
->yExt
, off
, ptr
- beg
, hsoff
);
708 bits
= HLPFILE_DecompressGfx(beg
+ off
, csize
, size
, pack
);
709 if (!bits
) return FALSE
;
711 paragraph
->cookie
= para_metafile
;
713 lpmfp
->hMF
= SetMetaFileBitsEx(size
, bits
);
716 WINE_FIXME("Couldn't load metafile\n");
718 if (bits
!= beg
+ off
) HeapFree(GetProcessHeap(), 0, bits
);
723 /******************************************************************
724 * HLPFILE_LoadGfxByAddr
728 static BOOL
HLPFILE_LoadGfxByAddr(HLPFILE
*hlpfile
, BYTE
* ref
,
730 HLPFILE_PARAGRAPH
* paragraph
)
734 numpict
= GET_USHORT(ref
, 2);
735 WINE_TRACE("Got picture magic=%04x #=%d\n",
736 GET_USHORT(ref
, 0), numpict
);
738 for (i
= 0; i
< numpict
; i
++)
744 WINE_TRACE("Offset[%d] = %x\n", i
, GET_UINT(ref
, (1 + i
) * 4));
745 beg
= ptr
= ref
+ GET_UINT(ref
, (1 + i
) * 4);
752 case 5: /* device dependent bmp */
753 case 6: /* device independent bmp */
754 HLPFILE_LoadBitmap(beg
, type
, pack
, paragraph
);
757 HLPFILE_LoadMetaFile(beg
, pack
, paragraph
);
759 default: WINE_FIXME("Unknown type %u\n", type
); return FALSE
;
762 /* FIXME: hotspots */
764 /* FIXME: implement support for multiple picture format */
765 if (numpict
!= 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
771 /******************************************************************
772 * HLPFILE_LoadGfxByIndex
776 static BOOL
HLPFILE_LoadGfxByIndex(HLPFILE
*hlpfile
, unsigned index
,
777 HLPFILE_PARAGRAPH
* paragraph
)
783 WINE_TRACE("Loading picture #%d\n", index
);
785 if (index
< hlpfile
->numBmps
&& hlpfile
->bmps
[index
] != NULL
)
787 paragraph
->u
.gfx
.u
.bmp
.hBitmap
= hlpfile
->bmps
[index
];
791 sprintf(tmp
, "|bm%u", index
);
793 if (!HLPFILE_FindSubFile(tmp
, &ref
, &end
)) {WINE_WARN("no sub file\n"); return FALSE
;}
797 ret
= HLPFILE_LoadGfxByAddr(hlpfile
, ref
, end
- ref
, paragraph
);
800 if (ret
&& paragraph
->cookie
== para_bitmap
)
802 if (index
>= hlpfile
->numBmps
)
804 hlpfile
->numBmps
= index
+ 1;
806 hlpfile
->bmps
= HeapReAlloc(GetProcessHeap(), 0, hlpfile
->bmps
,
807 hlpfile
->numBmps
* sizeof(hlpfile
->bmps
[0]));
809 hlpfile
->bmps
= HeapAlloc(GetProcessHeap(), 0,
810 hlpfile
->numBmps
* sizeof(hlpfile
->bmps
[0]));
813 hlpfile
->bmps
[index
] = paragraph
->u
.gfx
.u
.bmp
.hBitmap
;
818 /******************************************************************
823 static HLPFILE_LINK
* HLPFILE_AllocLink(int cookie
, const char* str
, LONG hash
,
824 BOOL clrChange
, unsigned wnd
)
829 /* FIXME: should build a string table for the attributes.link.lpszPath
830 * they are reallocated for each link
832 link
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK
) + strlen(str
) + 1);
833 if (!link
) return NULL
;
835 link
->cookie
= cookie
;
836 link
->lpszString
= link_str
= (char*)link
+ sizeof(HLPFILE_LINK
);
837 strcpy(link_str
, str
);
839 link
->bClrChange
= clrChange
? 1 : 0;
843 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
844 link
->cookie
, link
->lpszString
,
845 link
->lHash
, link
->window
);
849 /***********************************************************************
851 * HLPFILE_AddParagraph
853 static BOOL
HLPFILE_AddParagraph(HLPFILE
*hlpfile
, BYTE
*buf
, BYTE
*end
, unsigned* len
)
856 HLPFILE_PARAGRAPH
*paragraph
, **paragraphptr
;
858 BYTE
*format
, *format_end
;
859 char *text
, *text_end
;
862 unsigned nc
, ncol
= 1;
864 if (!hlpfile
->first_page
) {WINE_WARN("no page\n"); return FALSE
;};
866 for (page
= hlpfile
->first_page
; page
->next
; page
= page
->next
) /* Nothing */;
867 for (paragraphptr
= &page
->first_paragraph
; *paragraphptr
;
868 paragraphptr
= &(*paragraphptr
)->next
) /* Nothing */;
870 if (buf
+ 0x19 > end
) {WINE_WARN("header too small\n"); return FALSE
;};
872 size
= GET_UINT(buf
, 0x4);
873 text
= HeapAlloc(GetProcessHeap(), 0, size
);
874 if (!text
) return FALSE
;
875 if (hlpfile
->hasPhrases
)
877 HLPFILE_Uncompress2(buf
+ GET_UINT(buf
, 0x10), end
, (BYTE
*)text
, (BYTE
*)text
+ size
);
881 if (GET_UINT(buf
, 0x4) > GET_UINT(buf
, 0) - GET_UINT(buf
, 0x10))
883 /* block is compressed */
884 HLPFILE_Uncompress3(text
, text
+ size
, buf
+ GET_UINT(buf
, 0x10), end
);
888 text
= (char*)buf
+ GET_UINT(buf
, 0x10);
891 text_end
= text
+ size
;
894 format_end
= buf
+ GET_UINT(buf
, 0x10);
897 *len
= fetch_ushort(&format
);
899 if (buf
[0x14] == 0x23)
905 WINE_TRACE("#cols %u\n", ncol
);
907 if (type
== 0 || type
== 2)
912 for (nc
= 0; nc
< ncol
; nc
++)
914 WINE_TRACE("looking for format at offset %u for column %d\n", format
- (buf
+ 0x15), nc
);
915 if (buf
[0x14] == 0x23)
918 bits
= GET_USHORT(format
, 0); format
+= 2;
919 if (bits
& 0x0001) fetch_long(&format
);
920 if (bits
& 0x0002) fetch_short(&format
);
921 if (bits
& 0x0004) fetch_short(&format
);
922 if (bits
& 0x0008) fetch_short(&format
);
923 if (bits
& 0x0010) fetch_short(&format
);
924 if (bits
& 0x0020) fetch_short(&format
);
925 if (bits
& 0x0040) fetch_short(&format
);
926 if (bits
& 0x0100) format
+= 3;
929 int ntab
= fetch_short(&format
);
934 ts
= fetch_ushort(&format
);
935 if (ts
& 0x4000) fetch_ushort(&format
);
938 /* 0x0400, 0x0800 and 0x1000 don't need space */
939 if ((bits
& 0xE080) != 0)
940 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits
);
942 while (text
< text_end
&& format
< format_end
)
944 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text
), text
, text_end
, format
, format_end
);
945 textsize
= strlen(text
) + 1;
948 paragraph
= HeapAlloc(GetProcessHeap(), 0,
949 sizeof(HLPFILE_PARAGRAPH
) + textsize
);
950 if (!paragraph
) return FALSE
;
951 *paragraphptr
= paragraph
;
952 paragraphptr
= ¶graph
->next
;
954 paragraph
->next
= NULL
;
955 paragraph
->link
= attributes
.link
;
956 if (paragraph
->link
) paragraph
->link
->wRefCount
++;
957 paragraph
->cookie
= para_normal_text
;
958 paragraph
->u
.text
.wFont
= attributes
.wFont
;
959 paragraph
->u
.text
.wVSpace
= attributes
.wVSpace
;
960 paragraph
->u
.text
.wHSpace
= attributes
.wHSpace
;
961 paragraph
->u
.text
.wIndent
= attributes
.wIndent
;
962 paragraph
->u
.text
.lpszText
= (char*)paragraph
+ sizeof(HLPFILE_PARAGRAPH
);
963 strcpy(paragraph
->u
.text
.lpszText
, text
);
965 attributes
.wVSpace
= 0;
966 attributes
.wHSpace
= 0;
968 /* else: null text, keep on storing attributes */
977 WINE_TRACE("format=%02x\n", *format
);
981 WINE_FIXME("NIY20\n");
986 WINE_FIXME("NIY21\n");
991 attributes
.wFont
= GET_USHORT(format
, 1);
992 WINE_TRACE("Changing font to %d\n", attributes
.wFont
);
997 attributes
.wVSpace
++;
1002 attributes
.wVSpace
++;
1003 attributes
.wIndent
= 0;
1008 attributes
.wIndent
++;
1022 BYTE pos
= (*format
- 0x86);
1023 BYTE type
= format
[1];
1027 size
= fetch_long(&format
);
1029 paragraph
= HeapAlloc(GetProcessHeap(), 0,
1030 sizeof(HLPFILE_PARAGRAPH
) + textsize
);
1031 if (!paragraph
) return FALSE
;
1032 *paragraphptr
= paragraph
;
1033 paragraphptr
= ¶graph
->next
;
1035 paragraph
->next
= NULL
;
1036 paragraph
->link
= attributes
.link
;
1037 if (paragraph
->link
) paragraph
->link
->wRefCount
++;
1038 paragraph
->cookie
= para_bitmap
;
1039 paragraph
->u
.gfx
.pos
= pos
;
1043 fetch_ushort(&format
); /* hot spot */
1046 switch (GET_SHORT(format
, 0))
1049 HLPFILE_LoadGfxByIndex(hlpfile
, GET_SHORT(format
, 2),
1053 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1054 GET_SHORT(format
, 0),
1055 size
, GET_SHORT(format
, 2));
1056 HLPFILE_LoadGfxByAddr(hlpfile
, format
+ 2, size
- 4,
1060 WINE_FIXME("??? %u\n", GET_SHORT(format
, 0));
1065 WINE_FIXME("Got an embedded element %s\n", format
+ 6);
1068 WINE_FIXME("Got a type %d picture\n", type
);
1071 if (attributes
.wVSpace
) paragraph
->u
.gfx
.pos
|= 0x8000;
1078 HLPFILE_FreeLink(attributes
.link
);
1079 attributes
.link
= NULL
;
1085 WINE_FIXME("NIY non-break space/hyphen\n");
1097 WINE_TRACE("macro => %s\n", format
+ 3);
1098 HLPFILE_FreeLink(attributes
.link
);
1099 attributes
.link
= HLPFILE_AllocLink(hlp_link_macro
, (const char*)format
+ 3,
1100 0, !(*format
& 4), -1);
1101 format
+= 3 + GET_USHORT(format
, 1);
1106 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format
, 1));
1114 HLPFILE_FreeLink(attributes
.link
);
1115 attributes
.link
= HLPFILE_AllocLink((*format
& 1) ? hlp_link_link
: hlp_link_popup
,
1117 GET_UINT(format
, 1),
1118 !(*format
& 4), -1);
1127 char* ptr
= (char*) format
+ 8;
1128 BYTE type
= format
[3];
1132 if (type
== 1) wnd
= *ptr
++;
1133 if (type
== 4 || type
== 6)
1136 ptr
+= strlen(ptr
) + 1;
1139 str
= hlpfile
->lpszPath
;
1142 for (wnd
= hlpfile
->numWindows
- 1; wnd
>= 0; wnd
--)
1144 if (!strcmp(ptr
, hlpfile
->windows
[wnd
].name
)) break;
1147 WINE_WARN("Couldn't find window info for %s\n", ptr
);
1149 HLPFILE_FreeLink(attributes
.link
);
1150 attributes
.link
= HLPFILE_AllocLink((*format
& 4) ? hlp_link_link
: hlp_link_popup
,
1151 str
, GET_UINT(format
, 4),
1152 !(*format
& 1), wnd
);
1154 format
+= 3 + GET_USHORT(format
, 1);
1158 WINE_WARN("format %02x\n", *format
);
1163 if (text_end
!= (char*)buf
+ GET_UINT(buf
, 0x10) + size
)
1164 HeapFree(GetProcessHeap(), 0, text_end
- size
);
1168 /******************************************************************
1173 static BOOL
HLPFILE_ReadFont(HLPFILE
* hlpfile
)
1176 unsigned i
, len
, idx
;
1177 unsigned face_num
, dscr_num
, face_offset
, dscr_offset
;
1180 if (!HLPFILE_FindSubFile("|FONT", &ref
, &end
))
1182 WINE_WARN("no subfile FONT\n");
1183 hlpfile
->numFonts
= 0;
1184 hlpfile
->fonts
= NULL
;
1190 face_num
= GET_USHORT(ref
, 0);
1191 dscr_num
= GET_USHORT(ref
, 2);
1192 face_offset
= GET_USHORT(ref
, 4);
1193 dscr_offset
= GET_USHORT(ref
, 6);
1195 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1196 face_num
, face_offset
, dscr_num
, dscr_offset
);
1198 hlpfile
->numFonts
= dscr_num
;
1199 hlpfile
->fonts
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT
) * dscr_num
);
1201 len
= (dscr_offset
- face_offset
) / face_num
;
1202 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1203 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1204 for (i
= 0; i
< dscr_num
; i
++)
1206 flag
= ref
[dscr_offset
+ i
* 11 + 0];
1207 family
= ref
[dscr_offset
+ i
* 11 + 2];
1209 hlpfile
->fonts
[i
].LogFont
.lfHeight
= -ref
[dscr_offset
+ i
* 11 + 1] / 2;
1210 hlpfile
->fonts
[i
].LogFont
.lfWidth
= 0;
1211 hlpfile
->fonts
[i
].LogFont
.lfEscapement
= 0;
1212 hlpfile
->fonts
[i
].LogFont
.lfOrientation
= 0;
1213 hlpfile
->fonts
[i
].LogFont
.lfWeight
= (flag
& 1) ? 700 : 400;
1214 hlpfile
->fonts
[i
].LogFont
.lfItalic
= (flag
& 2) ? TRUE
: FALSE
;
1215 hlpfile
->fonts
[i
].LogFont
.lfUnderline
= (flag
& 4) ? TRUE
: FALSE
;
1216 hlpfile
->fonts
[i
].LogFont
.lfStrikeOut
= (flag
& 8) ? TRUE
: FALSE
;
1217 hlpfile
->fonts
[i
].LogFont
.lfCharSet
= ANSI_CHARSET
;
1218 hlpfile
->fonts
[i
].LogFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
1219 hlpfile
->fonts
[i
].LogFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
1220 hlpfile
->fonts
[i
].LogFont
.lfQuality
= DEFAULT_QUALITY
;
1221 hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
= DEFAULT_PITCH
;
1225 case 0x01: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_MODERN
; break;
1226 case 0x02: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_ROMAN
; break;
1227 case 0x03: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_SWISS
; break;
1228 case 0x04: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_SCRIPT
; break;
1229 case 0x05: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_DECORATIVE
; break;
1230 default: WINE_FIXME("Unknown family %u\n", family
);
1232 idx
= GET_USHORT(ref
, dscr_offset
+ i
* 11 + 3);
1236 memcpy(hlpfile
->fonts
[i
].LogFont
.lfFaceName
, ref
+ face_offset
+ idx
* len
, min(len
, LF_FACESIZE
- 1));
1237 hlpfile
->fonts
[i
].LogFont
.lfFaceName
[min(len
, LF_FACESIZE
- 1)] = '\0';
1241 WINE_FIXME("Too high face ref (%u/%u)\n", idx
, face_num
);
1242 strcpy(hlpfile
->fonts
[i
].LogFont
.lfFaceName
, "Helv");
1244 hlpfile
->fonts
[i
].hFont
= 0;
1245 hlpfile
->fonts
[i
].color
= RGB(ref
[dscr_offset
+ i
* 11 + 5],
1246 ref
[dscr_offset
+ i
* 11 + 6],
1247 ref
[dscr_offset
+ i
* 11 + 7]);
1248 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1249 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1255 X(4, "dblUnderline"),
1257 ref
[dscr_offset
+ i
* 11 + 1],
1259 hlpfile
->fonts
[i
].LogFont
.lfFaceName
, idx
,
1260 GET_UINT(ref
, dscr_offset
+ i
* 11 + 5) & 0x00FFFFFF);
1265 /***********************************************************************
1267 * HLPFILE_ReadFileToBuffer
1269 static BOOL
HLPFILE_ReadFileToBuffer(HFILE hFile
)
1271 BYTE header
[16], dummy
[1];
1274 if (_hread(hFile
, header
, 16) != 16) {WINE_WARN("header\n"); return FALSE
;};
1277 if (GET_UINT(header
, 0) != 0x00035F3F)
1278 {WINE_WARN("wrong header\n"); return FALSE
;};
1280 size
= GET_UINT(header
, 12);
1281 file_buffer
= HeapAlloc(GetProcessHeap(), 0, size
+ 1);
1282 if (!file_buffer
) return FALSE
;
1284 memcpy(file_buffer
, header
, 16);
1285 if (_hread(hFile
, file_buffer
+ 16, size
- 16) != size
- 16)
1286 {WINE_WARN("filesize1\n"); return FALSE
;};
1288 if (_hread(hFile
, dummy
, 1) != 0) WINE_WARN("filesize2\n");
1290 file_buffer
[size
] = '\0'; /* FIXME: was '0', sounds ackward to me */
1295 /***********************************************************************
1297 * HLPFILE_FindSubFile
1299 static BOOL
HLPFILE_FindSubFile(LPCSTR name
, BYTE
**subbuf
, BYTE
**subend
)
1301 BYTE
*root
= file_buffer
+ GET_UINT(file_buffer
, 4);
1302 BYTE
*end
= file_buffer
+ GET_UINT(file_buffer
, 12);
1313 /* FIXME: this should be using the EnumBTree functions from this file */
1314 pgsize
= GET_USHORT(bth
, 4);
1315 WINE_TRACE("%s => pgsize=%u #pg=%u rootpg=%u #lvl=%u\n",
1316 name
, pgsize
, GET_USHORT(bth
, 30), GET_USHORT(bth
, 26), GET_USHORT(bth
, 32));
1318 ptr
= bth
+ 38 + GET_USHORT(bth
, 26) * pgsize
;
1320 for (n
= 1; n
< GET_USHORT(bth
, 32); n
++)
1322 nentries
= GET_USHORT(ptr
, 2);
1323 pglast
= GET_USHORT(ptr
, 4);
1324 WINE_TRACE("[%u]: #entries=%u next=%u\n", n
, nentries
, pglast
);
1327 for (i
= 0; i
< nentries
; i
++)
1329 char *str
= (char*) ptr
;
1330 WINE_TRACE("<= %s\n", str
);
1331 if (strcmp(name
, str
) < 0) break;
1332 ptr
+= strlen(str
) + 1;
1333 pglast
= GET_USHORT(ptr
, 0);
1336 ptr
= bth
+ 38 + pglast
* pgsize
;
1339 nentries
= GET_USHORT(ptr
, 2);
1341 for (i
= 0; i
< nentries
; i
++)
1343 char* fname
= (char*)ptr
;
1344 ptr
+= strlen(fname
) + 1;
1345 WINE_TRACE("\\- %s\n", fname
);
1346 if (strcmp(fname
, name
) == 0)
1348 *subbuf
= file_buffer
+ GET_UINT(ptr
, 0);
1349 *subend
= *subbuf
+ GET_UINT(*subbuf
, 0);
1350 if (file_buffer
> *subbuf
|| *subbuf
> *subend
|| *subend
> end
)
1352 WINE_WARN("size mismatch\n");
1363 /***********************************************************************
1365 * HLPFILE_SystemCommands
1367 static BOOL
HLPFILE_SystemCommands(HLPFILE
* hlpfile
)
1369 BYTE
*buf
, *ptr
, *end
;
1370 HLPFILE_MACRO
*macro
, **m
;
1372 unsigned short magic
, minor
, major
, flags
;
1374 hlpfile
->lpszTitle
= NULL
;
1376 if (!HLPFILE_FindSubFile("|SYSTEM", &buf
, &end
)) return FALSE
;
1378 magic
= GET_USHORT(buf
+ 9, 0);
1379 minor
= GET_USHORT(buf
+ 9, 2);
1380 major
= GET_USHORT(buf
+ 9, 4);
1381 /* gen date on 4 bytes */
1382 flags
= GET_USHORT(buf
+ 9, 10);
1383 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1384 magic
, major
, minor
, flags
);
1385 if (magic
!= 0x036C || major
!= 1)
1386 {WINE_WARN("Wrong system header\n"); return FALSE
;}
1387 if (minor
<= 16) {WINE_WARN("too old file format (NIY)\n"); return FALSE
;}
1388 if (flags
& 8) {WINE_WARN("Unsupported yet page size\n"); return FALSE
;}
1390 hlpfile
->version
= minor
;
1391 hlpfile
->flags
= flags
;
1393 for (ptr
= buf
+ 0x15; ptr
+ 4 <= end
; ptr
+= GET_USHORT(ptr
, 2) + 4)
1395 char *str
= (char*) ptr
+ 4;
1396 switch (GET_USHORT(ptr
, 0))
1399 if (hlpfile
->lpszTitle
) {WINE_WARN("title\n"); break;}
1400 hlpfile
->lpszTitle
= HeapAlloc(GetProcessHeap(), 0, strlen(str
) + 1);
1401 if (!hlpfile
->lpszTitle
) return FALSE
;
1402 lstrcpy(hlpfile
->lpszTitle
, str
);
1403 WINE_TRACE("Title: %s\n", hlpfile
->lpszTitle
);
1407 if (hlpfile
->lpszCopyright
) {WINE_WARN("copyright\n"); break;}
1408 hlpfile
->lpszCopyright
= HeapAlloc(GetProcessHeap(), 0, strlen(str
) + 1);
1409 if (!hlpfile
->lpszCopyright
) return FALSE
;
1410 lstrcpy(hlpfile
->lpszCopyright
, str
);
1411 WINE_TRACE("Copyright: %s\n", hlpfile
->lpszCopyright
);
1415 if (GET_USHORT(ptr
, 2) != 4) {WINE_WARN("system3\n");break;}
1416 hlpfile
->contents_start
= GET_UINT(ptr
, 4);
1417 WINE_TRACE("Setting contents start at %08lx\n", hlpfile
->contents_start
);
1421 macro
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO
) + lstrlen(str
) + 1);
1423 p
= (char*)macro
+ sizeof(HLPFILE_MACRO
);
1425 macro
->lpszMacro
= p
;
1427 for (m
= &hlpfile
->first_macro
; *m
; m
= &(*m
)->next
);
1432 if (GET_USHORT(ptr
, 2) != 90) {WINE_WARN("system6\n");break;}
1434 if (hlpfile
->windows
)
1435 hlpfile
->windows
= HeapReAlloc(GetProcessHeap(), 0, hlpfile
->windows
,
1436 sizeof(HLPFILE_WINDOWINFO
) * ++hlpfile
->numWindows
);
1438 hlpfile
->windows
= HeapAlloc(GetProcessHeap(), 0,
1439 sizeof(HLPFILE_WINDOWINFO
) * ++hlpfile
->numWindows
);
1441 if (hlpfile
->windows
)
1443 unsigned flags
= GET_USHORT(ptr
, 4);
1444 HLPFILE_WINDOWINFO
* wi
= &hlpfile
->windows
[hlpfile
->numWindows
- 1];
1446 if (flags
& 0x0001) strcpy(wi
->type
, &str
[2]);
1447 else wi
->type
[0] = '\0';
1448 if (flags
& 0x0002) strcpy(wi
->name
, &str
[12]);
1449 else wi
->name
[0] = '\0';
1450 if (flags
& 0x0004) strcpy(wi
->caption
, &str
[23]);
1451 else lstrcpynA(wi
->caption
, hlpfile
->lpszTitle
, sizeof(wi
->caption
));
1452 wi
->origin
.x
= (flags
& 0x0008) ? GET_USHORT(ptr
, 76) : CW_USEDEFAULT
;
1453 wi
->origin
.y
= (flags
& 0x0010) ? GET_USHORT(ptr
, 78) : CW_USEDEFAULT
;
1454 wi
->size
.cx
= (flags
& 0x0020) ? GET_USHORT(ptr
, 80) : CW_USEDEFAULT
;
1455 wi
->size
.cy
= (flags
& 0x0040) ? GET_USHORT(ptr
, 82) : CW_USEDEFAULT
;
1456 wi
->style
= (flags
& 0x0080) ? GET_USHORT(ptr
, 84) : SW_SHOW
;
1457 wi
->win_style
= WS_OVERLAPPEDWINDOW
;
1458 wi
->sr_color
= (flags
& 0x0100) ? GET_UINT(ptr
, 86) : 0xFFFFFF;
1459 wi
->nsr_color
= (flags
& 0x0200) ? GET_UINT(ptr
, 90) : 0xFFFFFF;
1460 WINE_TRACE("System-Window: flags=%c%c%c%c%c%c%c%c type=%s name=%s caption=%s (%d,%d)x(%d,%d)\n",
1461 flags
& 0x0001 ? 'T' : 't',
1462 flags
& 0x0002 ? 'N' : 'n',
1463 flags
& 0x0004 ? 'C' : 'c',
1464 flags
& 0x0008 ? 'X' : 'x',
1465 flags
& 0x0010 ? 'Y' : 'y',
1466 flags
& 0x0020 ? 'W' : 'w',
1467 flags
& 0x0040 ? 'H' : 'h',
1468 flags
& 0x0080 ? 'S' : 's',
1469 wi
->type
, wi
->name
, wi
->caption
, wi
->origin
.x
, wi
->origin
.y
,
1470 wi
->size
.cx
, wi
->size
.cy
);
1474 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr
, 0));
1477 if (!hlpfile
->lpszTitle
)
1478 hlpfile
->lpszTitle
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 1);
1482 /***********************************************************************
1484 * HLPFILE_UncompressedLZ77_Size
1486 static INT
HLPFILE_UncompressedLZ77_Size(BYTE
*ptr
, BYTE
*end
)
1493 for (i
= 0; i
< 8 && ptr
< end
; i
++, mask
>>= 1)
1497 int code
= GET_USHORT(ptr
, 0);
1498 int len
= 3 + (code
>> 12);
1502 else newsize
++, ptr
++;
1509 /***********************************************************************
1511 * HLPFILE_UncompressLZ77
1513 static BYTE
*HLPFILE_UncompressLZ77(BYTE
*ptr
, BYTE
*end
, BYTE
*newptr
)
1520 for (i
= 0; i
< 8 && ptr
< end
; i
++, mask
>>= 1)
1524 int code
= GET_USHORT(ptr
, 0);
1525 int len
= 3 + (code
>> 12);
1526 int offset
= code
& 0xfff;
1528 * We must copy byte-by-byte here. We cannot use memcpy nor
1529 * memmove here. Just example:
1530 * a[]={1,2,3,4,5,6,7,8,9,10}
1534 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
1536 for (; len
>0; len
--, newptr
++) *newptr
= *(newptr
-offset
-1);
1539 else *newptr
++ = *ptr
++;
1546 /***********************************************************************
1548 * HLPFILE_UncompressLZ77_Phrases
1550 static BOOL
HLPFILE_UncompressLZ77_Phrases(HLPFILE
* hlpfile
)
1552 UINT i
, num
, dec_size
;
1555 if (!HLPFILE_FindSubFile("|Phrases", &buf
, &end
)) return FALSE
;
1557 num
= phrases
.num
= GET_USHORT(buf
, 9);
1558 if (buf
+ 2 * num
+ 0x13 >= end
) {WINE_WARN("1a\n"); return FALSE
;};
1560 dec_size
= HLPFILE_UncompressedLZ77_Size(buf
+ 0x13 + 2 * num
, end
);
1562 phrases
.offsets
= HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num
+ 1));
1563 phrases
.buffer
= HeapAlloc(GetProcessHeap(), 0, dec_size
);
1564 if (!phrases
.offsets
|| !phrases
.buffer
) return FALSE
;
1566 for (i
= 0; i
<= num
; i
++)
1567 phrases
.offsets
[i
] = GET_USHORT(buf
, 0x11 + 2 * i
) - 2 * num
- 2;
1569 HLPFILE_UncompressLZ77(buf
+ 0x13 + 2 * num
, end
, (BYTE
*)phrases
.buffer
);
1571 hlpfile
->hasPhrases
= TRUE
;
1575 /***********************************************************************
1577 * HLPFILE_Uncompress_Phrases40
1579 static BOOL
HLPFILE_Uncompress_Phrases40(HLPFILE
* hlpfile
)
1581 UINT num
, dec_size
, cpr_size
;
1582 BYTE
*buf_idx
, *end_idx
;
1583 BYTE
*buf_phs
, *end_phs
;
1585 long* ptr
, mask
= 0;
1588 if (!HLPFILE_FindSubFile("|PhrIndex", &buf_idx
, &end_idx
) ||
1589 !HLPFILE_FindSubFile("|PhrImage", &buf_phs
, &end_phs
)) return FALSE
;
1591 ptr
= (long*)(buf_idx
+ 9 + 28);
1592 bc
= GET_USHORT(buf_idx
, 9 + 24) & 0x0F;
1593 num
= phrases
.num
= GET_USHORT(buf_idx
, 9 + 4);
1595 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
1596 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
1597 GET_UINT(buf_idx
, 9 + 0),
1598 GET_UINT(buf_idx
, 9 + 4),
1599 GET_UINT(buf_idx
, 9 + 8),
1600 GET_UINT(buf_idx
, 9 + 12),
1601 GET_UINT(buf_idx
, 9 + 16),
1602 GET_UINT(buf_idx
, 9 + 20),
1603 GET_USHORT(buf_idx
, 9 + 24),
1604 GET_USHORT(buf_idx
, 9 + 26));
1606 dec_size
= GET_UINT(buf_idx
, 9 + 12);
1607 cpr_size
= GET_UINT(buf_idx
, 9 + 16);
1609 if (dec_size
!= cpr_size
&&
1610 dec_size
!= HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
))
1612 WINE_WARN("size mismatch %u %u\n",
1613 dec_size
, HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
));
1614 dec_size
= max(dec_size
, HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
));
1617 phrases
.offsets
= HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num
+ 1));
1618 phrases
.buffer
= HeapAlloc(GetProcessHeap(), 0, dec_size
);
1619 if (!phrases
.offsets
|| !phrases
.buffer
) return FALSE
;
1621 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
1623 phrases
.offsets
[0] = 0;
1624 for (i
= 0; i
< num
; i
++)
1626 for (n
= 1; getbit(); n
+= 1 << bc
);
1628 if (bc
> 1 && getbit()) n
+= 2;
1629 if (bc
> 2 && getbit()) n
+= 4;
1630 if (bc
> 3 && getbit()) n
+= 8;
1631 if (bc
> 4 && getbit()) n
+= 16;
1632 phrases
.offsets
[i
+ 1] = phrases
.offsets
[i
] + n
;
1636 if (dec_size
== cpr_size
)
1637 memcpy(phrases
.buffer
, buf_phs
+ 9, dec_size
);
1639 HLPFILE_UncompressLZ77(buf_phs
+ 9, end_phs
, (BYTE
*)phrases
.buffer
);
1641 hlpfile
->hasPhrases
= FALSE
;
1645 /***********************************************************************
1647 * HLPFILE_Uncompress_Topic
1649 static BOOL
HLPFILE_Uncompress_Topic(HLPFILE
* hlpfile
)
1651 BYTE
*buf
, *ptr
, *end
, *newptr
;
1654 if (!HLPFILE_FindSubFile("|TOPIC", &buf
, &end
))
1655 {WINE_WARN("topic0\n"); return FALSE
;}
1657 switch (hlpfile
->flags
& (8|4))
1660 WINE_FIXME("Unsupported format\n");
1664 topic
.wMapLen
= (end
- buf
- 1) / 0x1000 + 1;
1666 for (i
= 0; i
< topic
.wMapLen
; i
++)
1668 ptr
= buf
+ i
* 0x1000;
1670 /* I don't know why, it's necessary for printman.hlp */
1671 if (ptr
+ 0x44 > end
) ptr
= end
- 0x44;
1673 newsize
+= HLPFILE_UncompressedLZ77_Size(ptr
+ 0xc, min(end
, ptr
+ 0x1000));
1676 topic
.map
= HeapAlloc(GetProcessHeap(), 0,
1677 topic
.wMapLen
* sizeof(topic
.map
[0]) + newsize
);
1678 if (!topic
.map
) return FALSE
;
1679 newptr
= (BYTE
*)(topic
.map
+ topic
.wMapLen
);
1680 topic
.end
= newptr
+ newsize
;
1682 for (i
= 0; i
< topic
.wMapLen
; i
++)
1684 ptr
= buf
+ i
* 0x1000;
1685 if (ptr
+ 0x44 > end
) ptr
= end
- 0x44;
1687 topic
.map
[i
] = newptr
;
1688 newptr
= HLPFILE_UncompressLZ77(ptr
+ 0xc, min(end
, ptr
+ 0x1000), newptr
);
1692 /* basically, we need to copy the 0x1000 byte pages (removing the first 0x0C) in
1693 * one single are in memory
1695 #define DST_LEN (0x1000 - 0x0C)
1697 newsize
= end
- buf
;
1698 /* number of destination pages */
1699 topic
.wMapLen
= (newsize
- 1) / DST_LEN
+ 1;
1700 topic
.map
= HeapAlloc(GetProcessHeap(), 0,
1701 topic
.wMapLen
* (sizeof(topic
.map
[0]) + DST_LEN
));
1702 if (!topic
.map
) return FALSE
;
1703 newptr
= (BYTE
*)(topic
.map
+ topic
.wMapLen
);
1704 topic
.end
= newptr
+ newsize
;
1706 for (i
= 0; i
< topic
.wMapLen
; i
++)
1708 topic
.map
[i
] = newptr
+ i
* DST_LEN
;
1709 memcpy(topic
.map
[i
], buf
+ i
* 0x1000 + 0x0C, DST_LEN
);
1717 /***********************************************************************
1719 * HLPFILE_Uncompress2
1722 static void HLPFILE_Uncompress2(const BYTE
*ptr
, const BYTE
*end
, BYTE
*newptr
, const BYTE
*newend
)
1724 BYTE
*phptr
, *phend
;
1728 while (ptr
< end
&& newptr
< newend
)
1730 if (!*ptr
|| *ptr
>= 0x10)
1734 code
= 0x100 * ptr
[0] + ptr
[1];
1735 index
= (code
- 0x100) / 2;
1737 phptr
= (BYTE
*)phrases
.buffer
+ phrases
.offsets
[index
];
1738 phend
= (BYTE
*)phrases
.buffer
+ phrases
.offsets
[index
+ 1];
1740 if (newptr
+ (phend
- phptr
) > newend
)
1742 WINE_FIXME("buffer overflow %p > %p for %d bytes\n",
1743 newptr
, newend
, phend
- phptr
);
1746 memcpy(newptr
, phptr
, phend
- phptr
);
1747 newptr
+= phend
- phptr
;
1748 if (code
& 1) *newptr
++ = ' ';
1753 if (newptr
> newend
) WINE_FIXME("buffer overflow %p > %p\n", newptr
, newend
);
1756 /******************************************************************
1757 * HLPFILE_Uncompress3
1761 static BOOL
HLPFILE_Uncompress3(char* dst
, const char* dst_end
,
1762 const BYTE
* src
, const BYTE
* src_end
)
1766 for (; src
< src_end
; src
++)
1768 if ((*src
& 1) == 0)
1771 if (idx
> phrases
.num
)
1773 WINE_ERR("index in phrases %d/%d\n", idx
, phrases
.num
);
1778 len
= phrases
.offsets
[idx
+ 1] - phrases
.offsets
[idx
];
1779 if (dst
+ len
<= dst_end
)
1780 memcpy(dst
, &phrases
.buffer
[phrases
.offsets
[idx
]], len
);
1783 else if ((*src
& 0x03) == 0x01)
1785 idx
= (*src
+ 1) * 64;
1787 if (idx
> phrases
.num
)
1789 WINE_ERR("index in phrases %d/%d\n", idx
, phrases
.num
);
1794 len
= phrases
.offsets
[idx
+ 1] - phrases
.offsets
[idx
];
1795 if (dst
+ len
<= dst_end
)
1796 memcpy(dst
, &phrases
.buffer
[phrases
.offsets
[idx
]], len
);
1799 else if ((*src
& 0x07) == 0x03)
1801 len
= (*src
/ 8) + 1;
1802 if (dst
+ len
<= dst_end
)
1803 memcpy(dst
, src
+ 1, len
);
1808 len
= (*src
/ 16) + 1;
1809 if (dst
+ len
<= dst_end
)
1810 memset(dst
, ((*src
& 0x0F) == 0x07) ? ' ' : 0, len
);
1815 if (dst
> dst_end
) WINE_ERR("buffer overflow (%p > %p)\n", dst
, dst_end
);
1819 /******************************************************************
1820 * HLPFILE_UncompressRLE
1824 static void HLPFILE_UncompressRLE(const BYTE
* src
, const BYTE
* end
, BYTE
** dst
, unsigned dstsz
)
1827 BYTE
* sdst
= *dst
+ dstsz
;
1835 if ((*dst
) + ch
<= sdst
)
1836 memcpy(*dst
, src
, ch
);
1841 if ((*dst
) + ch
<= sdst
)
1842 memset(*dst
, (char)*src
, ch
);
1848 WINE_WARN("Buffer X-flow: d(%u) instead of d(%u)\n",
1849 *dst
- (sdst
- dstsz
), dstsz
);
1852 /******************************************************************
1853 * HLPFILE_EnumBTreeLeaves
1857 static void HLPFILE_EnumBTreeLeaves(const BYTE
* buf
, const BYTE
* end
, unsigned (*fn
)(const BYTE
*, void*), void* user
)
1859 unsigned psize
, pnext
;
1863 num
= GET_UINT(buf
, 9 + 34);
1864 psize
= GET_USHORT(buf
, 9 + 4);
1865 nlvl
= GET_USHORT(buf
, 9 + 32);
1866 pnext
= GET_USHORT(buf
, 9 + 26);
1868 WINE_TRACE("BTree: #entries=%u pagSize=%u #levels=%u #pages=%u root=%u struct%16s\n",
1869 num
, psize
, nlvl
, GET_USHORT(buf
, 9 + 30), pnext
, buf
+ 9 + 6);
1874 ptr
= (buf
+ 9 + 38) + pnext
* psize
;
1875 WINE_TRACE("BTree: (index[%u]) unused=%u #entries=%u <%u\n",
1876 pnext
, GET_USHORT(ptr
, 0), GET_USHORT(ptr
, 2), GET_USHORT(ptr
, 4));
1877 pnext
= GET_USHORT(ptr
, 4);
1879 while (pnext
!= 0xFFFF)
1881 const BYTE
* node_page
;
1882 unsigned short limit
;
1884 node_page
= ptr
= (buf
+ 9 + 38) + pnext
* psize
;
1885 limit
= GET_USHORT(ptr
, 2);
1886 WINE_TRACE("BTree: (leaf [%u]) unused=%u #entries=%u <%u >%u\n",
1887 pnext
, GET_USHORT(ptr
, 0), limit
, GET_USHORT(ptr
, 4), GET_USHORT(ptr
, 6));
1890 ptr
+= (fn
)(ptr
, user
);
1891 pnext
= GET_USHORT(node_page
, 6);
1900 static unsigned myfn(const BYTE
* ptr
, void* user
)
1902 struct myfncb
* m
= user
;
1904 m
->hlpfile
->Context
[m
->i
].lHash
= GET_UINT(ptr
, 0);
1905 m
->hlpfile
->Context
[m
->i
].offset
= GET_UINT(ptr
, 4);
1910 /***********************************************************************
1912 * HLPFILE_GetContext
1914 static BOOL
HLPFILE_GetContext(HLPFILE
*hlpfile
)
1920 if (!HLPFILE_FindSubFile("|CONTEXT", &cbuf
, &cend
)) {WINE_WARN("context0\n"); return FALSE
;}
1922 clen
= GET_UINT(cbuf
, 0x2b);
1923 hlpfile
->Context
= HeapAlloc(GetProcessHeap(), 0, clen
* sizeof(HLPFILE_CONTEXT
));
1924 if (!hlpfile
->Context
) return FALSE
;
1925 hlpfile
->wContextLen
= clen
;
1927 m
.hlpfile
= hlpfile
;
1929 HLPFILE_EnumBTreeLeaves(cbuf
, cend
, myfn
, &m
);
1934 /***********************************************************************
1938 static BOOL
HLPFILE_GetMap(HLPFILE
*hlpfile
)
1941 unsigned entries
, i
;
1943 if (!HLPFILE_FindSubFile("|CTXOMAP", &cbuf
, &cend
)) {WINE_WARN("no map section\n"); return FALSE
;}
1945 entries
= GET_USHORT(cbuf
, 9);
1946 hlpfile
->Map
= HeapAlloc(GetProcessHeap(), 0, entries
* sizeof(HLPFILE_MAP
));
1947 if (!hlpfile
->Map
) return FALSE
;
1948 hlpfile
->wMapLen
= entries
;
1949 for (i
= 0; i
< entries
; i
++)
1951 hlpfile
->Map
[i
].lMap
= GET_UINT(cbuf
+11,i
*8);
1952 hlpfile
->Map
[i
].offset
= GET_UINT(cbuf
+11,i
*8+4);
1957 /******************************************************************
1958 * HLPFILE_DeleteLink
1962 void HLPFILE_FreeLink(HLPFILE_LINK
* link
)
1964 if (link
&& !--link
->wRefCount
)
1965 HeapFree(GetProcessHeap(), 0, link
);
1968 /***********************************************************************
1970 * HLPFILE_DeleteParagraph
1972 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH
* paragraph
)
1974 HLPFILE_PARAGRAPH
* next
;
1978 next
= paragraph
->next
;
1980 if (paragraph
->cookie
== para_metafile
)
1981 DeleteMetaFile(paragraph
->u
.gfx
.u
.mfp
.hMF
);
1983 HLPFILE_FreeLink(paragraph
->link
);
1985 HeapFree(GetProcessHeap(), 0, paragraph
);
1990 /***********************************************************************
1994 static void HLPFILE_DeleteMacro(HLPFILE_MACRO
* macro
)
1996 HLPFILE_MACRO
* next
;
2001 HeapFree(GetProcessHeap(), 0, macro
);
2006 /***********************************************************************
2010 static void HLPFILE_DeletePage(HLPFILE_PAGE
* page
)
2017 HLPFILE_DeleteParagraph(page
->first_paragraph
);
2018 HLPFILE_DeleteMacro(page
->first_macro
);
2019 HeapFree(GetProcessHeap(), 0, page
);
2024 /***********************************************************************
2026 * HLPFILE_FreeHlpFile
2028 void HLPFILE_FreeHlpFile(HLPFILE
* hlpfile
)
2032 if (!hlpfile
|| --hlpfile
->wRefCount
> 0) return;
2034 if (hlpfile
->next
) hlpfile
->next
->prev
= hlpfile
->prev
;
2035 if (hlpfile
->prev
) hlpfile
->prev
->next
= hlpfile
->next
;
2036 else first_hlpfile
= hlpfile
->next
;
2038 if (hlpfile
->numFonts
)
2040 for (i
= 0; i
< hlpfile
->numFonts
; i
++)
2042 DeleteObject(hlpfile
->fonts
[i
].hFont
);
2044 HeapFree(GetProcessHeap(), 0, hlpfile
->fonts
);
2047 if (hlpfile
->numBmps
)
2049 for (i
= 0; i
< hlpfile
->numBmps
; i
++)
2051 DeleteObject(hlpfile
->bmps
[i
]);
2053 HeapFree(GetProcessHeap(), 0, hlpfile
->bmps
);
2056 HLPFILE_DeletePage(hlpfile
->first_page
);
2057 HLPFILE_DeleteMacro(hlpfile
->first_macro
);
2059 if (hlpfile
->numWindows
) HeapFree(GetProcessHeap(), 0, hlpfile
->windows
);
2060 HeapFree(GetProcessHeap(), 0, hlpfile
->Context
);
2061 HeapFree(GetProcessHeap(), 0, hlpfile
->Map
);
2062 HeapFree(GetProcessHeap(), 0, hlpfile
->lpszTitle
);
2063 HeapFree(GetProcessHeap(), 0, hlpfile
->lpszCopyright
);
2064 HeapFree(GetProcessHeap(), 0, hlpfile
);