4 * Copyright 1996 Ulrich Schmid
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
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;
53 static BYTE *file_buffer;
54 static UINT file_buffer_size;
80 * Compare function type for HLPFILE_BPTreeSearch function.
83 * p [I] pointer to testing block (key + data)
84 * key [I] pointer to key value to look for
85 * leaf [I] whether this function called for index of leaf page
86 * next [O] pointer to pointer to next block
88 typedef int (*HLPFILE_BPTreeCompare)(void *p, const void *key,
89 int leaf, void **next);
91 static BOOL HLPFILE_DoReadHlpFile(HLPFILE*, LPCSTR);
92 static BOOL HLPFILE_ReadFileToBuffer(HFILE);
93 static BOOL HLPFILE_FindSubFile(LPCSTR name, BYTE**, BYTE**);
94 static BOOL HLPFILE_SystemCommands(HLPFILE*);
95 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end);
96 static BYTE* HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr);
97 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE*);
98 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE*);
99 static BOOL HLPFILE_Uncompress_Topic(HLPFILE*);
100 static BOOL HLPFILE_GetContext(HLPFILE*);
101 static BOOL HLPFILE_GetMap(HLPFILE*);
102 static BOOL HLPFILE_AddPage(HLPFILE*, BYTE*, BYTE*, unsigned);
103 static BOOL HLPFILE_AddParagraph(HLPFILE*, BYTE *, BYTE*, unsigned*);
104 static void HLPFILE_Uncompress2(const BYTE*, const BYTE*, BYTE*, const BYTE*);
105 static BOOL HLPFILE_Uncompress3(char*, const char*, const BYTE*, const BYTE*);
106 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz);
107 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile);
109 static void* HLPFILE_BPTreeSearch(BYTE*, const void*, HLPFILE_BPTreeCompare);
111 /***********************************************************************
113 * HLPFILE_PageByNumber
115 static HLPFILE_PAGE *HLPFILE_PageByNumber(HLPFILE* hlpfile, UINT wNum)
120 WINE_TRACE("<%s>[%u]\n", hlpfile->lpszPath, wNum);
122 for (page = hlpfile->first_page; page && temp; page = page->next) temp--;
124 WINE_ERR("Page of number %u not found in file %s\n", wNum, hlpfile->lpszPath);
129 * this finds the page containing the offset. The offset can either
130 * refer to the top of the page (offset == page->offset), or
131 * to some paragraph inside the page...
132 * As of today, we only return the page... we should also return
133 * a paragraph, and then, while opening a new page, compute the
134 * y-offset of the paragraph to be shown and scroll the window
137 /******************************************************************
138 * HLPFILE_PageByOffset
142 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset)
147 if (!hlpfile) return 0;
149 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, offset);
151 if (offset == 0xFFFFFFFF) return NULL;
154 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
156 if (page->offset <= offset && (!found || found->offset < page->offset))
160 WINE_ERR("Page of offset %u not found in file %s\n",
161 offset, hlpfile->lpszPath);
165 /**************************************************************************
168 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
171 static int comp_PageByHash(void *p, const void *key,
172 int leaf, void** next)
174 LONG lKey = (LONG)key;
175 LONG lTest = GET_UINT(p, 0);
177 *next = (char *)p+(leaf?8:6);
178 WINE_TRACE("Comparing '%u' with '%u'\n", lKey, lTest);
179 if (lTest < lKey) return -1;
180 if (lTest > lKey) return 1;
184 /***********************************************************************
186 * HLPFILE_HlpFilePageByHash
188 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash)
192 if (!hlpfile) return 0;
194 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lHash);
196 /* For win 3.0 files hash values are really page numbers */
197 if (hlpfile->version <= 16)
198 return HLPFILE_PageByNumber(hlpfile, lHash);
200 ptr = HLPFILE_BPTreeSearch(hlpfile->Context, (void*)lHash, comp_PageByHash);
203 WINE_ERR("Page of hash %x not found in file %s\n", lHash, hlpfile->lpszPath);
207 return HLPFILE_PageByOffset(hlpfile, GET_UINT(ptr, 4));
210 /***********************************************************************
214 HLPFILE_PAGE *HLPFILE_PageByMap(HLPFILE* hlpfile, LONG lMap)
218 if (!hlpfile) return 0;
220 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lMap);
222 for (i = 0; i < hlpfile->wMapLen; i++)
224 if (hlpfile->Map[i].lMap == lMap)
225 return HLPFILE_PageByOffset(hlpfile, hlpfile->Map[i].offset);
228 WINE_ERR("Page of Map %x not found in file %s\n", lMap, hlpfile->lpszPath);
232 /***********************************************************************
236 HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile)
238 HLPFILE_PAGE* page = NULL;
240 if (!hlpfile) return NULL;
242 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start);
243 if (!page) page = hlpfile->first_page;
247 /***********************************************************************
251 LONG HLPFILE_Hash(LPCSTR lpszContext)
256 while ((c = *lpszContext++))
259 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
260 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
261 if (c >= '1' && c <= '9') x = c - '0';
262 if (c == '0') x = 10;
263 if (c == '.') x = 12;
264 if (c == '_') x = 13;
265 if (x) lHash = lHash * 43 + x;
270 /***********************************************************************
272 * HLPFILE_ReadHlpFile
274 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
278 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
280 if (!strcmp(lpszPath, hlpfile->lpszPath))
282 hlpfile->wRefCount++;
287 hlpfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
288 sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
289 if (!hlpfile) return 0;
291 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
292 hlpfile->contents_start = 0xFFFFFFFF;
293 hlpfile->next = first_hlpfile;
294 hlpfile->wRefCount = 1;
296 strcpy(hlpfile->lpszPath, lpszPath);
298 first_hlpfile = hlpfile;
299 if (hlpfile->next) hlpfile->next->prev = hlpfile;
301 phrases.offsets = NULL;
302 phrases.buffer = NULL;
307 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
309 HLPFILE_FreeHlpFile(hlpfile);
313 HeapFree(GetProcessHeap(), 0, phrases.offsets);
314 HeapFree(GetProcessHeap(), 0, phrases.buffer);
315 HeapFree(GetProcessHeap(), 0, topic.map);
316 HeapFree(GetProcessHeap(), 0, file_buffer);
321 /***********************************************************************
323 * HLPFILE_DoReadHlpFile
325 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
332 unsigned index, old_index, offset, len, offs;
334 hFile = OpenFile(lpszPath, &ofs, OF_READ);
335 if (hFile == HFILE_ERROR) return FALSE;
337 ret = HLPFILE_ReadFileToBuffer(hFile);
339 if (!ret) return FALSE;
341 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
343 /* load phrases support */
344 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
345 HLPFILE_Uncompress_Phrases40(hlpfile);
347 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
348 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
357 if (hlpfile->version <= 16)
359 index = (ref - 0x0C) / hlpfile->dsize;
360 offset = (ref - 0x0C) % hlpfile->dsize;
364 index = (ref - 0x0C) >> 14;
365 offset = (ref - 0x0C) & 0x3FFF;
368 if (hlpfile->version <= 16 && index != old_index && index != 0)
370 /* we jumped to the next block, adjust pointers */
375 WINE_TRACE("ref=%08x => [%u/%u]\n", ref, index, offset);
377 if (index >= topic.wMapLen) {WINE_WARN("maplen\n"); break;}
378 buf = topic.map[index] + offset;
379 if (buf + 0x15 >= topic.end) {WINE_WARN("extra\n"); break;}
380 end = min(buf + GET_UINT(buf, 0), topic.end);
381 if (index != old_index) {offs = 0; old_index = index;}
386 if (!HLPFILE_AddPage(hlpfile, buf, end, index * 0x8000L + offs)) return FALSE;
392 if (!HLPFILE_AddParagraph(hlpfile, buf, end, &len)) return FALSE;
397 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
400 if (hlpfile->version <= 16)
402 ref += GET_UINT(buf, 0xc);
403 if (GET_UINT(buf, 0xc) == 0)
407 ref = GET_UINT(buf, 0xc);
408 } while (ref != 0xffffffff);
410 HLPFILE_GetMap(hlpfile);
411 if (hlpfile->version <= 16) return TRUE;
412 return HLPFILE_GetContext(hlpfile);
415 /***********************************************************************
419 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned offset)
423 UINT titlesize, blocksize, datalen;
427 blocksize = GET_UINT(buf, 0);
428 datalen = GET_UINT(buf, 0x10);
429 title = buf + datalen;
430 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
432 titlesize = GET_UINT(buf, 4);
433 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
434 if (!page) return FALSE;
435 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
437 if (titlesize > blocksize - datalen)
439 /* need to decompress */
440 if (hlpfile->hasPhrases)
441 HLPFILE_Uncompress2(title, end, (BYTE*)page->lpszTitle, (BYTE*)page->lpszTitle + titlesize);
442 else if (hlpfile->hasPhrases40)
443 HLPFILE_Uncompress3(page->lpszTitle, page->lpszTitle + titlesize, title, end);
446 WINE_FIXME("Text size is too long, splitting\n");
447 titlesize = blocksize - datalen;
448 memcpy(page->lpszTitle, title, titlesize);
452 memcpy(page->lpszTitle, title, titlesize);
454 page->lpszTitle[titlesize] = '\0';
456 if (hlpfile->first_page)
460 for (p = hlpfile->first_page; p->next; p = p->next);
466 hlpfile->first_page = page;
470 page->file = hlpfile;
472 page->first_paragraph = NULL;
473 page->first_macro = NULL;
474 page->wNumber = GET_UINT(buf, 0x21);
475 page->offset = offset;
477 page->browse_bwd = GET_UINT(buf, 0x19);
478 page->browse_fwd = GET_UINT(buf, 0x1D);
480 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
481 page->wNumber, page->lpszTitle,
482 page->browse_bwd, page->offset, page->browse_fwd);
484 memset(&attributes, 0, sizeof(attributes));
486 /* now load macros */
487 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
488 while (ptr < page->lpszTitle + titlesize)
490 unsigned len = strlen(ptr);
493 WINE_TRACE("macro: %s\n", ptr);
494 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
495 macro->lpszMacro = macro_str = (char*)(macro + 1);
496 memcpy(macro_str, ptr, len + 1);
497 /* FIXME: shall we really link macro in reverse order ??
498 * may produce strange results when played at page opening
500 macro->next = page->first_macro;
501 page->first_macro = macro;
508 static long fetch_long(BYTE** ptr)
514 ret = (*(unsigned long*)(*ptr) - 0x80000000L) / 2;
519 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
526 static unsigned long fetch_ulong(BYTE** ptr)
532 ret = *(unsigned long*)(*ptr) / 2;
537 ret = *(unsigned short*)(*ptr) / 2;
543 static short fetch_short(BYTE** ptr)
549 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
554 ret = (*(unsigned char*)(*ptr) - 0x80) / 2;
560 static unsigned short fetch_ushort(BYTE** ptr)
566 ret = *(unsigned short*)(*ptr) / 2;
571 ret = *(unsigned char*)(*ptr) / 2;
577 /******************************************************************
578 * HLPFILE_DecompressGfx
580 * Decompress the data part of a bitmap or a metafile
582 static BYTE* HLPFILE_DecompressGfx(BYTE* src, unsigned csz, unsigned sz, BYTE packing)
589 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
593 case 0: /* uncompressed */
595 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
599 tmp = dst = HeapAlloc(GetProcessHeap(), 0, sz);
600 if (!dst) return NULL;
601 HLPFILE_UncompressRLE(src, src + csz, &tmp, sz);
603 WINE_WARN("Bogus gfx sizes (RunLen): %u/%u\n", tmp - dst, sz);
606 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
607 dst = HeapAlloc(GetProcessHeap(), 0, sz77);
608 if (!dst) return NULL;
609 HLPFILE_UncompressLZ77(src, src + csz, dst);
611 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
613 case 3: /* LZ77 then RLE */
614 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
615 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
616 if (!tmp) return FALSE;
617 HLPFILE_UncompressLZ77(src, src + csz, tmp);
618 dst = tmp2 = HeapAlloc(GetProcessHeap(), 0, sz);
621 HeapFree(GetProcessHeap(), 0, tmp);
624 HLPFILE_UncompressRLE(tmp, tmp + sz77, &tmp2, sz);
625 if (tmp2 - dst != sz)
626 WINE_WARN("Bogus gfx sizes (LZ77+RunLen): %u / %u\n", tmp2 - dst, sz);
627 HeapFree(GetProcessHeap(), 0, tmp);
630 WINE_FIXME("Unsupported packing %u\n", packing);
636 /******************************************************************
641 static BOOL HLPFILE_LoadBitmap(BYTE* beg, BYTE type, BYTE pack,
642 HLPFILE_PARAGRAPH* paragraph)
647 unsigned long off, csz;
650 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
651 if (!bi) return FALSE;
653 ptr = beg + 2; /* for type and pack */
655 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
656 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
657 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
658 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
659 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
660 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
661 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
662 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
663 bi->bmiHeader.biClrImportant = fetch_ulong(&ptr);
664 bi->bmiHeader.biCompression = BI_RGB;
665 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
666 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
667 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
668 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
669 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
670 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
672 csz = fetch_ulong(&ptr);
673 fetch_ulong(&ptr); /* hotspot size */
675 off = GET_UINT(ptr, 0); ptr += 4;
676 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
678 /* now read palette info */
681 unsigned nc = bi->bmiHeader.biClrUsed;
684 /* not quite right, especially for bitfields type of compression */
685 if (!nc && bi->bmiHeader.biBitCount <= 8)
686 nc = 1 << bi->bmiHeader.biBitCount;
688 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
689 if (!bi) return FALSE;
690 for (i = 0; i < nc; i++)
692 bi->bmiColors[i].rgbBlue = ptr[0];
693 bi->bmiColors[i].rgbGreen = ptr[1];
694 bi->bmiColors[i].rgbRed = ptr[2];
695 bi->bmiColors[i].rgbReserved = 0;
699 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
701 paragraph->u.gfx.u.bmp.hBitmap = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
705 if (!paragraph->u.gfx.u.bmp.hBitmap)
706 WINE_ERR("Couldn't create bitmap\n");
708 HeapFree(GetProcessHeap(), 0, bi);
709 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
714 /******************************************************************
715 * HLPFILE_LoadMetaFile
719 static BOOL HLPFILE_LoadMetaFile(BYTE* beg, BYTE pack, HLPFILE_PARAGRAPH* paragraph)
722 unsigned long size, csize;
723 unsigned long off, hsoff;
725 LPMETAFILEPICT lpmfp;
727 WINE_TRACE("Loading metafile\n");
729 ptr = beg + 2; /* for type and pack */
731 lpmfp = ¶graph->u.gfx.u.mfp;
732 lpmfp->mm = fetch_ushort(&ptr); /* mapping mode */
734 lpmfp->xExt = GET_USHORT(ptr, 0);
735 lpmfp->yExt = GET_USHORT(ptr, 2);
738 size = fetch_ulong(&ptr); /* decompressed size */
739 csize = fetch_ulong(&ptr); /* compressed size */
740 fetch_ulong(&ptr); /* hotspot size */
741 off = GET_UINT(ptr, 0);
742 hsoff = GET_UINT(ptr, 4);
745 WINE_TRACE("sz=%lu csz=%lu (%d,%d) offs=%lu/%u,%lu\n",
746 size, csize, lpmfp->xExt, lpmfp->yExt, off, ptr - beg, hsoff);
748 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
749 if (!bits) return FALSE;
751 paragraph->cookie = para_metafile;
753 lpmfp->hMF = SetMetaFileBitsEx(size, bits);
756 WINE_FIXME("Couldn't load metafile\n");
758 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
763 /******************************************************************
764 * HLPFILE_LoadGfxByAddr
768 static BOOL HLPFILE_LoadGfxByAddr(HLPFILE *hlpfile, BYTE* ref,
770 HLPFILE_PARAGRAPH* paragraph)
774 numpict = GET_USHORT(ref, 2);
775 WINE_TRACE("Got picture magic=%04x #=%d\n",
776 GET_USHORT(ref, 0), numpict);
778 for (i = 0; i < numpict; i++)
784 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
785 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
792 case 5: /* device dependent bmp */
793 case 6: /* device independent bmp */
794 HLPFILE_LoadBitmap(beg, type, pack, paragraph);
797 HLPFILE_LoadMetaFile(beg, pack, paragraph);
799 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
802 /* FIXME: hotspots */
804 /* FIXME: implement support for multiple picture format */
805 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
811 /******************************************************************
812 * HLPFILE_LoadGfxByIndex
816 static BOOL HLPFILE_LoadGfxByIndex(HLPFILE *hlpfile, unsigned index,
817 HLPFILE_PARAGRAPH* paragraph)
823 WINE_TRACE("Loading picture #%d\n", index);
825 if (index < hlpfile->numBmps && hlpfile->bmps[index] != NULL)
827 paragraph->u.gfx.u.bmp.hBitmap = hlpfile->bmps[index];
831 sprintf(tmp, "|bm%u", index);
833 if (!HLPFILE_FindSubFile(tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
837 ret = HLPFILE_LoadGfxByAddr(hlpfile, ref, end - ref, paragraph);
840 if (ret && paragraph->cookie == para_bitmap)
842 if (index >= hlpfile->numBmps)
844 hlpfile->numBmps = index + 1;
846 hlpfile->bmps = HeapReAlloc(GetProcessHeap(), 0, hlpfile->bmps,
847 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
849 hlpfile->bmps = HeapAlloc(GetProcessHeap(), 0,
850 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
853 hlpfile->bmps[index] = paragraph->u.gfx.u.bmp.hBitmap;
858 /******************************************************************
863 static HLPFILE_LINK* HLPFILE_AllocLink(int cookie, const char* str, LONG hash,
864 BOOL clrChange, unsigned wnd)
869 /* FIXME: should build a string table for the attributes.link.lpszPath
870 * they are reallocated for each link
872 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + strlen(str) + 1);
873 if (!link) return NULL;
875 link->cookie = cookie;
876 link->lpszString = link_str = (char*)link + sizeof(HLPFILE_LINK);
877 strcpy(link_str, str);
879 link->bClrChange = clrChange ? 1 : 0;
883 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
884 link->cookie, link->lpszString,
885 link->lHash, link->window);
889 /***********************************************************************
891 * HLPFILE_AddParagraph
893 static BOOL HLPFILE_AddParagraph(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned* len)
896 HLPFILE_PARAGRAPH *paragraph, **paragraphptr;
898 BYTE *format, *format_end;
899 char *text, *text_base, *text_end;
900 long size, blocksize, datalen;
902 unsigned nc, ncol = 1;
904 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
906 for (page = hlpfile->first_page; page->next; page = page->next) /* Nothing */;
907 for (paragraphptr = &page->first_paragraph; *paragraphptr;
908 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
910 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
912 blocksize = GET_UINT(buf, 0);
913 size = GET_UINT(buf, 0x4);
914 datalen = GET_UINT(buf, 0x10);
915 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
916 if (!text) return FALSE;
917 if (size > blocksize - datalen)
919 /* need to decompress */
920 if (hlpfile->hasPhrases)
921 HLPFILE_Uncompress2(buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
922 else if (hlpfile->hasPhrases40)
923 HLPFILE_Uncompress3(text, text + size, buf + datalen, end);
926 WINE_FIXME("Text size is too long, splitting\n");
927 size = blocksize - datalen;
928 memcpy(text, buf + datalen, size);
932 memcpy(text, buf + datalen, size);
934 text_end = text + size;
937 format_end = buf + GET_UINT(buf, 0x10);
939 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
942 *len = fetch_ushort(&format);
944 else *len = end-buf-15;
946 if (buf[0x14] == 0x23)
952 WINE_TRACE("#cols %u\n", ncol);
954 if (type == 0 || type == 2)
959 for (nc = 0; nc < ncol; nc++)
961 WINE_TRACE("looking for format at offset %u for column %d\n", format - (buf + 0x15), nc);
962 if (buf[0x14] == 0x23)
964 if (buf[0x14] == 0x01)
968 bits = GET_USHORT(format, 0); format += 2;
969 if (bits & 0x0001) fetch_long(&format);
970 if (bits & 0x0002) fetch_short(&format);
971 if (bits & 0x0004) fetch_short(&format);
972 if (bits & 0x0008) fetch_short(&format);
973 if (bits & 0x0010) fetch_short(&format);
974 if (bits & 0x0020) fetch_short(&format);
975 if (bits & 0x0040) fetch_short(&format);
976 if (bits & 0x0100) format += 3;
979 int ntab = fetch_short(&format);
984 ts = fetch_ushort(&format);
985 if (ts & 0x4000) fetch_ushort(&format);
988 /* 0x0400, 0x0800 and 0x1000 don't need space */
989 if ((bits & 0xE080) != 0)
990 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
992 while (text < text_end && format < format_end)
994 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
995 textsize = strlen(text) + 1;
998 paragraph = HeapAlloc(GetProcessHeap(), 0,
999 sizeof(HLPFILE_PARAGRAPH) + textsize);
1000 if (!paragraph) return FALSE;
1001 *paragraphptr = paragraph;
1002 paragraphptr = ¶graph->next;
1004 paragraph->next = NULL;
1005 paragraph->link = attributes.link;
1006 if (paragraph->link) paragraph->link->wRefCount++;
1007 paragraph->cookie = para_normal_text;
1008 paragraph->u.text.wFont = attributes.wFont;
1009 paragraph->u.text.wVSpace = attributes.wVSpace;
1010 paragraph->u.text.wHSpace = attributes.wHSpace;
1011 paragraph->u.text.wIndent = attributes.wIndent;
1012 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
1013 strcpy(paragraph->u.text.lpszText, text);
1015 attributes.wVSpace = 0;
1016 attributes.wHSpace = 0;
1018 /* else: null text, keep on storing attributes */
1021 if (*format == 0xff)
1027 WINE_TRACE("format=%02x\n", *format);
1031 WINE_FIXME("NIY20\n");
1036 WINE_FIXME("NIY21\n");
1041 attributes.wFont = GET_USHORT(format, 1);
1042 WINE_TRACE("Changing font to %d\n", attributes.wFont);
1047 attributes.wVSpace++;
1052 attributes.wVSpace++;
1053 attributes.wIndent = 0;
1058 attributes.wIndent++;
1072 BYTE pos = (*format - 0x86);
1073 BYTE type = format[1];
1077 size = fetch_long(&format);
1079 paragraph = HeapAlloc(GetProcessHeap(), 0,
1080 sizeof(HLPFILE_PARAGRAPH) + textsize);
1081 if (!paragraph) return FALSE;
1082 *paragraphptr = paragraph;
1083 paragraphptr = ¶graph->next;
1085 paragraph->next = NULL;
1086 paragraph->link = attributes.link;
1087 if (paragraph->link) paragraph->link->wRefCount++;
1088 paragraph->cookie = para_bitmap;
1089 paragraph->u.gfx.pos = pos;
1093 fetch_ushort(&format); /* hot spot */
1096 switch (GET_SHORT(format, 0))
1099 HLPFILE_LoadGfxByIndex(hlpfile, GET_SHORT(format, 2),
1103 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1104 GET_SHORT(format, 0),
1105 size, GET_SHORT(format, 2));
1106 HLPFILE_LoadGfxByAddr(hlpfile, format + 2, size - 4,
1110 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1115 WINE_FIXME("Got an embedded element %s\n", format + 6);
1118 WINE_FIXME("Got a type %d picture\n", type);
1121 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1128 HLPFILE_FreeLink(attributes.link);
1129 attributes.link = NULL;
1135 WINE_FIXME("NIY non-break space/hyphen\n");
1147 WINE_TRACE("macro => %s\n", format + 3);
1148 HLPFILE_FreeLink(attributes.link);
1149 attributes.link = HLPFILE_AllocLink(hlp_link_macro, (const char*)format + 3,
1150 0, !(*format & 4), -1);
1151 format += 3 + GET_USHORT(format, 1);
1156 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1157 HLPFILE_FreeLink(attributes.link);
1158 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1160 GET_UINT(format, 1)-16,
1171 HLPFILE_FreeLink(attributes.link);
1172 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1174 GET_UINT(format, 1),
1175 !(*format & 4), -1);
1184 char* ptr = (char*) format + 8;
1185 BYTE type = format[3];
1189 if (type == 1) wnd = *ptr++;
1190 if (type == 4 || type == 6)
1193 ptr += strlen(ptr) + 1;
1196 str = hlpfile->lpszPath;
1199 for (wnd = hlpfile->numWindows - 1; wnd >= 0; wnd--)
1201 if (!strcmp(ptr, hlpfile->windows[wnd].name)) break;
1204 WINE_WARN("Couldn't find window info for %s\n", ptr);
1206 HLPFILE_FreeLink(attributes.link);
1207 attributes.link = HLPFILE_AllocLink((*format & 4) ? hlp_link_link : hlp_link_popup,
1208 str, GET_UINT(format, 4),
1209 !(*format & 1), wnd);
1211 format += 3 + GET_USHORT(format, 1);
1215 WINE_WARN("format %02x\n", *format);
1220 HeapFree(GetProcessHeap(), 0, text_base);
1224 /******************************************************************
1229 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1232 unsigned i, len, idx;
1233 unsigned face_num, dscr_num, face_offset, dscr_offset;
1236 if (!HLPFILE_FindSubFile("|FONT", &ref, &end))
1238 WINE_WARN("no subfile FONT\n");
1239 hlpfile->numFonts = 0;
1240 hlpfile->fonts = NULL;
1246 face_num = GET_USHORT(ref, 0);
1247 dscr_num = GET_USHORT(ref, 2);
1248 face_offset = GET_USHORT(ref, 4);
1249 dscr_offset = GET_USHORT(ref, 6);
1251 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1252 face_num, face_offset, dscr_num, dscr_offset);
1254 hlpfile->numFonts = dscr_num;
1255 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1257 len = (dscr_offset - face_offset) / face_num;
1258 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1259 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1260 for (i = 0; i < dscr_num; i++)
1262 flag = ref[dscr_offset + i * 11 + 0];
1263 family = ref[dscr_offset + i * 11 + 2];
1265 hlpfile->fonts[i].LogFont.lfHeight = -ref[dscr_offset + i * 11 + 1] / 2 - 3;
1266 hlpfile->fonts[i].LogFont.lfWidth = 0;
1267 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1268 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1269 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1270 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1271 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1272 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1273 hlpfile->fonts[i].LogFont.lfCharSet = DEFAULT_CHARSET;
1274 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1275 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1276 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1277 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1281 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1282 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1283 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1284 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1285 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1286 default: WINE_FIXME("Unknown family %u\n", family);
1288 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1292 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1293 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1297 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1298 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1300 hlpfile->fonts[i].hFont = 0;
1301 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1302 ref[dscr_offset + i * 11 + 6],
1303 ref[dscr_offset + i * 11 + 7]);
1304 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1305 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1311 X(4, "dblUnderline"),
1313 ref[dscr_offset + i * 11 + 1],
1315 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1316 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1321 /***********************************************************************
1323 * HLPFILE_ReadFileToBuffer
1325 static BOOL HLPFILE_ReadFileToBuffer(HFILE hFile)
1327 BYTE header[16], dummy[1];
1329 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1332 if (GET_UINT(header, 0) != 0x00035F3F)
1333 {WINE_WARN("wrong header\n"); return FALSE;};
1335 file_buffer_size = GET_UINT(header, 12);
1336 file_buffer = HeapAlloc(GetProcessHeap(), 0, file_buffer_size + 1);
1337 if (!file_buffer) return FALSE;
1339 memcpy(file_buffer, header, 16);
1340 if (_hread(hFile, file_buffer + 16, file_buffer_size - 16) != file_buffer_size - 16)
1341 {WINE_WARN("filesize1\n"); return FALSE;};
1343 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1345 file_buffer[file_buffer_size] = '\0'; /* FIXME: was '0', sounds ackward to me */
1350 /**************************************************************************
1353 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1356 static int comp_FindSubFile(void *p, const void *key,
1357 int leaf, void** next)
1359 *next = (char *)p+strlen(p)+(leaf?5:3);
1360 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1361 return strcmp(p, key);
1364 /***********************************************************************
1366 * HLPFILE_FindSubFile
1368 static BOOL HLPFILE_FindSubFile(LPCSTR name, BYTE **subbuf, BYTE **subend)
1372 WINE_TRACE("looking for file '%s'\n", name);
1373 ptr = HLPFILE_BPTreeSearch(file_buffer + GET_UINT(file_buffer, 4),
1374 name, comp_FindSubFile);
1375 if (!ptr) return FALSE;
1376 *subbuf = file_buffer + GET_UINT(ptr, strlen(name)+1);
1377 if (*subbuf >= file_buffer + file_buffer_size)
1379 WINE_ERR("internal file %s does not fit\n", name);
1382 *subend = *subbuf + GET_UINT(*subbuf, 0);
1383 if (*subend > file_buffer + file_buffer_size)
1385 WINE_ERR("internal file %s does not fit\n", name);
1388 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1390 WINE_ERR("invalid size provided for internal file %s\n", name);
1396 /***********************************************************************
1398 * HLPFILE_SystemCommands
1400 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1402 BYTE *buf, *ptr, *end;
1403 HLPFILE_MACRO *macro, **m;
1405 unsigned short magic, minor, major, flags;
1407 hlpfile->lpszTitle = NULL;
1409 if (!HLPFILE_FindSubFile("|SYSTEM", &buf, &end)) return FALSE;
1411 magic = GET_USHORT(buf + 9, 0);
1412 minor = GET_USHORT(buf + 9, 2);
1413 major = GET_USHORT(buf + 9, 4);
1414 /* gen date on 4 bytes */
1415 flags = GET_USHORT(buf + 9, 10);
1416 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1417 magic, major, minor, flags);
1418 if (magic != 0x036C || major != 1)
1419 {WINE_WARN("Wrong system header\n"); return FALSE;}
1422 hlpfile->tbsize = 0x800;
1423 hlpfile->compressed = 0;
1425 else if (flags == 0)
1427 hlpfile->tbsize = 0x1000;
1428 hlpfile->compressed = 0;
1430 else if (flags == 4)
1432 hlpfile->tbsize = 0x1000;
1433 hlpfile->compressed = 1;
1437 hlpfile->tbsize = 0x800;
1438 hlpfile->compressed = 1;
1441 if (hlpfile->compressed)
1442 hlpfile->dsize = 0x4000;
1444 hlpfile->dsize = hlpfile->tbsize - 0x0C;
1446 hlpfile->version = minor;
1447 hlpfile->flags = flags;
1449 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1451 char *str = (char*) ptr + 4;
1452 switch (GET_USHORT(ptr, 0))
1455 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1456 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1457 if (!hlpfile->lpszTitle) return FALSE;
1458 lstrcpy(hlpfile->lpszTitle, str);
1459 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1463 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1464 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1465 if (!hlpfile->lpszCopyright) return FALSE;
1466 lstrcpy(hlpfile->lpszCopyright, str);
1467 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1471 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1472 hlpfile->contents_start = GET_UINT(ptr, 4);
1473 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1477 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1479 p = (char*)macro + sizeof(HLPFILE_MACRO);
1481 macro->lpszMacro = p;
1483 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1488 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1490 if (hlpfile->windows)
1491 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1492 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1494 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1495 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1497 if (hlpfile->windows)
1499 unsigned flags = GET_USHORT(ptr, 4);
1500 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1502 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1503 else wi->type[0] = '\0';
1504 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1505 else wi->name[0] = '\0';
1506 if (flags & 0x0004) strcpy(wi->caption, &str[23]);
1507 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1508 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1509 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1510 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1511 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1512 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1513 wi->win_style = WS_OVERLAPPEDWINDOW;
1514 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1515 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1516 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",
1517 flags & 0x0001 ? 'T' : 't',
1518 flags & 0x0002 ? 'N' : 'n',
1519 flags & 0x0004 ? 'C' : 'c',
1520 flags & 0x0008 ? 'X' : 'x',
1521 flags & 0x0010 ? 'Y' : 'y',
1522 flags & 0x0020 ? 'W' : 'w',
1523 flags & 0x0040 ? 'H' : 'h',
1524 flags & 0x0080 ? 'S' : 's',
1525 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1526 wi->size.cx, wi->size.cy);
1530 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1533 if (!hlpfile->lpszTitle)
1534 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
1538 /***********************************************************************
1540 * HLPFILE_UncompressedLZ77_Size
1542 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
1549 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1553 int code = GET_USHORT(ptr, 0);
1554 int len = 3 + (code >> 12);
1558 else newsize++, ptr++;
1565 /***********************************************************************
1567 * HLPFILE_UncompressLZ77
1569 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
1576 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1580 int code = GET_USHORT(ptr, 0);
1581 int len = 3 + (code >> 12);
1582 int offset = code & 0xfff;
1584 * We must copy byte-by-byte here. We cannot use memcpy nor
1585 * memmove here. Just example:
1586 * a[]={1,2,3,4,5,6,7,8,9,10}
1590 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
1592 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
1595 else *newptr++ = *ptr++;
1602 /***********************************************************************
1604 * HLPFILE_UncompressLZ77_Phrases
1606 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
1608 UINT i, num, dec_size, head_size;
1611 if (!HLPFILE_FindSubFile("|Phrases", &buf, &end)) return FALSE;
1613 if (hlpfile->version <= 16)
1618 num = phrases.num = GET_USHORT(buf, 9);
1619 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
1621 if (hlpfile->version <= 16)
1622 dec_size = end - buf - 15 - 2 * num;
1624 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
1626 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1627 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1628 if (!phrases.offsets || !phrases.buffer) return FALSE;
1630 for (i = 0; i <= num; i++)
1631 phrases.offsets[i] = GET_USHORT(buf, head_size + 2 * i) - 2 * num - 2;
1633 if (hlpfile->version <= 16)
1634 memcpy(phrases.buffer, buf + 15 + 2*num, dec_size);
1636 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)phrases.buffer);
1638 hlpfile->hasPhrases = TRUE;
1642 /***********************************************************************
1644 * HLPFILE_Uncompress_Phrases40
1646 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
1649 INT dec_size, cpr_size;
1650 BYTE *buf_idx, *end_idx;
1651 BYTE *buf_phs, *end_phs;
1652 long* ptr, mask = 0;
1654 unsigned short bc, n;
1656 if (!HLPFILE_FindSubFile("|PhrIndex", &buf_idx, &end_idx) ||
1657 !HLPFILE_FindSubFile("|PhrImage", &buf_phs, &end_phs)) return FALSE;
1659 ptr = (long*)(buf_idx + 9 + 28);
1660 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
1661 num = phrases.num = GET_USHORT(buf_idx, 9 + 4);
1663 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
1664 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
1665 GET_UINT(buf_idx, 9 + 0),
1666 GET_UINT(buf_idx, 9 + 4),
1667 GET_UINT(buf_idx, 9 + 8),
1668 GET_UINT(buf_idx, 9 + 12),
1669 GET_UINT(buf_idx, 9 + 16),
1670 GET_UINT(buf_idx, 9 + 20),
1671 GET_USHORT(buf_idx, 9 + 24),
1672 GET_USHORT(buf_idx, 9 + 26));
1674 dec_size = GET_UINT(buf_idx, 9 + 12);
1675 cpr_size = GET_UINT(buf_idx, 9 + 16);
1677 if (dec_size != cpr_size &&
1678 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
1680 WINE_WARN("size mismatch %u %u\n",
1681 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1682 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1685 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1686 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1687 if (!phrases.offsets || !phrases.buffer) return FALSE;
1689 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
1691 phrases.offsets[0] = 0;
1692 for (i = 0; i < num; i++)
1694 for (n = 1; getbit(); n += 1 << bc);
1696 if (bc > 1 && getbit()) n += 2;
1697 if (bc > 2 && getbit()) n += 4;
1698 if (bc > 3 && getbit()) n += 8;
1699 if (bc > 4 && getbit()) n += 16;
1700 phrases.offsets[i + 1] = phrases.offsets[i] + n;
1704 if (dec_size == cpr_size)
1705 memcpy(phrases.buffer, buf_phs + 9, dec_size);
1707 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)phrases.buffer);
1709 hlpfile->hasPhrases40 = TRUE;
1713 /***********************************************************************
1715 * HLPFILE_Uncompress_Topic
1717 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
1719 BYTE *buf, *ptr, *end, *newptr;
1720 unsigned int i, newsize = 0;
1721 unsigned int topic_size;
1723 if (!HLPFILE_FindSubFile("|TOPIC", &buf, &end))
1724 {WINE_WARN("topic0\n"); return FALSE;}
1726 buf += 9; /* Skip file header */
1727 topic_size = end - buf;
1728 if (hlpfile->compressed)
1730 topic.wMapLen = (topic_size - 1) / hlpfile->tbsize + 1;
1732 for (i = 0; i < topic.wMapLen; i++)
1734 ptr = buf + i * hlpfile->tbsize;
1736 /* I don't know why, it's necessary for printman.hlp */
1737 if (ptr + 0x44 > end) ptr = end - 0x44;
1739 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
1742 topic.map = HeapAlloc(GetProcessHeap(), 0,
1743 topic.wMapLen * sizeof(topic.map[0]) + newsize);
1744 if (!topic.map) return FALSE;
1745 newptr = (BYTE*)(topic.map + topic.wMapLen);
1746 topic.end = newptr + newsize;
1748 for (i = 0; i < topic.wMapLen; i++)
1750 ptr = buf + i * hlpfile->tbsize;
1751 if (ptr + 0x44 > end) ptr = end - 0x44;
1753 topic.map[i] = newptr;
1754 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
1759 /* basically, we need to copy the TopicBlockSize byte pages
1760 * (removing the first 0x0C) in one single area in memory
1762 topic.wMapLen = (topic_size - 1) / hlpfile->tbsize + 1;
1763 topic.map = HeapAlloc(GetProcessHeap(), 0,
1764 topic.wMapLen * (sizeof(topic.map[0]) + hlpfile->dsize));
1765 if (!topic.map) return FALSE;
1766 newptr = (BYTE*)(topic.map + topic.wMapLen);
1767 topic.end = newptr + topic_size;
1769 for (i = 0; i < topic.wMapLen; i++)
1771 topic.map[i] = newptr + i * hlpfile->dsize;
1772 memcpy(topic.map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
1778 /***********************************************************************
1780 * HLPFILE_Uncompress2
1783 static void HLPFILE_Uncompress2(const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
1785 BYTE *phptr, *phend;
1789 while (ptr < end && newptr < newend)
1791 if (!*ptr || *ptr >= 0x10)
1795 code = 0x100 * ptr[0] + ptr[1];
1796 index = (code - 0x100) / 2;
1798 phptr = (BYTE*)phrases.buffer + phrases.offsets[index];
1799 phend = (BYTE*)phrases.buffer + phrases.offsets[index + 1];
1801 if (newptr + (phend - phptr) > newend)
1803 WINE_FIXME("buffer overflow %p > %p for %d bytes\n",
1804 newptr, newend, phend - phptr);
1807 memcpy(newptr, phptr, phend - phptr);
1808 newptr += phend - phptr;
1809 if (code & 1) *newptr++ = ' ';
1814 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
1817 /******************************************************************
1818 * HLPFILE_Uncompress3
1822 static BOOL HLPFILE_Uncompress3(char* dst, const char* dst_end,
1823 const BYTE* src, const BYTE* src_end)
1825 unsigned int idx, len;
1827 for (; src < src_end; src++)
1829 if ((*src & 1) == 0)
1832 if (idx > phrases.num)
1834 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1839 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1840 if (dst + len <= dst_end)
1841 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1844 else if ((*src & 0x03) == 0x01)
1846 idx = (*src + 1) * 64;
1848 if (idx > phrases.num)
1850 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1855 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1856 if (dst + len <= dst_end)
1857 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1860 else if ((*src & 0x07) == 0x03)
1862 len = (*src / 8) + 1;
1863 if (dst + len <= dst_end)
1864 memcpy(dst, src + 1, len);
1869 len = (*src / 16) + 1;
1870 if (dst + len <= dst_end)
1871 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
1876 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
1880 /******************************************************************
1881 * HLPFILE_UncompressRLE
1885 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
1888 BYTE* sdst = *dst + dstsz;
1896 if ((*dst) + ch <= sdst)
1897 memcpy(*dst, src, ch);
1902 if ((*dst) + ch <= sdst)
1903 memset(*dst, (char)*src, ch);
1909 WINE_WARN("Buffer X-flow: d(%u) instead of d(%u)\n",
1910 *dst - (sdst - dstsz), dstsz);
1913 /**************************************************************************
1914 * HLPFILE_BPTreeSearch
1916 * Searches for an element in B+ tree
1919 * buf [I] pointer to the embedded file structured as a B+ tree
1920 * key [I] pointer to data to find
1921 * comp [I] compare function
1924 * Pointer to block identified by key, or NULL if failure.
1927 static void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
1928 HLPFILE_BPTreeCompare comp)
1934 BYTE *pages, *ptr, *newptr;
1938 magic = GET_USHORT(buf, 9);
1939 if (magic != 0x293B)
1941 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
1944 page_size = GET_USHORT(buf, 9+4);
1945 cur_page = GET_USHORT(buf, 9+26);
1946 level = GET_USHORT(buf, 9+32);
1947 pages = buf + 9 + 38;
1950 ptr = pages + cur_page*page_size;
1951 entries = GET_SHORT(ptr, 2);
1953 for (i = 0; i < entries; i++)
1955 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
1958 cur_page = GET_USHORT(ptr-2, 0);
1960 ptr = pages + cur_page*page_size;
1961 entries = GET_SHORT(ptr, 2);
1963 for (i = 0; i < entries; i++)
1965 ret = comp(ptr, key, 1, (void **)&newptr);
1966 if (ret == 0) return ptr;
1967 if (ret > 0) return NULL;
1974 /***********************************************************************
1976 * HLPFILE_GetContext
1978 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
1983 if (!HLPFILE_FindSubFile("|CONTEXT", &cbuf, &cend)) {WINE_WARN("context0\n"); return FALSE;}
1986 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
1987 if (!hlpfile->Context) return FALSE;
1988 memcpy(hlpfile->Context, cbuf, clen);
1993 /***********************************************************************
1997 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2000 unsigned entries, i;
2002 if (!HLPFILE_FindSubFile("|CTXOMAP", &cbuf, &cend)) {WINE_WARN("no map section\n"); return FALSE;}
2004 entries = GET_USHORT(cbuf, 9);
2005 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2006 if (!hlpfile->Map) return FALSE;
2007 hlpfile->wMapLen = entries;
2008 for (i = 0; i < entries; i++)
2010 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2011 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2016 /******************************************************************
2017 * HLPFILE_DeleteLink
2021 void HLPFILE_FreeLink(HLPFILE_LINK* link)
2023 if (link && !--link->wRefCount)
2024 HeapFree(GetProcessHeap(), 0, link);
2027 /***********************************************************************
2029 * HLPFILE_DeleteParagraph
2031 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
2033 HLPFILE_PARAGRAPH* next;
2037 next = paragraph->next;
2039 if (paragraph->cookie == para_metafile)
2040 DeleteMetaFile(paragraph->u.gfx.u.mfp.hMF);
2042 HLPFILE_FreeLink(paragraph->link);
2044 HeapFree(GetProcessHeap(), 0, paragraph);
2049 /***********************************************************************
2053 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2055 HLPFILE_MACRO* next;
2060 HeapFree(GetProcessHeap(), 0, macro);
2065 /***********************************************************************
2069 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2076 HLPFILE_DeleteParagraph(page->first_paragraph);
2077 HLPFILE_DeleteMacro(page->first_macro);
2078 HeapFree(GetProcessHeap(), 0, page);
2083 /***********************************************************************
2085 * HLPFILE_FreeHlpFile
2087 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2091 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2093 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2094 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2095 else first_hlpfile = hlpfile->next;
2097 if (hlpfile->numFonts)
2099 for (i = 0; i < hlpfile->numFonts; i++)
2101 DeleteObject(hlpfile->fonts[i].hFont);
2103 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2106 if (hlpfile->numBmps)
2108 for (i = 0; i < hlpfile->numBmps; i++)
2110 DeleteObject(hlpfile->bmps[i]);
2112 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2115 HLPFILE_DeletePage(hlpfile->first_page);
2116 HLPFILE_DeleteMacro(hlpfile->first_macro);
2118 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2119 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2120 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2121 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2122 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2123 HeapFree(GetProcessHeap(), 0, hlpfile);