4 * Copyright 1996 Ulrich Schmid
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
36 static inline unsigned short GET_USHORT(const BYTE* buffer, unsigned i)
38 return (BYTE)buffer[i] + 0x100 * (BYTE)buffer[i + 1];
41 static inline short GET_SHORT(const BYTE* buffer, unsigned i)
43 return (BYTE)buffer[i] + 0x100 * (signed char)buffer[i+1];
46 static inline unsigned GET_UINT(const BYTE* buffer, unsigned i)
48 return GET_USHORT(buffer, i) + 0x10000 * GET_USHORT(buffer, i + 2);
51 static HLPFILE *first_hlpfile = 0;
52 static BYTE *file_buffer;
77 static BOOL HLPFILE_DoReadHlpFile(HLPFILE*, LPCSTR);
78 static BOOL HLPFILE_ReadFileToBuffer(HFILE);
79 static BOOL HLPFILE_FindSubFile(LPCSTR name, BYTE**, BYTE**);
80 static BOOL HLPFILE_SystemCommands(HLPFILE*);
81 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end);
82 static BYTE* HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr);
83 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE*);
84 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE*);
85 static BOOL HLPFILE_Uncompress_Topic(HLPFILE*);
86 static BOOL HLPFILE_GetContext(HLPFILE*);
87 static BOOL HLPFILE_AddPage(HLPFILE*, BYTE*, BYTE*, unsigned);
88 static BOOL HLPFILE_AddParagraph(HLPFILE*, BYTE *, BYTE*, unsigned*);
89 static void HLPFILE_Uncompress2(const BYTE*, const BYTE*, BYTE*, const BYTE*);
90 static BOOL HLPFILE_Uncompress3(char*, const char*, const BYTE*, const BYTE*);
91 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz);
92 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile);
95 /***********************************************************************
97 * HLPFILE_PageByNumber
99 static HLPFILE_PAGE *HLPFILE_PageByNumber(LPCSTR lpszPath, UINT wNum)
102 HLPFILE *hlpfile = HLPFILE_ReadHlpFile(lpszPath);
104 if (!hlpfile) return 0;
106 WINE_TRACE("[%s/%u]\n", lpszPath, wNum);
108 for (page = hlpfile->first_page; page && wNum; page = page->next) wNum--;
110 /* HLPFILE_FreeHlpFile(lpszPath); */
117 * this finds the page containing the offset. The offset can either
118 * refer to the top of the page (offset == page->offset), or
119 * to some paragraph inside the page...
120 * As of today, we only return the page... we should also return
121 * a paragraph, and then, while opening a new page, compute the
122 * y-offset of the paragraph to be shown and scroll the window
125 /******************************************************************
126 * HLPFILE_PageByOffset
130 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset)
135 if (!hlpfile) return 0;
137 WINE_TRACE("<%s>[%lx]\n", hlpfile->lpszPath, offset);
139 if (offset == 0xFFFFFFFF) return NULL;
142 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
144 if (page->offset <= offset && (!found || found->offset < page->offset))
148 WINE_ERR("Page of offset %lu not found in file %s\n",
149 offset, hlpfile->lpszPath);
153 /***********************************************************************
155 * HLPFILE_HlpFilePageByHash
157 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash)
161 if (!hlpfile) return 0;
163 WINE_TRACE("<%s>[%lx]\n", hlpfile->lpszPath, lHash);
165 for (i = 0; i < hlpfile->wContextLen; i++)
167 if (hlpfile->Context[i].lHash == lHash)
168 return HLPFILE_PageByOffset(hlpfile, hlpfile->Context[i].offset);
171 WINE_ERR("Page of hash %lx not found in file %s\n", lHash, hlpfile->lpszPath);
175 /***********************************************************************
179 HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile)
181 HLPFILE_PAGE* page = NULL;
183 if (!hlpfile) return NULL;
185 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start);
186 if (!page) page = hlpfile->first_page;
190 /***********************************************************************
194 LONG HLPFILE_Hash(LPCSTR lpszContext)
199 while ((c = *lpszContext++))
202 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
203 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
204 if (c >= '1' && c <= '9') x = c - '0';
205 if (c == '0') x = 10;
206 if (c == '.') x = 12;
207 if (c == '_') x = 13;
208 if (x) lHash = lHash * 43 + x;
213 /***********************************************************************
215 * HLPFILE_ReadHlpFile
217 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
221 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
223 if (!strcmp(lpszPath, hlpfile->lpszPath))
225 hlpfile->wRefCount++;
230 hlpfile = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
231 if (!hlpfile) return 0;
233 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
234 hlpfile->lpszTitle = NULL;
235 hlpfile->lpszCopyright = NULL;
236 hlpfile->first_page = NULL;
237 hlpfile->first_macro = NULL;
238 hlpfile->wContextLen = 0;
239 hlpfile->Context = NULL;
240 hlpfile->contents_start = 0xFFFFFFFF;
241 hlpfile->prev = NULL;
242 hlpfile->next = first_hlpfile;
243 hlpfile->wRefCount = 1;
245 hlpfile->numBmps = 0;
246 hlpfile->bmps = NULL;
248 hlpfile->numFonts = 0;
249 hlpfile->fonts = NULL;
251 hlpfile->numWindows = 0;
252 hlpfile->windows = NULL;
254 strcpy(hlpfile->lpszPath, lpszPath);
256 first_hlpfile = hlpfile;
257 if (hlpfile->next) hlpfile->next->prev = hlpfile;
259 phrases.offsets = NULL;
260 phrases.buffer = NULL;
265 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
267 HLPFILE_FreeHlpFile(hlpfile);
271 HeapFree(GetProcessHeap(), 0, phrases.offsets);
272 HeapFree(GetProcessHeap(), 0, phrases.buffer);
273 HeapFree(GetProcessHeap(), 0, topic.map);
274 HeapFree(GetProcessHeap(), 0, file_buffer);
279 /***********************************************************************
281 * HLPFILE_DoReadHlpFile
283 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
290 unsigned index, old_index, offset, len, offs;
292 hFile = OpenFile(lpszPath, &ofs, OF_READ);
293 if (hFile == HFILE_ERROR) return FALSE;
295 ret = HLPFILE_ReadFileToBuffer(hFile);
297 if (!ret) return FALSE;
299 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
301 /* load phrases support */
302 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
303 HLPFILE_Uncompress_Phrases40(hlpfile);
305 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
306 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
315 /* FIXME this depends on the blocksize, can be 2k in some cases */
316 index = (ref - 0x0C) >> 14;
317 offset = (ref - 0x0C) & 0x3fff;
319 WINE_TRACE("ref=%08lx => [%u/%u]\n", ref, index, offset);
321 if (index >= topic.wMapLen) {WINE_WARN("maplen\n"); break;}
322 buf = topic.map[index] + offset;
323 if (buf + 0x15 >= topic.end) {WINE_WARN("extra\n"); break;}
324 end = min(buf + GET_UINT(buf, 0), topic.end);
325 if (index != old_index) {offs = 0; old_index = index;}
330 if (!HLPFILE_AddPage(hlpfile, buf, end, index * 0x8000L + offs)) return FALSE;
334 if (!HLPFILE_AddParagraph(hlpfile, buf, end, &len)) return FALSE;
339 if (!HLPFILE_AddParagraph(hlpfile, buf, end, &len)) return FALSE;
344 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
347 ref = GET_UINT(buf, 0xc);
348 } while (ref != 0xffffffff);
350 return HLPFILE_GetContext(hlpfile);
353 /***********************************************************************
357 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned offset)
365 if (buf + 0x31 > end) {WINE_WARN("page1\n"); return FALSE;};
366 title = buf + GET_UINT(buf, 0x10);
367 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
369 titlesize = GET_UINT(buf, 4);
370 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
371 if (!page) return FALSE;
372 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
374 if (hlpfile->hasPhrases)
376 HLPFILE_Uncompress2(title, end, (BYTE*)page->lpszTitle, (BYTE*)page->lpszTitle + titlesize);
380 if (GET_UINT(buf, 0x4) > GET_UINT(buf, 0) - GET_UINT(buf, 0x10))
382 /* need to decompress */
383 HLPFILE_Uncompress3(page->lpszTitle, page->lpszTitle + titlesize,
388 memcpy(page->lpszTitle, title, titlesize);
392 page->lpszTitle[titlesize] = '\0';
394 if (hlpfile->first_page)
398 for (p = hlpfile->first_page; p->next; p = p->next);
404 hlpfile->first_page = page;
408 page->file = hlpfile;
410 page->first_paragraph = NULL;
411 page->first_macro = NULL;
412 page->wNumber = GET_UINT(buf, 0x21);
413 page->offset = offset;
415 page->browse_bwd = GET_UINT(buf, 0x19);
416 page->browse_fwd = GET_UINT(buf, 0x1D);
418 WINE_TRACE("Added page[%d]: title='%s' %08lx << %08x >> %08lx\n",
419 page->wNumber, page->lpszTitle,
420 page->browse_bwd, page->offset, page->browse_fwd);
422 memset(&attributes, 0, sizeof(attributes));
424 /* now load macros */
425 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
426 while (ptr < page->lpszTitle + titlesize)
428 unsigned len = strlen(ptr);
429 WINE_TRACE("macro: %s\n", ptr);
430 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
431 macro->lpszMacro = (char*)(macro + 1);
432 memcpy((char*)macro->lpszMacro, ptr, len + 1);
433 /* FIXME: shall we really link macro in reverse order ??
434 * may produce strange results when played at page opening
436 macro->next = page->first_macro;
437 page->first_macro = macro;
444 static long fetch_long(BYTE** ptr)
450 ret = (*(unsigned long*)(*ptr) - 0x80000000L) / 2;
455 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
462 static unsigned long fetch_ulong(BYTE** ptr)
468 ret = *(unsigned long*)(*ptr) / 2;
473 ret = *(unsigned short*)(*ptr) / 2;
479 static short fetch_short(BYTE** ptr)
485 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
490 ret = (*(unsigned char*)(*ptr) - 0x80) / 2;
496 static unsigned short fetch_ushort(BYTE** ptr)
502 ret = *(unsigned short*)(*ptr) / 2;
507 ret = *(unsigned char*)(*ptr) / 2;
513 /******************************************************************
514 * HLPFILE_DecompressGfx
516 * Decompress the data part of a bitmap or a metafile
518 static BYTE* HLPFILE_DecompressGfx(BYTE* src, unsigned csz, unsigned sz, BYTE packing)
525 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
529 case 0: /* uncompressed */
531 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
535 tmp = dst = HeapAlloc(GetProcessHeap(), 0, sz);
536 if (!dst) return NULL;
537 HLPFILE_UncompressRLE(src, src + csz, &tmp, sz);
539 WINE_WARN("Bogus gfx sizes (RunLen): %u/%u\n", tmp - dst, sz);
542 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
543 dst = HeapAlloc(GetProcessHeap(), 0, sz77);
544 if (!dst) return NULL;
545 HLPFILE_UncompressLZ77(src, src + csz, dst);
547 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
549 case 3: /* LZ77 then RLE */
550 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
551 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
552 if (!tmp) return FALSE;
553 HLPFILE_UncompressLZ77(src, src + csz, tmp);
554 dst = tmp2 = HeapAlloc(GetProcessHeap(), 0, sz);
555 if (!dst) return FALSE;
556 HLPFILE_UncompressRLE(tmp, tmp + sz77, &tmp2, sz);
557 if (tmp2 - dst != sz)
558 WINE_WARN("Bogus gfx sizes (LZ77+RunLen): %u / %u\n", tmp2 - dst, sz);
559 HeapFree(GetProcessHeap(), 0, tmp);
562 WINE_FIXME("Unsupported packing %u\n", packing);
568 /******************************************************************
573 static BOOL HLPFILE_LoadBitmap(BYTE* beg, BYTE type, BYTE pack,
574 HLPFILE_PARAGRAPH* paragraph)
579 unsigned long off, csz;
582 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
583 if (!bi) return FALSE;
585 ptr = beg + 2; /* for type and pack */
587 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
588 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
589 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
590 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
591 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
592 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
593 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
594 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
595 bi->bmiHeader.biClrImportant = fetch_ulong(&ptr);
596 bi->bmiHeader.biCompression = BI_RGB;
597 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
598 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
599 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
600 WINE_TRACE("planes=%d bc=%d size=(%ld,%ld)\n",
601 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
602 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
604 csz = fetch_ulong(&ptr);
605 fetch_ulong(&ptr); /* hotspot size */
607 off = GET_UINT(ptr, 0); ptr += 4;
608 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
610 /* now read palette info */
613 unsigned nc = bi->bmiHeader.biClrUsed;
616 /* not quite right, especially for bitfields type of compression */
617 if (!nc && bi->bmiHeader.biBitCount <= 8)
618 nc = 1 << bi->bmiHeader.biBitCount;
620 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
621 if (!bi) return FALSE;
622 for (i = 0; i < nc; i++)
624 bi->bmiColors[i].rgbBlue = ptr[0];
625 bi->bmiColors[i].rgbGreen = ptr[1];
626 bi->bmiColors[i].rgbRed = ptr[2];
627 bi->bmiColors[i].rgbReserved = 0;
631 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
633 paragraph->u.gfx.u.bmp.hBitmap = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
637 if (!paragraph->u.gfx.u.bmp.hBitmap)
638 WINE_ERR("Couldn't create bitmap\n");
640 HeapFree(GetProcessHeap(), 0, bi);
641 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
646 /******************************************************************
647 * HLPFILE_LoadMetaFile
651 static BOOL HLPFILE_LoadMetaFile(BYTE* beg, BYTE pack, HLPFILE_PARAGRAPH* paragraph)
654 unsigned long size, csize;
655 unsigned long off, hsoff;
659 WINE_TRACE("Loading metafile\n");
661 ptr = beg + 2; /* for type and pack */
663 mfp.mm = fetch_ushort(&ptr); /* mapping mode */
665 mfp.xExt = GET_USHORT(ptr, 0);
666 mfp.yExt = GET_USHORT(ptr, 2);
669 size = fetch_ulong(&ptr); /* decompressed size */
670 csize = fetch_ulong(&ptr); /* compressed size */
671 fetch_ulong(&ptr); /* hotspot size */
672 off = GET_UINT(ptr, 0);
673 hsoff = GET_UINT(ptr, 4);
676 WINE_TRACE("sz=%lu csz=%lu (%ld,%ld) offs=%lu/%u,%lu\n",
677 size, csize, mfp.xExt, mfp.yExt, off, ptr - beg, hsoff);
679 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
680 if (!bits) return FALSE;
682 paragraph->cookie = para_metafile;
686 paragraph->u.gfx.u.mf.hMetaFile = SetMetaFileBitsEx(size, bits);
688 if (!paragraph->u.gfx.u.mf.hMetaFile)
689 WINE_FIXME("Couldn't load metafile\n");
691 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
693 paragraph->u.gfx.u.mf.mfSize.cx = mfp.xExt;
694 paragraph->u.gfx.u.mf.mfSize.cy = mfp.yExt;
699 /******************************************************************
700 * HLPFILE_LoadGfxByAddr
704 static BOOL HLPFILE_LoadGfxByAddr(HLPFILE *hlpfile, BYTE* ref,
706 HLPFILE_PARAGRAPH* paragraph)
710 numpict = GET_USHORT(ref, 2);
711 WINE_TRACE("Got picture magic=%04x #=%d\n",
712 GET_USHORT(ref, 0), numpict);
714 for (i = 0; i < numpict; i++)
720 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
721 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
728 case 5: /* device dependent bmp */
729 case 6: /* device independent bmp */
730 HLPFILE_LoadBitmap(beg, type, pack, paragraph);
733 HLPFILE_LoadMetaFile(beg, pack, paragraph);
735 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
738 /* FIXME: hotspots */
740 /* FIXME: implement support for multiple picture format */
741 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
747 /******************************************************************
748 * HLPFILE_LoadGfxByIndex
752 static BOOL HLPFILE_LoadGfxByIndex(HLPFILE *hlpfile, unsigned index,
753 HLPFILE_PARAGRAPH* paragraph)
759 WINE_TRACE("Loading picture #%d\n", index);
761 if (index < hlpfile->numBmps && hlpfile->bmps[index] != NULL)
763 paragraph->u.gfx.u.bmp.hBitmap = hlpfile->bmps[index];
767 sprintf(tmp, "|bm%u", index);
769 if (!HLPFILE_FindSubFile(tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
773 ret = HLPFILE_LoadGfxByAddr(hlpfile, ref, end - ref, paragraph);
776 if (ret && paragraph->cookie == para_bitmap)
778 if (index >= hlpfile->numBmps)
780 hlpfile->numBmps = index + 1;
782 hlpfile->bmps = HeapReAlloc(GetProcessHeap(), 0, hlpfile->bmps,
783 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
785 hlpfile->bmps = HeapAlloc(GetProcessHeap(), 0,
786 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
789 hlpfile->bmps[index] = paragraph->u.gfx.u.bmp.hBitmap;
794 /******************************************************************
799 static HLPFILE_LINK* HLPFILE_AllocLink(int cookie, const char* str, LONG hash,
800 BOOL clrChange, unsigned wnd)
804 /* FIXME: should build a string table for the attributes.link.lpszPath
805 * they are reallocated for each link
807 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + strlen(str) + 1);
808 if (!link) return NULL;
810 link->cookie = cookie;
811 link->lpszString = (char*)link + sizeof(HLPFILE_LINK);
812 strcpy((char*)link->lpszString, str);
814 link->bClrChange = clrChange ? 1 : 0;
818 WINE_TRACE("Link[%d] to %s@%08lx:%d\n",
819 link->cookie, link->lpszString,
820 link->lHash, link->window);
824 /***********************************************************************
826 * HLPFILE_AddParagraph
828 static BOOL HLPFILE_AddParagraph(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned* len)
831 HLPFILE_PARAGRAPH *paragraph, **paragraphptr;
833 BYTE *format, *format_end;
834 char *text, *text_end;
837 unsigned nc, ncol = 1;
839 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
841 for (page = hlpfile->first_page; page->next; page = page->next) /* Nothing */;
842 for (paragraphptr = &page->first_paragraph; *paragraphptr;
843 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
845 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
847 size = GET_UINT(buf, 0x4);
848 text = HeapAlloc(GetProcessHeap(), 0, size);
849 if (!text) return FALSE;
850 if (hlpfile->hasPhrases)
852 HLPFILE_Uncompress2(buf + GET_UINT(buf, 0x10), end, (BYTE*)text, (BYTE*)text + size);
856 if (GET_UINT(buf, 0x4) > GET_UINT(buf, 0) - GET_UINT(buf, 0x10))
858 /* block is compressed */
859 HLPFILE_Uncompress3(text, text + size, buf + GET_UINT(buf, 0x10), end);
863 text = (char*)buf + GET_UINT(buf, 0x10);
866 text_end = text + size;
869 format_end = buf + GET_UINT(buf, 0x10);
872 *len = fetch_ushort(&format);
874 if (buf[0x14] == 0x23)
880 WINE_TRACE("#cols %u\n", ncol);
882 if (type == 0 || type == 2)
887 for (nc = 0; nc < ncol; nc++)
889 WINE_TRACE("looking for format at offset %u for column %d\n", format - (buf + 0x15), nc);
890 if (buf[0x14] == 0x23)
893 bits = GET_USHORT(format, 0); format += 2;
894 if (bits & 0x0001) fetch_long(&format);
895 if (bits & 0x0002) fetch_short(&format);
896 if (bits & 0x0004) fetch_short(&format);
897 if (bits & 0x0008) fetch_short(&format);
898 if (bits & 0x0010) fetch_short(&format);
899 if (bits & 0x0020) fetch_short(&format);
900 if (bits & 0x0040) fetch_short(&format);
901 if (bits & 0x0100) format += 3;
904 int ntab = fetch_short(&format);
909 ts = fetch_ushort(&format);
910 if (ts & 0x4000) fetch_ushort(&format);
913 /* 0x0400, 0x0800 and 0x1000 don't need space */
914 if ((bits & 0xE080) != 0)
915 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
917 while (text < text_end && format < format_end)
919 WINE_TRACE("Got text: '%s' (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
920 textsize = strlen(text) + 1;
923 paragraph = HeapAlloc(GetProcessHeap(), 0,
924 sizeof(HLPFILE_PARAGRAPH) + textsize);
925 if (!paragraph) return FALSE;
926 *paragraphptr = paragraph;
927 paragraphptr = ¶graph->next;
929 paragraph->next = NULL;
930 paragraph->link = attributes.link;
931 if (paragraph->link) paragraph->link->wRefCount++;
932 paragraph->cookie = para_normal_text;
933 paragraph->u.text.wFont = attributes.wFont;
934 paragraph->u.text.wVSpace = attributes.wVSpace;
935 paragraph->u.text.wHSpace = attributes.wHSpace;
936 paragraph->u.text.wIndent = attributes.wIndent;
937 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
938 strcpy(paragraph->u.text.lpszText, text);
940 attributes.wVSpace = 0;
941 attributes.wHSpace = 0;
943 /* else: null text, keep on storing attributes */
952 WINE_TRACE("format=%02x\n", *format);
956 WINE_FIXME("NIY20\n");
961 WINE_FIXME("NIY21\n");
966 attributes.wFont = GET_USHORT(format, 1);
967 WINE_TRACE("Changing font to %d\n", attributes.wFont);
972 attributes.wVSpace++;
977 attributes.wVSpace++;
978 attributes.wIndent = 0;
983 attributes.wIndent++;
997 BYTE pos = (*format - 0x86);
998 BYTE type = format[1];
1002 size = fetch_long(&format);
1004 paragraph = HeapAlloc(GetProcessHeap(), 0,
1005 sizeof(HLPFILE_PARAGRAPH) + textsize);
1006 if (!paragraph) return FALSE;
1007 *paragraphptr = paragraph;
1008 paragraphptr = ¶graph->next;
1010 paragraph->next = NULL;
1011 paragraph->link = attributes.link;
1012 if (paragraph->link) paragraph->link->wRefCount++;
1013 paragraph->cookie = para_bitmap;
1014 paragraph->u.gfx.pos = pos;
1018 fetch_ushort(&format); /* hot spot */
1021 switch (GET_SHORT(format, 0))
1024 HLPFILE_LoadGfxByIndex(hlpfile, GET_SHORT(format, 2),
1028 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1029 GET_SHORT(format, 0),
1030 size, GET_SHORT(format, 2));
1031 HLPFILE_LoadGfxByAddr(hlpfile, format + 2, size - 4,
1035 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1040 WINE_FIXME("Got an embedded element %s\n", format + 6);
1043 WINE_FIXME("Got a type %d picture\n", type);
1046 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1053 HLPFILE_FreeLink(attributes.link);
1054 attributes.link = NULL;
1060 WINE_FIXME("NIY non-break space/hyphen\n");
1072 WINE_TRACE("macro => %s\n", format + 3);
1073 HLPFILE_FreeLink(attributes.link);
1074 attributes.link = HLPFILE_AllocLink(hlp_link_macro, (const char*)format + 3,
1075 0, !(*format & 4), -1);
1076 format += 3 + GET_USHORT(format, 1);
1081 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1089 HLPFILE_FreeLink(attributes.link);
1090 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1092 GET_UINT(format, 1),
1093 !(*format & 4), -1);
1102 char* ptr = (char*) format + 8;
1103 BYTE type = format[3];
1107 if (type == 1) wnd = *ptr++;
1108 if (type == 4 || type == 6)
1111 ptr += strlen(ptr) + 1;
1114 str = hlpfile->lpszPath;
1117 for (wnd = hlpfile->numWindows - 1; wnd >= 0; wnd--)
1119 if (!strcmp(ptr, hlpfile->windows[wnd].name)) break;
1122 WINE_WARN("Couldn't find window info for %s\n", ptr);
1124 HLPFILE_FreeLink(attributes.link);
1125 attributes.link = HLPFILE_AllocLink((*format & 4) ? hlp_link_link : hlp_link_popup,
1126 str, GET_UINT(format, 4),
1127 !(*format & 1), wnd);
1129 format += 3 + GET_USHORT(format, 1);
1133 WINE_WARN("format %02x\n", *format);
1138 if (text_end != (char*)buf + GET_UINT(buf, 0x10) + size)
1139 HeapFree(GetProcessHeap(), 0, text_end - size);
1143 /******************************************************************
1148 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1151 unsigned i, len, idx;
1152 unsigned face_num, dscr_num, face_offset, dscr_offset;
1155 if (!HLPFILE_FindSubFile("|FONT", &ref, &end))
1157 WINE_WARN("no subfile FONT\n");
1158 hlpfile->numFonts = 0;
1159 hlpfile->fonts = NULL;
1165 face_num = GET_USHORT(ref, 0);
1166 dscr_num = GET_USHORT(ref, 2);
1167 face_offset = GET_USHORT(ref, 4);
1168 dscr_offset = GET_USHORT(ref, 6);
1170 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1171 face_num, face_offset, dscr_num, dscr_offset);
1173 hlpfile->numFonts = dscr_num;
1174 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1176 len = (dscr_offset - face_offset) / face_num;
1177 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1178 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1179 for (i = 0; i < dscr_num; i++)
1181 flag = ref[dscr_offset + i * 11 + 0];
1182 family = ref[dscr_offset + i * 11 + 2];
1184 hlpfile->fonts[i].LogFont.lfHeight = -ref[dscr_offset + i * 11 + 1] / 2;
1185 hlpfile->fonts[i].LogFont.lfWidth = 0;
1186 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1187 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1188 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1189 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1190 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1191 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1192 hlpfile->fonts[i].LogFont.lfCharSet = ANSI_CHARSET;
1193 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1194 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1195 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1196 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1200 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1201 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1202 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1203 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1204 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1205 default: WINE_FIXME("Unknown family %u\n", family);
1207 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1211 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1212 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1216 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1217 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1219 hlpfile->fonts[i].hFont = 0;
1220 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1221 ref[dscr_offset + i * 11 + 6],
1222 ref[dscr_offset + i * 11 + 7]);
1223 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1224 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1230 X(4, "dblUnderline"),
1232 ref[dscr_offset + i * 11 + 1],
1234 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1235 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1240 /***********************************************************************
1242 * HLPFILE_ReadFileToBuffer
1244 static BOOL HLPFILE_ReadFileToBuffer(HFILE hFile)
1246 BYTE header[16], dummy[1];
1249 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1252 if (GET_UINT(header, 0) != 0x00035F3F)
1253 {WINE_WARN("wrong header\n"); return FALSE;};
1255 size = GET_UINT(header, 12);
1256 file_buffer = HeapAlloc(GetProcessHeap(), 0, size + 1);
1257 if (!file_buffer) return FALSE;
1259 memcpy(file_buffer, header, 16);
1260 if (_hread(hFile, file_buffer + 16, size - 16) != size - 16)
1261 {WINE_WARN("filesize1\n"); return FALSE;};
1263 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1265 file_buffer[size] = '\0'; /* FIXME: was '0', sounds ackward to me */
1270 /***********************************************************************
1272 * HLPFILE_FindSubFile
1274 static BOOL HLPFILE_FindSubFile(LPCSTR name, BYTE **subbuf, BYTE **subend)
1276 BYTE *root = file_buffer + GET_UINT(file_buffer, 4);
1277 BYTE *end = file_buffer + GET_UINT(file_buffer, 12);
1288 /* FIXME: this should be using the EnumBTree functions from this file */
1289 pgsize = GET_USHORT(bth, 4);
1290 WINE_TRACE("%s => pgsize=%u #pg=%u rootpg=%u #lvl=%u\n",
1291 name, pgsize, GET_USHORT(bth, 30), GET_USHORT(bth, 26), GET_USHORT(bth, 32));
1293 ptr = bth + 38 + GET_USHORT(bth, 26) * pgsize;
1295 for (n = 1; n < GET_USHORT(bth, 32); n++)
1297 nentries = GET_USHORT(ptr, 2);
1298 pglast = GET_USHORT(ptr, 4);
1299 WINE_TRACE("[%u]: #entries=%u next=%u\n", n, nentries, pglast);
1302 for (i = 0; i < nentries; i++)
1304 char *str = (char*) ptr;
1305 WINE_TRACE("<= %s\n", str);
1306 if (strcmp(name, str) < 0) break;
1307 ptr += strlen(str) + 1;
1308 pglast = GET_USHORT(ptr, 0);
1311 ptr = bth + 38 + pglast * pgsize;
1314 nentries = GET_USHORT(ptr, 2);
1316 for (i = 0; i < nentries; i++)
1318 char* fname = (char*)ptr;
1319 ptr += strlen(fname) + 1;
1320 WINE_TRACE("\\- %s\n", fname);
1321 if (strcmp(fname, name) == 0)
1323 *subbuf = file_buffer + GET_UINT(ptr, 0);
1324 *subend = *subbuf + GET_UINT(*subbuf, 0);
1325 if (file_buffer > *subbuf || *subbuf > *subend || *subend > end)
1327 WINE_WARN("size mismatch\n");
1338 /***********************************************************************
1340 * HLPFILE_SystemCommands
1342 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1344 BYTE *buf, *ptr, *end;
1345 HLPFILE_MACRO *macro, **m;
1347 unsigned short magic, minor, major, flags;
1349 hlpfile->lpszTitle = NULL;
1351 if (!HLPFILE_FindSubFile("|SYSTEM", &buf, &end)) return FALSE;
1353 magic = GET_USHORT(buf + 9, 0);
1354 minor = GET_USHORT(buf + 9, 2);
1355 major = GET_USHORT(buf + 9, 4);
1356 /* gen date on 4 bytes */
1357 flags = GET_USHORT(buf + 9, 10);
1358 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1359 magic, major, minor, flags);
1360 if (magic != 0x036C || major != 1)
1361 {WINE_WARN("Wrong system header\n"); return FALSE;}
1362 if (minor <= 16) {WINE_WARN("too old file format (NIY)\n"); return FALSE;}
1363 if (flags & 8) {WINE_WARN("Unsupported yet page size\n"); return FALSE;}
1365 hlpfile->version = minor;
1366 hlpfile->flags = flags;
1368 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1370 char *str = (char*) ptr + 4;
1371 switch (GET_USHORT(ptr, 0))
1374 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1375 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1376 if (!hlpfile->lpszTitle) return FALSE;
1377 lstrcpy(hlpfile->lpszTitle, str);
1378 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1382 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1383 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1384 if (!hlpfile->lpszCopyright) return FALSE;
1385 lstrcpy(hlpfile->lpszCopyright, str);
1386 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1390 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1391 hlpfile->contents_start = GET_UINT(ptr, 4);
1392 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1396 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1398 p = (char*)macro + sizeof(HLPFILE_MACRO);
1400 macro->lpszMacro = p;
1402 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1407 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1409 if (hlpfile->windows)
1410 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1411 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1413 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1414 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1416 if (hlpfile->windows)
1418 unsigned flags = GET_USHORT(ptr, 4);
1419 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1421 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1422 else wi->type[0] = '\0';
1423 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1424 else wi->name[0] = '\0';
1425 if (flags & 0x0004) strcpy(wi->caption, &str[23]);
1426 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1427 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1428 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1429 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1430 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1431 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1432 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1433 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1434 WINE_TRACE("System-Window: flags=%c%c%c%c%c%c%c%c type=%s name=%s caption=%s (%ld,%ld)x(%ld,%ld)\n",
1435 flags & 0x0001 ? 'T' : 't',
1436 flags & 0x0002 ? 'N' : 'n',
1437 flags & 0x0004 ? 'C' : 'c',
1438 flags & 0x0008 ? 'X' : 'x',
1439 flags & 0x0010 ? 'Y' : 'y',
1440 flags & 0x0020 ? 'W' : 'w',
1441 flags & 0x0040 ? 'H' : 'h',
1442 flags & 0x0080 ? 'S' : 's',
1443 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1444 wi->size.cx, wi->size.cy);
1448 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1454 /***********************************************************************
1456 * HLPFILE_UncompressedLZ77_Size
1458 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
1465 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1469 int code = GET_USHORT(ptr, 0);
1470 int len = 3 + (code >> 12);
1474 else newsize++, ptr++;
1481 /***********************************************************************
1483 * HLPFILE_UncompressLZ77
1485 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
1492 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1496 int code = GET_USHORT(ptr, 0);
1497 int len = 3 + (code >> 12);
1498 int offset = code & 0xfff;
1499 memcpy(newptr, newptr - offset - 1, len);
1503 else *newptr++ = *ptr++;
1510 /***********************************************************************
1512 * HLPFILE_UncompressLZ77_Phrases
1514 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
1516 UINT i, num, dec_size;
1519 if (!HLPFILE_FindSubFile("|Phrases", &buf, &end)) return FALSE;
1521 num = phrases.num = GET_USHORT(buf, 9);
1522 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
1524 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
1526 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1527 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1528 if (!phrases.offsets || !phrases.buffer) return FALSE;
1530 for (i = 0; i <= num; i++)
1531 phrases.offsets[i] = GET_USHORT(buf, 0x11 + 2 * i) - 2 * num - 2;
1533 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)phrases.buffer);
1535 hlpfile->hasPhrases = TRUE;
1539 /***********************************************************************
1541 * HLPFILE_Uncompress_Phrases40
1543 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
1545 UINT num, dec_size, cpr_size;
1546 BYTE *buf_idx, *end_idx;
1547 BYTE *buf_phs, *end_phs;
1549 long* ptr, mask = 0;
1552 if (!HLPFILE_FindSubFile("|PhrIndex", &buf_idx, &end_idx) ||
1553 !HLPFILE_FindSubFile("|PhrImage", &buf_phs, &end_phs)) return FALSE;
1555 ptr = (long*)(buf_idx + 9 + 28);
1556 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
1557 num = phrases.num = GET_USHORT(buf_idx, 9 + 4);
1559 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
1560 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
1561 GET_UINT(buf_idx, 9 + 0),
1562 GET_UINT(buf_idx, 9 + 4),
1563 GET_UINT(buf_idx, 9 + 8),
1564 GET_UINT(buf_idx, 9 + 12),
1565 GET_UINT(buf_idx, 9 + 16),
1566 GET_UINT(buf_idx, 9 + 20),
1567 GET_USHORT(buf_idx, 9 + 24),
1568 GET_USHORT(buf_idx, 9 + 26));
1570 dec_size = GET_UINT(buf_idx, 9 + 12);
1571 cpr_size = GET_UINT(buf_idx, 9 + 16);
1573 if (dec_size != cpr_size &&
1574 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
1576 WINE_WARN("size mismatch %u %u\n",
1577 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1578 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1581 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1582 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1583 if (!phrases.offsets || !phrases.buffer) return FALSE;
1585 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
1587 phrases.offsets[0] = 0;
1588 for (i = 0; i < num; i++)
1590 for (n = 1; getbit(); n += 1 << bc);
1592 if (bc > 1 && getbit()) n += 2;
1593 if (bc > 2 && getbit()) n += 4;
1594 if (bc > 3 && getbit()) n += 8;
1595 if (bc > 4 && getbit()) n += 16;
1596 phrases.offsets[i + 1] = phrases.offsets[i] + n;
1600 if (dec_size == cpr_size)
1601 memcpy(phrases.buffer, buf_phs + 9, dec_size);
1603 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)phrases.buffer);
1605 hlpfile->hasPhrases = FALSE;
1609 /***********************************************************************
1611 * HLPFILE_Uncompress_Topic
1613 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
1615 BYTE *buf, *ptr, *end, *newptr;
1618 if (!HLPFILE_FindSubFile("|TOPIC", &buf, &end))
1619 {WINE_WARN("topic0\n"); return FALSE;}
1621 switch (hlpfile->flags & (8|4))
1624 WINE_FIXME("Unsupported format\n");
1628 topic.wMapLen = (end - buf - 1) / 0x1000 + 1;
1630 for (i = 0; i < topic.wMapLen; i++)
1632 ptr = buf + i * 0x1000;
1634 /* I don't know why, it's necessary for printman.hlp */
1635 if (ptr + 0x44 > end) ptr = end - 0x44;
1637 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + 0x1000));
1640 topic.map = HeapAlloc(GetProcessHeap(), 0,
1641 topic.wMapLen * sizeof(topic.map[0]) + newsize);
1642 if (!topic.map) return FALSE;
1643 newptr = (BYTE*)(topic.map + topic.wMapLen);
1644 topic.end = newptr + newsize;
1646 for (i = 0; i < topic.wMapLen; i++)
1648 ptr = buf + i * 0x1000;
1649 if (ptr + 0x44 > end) ptr = end - 0x44;
1651 topic.map[i] = newptr;
1652 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + 0x1000), newptr);
1656 /* basically, we need to copy the 0x1000 byte pages (removing the first 0x0C) in
1657 * one single are in memory
1659 #define DST_LEN (0x1000 - 0x0C)
1661 newsize = end - buf;
1662 /* number of destination pages */
1663 topic.wMapLen = (newsize - 1) / DST_LEN + 1;
1664 topic.map = HeapAlloc(GetProcessHeap(), 0,
1665 topic.wMapLen * (sizeof(topic.map[0]) + DST_LEN));
1666 if (!topic.map) return FALSE;
1667 newptr = (BYTE*)(topic.map + topic.wMapLen);
1668 topic.end = newptr + newsize;
1670 for (i = 0; i < topic.wMapLen; i++)
1672 topic.map[i] = newptr + i * DST_LEN;
1673 memcpy(topic.map[i], buf + i * 0x1000 + 0x0C, DST_LEN);
1681 /***********************************************************************
1683 * HLPFILE_Uncompress2
1686 static void HLPFILE_Uncompress2(const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
1688 BYTE *phptr, *phend;
1692 while (ptr < end && newptr < newend)
1694 if (!*ptr || *ptr >= 0x10)
1698 code = 0x100 * ptr[0] + ptr[1];
1699 index = (code - 0x100) / 2;
1701 phptr = (BYTE*)phrases.buffer + phrases.offsets[index];
1702 phend = (BYTE*)phrases.buffer + phrases.offsets[index + 1];
1704 if (newptr + (phend - phptr) > newend)
1706 WINE_FIXME("buffer overflow %p > %p for %d bytes\n",
1707 newptr, newend, phend - phptr);
1710 memcpy(newptr, phptr, phend - phptr);
1711 newptr += phend - phptr;
1712 if (code & 1) *newptr++ = ' ';
1717 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
1720 /******************************************************************
1721 * HLPFILE_Uncompress3
1725 static BOOL HLPFILE_Uncompress3(char* dst, const char* dst_end,
1726 const BYTE* src, const BYTE* src_end)
1730 for (; src < src_end; src++)
1732 if ((*src & 1) == 0)
1735 if (idx > phrases.num)
1737 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1742 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1743 if (dst + len <= dst_end)
1744 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1747 else if ((*src & 0x03) == 0x01)
1749 idx = (*src + 1) * 64;
1751 if (idx > phrases.num)
1753 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1758 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1759 if (dst + len <= dst_end)
1760 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1763 else if ((*src & 0x07) == 0x03)
1765 len = (*src / 8) + 1;
1766 if (dst + len <= dst_end)
1767 memcpy(dst, src + 1, len);
1772 len = (*src / 16) + 1;
1773 if (dst + len <= dst_end)
1774 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
1779 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
1783 /******************************************************************
1784 * HLPFILE_UncompressRLE
1788 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
1791 BYTE* sdst = *dst + dstsz;
1799 if ((*dst) + ch <= sdst)
1800 memcpy(*dst, src, ch);
1805 if ((*dst) + ch <= sdst)
1806 memset(*dst, (char)*src, ch);
1812 WINE_WARN("Buffer X-flow: d(%u) instead of d(%u)\n",
1813 *dst - (sdst - dstsz), dstsz);
1816 /******************************************************************
1817 * HLPFILE_EnumBTreeLeaves
1821 static void HLPFILE_EnumBTreeLeaves(const BYTE* buf, const BYTE* end, unsigned (*fn)(const BYTE*, void*), void* user)
1823 unsigned psize, pnext;
1827 num = GET_UINT(buf, 9 + 34);
1828 psize = GET_USHORT(buf, 9 + 4);
1829 nlvl = GET_USHORT(buf, 9 + 32);
1830 pnext = GET_USHORT(buf, 9 + 26);
1832 WINE_TRACE("BTree: #entries=%u pagSize=%u #levels=%u #pages=%u root=%u struct%16s\n",
1833 num, psize, nlvl, GET_USHORT(buf, 9 + 30), pnext, buf + 9 + 6);
1838 ptr = (buf + 9 + 38) + pnext * psize;
1839 WINE_TRACE("BTree: (index[%u]) unused=%u #entries=%u <%u\n",
1840 pnext, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2), GET_USHORT(ptr, 4));
1841 pnext = GET_USHORT(ptr, 4);
1843 while (pnext != 0xFFFF)
1845 const BYTE* node_page;
1846 unsigned short limit;
1848 node_page = ptr = (buf + 9 + 38) + pnext * psize;
1849 limit = GET_USHORT(ptr, 2);
1850 WINE_TRACE("BTree: (leaf [%u]) unused=%u #entries=%u <%u >%u\n",
1851 pnext, GET_USHORT(ptr, 0), limit, GET_USHORT(ptr, 4), GET_USHORT(ptr, 6));
1854 ptr += (fn)(ptr, user);
1855 pnext = GET_USHORT(node_page, 6);
1864 static unsigned myfn(const BYTE* ptr, void* user)
1866 struct myfncb* m = user;
1868 m->hlpfile->Context[m->i].lHash = GET_UINT(ptr, 0);
1869 m->hlpfile->Context[m->i].offset = GET_UINT(ptr, 4);
1874 /***********************************************************************
1876 * HLPFILE_GetContext
1878 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
1884 if (!HLPFILE_FindSubFile("|CONTEXT", &cbuf, &cend)) {WINE_WARN("context0\n"); return FALSE;}
1886 clen = GET_UINT(cbuf, 0x2b);
1887 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen * sizeof(HLPFILE_CONTEXT));
1888 if (!hlpfile->Context) return FALSE;
1889 hlpfile->wContextLen = clen;
1891 m.hlpfile = hlpfile;
1893 HLPFILE_EnumBTreeLeaves(cbuf, cend, myfn, &m);
1898 /******************************************************************
1899 * HLPFILE_DeleteLink
1903 void HLPFILE_FreeLink(HLPFILE_LINK* link)
1905 if (link && !--link->wRefCount)
1906 HeapFree(GetProcessHeap(), 0, link);
1909 /***********************************************************************
1911 * HLPFILE_DeleteParagraph
1913 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
1915 HLPFILE_PARAGRAPH* next;
1919 next = paragraph->next;
1921 if (paragraph->cookie == para_metafile)
1922 DeleteMetaFile(paragraph->u.gfx.u.mf.hMetaFile);
1924 HLPFILE_FreeLink(paragraph->link);
1926 HeapFree(GetProcessHeap(), 0, paragraph);
1931 /***********************************************************************
1935 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
1937 HLPFILE_MACRO* next;
1942 HeapFree(GetProcessHeap(), 0, macro);
1947 /***********************************************************************
1951 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
1958 HLPFILE_DeleteParagraph(page->first_paragraph);
1959 HLPFILE_DeleteMacro(page->first_macro);
1960 HeapFree(GetProcessHeap(), 0, page);
1965 /***********************************************************************
1967 * HLPFILE_FreeHlpFile
1969 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
1973 if (!hlpfile || --hlpfile->wRefCount > 0) return;
1975 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
1976 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
1977 else first_hlpfile = hlpfile->next;
1979 if (hlpfile->numFonts)
1981 for (i = 0; i < hlpfile->numFonts; i++)
1983 DeleteObject(hlpfile->fonts[i].hFont);
1985 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
1988 if (hlpfile->numBmps)
1990 for (i = 0; i < hlpfile->numBmps; i++)
1992 DeleteObject(hlpfile->bmps[i]);
1994 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
1997 HLPFILE_DeletePage(hlpfile->first_page);
1998 HLPFILE_DeleteMacro(hlpfile->first_macro);
2000 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2001 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2002 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2003 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2004 HeapFree(GetProcessHeap(), 0, hlpfile);