winhelp: Moved all static data used when parsing HLP files into the internal structures.
[wine/multimedia.git] / programs / winhelp / hlpfile.c
blob80bf7b8c45337470346eefa88558434e683f80b6
1 /*
2 * Help Viewer
4 * Copyright 1996 Ulrich Schmid
5 * 2002, 2008 Eric Pouech
6 * 2007 Kirill K. Smirnov
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winhelp.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
37 static inline unsigned short GET_USHORT(const BYTE* buffer, unsigned i)
39 return (BYTE)buffer[i] + 0x100 * (BYTE)buffer[i + 1];
42 static inline short GET_SHORT(const BYTE* buffer, unsigned i)
44 return (BYTE)buffer[i] + 0x100 * (signed char)buffer[i+1];
47 static inline unsigned GET_UINT(const BYTE* buffer, unsigned i)
49 return GET_USHORT(buffer, i) + 0x10000 * GET_USHORT(buffer, i + 2);
52 static HLPFILE *first_hlpfile = 0;
54 static struct
56 UINT wFont;
57 UINT wIndent;
58 UINT wHSpace;
59 UINT wVSpace;
60 HLPFILE_LINK* link;
61 } attributes;
63 static BOOL HLPFILE_DoReadHlpFile(HLPFILE*, LPCSTR);
64 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE*, HFILE);
65 static BOOL HLPFILE_FindSubFile(HLPFILE*, LPCSTR, BYTE**, BYTE**);
66 static BOOL HLPFILE_SystemCommands(HLPFILE*);
67 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end);
68 static BYTE* HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr);
69 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE*);
70 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE*);
71 static BOOL HLPFILE_Uncompress_Topic(HLPFILE*);
72 static BOOL HLPFILE_GetContext(HLPFILE*);
73 static BOOL HLPFILE_GetKeywords(HLPFILE*);
74 static BOOL HLPFILE_GetMap(HLPFILE*);
75 static BOOL HLPFILE_AddPage(HLPFILE*, BYTE*, BYTE*, unsigned);
76 static BOOL HLPFILE_AddParagraph(HLPFILE*, BYTE *, BYTE*, unsigned*);
77 static void HLPFILE_Uncompress2(HLPFILE*, const BYTE*, const BYTE*, BYTE*, const BYTE*);
78 static BOOL HLPFILE_Uncompress3(HLPFILE*, char*, const char*, const BYTE*, const BYTE*);
79 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz);
80 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile);
82 /***********************************************************************
84 * HLPFILE_PageByNumber
86 static HLPFILE_PAGE *HLPFILE_PageByNumber(HLPFILE* hlpfile, UINT wNum)
88 HLPFILE_PAGE *page;
89 UINT temp = wNum;
91 WINE_TRACE("<%s>[%u]\n", hlpfile->lpszPath, wNum);
93 for (page = hlpfile->first_page; page && temp; page = page->next) temp--;
94 if (!page)
95 WINE_ERR("Page of number %u not found in file %s\n", wNum, hlpfile->lpszPath);
96 return page;
99 /* FIXME:
100 * this finds the page containing the offset. The offset can either
101 * refer to the top of the page (offset == page->offset), or
102 * to some paragraph inside the page...
103 * As of today, we only return the page... we should also return
104 * a paragraph, and then, while opening a new page, compute the
105 * y-offset of the paragraph to be shown and scroll the window
106 * accordingly
108 /******************************************************************
109 * HLPFILE_PageByOffset
113 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset)
115 HLPFILE_PAGE* page;
116 HLPFILE_PAGE* found;
118 if (!hlpfile) return 0;
120 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, offset);
122 if (offset == 0xFFFFFFFF) return NULL;
123 page = NULL;
125 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
127 if (page->offset <= offset && (!found || found->offset < page->offset))
128 found = page;
130 if (!found)
131 WINE_ERR("Page of offset %u not found in file %s\n",
132 offset, hlpfile->lpszPath);
133 return found;
136 /**************************************************************************
137 * comp_PageByHash
139 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
142 static int comp_PageByHash(void *p, const void *key,
143 int leaf, void** next)
145 LONG lKey = (LONG_PTR)key;
146 LONG lTest = (INT)GET_UINT(p, 0);
148 *next = (char *)p+(leaf?8:6);
149 WINE_TRACE("Comparing '%d' with '%d'\n", lKey, lTest);
150 if (lTest < lKey) return -1;
151 if (lTest > lKey) return 1;
152 return 0;
155 /***********************************************************************
157 * HLPFILE_HlpFilePageByHash
159 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash)
161 BYTE *ptr;
163 if (!hlpfile) return 0;
165 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lHash);
167 /* For win 3.0 files hash values are really page numbers */
168 if (hlpfile->version <= 16)
169 return HLPFILE_PageByNumber(hlpfile, lHash);
171 ptr = HLPFILE_BPTreeSearch(hlpfile->Context, LongToPtr(lHash), comp_PageByHash);
172 if (!ptr)
174 WINE_ERR("Page of hash %x not found in file %s\n", lHash, hlpfile->lpszPath);
175 return NULL;
178 return HLPFILE_PageByOffset(hlpfile, GET_UINT(ptr, 4));
181 /***********************************************************************
183 * HLPFILE_PageByMap
185 HLPFILE_PAGE *HLPFILE_PageByMap(HLPFILE* hlpfile, LONG lMap)
187 unsigned int i;
189 if (!hlpfile) return 0;
191 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lMap);
193 for (i = 0; i < hlpfile->wMapLen; i++)
195 if (hlpfile->Map[i].lMap == lMap)
196 return HLPFILE_PageByOffset(hlpfile, hlpfile->Map[i].offset);
199 WINE_ERR("Page of Map %x not found in file %s\n", lMap, hlpfile->lpszPath);
200 return NULL;
203 /***********************************************************************
205 * HLPFILE_Contents
207 HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile)
209 HLPFILE_PAGE* page = NULL;
211 if (!hlpfile) return NULL;
213 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start);
214 if (!page) page = hlpfile->first_page;
215 return page;
218 /***********************************************************************
220 * HLPFILE_Hash
222 LONG HLPFILE_Hash(LPCSTR lpszContext)
224 LONG lHash = 0;
225 CHAR c;
227 while ((c = *lpszContext++))
229 CHAR x = 0;
230 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
231 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
232 if (c >= '1' && c <= '9') x = c - '0';
233 if (c == '0') x = 10;
234 if (c == '.') x = 12;
235 if (c == '_') x = 13;
236 if (x) lHash = lHash * 43 + x;
238 return lHash;
241 /***********************************************************************
243 * HLPFILE_ReadHlpFile
245 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
247 HLPFILE* hlpfile;
249 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
251 if (!strcmp(lpszPath, hlpfile->lpszPath))
253 hlpfile->wRefCount++;
254 return hlpfile;
258 hlpfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
259 sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
260 if (!hlpfile) return 0;
262 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
263 hlpfile->contents_start = 0xFFFFFFFF;
264 hlpfile->next = first_hlpfile;
265 hlpfile->wRefCount = 1;
267 strcpy(hlpfile->lpszPath, lpszPath);
269 first_hlpfile = hlpfile;
270 if (hlpfile->next) hlpfile->next->prev = hlpfile;
272 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
274 HLPFILE_FreeHlpFile(hlpfile);
275 hlpfile = 0;
278 return hlpfile;
281 /***********************************************************************
283 * HLPFILE_DoReadHlpFile
285 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
287 BOOL ret;
288 HFILE hFile;
289 OFSTRUCT ofs;
290 BYTE* buf;
291 DWORD ref = 0x0C;
292 unsigned index, old_index, offset, len, offs;
294 hFile = OpenFile(lpszPath, &ofs, OF_READ);
295 if (hFile == HFILE_ERROR) return FALSE;
297 ret = HLPFILE_ReadFileToBuffer(hlpfile, hFile);
298 _lclose(hFile);
299 if (!ret) return FALSE;
301 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
303 /* load phrases support */
304 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
305 HLPFILE_Uncompress_Phrases40(hlpfile);
307 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
308 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
310 buf = hlpfile->topic_map[0];
311 old_index = -1;
312 offs = 0;
315 BYTE* end;
317 if (hlpfile->version <= 16)
319 index = (ref - 0x0C) / hlpfile->dsize;
320 offset = (ref - 0x0C) % hlpfile->dsize;
322 else
324 index = (ref - 0x0C) >> 14;
325 offset = (ref - 0x0C) & 0x3FFF;
328 if (hlpfile->version <= 16 && index != old_index && index != 0)
330 /* we jumped to the next block, adjust pointers */
331 ref -= 12;
332 offset -= 12;
335 WINE_TRACE("ref=%08x => [%u/%u]\n", ref, index, offset);
337 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
338 buf = hlpfile->topic_map[index] + offset;
339 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
340 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
341 if (index != old_index) {offs = 0; old_index = index;}
343 switch (buf[0x14])
345 case 0x02:
346 if (!HLPFILE_AddPage(hlpfile, buf, end, index * 0x8000L + offs)) return FALSE;
347 break;
349 case 0x01:
350 case 0x20:
351 case 0x23:
352 if (!HLPFILE_AddParagraph(hlpfile, buf, end, &len)) return FALSE;
353 offs += len;
354 break;
356 default:
357 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
360 if (hlpfile->version <= 16)
362 ref += GET_UINT(buf, 0xc);
363 if (GET_UINT(buf, 0xc) == 0)
364 break;
366 else
367 ref = GET_UINT(buf, 0xc);
368 } while (ref != 0xffffffff);
370 HLPFILE_GetKeywords(hlpfile);
371 HLPFILE_GetMap(hlpfile);
372 if (hlpfile->version <= 16) return TRUE;
373 return HLPFILE_GetContext(hlpfile);
376 /***********************************************************************
378 * HLPFILE_AddPage
380 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned offset)
382 HLPFILE_PAGE* page;
383 BYTE* title;
384 UINT titlesize, blocksize, datalen;
385 char* ptr;
386 HLPFILE_MACRO*macro;
388 blocksize = GET_UINT(buf, 0);
389 datalen = GET_UINT(buf, 0x10);
390 title = buf + datalen;
391 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
393 titlesize = GET_UINT(buf, 4);
394 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
395 if (!page) return FALSE;
396 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
398 if (titlesize > blocksize - datalen)
400 /* need to decompress */
401 if (hlpfile->hasPhrases)
402 HLPFILE_Uncompress2(hlpfile, title, end, (BYTE*)page->lpszTitle, (BYTE*)page->lpszTitle + titlesize);
403 else if (hlpfile->hasPhrases40)
404 HLPFILE_Uncompress3(hlpfile, page->lpszTitle, page->lpszTitle + titlesize, title, end);
405 else
407 WINE_FIXME("Text size is too long, splitting\n");
408 titlesize = blocksize - datalen;
409 memcpy(page->lpszTitle, title, titlesize);
412 else
413 memcpy(page->lpszTitle, title, titlesize);
415 page->lpszTitle[titlesize] = '\0';
417 if (hlpfile->first_page)
419 hlpfile->last_page->next = page;
420 page->prev = hlpfile->last_page;
421 hlpfile->last_page = page;
423 else
425 hlpfile->first_page = page;
426 hlpfile->last_page = page;
427 page->prev = NULL;
430 page->file = hlpfile;
431 page->next = NULL;
432 page->first_paragraph = NULL;
433 page->first_macro = NULL;
434 page->wNumber = GET_UINT(buf, 0x21);
435 page->offset = offset;
437 page->browse_bwd = GET_UINT(buf, 0x19);
438 page->browse_fwd = GET_UINT(buf, 0x1D);
440 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
441 page->wNumber, page->lpszTitle,
442 page->browse_bwd, page->offset, page->browse_fwd);
444 memset(&attributes, 0, sizeof(attributes));
446 /* now load macros */
447 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
448 while (ptr < page->lpszTitle + titlesize)
450 unsigned len = strlen(ptr);
451 char* macro_str;
453 WINE_TRACE("macro: %s\n", ptr);
454 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
455 macro->lpszMacro = macro_str = (char*)(macro + 1);
456 memcpy(macro_str, ptr, len + 1);
457 /* FIXME: shall we really link macro in reverse order ??
458 * may produce strange results when played at page opening
460 macro->next = page->first_macro;
461 page->first_macro = macro;
462 ptr += len + 1;
465 return TRUE;
468 static long fetch_long(BYTE** ptr)
470 long ret;
472 if (*(*ptr) & 1)
474 ret = (*(unsigned long*)(*ptr) - 0x80000000L) / 2;
475 (*ptr) += 4;
477 else
479 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
480 (*ptr) += 2;
483 return ret;
486 static unsigned long fetch_ulong(BYTE** ptr)
488 unsigned long ret;
490 if (*(*ptr) & 1)
492 ret = *(unsigned long*)(*ptr) / 2;
493 (*ptr) += 4;
495 else
497 ret = *(unsigned short*)(*ptr) / 2;
498 (*ptr) += 2;
500 return ret;
503 static short fetch_short(BYTE** ptr)
505 short ret;
507 if (*(*ptr) & 1)
509 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
510 (*ptr) += 2;
512 else
514 ret = (*(unsigned char*)(*ptr) - 0x80) / 2;
515 (*ptr)++;
517 return ret;
520 static unsigned short fetch_ushort(BYTE** ptr)
522 unsigned short ret;
524 if (*(*ptr) & 1)
526 ret = *(unsigned short*)(*ptr) / 2;
527 (*ptr) += 2;
529 else
531 ret = *(unsigned char*)(*ptr) / 2;
532 (*ptr)++;
534 return ret;
537 /******************************************************************
538 * HLPFILE_DecompressGfx
540 * Decompress the data part of a bitmap or a metafile
542 static BYTE* HLPFILE_DecompressGfx(BYTE* src, unsigned csz, unsigned sz, BYTE packing)
544 BYTE* dst;
545 BYTE* tmp;
546 BYTE* tmp2;
547 unsigned sz77;
549 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
551 switch (packing)
553 case 0: /* uncompressed */
554 if (sz != csz)
555 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
556 dst = src;
557 break;
558 case 1: /* RunLen */
559 tmp = dst = HeapAlloc(GetProcessHeap(), 0, sz);
560 if (!dst) return NULL;
561 HLPFILE_UncompressRLE(src, src + csz, &tmp, sz);
562 if (tmp - dst != sz)
563 WINE_WARN("Bogus gfx sizes (RunLen): %lu/%u\n", (SIZE_T)(tmp - dst), sz);
564 break;
565 case 2: /* LZ77 */
566 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
567 dst = HeapAlloc(GetProcessHeap(), 0, sz77);
568 if (!dst) return NULL;
569 HLPFILE_UncompressLZ77(src, src + csz, dst);
570 if (sz77 != sz)
571 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
572 break;
573 case 3: /* LZ77 then RLE */
574 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
575 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
576 if (!tmp) return FALSE;
577 HLPFILE_UncompressLZ77(src, src + csz, tmp);
578 dst = tmp2 = HeapAlloc(GetProcessHeap(), 0, sz);
579 if (!dst)
581 HeapFree(GetProcessHeap(), 0, tmp);
582 return FALSE;
584 HLPFILE_UncompressRLE(tmp, tmp + sz77, &tmp2, sz);
585 if (tmp2 - dst != sz)
586 WINE_WARN("Bogus gfx sizes (LZ77+RunLen): %lu / %u\n", (SIZE_T)(tmp2 - dst), sz);
587 HeapFree(GetProcessHeap(), 0, tmp);
588 break;
589 default:
590 WINE_FIXME("Unsupported packing %u\n", packing);
591 return NULL;
593 return dst;
596 /******************************************************************
597 * HLPFILE_LoadBitmap
601 static BOOL HLPFILE_LoadBitmap(BYTE* beg, BYTE type, BYTE pack,
602 HLPFILE_PARAGRAPH* paragraph)
604 BYTE* ptr;
605 BYTE* pict_beg;
606 BITMAPINFO* bi;
607 unsigned long off, csz;
608 HDC hdc;
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 */
639 if (type == 0x06)
641 unsigned nc = bi->bmiHeader.biClrUsed;
642 unsigned i;
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;
656 ptr += 4;
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,
662 CBM_INIT, pict_beg,
663 bi, DIB_RGB_COLORS);
664 ReleaseDC(0, hdc);
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);
671 return TRUE;
674 /******************************************************************
675 * HLPFILE_LoadMetaFile
679 static BOOL HLPFILE_LoadMetaFile(BYTE* beg, BYTE pack, HLPFILE_PARAGRAPH* paragraph)
681 BYTE* ptr;
682 unsigned long size, csize;
683 unsigned long off, hsoff;
684 BYTE* bits;
685 LPMETAFILEPICT lpmfp;
687 WINE_TRACE("Loading metafile\n");
689 ptr = beg + 2; /* for type and pack */
691 lpmfp = &paragraph->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);
696 ptr += 4;
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);
703 ptr += 8;
705 WINE_TRACE("sz=%lu csz=%lu (%d,%d) offs=%lu/%lu,%lu\n",
706 size, csize, lpmfp->xExt, lpmfp->yExt, off, (SIZE_T)(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);
715 if (!lpmfp->hMF)
716 WINE_FIXME("Couldn't load metafile\n");
718 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
720 return TRUE;
723 /******************************************************************
724 * HLPFILE_LoadGfxByAddr
728 static BOOL HLPFILE_LoadGfxByAddr(HLPFILE *hlpfile, BYTE* ref,
729 unsigned long size,
730 HLPFILE_PARAGRAPH* paragraph)
732 unsigned i, numpict;
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++)
740 BYTE* beg;
741 BYTE* ptr;
742 BYTE type, pack;
744 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
745 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
747 type = *ptr++;
748 pack = *ptr++;
750 switch (type)
752 case 5: /* device dependent bmp */
753 case 6: /* device independent bmp */
754 HLPFILE_LoadBitmap(beg, type, pack, paragraph);
755 break;
756 case 8:
757 HLPFILE_LoadMetaFile(beg, pack, paragraph);
758 break;
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");
766 break;
768 return TRUE;
771 /******************************************************************
772 * HLPFILE_LoadGfxByIndex
776 static BOOL HLPFILE_LoadGfxByIndex(HLPFILE *hlpfile, unsigned index,
777 HLPFILE_PARAGRAPH* paragraph)
779 char tmp[16];
780 BYTE *ref, *end;
781 BOOL ret;
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];
788 return TRUE;
791 sprintf(tmp, "|bm%u", index);
793 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
795 ref += 9;
797 ret = HLPFILE_LoadGfxByAddr(hlpfile, ref, end - ref, paragraph);
799 /* cache bitmap */
800 if (ret && paragraph->cookie == para_bitmap)
802 if (index >= hlpfile->numBmps)
804 hlpfile->numBmps = index + 1;
805 if (hlpfile->bmps)
806 hlpfile->bmps = HeapReAlloc(GetProcessHeap(), 0, hlpfile->bmps,
807 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
808 else
809 hlpfile->bmps = HeapAlloc(GetProcessHeap(), 0,
810 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
813 hlpfile->bmps[index] = paragraph->u.gfx.u.bmp.hBitmap;
815 return ret;
818 /******************************************************************
819 * HLPFILE_AllocLink
823 static HLPFILE_LINK* HLPFILE_AllocLink(int cookie, const char* str, LONG hash,
824 BOOL clrChange, unsigned wnd)
826 HLPFILE_LINK* link;
827 char* link_str;
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);
838 link->lHash = hash;
839 link->bClrChange = clrChange ? 1 : 0;
840 link->window = wnd;
841 link->wRefCount = 1;
843 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
844 link->cookie, link->lpszString,
845 link->lHash, link->window);
846 return link;
849 /***********************************************************************
851 * HLPFILE_AddParagraph
853 static BOOL HLPFILE_AddParagraph(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned* len)
855 HLPFILE_PAGE *page;
856 HLPFILE_PARAGRAPH *paragraph, **paragraphptr;
857 UINT textsize;
858 BYTE *format, *format_end;
859 char *text, *text_base, *text_end;
860 long size, blocksize, datalen;
861 unsigned short bits;
862 unsigned nc, ncol = 1;
864 if (!hlpfile->last_page) {WINE_WARN("no page\n"); return FALSE;};
865 page = hlpfile->last_page;
866 for (paragraphptr = &page->first_paragraph; *paragraphptr;
867 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
869 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
871 blocksize = GET_UINT(buf, 0);
872 size = GET_UINT(buf, 0x4);
873 datalen = GET_UINT(buf, 0x10);
874 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
875 if (!text) return FALSE;
876 if (size > blocksize - datalen)
878 /* need to decompress */
879 if (hlpfile->hasPhrases)
880 HLPFILE_Uncompress2(hlpfile, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
881 else if (hlpfile->hasPhrases40)
882 HLPFILE_Uncompress3(hlpfile, text, text + size, buf + datalen, end);
883 else
885 WINE_FIXME("Text size is too long, splitting\n");
886 size = blocksize - datalen;
887 memcpy(text, buf + datalen, size);
890 else
891 memcpy(text, buf + datalen, size);
893 text_end = text + size;
895 format = buf + 0x15;
896 format_end = buf + GET_UINT(buf, 0x10);
898 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
900 fetch_long(&format);
901 *len = fetch_ushort(&format);
903 else *len = end-buf-15;
905 if (buf[0x14] == 0x23)
907 char type;
909 ncol = *format++;
911 WINE_TRACE("#cols %u\n", ncol);
912 type = *format++;
913 if (type == 0 || type == 2)
914 format += 2;
915 format += ncol * 4;
918 for (nc = 0; nc < ncol; nc++)
920 WINE_TRACE("looking for format at offset %lu for column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
921 if (buf[0x14] == 0x23)
922 format += 5;
923 if (buf[0x14] == 0x01)
924 format += 6;
925 else
926 format += 4;
927 bits = GET_USHORT(format, 0); format += 2;
928 if (bits & 0x0001) fetch_long(&format);
929 if (bits & 0x0002) fetch_short(&format);
930 if (bits & 0x0004) fetch_short(&format);
931 if (bits & 0x0008) fetch_short(&format);
932 if (bits & 0x0010) fetch_short(&format);
933 if (bits & 0x0020) fetch_short(&format);
934 if (bits & 0x0040) fetch_short(&format);
935 if (bits & 0x0100) format += 3;
936 if (bits & 0x0200)
938 int ntab = fetch_short(&format);
939 unsigned short ts;
941 while (ntab-- > 0)
943 ts = fetch_ushort(&format);
944 if (ts & 0x4000) fetch_ushort(&format);
947 /* 0x0400, 0x0800 and 0x1000 don't need space */
948 if ((bits & 0xE080) != 0)
949 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
951 while (text < text_end && format < format_end)
953 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
954 textsize = strlen(text) + 1;
955 if (textsize > 1)
957 paragraph = HeapAlloc(GetProcessHeap(), 0,
958 sizeof(HLPFILE_PARAGRAPH) + textsize);
959 if (!paragraph) return FALSE;
960 *paragraphptr = paragraph;
961 paragraphptr = &paragraph->next;
963 paragraph->next = NULL;
964 paragraph->link = attributes.link;
965 if (paragraph->link) paragraph->link->wRefCount++;
966 paragraph->cookie = para_normal_text;
967 paragraph->u.text.wFont = attributes.wFont;
968 paragraph->u.text.wVSpace = attributes.wVSpace;
969 paragraph->u.text.wHSpace = attributes.wHSpace;
970 paragraph->u.text.wIndent = attributes.wIndent;
971 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
972 strcpy(paragraph->u.text.lpszText, text);
974 attributes.wVSpace = 0;
975 attributes.wHSpace = 0;
977 /* else: null text, keep on storing attributes */
978 text += textsize;
980 if (*format == 0xff)
982 format++;
983 break;
986 WINE_TRACE("format=%02x\n", *format);
987 switch (*format)
989 case 0x20:
990 WINE_FIXME("NIY20\n");
991 format += 5;
992 break;
994 case 0x21:
995 WINE_FIXME("NIY21\n");
996 format += 3;
997 break;
999 case 0x80:
1000 attributes.wFont = GET_USHORT(format, 1);
1001 WINE_TRACE("Changing font to %d\n", attributes.wFont);
1002 format += 3;
1003 break;
1005 case 0x81:
1006 attributes.wVSpace++;
1007 format += 1;
1008 break;
1010 case 0x82:
1011 attributes.wVSpace++;
1012 attributes.wIndent = 0;
1013 format += 1;
1014 break;
1016 case 0x83:
1017 attributes.wIndent++;
1018 format += 1;
1019 break;
1021 #if 0
1022 case 0x84:
1023 format += 3;
1024 break;
1025 #endif
1027 case 0x86:
1028 case 0x87:
1029 case 0x88:
1031 BYTE pos = (*format - 0x86);
1032 BYTE type = format[1];
1033 long size;
1035 format += 2;
1036 size = fetch_long(&format);
1038 paragraph = HeapAlloc(GetProcessHeap(), 0,
1039 sizeof(HLPFILE_PARAGRAPH) + textsize);
1040 if (!paragraph) return FALSE;
1041 *paragraphptr = paragraph;
1042 paragraphptr = &paragraph->next;
1044 paragraph->next = NULL;
1045 paragraph->link = attributes.link;
1046 if (paragraph->link) paragraph->link->wRefCount++;
1047 paragraph->cookie = para_bitmap;
1048 paragraph->u.gfx.pos = pos;
1049 switch (type)
1051 case 0x22:
1052 fetch_ushort(&format); /* hot spot */
1053 /* fall thru */
1054 case 0x03:
1055 switch (GET_SHORT(format, 0))
1057 case 0:
1058 HLPFILE_LoadGfxByIndex(hlpfile, GET_SHORT(format, 2),
1059 paragraph);
1060 break;
1061 case 1:
1062 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1063 GET_SHORT(format, 0),
1064 size, GET_SHORT(format, 2));
1065 HLPFILE_LoadGfxByAddr(hlpfile, format + 2, size - 4,
1066 paragraph);
1067 break;
1068 default:
1069 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1070 break;
1072 break;
1073 case 0x05:
1074 WINE_FIXME("Got an embedded element %s\n", format + 6);
1075 break;
1076 default:
1077 WINE_FIXME("Got a type %d picture\n", type);
1078 break;
1080 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1082 format += size;
1084 break;
1086 case 0x89:
1087 HLPFILE_FreeLink(attributes.link);
1088 attributes.link = NULL;
1089 format += 1;
1090 break;
1092 case 0x8B:
1093 case 0x8C:
1094 WINE_FIXME("NIY non-break space/hyphen\n");
1095 format += 1;
1096 break;
1098 #if 0
1099 case 0xA9:
1100 format += 2;
1101 break;
1102 #endif
1104 case 0xC8:
1105 case 0xCC:
1106 WINE_TRACE("macro => %s\n", format + 3);
1107 HLPFILE_FreeLink(attributes.link);
1108 attributes.link = HLPFILE_AllocLink(hlp_link_macro, (const char*)format + 3,
1109 0, !(*format & 4), -1);
1110 format += 3 + GET_USHORT(format, 1);
1111 break;
1113 case 0xE0:
1114 case 0xE1:
1115 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1116 HLPFILE_FreeLink(attributes.link);
1117 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1118 hlpfile->lpszPath,
1119 GET_UINT(format, 1)-16,
1120 1, -1);
1123 format += 5;
1124 break;
1126 case 0xE2:
1127 case 0xE3:
1128 case 0xE6:
1129 case 0xE7:
1130 HLPFILE_FreeLink(attributes.link);
1131 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1132 hlpfile->lpszPath,
1133 GET_UINT(format, 1),
1134 !(*format & 4), -1);
1135 format += 5;
1136 break;
1138 case 0xEA:
1139 case 0xEB:
1140 case 0xEE:
1141 case 0xEF:
1143 char* ptr = (char*) format + 8;
1144 BYTE type = format[3];
1145 int wnd = -1;
1146 char* str;
1148 if (type == 1) wnd = *ptr++;
1149 if (type == 4 || type == 6)
1151 str = ptr;
1152 ptr += strlen(ptr) + 1;
1154 else
1155 str = hlpfile->lpszPath;
1156 if (type == 6)
1158 for (wnd = hlpfile->numWindows - 1; wnd >= 0; wnd--)
1160 if (!strcmp(ptr, hlpfile->windows[wnd].name)) break;
1162 if (wnd == -1)
1163 WINE_WARN("Couldn't find window info for %s\n", ptr);
1165 HLPFILE_FreeLink(attributes.link);
1166 attributes.link = HLPFILE_AllocLink((*format & 4) ? hlp_link_link : hlp_link_popup,
1167 str, GET_UINT(format, 4),
1168 !(*format & 1), wnd);
1170 format += 3 + GET_USHORT(format, 1);
1171 break;
1173 default:
1174 WINE_WARN("format %02x\n", *format);
1175 format++;
1179 HeapFree(GetProcessHeap(), 0, text_base);
1180 return TRUE;
1183 /******************************************************************
1184 * HLPFILE_ReadFont
1188 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1190 BYTE *ref, *end;
1191 unsigned i, len, idx;
1192 unsigned face_num, dscr_num, face_offset, dscr_offset;
1193 BYTE flag, family;
1195 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1197 WINE_WARN("no subfile FONT\n");
1198 hlpfile->numFonts = 0;
1199 hlpfile->fonts = NULL;
1200 return FALSE;
1203 ref += 9;
1205 face_num = GET_USHORT(ref, 0);
1206 dscr_num = GET_USHORT(ref, 2);
1207 face_offset = GET_USHORT(ref, 4);
1208 dscr_offset = GET_USHORT(ref, 6);
1210 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1211 face_num, face_offset, dscr_num, dscr_offset);
1213 hlpfile->numFonts = dscr_num;
1214 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1216 len = (dscr_offset - face_offset) / face_num;
1217 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1218 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1219 for (i = 0; i < dscr_num; i++)
1221 flag = ref[dscr_offset + i * 11 + 0];
1222 family = ref[dscr_offset + i * 11 + 2];
1224 hlpfile->fonts[i].LogFont.lfHeight = -ref[dscr_offset + i * 11 + 1] / 2 - 3;
1225 hlpfile->fonts[i].LogFont.lfWidth = 0;
1226 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1227 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1228 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1229 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1230 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1231 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1232 hlpfile->fonts[i].LogFont.lfCharSet = DEFAULT_CHARSET;
1233 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1234 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1235 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1236 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1238 switch (family)
1240 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1241 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1242 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1243 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1244 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1245 default: WINE_FIXME("Unknown family %u\n", family);
1247 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1249 if (idx < face_num)
1251 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1252 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1254 else
1256 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1257 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1259 hlpfile->fonts[i].hFont = 0;
1260 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1261 ref[dscr_offset + i * 11 + 6],
1262 ref[dscr_offset + i * 11 + 7]);
1263 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1264 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1265 i, flag,
1266 X(0, "bold"),
1267 X(1, "italic"),
1268 X(2, "underline"),
1269 X(3, "strikeOut"),
1270 X(4, "dblUnderline"),
1271 X(5, "smallCaps"),
1272 ref[dscr_offset + i * 11 + 1],
1273 family,
1274 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1275 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1277 return TRUE;
1280 /***********************************************************************
1282 * HLPFILE_ReadFileToBuffer
1284 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1286 BYTE header[16], dummy[1];
1288 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1290 /* sanity checks */
1291 if (GET_UINT(header, 0) != 0x00035F3F)
1292 {WINE_WARN("wrong header\n"); return FALSE;};
1294 hlpfile->file_buffer_size = GET_UINT(header, 12);
1295 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1296 if (!hlpfile->file_buffer) return FALSE;
1298 memcpy(hlpfile->file_buffer, header, 16);
1299 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1300 {WINE_WARN("filesize1\n"); return FALSE;};
1302 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1304 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds ackward to me */
1306 return TRUE;
1309 /**************************************************************************
1310 * comp_FindSubFile
1312 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1315 static int comp_FindSubFile(void *p, const void *key,
1316 int leaf, void** next)
1318 *next = (char *)p+strlen(p)+(leaf?5:3);
1319 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1320 return strcmp(p, key);
1323 /***********************************************************************
1325 * HLPFILE_FindSubFile
1327 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
1329 BYTE *ptr;
1331 WINE_TRACE("looking for file '%s'\n", name);
1332 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
1333 name, comp_FindSubFile);
1334 if (!ptr) return FALSE;
1335 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
1336 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
1338 WINE_ERR("internal file %s does not fit\n", name);
1339 return FALSE;
1341 *subend = *subbuf + GET_UINT(*subbuf, 0);
1342 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
1344 WINE_ERR("internal file %s does not fit\n", name);
1345 return FALSE;
1347 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1349 WINE_ERR("invalid size provided for internal file %s\n", name);
1350 return FALSE;
1352 return TRUE;
1355 /***********************************************************************
1357 * HLPFILE_SystemCommands
1359 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1361 BYTE *buf, *ptr, *end;
1362 HLPFILE_MACRO *macro, **m;
1363 LPSTR p;
1364 unsigned short magic, minor, major, flags;
1366 hlpfile->lpszTitle = NULL;
1368 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1370 magic = GET_USHORT(buf + 9, 0);
1371 minor = GET_USHORT(buf + 9, 2);
1372 major = GET_USHORT(buf + 9, 4);
1373 /* gen date on 4 bytes */
1374 flags = GET_USHORT(buf + 9, 10);
1375 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1376 magic, major, minor, flags);
1377 if (magic != 0x036C || major != 1)
1378 {WINE_WARN("Wrong system header\n"); return FALSE;}
1379 if (minor <= 16)
1381 hlpfile->tbsize = 0x800;
1382 hlpfile->compressed = 0;
1384 else if (flags == 0)
1386 hlpfile->tbsize = 0x1000;
1387 hlpfile->compressed = 0;
1389 else if (flags == 4)
1391 hlpfile->tbsize = 0x1000;
1392 hlpfile->compressed = 1;
1394 else
1396 hlpfile->tbsize = 0x800;
1397 hlpfile->compressed = 1;
1400 if (hlpfile->compressed)
1401 hlpfile->dsize = 0x4000;
1402 else
1403 hlpfile->dsize = hlpfile->tbsize - 0x0C;
1405 hlpfile->version = minor;
1406 hlpfile->flags = flags;
1408 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1410 char *str = (char*) ptr + 4;
1411 switch (GET_USHORT(ptr, 0))
1413 case 1:
1414 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1415 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1416 if (!hlpfile->lpszTitle) return FALSE;
1417 lstrcpy(hlpfile->lpszTitle, str);
1418 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1419 break;
1421 case 2:
1422 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1423 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1424 if (!hlpfile->lpszCopyright) return FALSE;
1425 lstrcpy(hlpfile->lpszCopyright, str);
1426 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1427 break;
1429 case 3:
1430 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1431 hlpfile->contents_start = GET_UINT(ptr, 4);
1432 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1433 break;
1435 case 4:
1436 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1437 if (!macro) break;
1438 p = (char*)macro + sizeof(HLPFILE_MACRO);
1439 lstrcpy(p, str);
1440 macro->lpszMacro = p;
1441 macro->next = 0;
1442 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1443 *m = macro;
1444 break;
1446 case 6:
1447 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1449 if (hlpfile->windows)
1450 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1451 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1452 else
1453 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1454 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1456 if (hlpfile->windows)
1458 unsigned flags = GET_USHORT(ptr, 4);
1459 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1461 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1462 else wi->type[0] = '\0';
1463 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1464 else wi->name[0] = '\0';
1465 if (flags & 0x0004) strcpy(wi->caption, &str[23]);
1466 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1467 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1468 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1469 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1470 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1471 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1472 wi->win_style = WS_OVERLAPPEDWINDOW;
1473 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1474 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1475 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",
1476 flags & 0x0001 ? 'T' : 't',
1477 flags & 0x0002 ? 'N' : 'n',
1478 flags & 0x0004 ? 'C' : 'c',
1479 flags & 0x0008 ? 'X' : 'x',
1480 flags & 0x0010 ? 'Y' : 'y',
1481 flags & 0x0020 ? 'W' : 'w',
1482 flags & 0x0040 ? 'H' : 'h',
1483 flags & 0x0080 ? 'S' : 's',
1484 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1485 wi->size.cx, wi->size.cy);
1487 break;
1488 default:
1489 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1492 if (!hlpfile->lpszTitle)
1493 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
1494 return TRUE;
1497 /***********************************************************************
1499 * HLPFILE_UncompressedLZ77_Size
1501 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
1503 int i, newsize = 0;
1505 while (ptr < end)
1507 int mask = *ptr++;
1508 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1510 if (mask & 1)
1512 int code = GET_USHORT(ptr, 0);
1513 int len = 3 + (code >> 12);
1514 newsize += len;
1515 ptr += 2;
1517 else newsize++, ptr++;
1521 return newsize;
1524 /***********************************************************************
1526 * HLPFILE_UncompressLZ77
1528 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
1530 int i;
1532 while (ptr < end)
1534 int mask = *ptr++;
1535 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1537 if (mask & 1)
1539 int code = GET_USHORT(ptr, 0);
1540 int len = 3 + (code >> 12);
1541 int offset = code & 0xfff;
1543 * We must copy byte-by-byte here. We cannot use memcpy nor
1544 * memmove here. Just example:
1545 * a[]={1,2,3,4,5,6,7,8,9,10}
1546 * newptr=a+2;
1547 * offset=1;
1548 * We expect:
1549 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
1551 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
1552 ptr += 2;
1554 else *newptr++ = *ptr++;
1558 return newptr;
1561 /***********************************************************************
1563 * HLPFILE_UncompressLZ77_Phrases
1565 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
1567 UINT i, num, dec_size, head_size;
1568 BYTE *buf, *end;
1570 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
1572 if (hlpfile->version <= 16)
1573 head_size = 13;
1574 else
1575 head_size = 17;
1577 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
1578 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
1580 if (hlpfile->version <= 16)
1581 dec_size = end - buf - 15 - 2 * num;
1582 else
1583 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
1585 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1586 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1587 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
1589 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
1590 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
1591 return FALSE;
1594 for (i = 0; i <= num; i++)
1595 hlpfile->phrases_offsets[i] = GET_USHORT(buf, 0x11 + 2 * i) - 2 * num - 2;
1597 if (hlpfile->version <= 16)
1598 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
1599 else
1600 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
1602 hlpfile->hasPhrases = TRUE;
1603 return TRUE;
1606 /***********************************************************************
1608 * HLPFILE_Uncompress_Phrases40
1610 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
1612 UINT num;
1613 INT dec_size, cpr_size;
1614 BYTE *buf_idx, *end_idx;
1615 BYTE *buf_phs, *end_phs;
1616 long* ptr, mask = 0;
1617 unsigned int i;
1618 unsigned short bc, n;
1620 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
1621 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
1623 ptr = (long*)(buf_idx + 9 + 28);
1624 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
1625 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
1627 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
1628 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
1629 GET_UINT(buf_idx, 9 + 0),
1630 GET_UINT(buf_idx, 9 + 4),
1631 GET_UINT(buf_idx, 9 + 8),
1632 GET_UINT(buf_idx, 9 + 12),
1633 GET_UINT(buf_idx, 9 + 16),
1634 GET_UINT(buf_idx, 9 + 20),
1635 GET_USHORT(buf_idx, 9 + 24),
1636 GET_USHORT(buf_idx, 9 + 26));
1638 dec_size = GET_UINT(buf_idx, 9 + 12);
1639 cpr_size = GET_UINT(buf_idx, 9 + 16);
1641 if (dec_size != cpr_size &&
1642 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
1644 WINE_WARN("size mismatch %u %u\n",
1645 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1646 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1649 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1650 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1651 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
1653 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
1654 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
1655 return FALSE;
1658 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
1660 hlpfile->phrases_offsets[0] = 0;
1661 for (i = 0; i < num; i++)
1663 for (n = 1; getbit(); n += 1 << bc);
1664 if (getbit()) n++;
1665 if (bc > 1 && getbit()) n += 2;
1666 if (bc > 2 && getbit()) n += 4;
1667 if (bc > 3 && getbit()) n += 8;
1668 if (bc > 4 && getbit()) n += 16;
1669 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
1671 #undef getbit
1673 if (dec_size == cpr_size)
1674 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
1675 else
1676 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
1678 hlpfile->hasPhrases40 = TRUE;
1679 return TRUE;
1682 /***********************************************************************
1684 * HLPFILE_Uncompress_Topic
1686 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
1688 BYTE *buf, *ptr, *end, *newptr;
1689 unsigned int i, newsize = 0;
1690 unsigned int topic_size;
1692 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
1693 {WINE_WARN("topic0\n"); return FALSE;}
1695 buf += 9; /* Skip file header */
1696 topic_size = end - buf;
1697 if (hlpfile->compressed)
1699 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
1701 for (i = 0; i < hlpfile->topic_maplen; i++)
1703 ptr = buf + i * hlpfile->tbsize;
1705 /* I don't know why, it's necessary for printman.hlp */
1706 if (ptr + 0x44 > end) ptr = end - 0x44;
1708 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
1711 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
1712 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
1713 if (!hlpfile->topic_map) return FALSE;
1714 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
1715 hlpfile->topic_end = newptr + newsize;
1717 for (i = 0; i < hlpfile->topic_maplen; i++)
1719 ptr = buf + i * hlpfile->tbsize;
1720 if (ptr + 0x44 > end) ptr = end - 0x44;
1722 hlpfile->topic_map[i] = newptr;
1723 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
1726 else
1728 /* basically, we need to copy the TopicBlockSize byte pages
1729 * (removing the first 0x0C) in one single area in memory
1731 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
1732 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
1733 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
1734 if (!hlpfile->topic_map) return FALSE;
1735 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
1736 hlpfile->topic_end = newptr + topic_size;
1738 for (i = 0; i < hlpfile->topic_maplen; i++)
1740 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
1741 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
1744 return TRUE;
1747 /***********************************************************************
1749 * HLPFILE_Uncompress2
1752 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
1754 BYTE *phptr, *phend;
1755 UINT code;
1756 UINT index;
1758 while (ptr < end && newptr < newend)
1760 if (!*ptr || *ptr >= 0x10)
1761 *newptr++ = *ptr++;
1762 else
1764 code = 0x100 * ptr[0] + ptr[1];
1765 index = (code - 0x100) / 2;
1767 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
1768 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
1770 if (newptr + (phend - phptr) > newend)
1772 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
1773 newptr, newend, (SIZE_T)(phend - phptr));
1774 return;
1776 memcpy(newptr, phptr, phend - phptr);
1777 newptr += phend - phptr;
1778 if (code & 1) *newptr++ = ' ';
1780 ptr += 2;
1783 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
1786 /******************************************************************
1787 * HLPFILE_Uncompress3
1791 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
1792 const BYTE* src, const BYTE* src_end)
1794 unsigned int idx, len;
1796 for (; src < src_end; src++)
1798 if ((*src & 1) == 0)
1800 idx = *src / 2;
1801 if (idx > hlpfile->num_phrases)
1803 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
1804 len = 0;
1806 else
1808 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
1809 if (dst + len <= dst_end)
1810 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
1813 else if ((*src & 0x03) == 0x01)
1815 idx = (*src + 1) * 64;
1816 idx += *++src;
1817 if (idx > hlpfile->num_phrases)
1819 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
1820 len = 0;
1822 else
1824 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
1825 if (dst + len <= dst_end)
1826 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
1829 else if ((*src & 0x07) == 0x03)
1831 len = (*src / 8) + 1;
1832 if (dst + len <= dst_end)
1833 memcpy(dst, src + 1, len);
1834 src += len;
1836 else
1838 len = (*src / 16) + 1;
1839 if (dst + len <= dst_end)
1840 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
1842 dst += len;
1845 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
1846 return TRUE;
1849 /******************************************************************
1850 * HLPFILE_UncompressRLE
1854 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
1856 BYTE ch;
1857 BYTE* sdst = *dst + dstsz;
1859 while (src < end)
1861 ch = *src++;
1862 if (ch & 0x80)
1864 ch &= 0x7F;
1865 if ((*dst) + ch <= sdst)
1866 memcpy(*dst, src, ch);
1867 src += ch;
1869 else
1871 if ((*dst) + ch <= sdst)
1872 memset(*dst, (char)*src, ch);
1873 src++;
1875 *dst += ch;
1877 if (*dst != sdst)
1878 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
1879 (SIZE_T)(*dst - (sdst - dstsz)), dstsz);
1882 /**************************************************************************
1883 * HLPFILE_BPTreeSearch
1885 * Searches for an element in B+ tree
1887 * PARAMS
1888 * buf [I] pointer to the embedded file structured as a B+ tree
1889 * key [I] pointer to data to find
1890 * comp [I] compare function
1892 * RETURNS
1893 * Pointer to block identified by key, or NULL if failure.
1896 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
1897 HLPFILE_BPTreeCompare comp)
1899 unsigned magic;
1900 unsigned page_size;
1901 unsigned cur_page;
1902 unsigned level;
1903 BYTE *pages, *ptr, *newptr;
1904 int i, entries;
1905 int ret;
1907 magic = GET_USHORT(buf, 9);
1908 if (magic != 0x293B)
1910 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
1911 return NULL;
1913 page_size = GET_USHORT(buf, 9+4);
1914 cur_page = GET_USHORT(buf, 9+26);
1915 level = GET_USHORT(buf, 9+32);
1916 pages = buf + 9 + 38;
1917 while (--level > 0)
1919 ptr = pages + cur_page*page_size;
1920 entries = GET_SHORT(ptr, 2);
1921 ptr += 6;
1922 for (i = 0; i < entries; i++)
1924 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
1925 ptr = newptr;
1927 cur_page = GET_USHORT(ptr-2, 0);
1929 ptr = pages + cur_page*page_size;
1930 entries = GET_SHORT(ptr, 2);
1931 ptr += 8;
1932 for (i = 0; i < entries; i++)
1934 ret = comp(ptr, key, 1, (void **)&newptr);
1935 if (ret == 0) return ptr;
1936 if (ret > 0) return NULL;
1937 ptr = newptr;
1939 return NULL;
1942 /**************************************************************************
1943 * HLPFILE_BPTreeEnum
1945 * Enumerates elements in B+ tree.
1947 * PARAMS
1948 * buf [I] pointer to the embedded file structured as a B+ tree
1949 * cb [I] compare function
1950 * cookie [IO] cookie for cb function
1952 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
1954 unsigned magic;
1955 unsigned page_size;
1956 unsigned cur_page;
1957 unsigned level;
1958 BYTE *pages, *ptr, *newptr;
1959 int i, entries;
1961 magic = GET_USHORT(buf, 9);
1962 if (magic != 0x293B)
1964 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
1965 return;
1967 page_size = GET_USHORT(buf, 9+4);
1968 cur_page = GET_USHORT(buf, 9+26);
1969 level = GET_USHORT(buf, 9+32);
1970 pages = buf + 9 + 38;
1971 while (--level > 0)
1973 ptr = pages + cur_page*page_size;
1974 cur_page = GET_USHORT(ptr, 4);
1976 while (cur_page != 0xFFFF)
1978 ptr = pages + cur_page*page_size;
1979 entries = GET_SHORT(ptr, 2);
1980 ptr += 8;
1981 for (i = 0; i < entries; i++)
1983 cb(ptr, (void **)&newptr, cookie);
1984 ptr = newptr;
1986 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
1991 /***********************************************************************
1993 * HLPFILE_GetContext
1995 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
1997 BYTE *cbuf, *cend;
1998 unsigned clen;
2000 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2001 {WINE_WARN("context0\n"); return FALSE;}
2003 clen = cend - cbuf;
2004 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2005 if (!hlpfile->Context) return FALSE;
2006 memcpy(hlpfile->Context, cbuf, clen);
2008 return TRUE;
2011 /***********************************************************************
2013 * HLPFILE_GetKeywords
2015 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2017 BYTE *cbuf, *cend;
2018 unsigned clen;
2020 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2021 clen = cend - cbuf;
2022 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2023 if (!hlpfile->kwbtree) return FALSE;
2024 memcpy(hlpfile->kwbtree, cbuf, clen);
2026 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2028 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2029 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2030 return FALSE;
2032 clen = cend - cbuf;
2033 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2034 if (!hlpfile->kwdata)
2036 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2037 return FALSE;
2039 memcpy(hlpfile->kwdata, cbuf, clen);
2041 return TRUE;
2044 /***********************************************************************
2046 * HLPFILE_GetMap
2048 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2050 BYTE *cbuf, *cend;
2051 unsigned entries, i;
2053 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2054 {WINE_WARN("no map section\n"); return FALSE;}
2056 entries = GET_USHORT(cbuf, 9);
2057 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2058 if (!hlpfile->Map) return FALSE;
2059 hlpfile->wMapLen = entries;
2060 for (i = 0; i < entries; i++)
2062 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2063 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2065 return TRUE;
2068 /******************************************************************
2069 * HLPFILE_DeleteLink
2073 void HLPFILE_FreeLink(HLPFILE_LINK* link)
2075 if (link && !--link->wRefCount)
2076 HeapFree(GetProcessHeap(), 0, link);
2079 /***********************************************************************
2081 * HLPFILE_DeleteParagraph
2083 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
2085 HLPFILE_PARAGRAPH* next;
2087 while (paragraph)
2089 next = paragraph->next;
2091 if (paragraph->cookie == para_metafile)
2092 DeleteMetaFile(paragraph->u.gfx.u.mfp.hMF);
2094 HLPFILE_FreeLink(paragraph->link);
2096 HeapFree(GetProcessHeap(), 0, paragraph);
2097 paragraph = next;
2101 /***********************************************************************
2103 * DeleteMacro
2105 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2107 HLPFILE_MACRO* next;
2109 while (macro)
2111 next = macro->next;
2112 HeapFree(GetProcessHeap(), 0, macro);
2113 macro = next;
2117 /***********************************************************************
2119 * DeletePage
2121 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2123 HLPFILE_PAGE* next;
2125 while (page)
2127 next = page->next;
2128 HLPFILE_DeleteParagraph(page->first_paragraph);
2129 HLPFILE_DeleteMacro(page->first_macro);
2130 HeapFree(GetProcessHeap(), 0, page);
2131 page = next;
2135 /***********************************************************************
2137 * HLPFILE_FreeHlpFile
2139 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2141 unsigned i;
2143 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2145 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2146 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2147 else first_hlpfile = hlpfile->next;
2149 if (hlpfile->numFonts)
2151 for (i = 0; i < hlpfile->numFonts; i++)
2153 DeleteObject(hlpfile->fonts[i].hFont);
2155 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2158 if (hlpfile->numBmps)
2160 for (i = 0; i < hlpfile->numBmps; i++)
2162 DeleteObject(hlpfile->bmps[i]);
2164 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2167 HLPFILE_DeletePage(hlpfile->first_page);
2168 HLPFILE_DeleteMacro(hlpfile->first_macro);
2170 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2171 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2172 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2173 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2174 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2175 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2176 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2177 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2178 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2179 HeapFree(GetProcessHeap(), 0, hlpfile);