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 if (phrases.offsets) HeapFree(GetProcessHeap(), 0, phrases.offsets);
272 if (phrases.buffer) HeapFree(GetProcessHeap(), 0, phrases.buffer);
273 if (topic.map) HeapFree(GetProcessHeap(), 0, topic.map);
274 if (file_buffer) 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, page->lpszTitle, 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, *text, *text_end;
836 unsigned nc, ncol = 1;
838 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
840 for (page = hlpfile->first_page; page->next; page = page->next) /* Nothing */;
841 for (paragraphptr = &page->first_paragraph; *paragraphptr;
842 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
844 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
846 size = GET_UINT(buf, 0x4);
847 text = HeapAlloc(GetProcessHeap(), 0, size);
848 if (!text) return FALSE;
849 if (hlpfile->hasPhrases)
851 HLPFILE_Uncompress2(buf + GET_UINT(buf, 0x10), end, text, text + size);
855 if (GET_UINT(buf, 0x4) > GET_UINT(buf, 0) - GET_UINT(buf, 0x10))
857 /* block is compressed */
858 HLPFILE_Uncompress3(text, text + size, buf + GET_UINT(buf, 0x10), end);
862 text = buf + GET_UINT(buf, 0x10);
865 text_end = text + size;
868 format_end = buf + GET_UINT(buf, 0x10);
871 *len = fetch_ushort(&format);
873 if (buf[0x14] == 0x23)
879 WINE_TRACE("#cols %u\n", ncol);
881 if (type == 0 || type == 2)
886 for (nc = 0; nc < ncol; nc++)
888 WINE_TRACE("looking for format at offset %u for column %d\n", format - (buf + 0x15), nc);
889 if (buf[0x14] == 0x23)
892 bits = GET_USHORT(format, 0); format += 2;
893 if (bits & 0x0001) fetch_long(&format);
894 if (bits & 0x0002) fetch_short(&format);
895 if (bits & 0x0004) fetch_short(&format);
896 if (bits & 0x0008) fetch_short(&format);
897 if (bits & 0x0010) fetch_short(&format);
898 if (bits & 0x0020) fetch_short(&format);
899 if (bits & 0x0040) fetch_short(&format);
900 if (bits & 0x0100) format += 3;
903 int ntab = fetch_short(&format);
908 ts = fetch_ushort(&format);
909 if (ts & 0x4000) fetch_ushort(&format);
912 /* 0x0400, 0x0800 and 0x1000 don't need space */
913 if ((bits & 0xE080) != 0)
914 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
916 while (text < text_end && format < format_end)
918 WINE_TRACE("Got text: '%s' (%p/%p - %p/%p)\n", text, text, text_end, format, format_end);
919 textsize = strlen(text) + 1;
922 paragraph = HeapAlloc(GetProcessHeap(), 0,
923 sizeof(HLPFILE_PARAGRAPH) + textsize);
924 if (!paragraph) return FALSE;
925 *paragraphptr = paragraph;
926 paragraphptr = ¶graph->next;
928 paragraph->next = NULL;
929 paragraph->link = attributes.link;
930 if (paragraph->link) paragraph->link->wRefCount++;
931 paragraph->cookie = para_normal_text;
932 paragraph->u.text.wFont = attributes.wFont;
933 paragraph->u.text.wVSpace = attributes.wVSpace;
934 paragraph->u.text.wHSpace = attributes.wHSpace;
935 paragraph->u.text.wIndent = attributes.wIndent;
936 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
937 strcpy(paragraph->u.text.lpszText, text);
939 attributes.wVSpace = 0;
940 attributes.wHSpace = 0;
942 /* else: null text, keep on storing attributes */
951 WINE_TRACE("format=%02x\n", *format);
955 WINE_FIXME("NIY20\n");
960 WINE_FIXME("NIY21\n");
965 attributes.wFont = GET_USHORT(format, 1);
966 WINE_TRACE("Changing font to %d\n", attributes.wFont);
971 attributes.wVSpace++;
976 attributes.wVSpace++;
977 attributes.wIndent = 0;
982 attributes.wIndent++;
996 BYTE pos = (*format - 0x86);
997 BYTE type = format[1];
1001 size = fetch_long(&format);
1003 paragraph = HeapAlloc(GetProcessHeap(), 0,
1004 sizeof(HLPFILE_PARAGRAPH) + textsize);
1005 if (!paragraph) return FALSE;
1006 *paragraphptr = paragraph;
1007 paragraphptr = ¶graph->next;
1009 paragraph->next = NULL;
1010 paragraph->link = attributes.link;
1011 if (paragraph->link) paragraph->link->wRefCount++;
1012 paragraph->cookie = para_bitmap;
1013 paragraph->u.gfx.pos = pos;
1017 fetch_ushort(&format); /* hot spot */
1020 switch (GET_SHORT(format, 0))
1023 HLPFILE_LoadGfxByIndex(hlpfile, GET_SHORT(format, 2),
1027 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1028 GET_SHORT(format, 0),
1029 size, GET_SHORT(format, 2));
1030 HLPFILE_LoadGfxByAddr(hlpfile, format + 2, size - 4,
1034 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1039 WINE_FIXME("Got an embedded element %s\n", format + 6);
1042 WINE_FIXME("Got a type %d picture\n", type);
1045 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1052 HLPFILE_FreeLink(attributes.link);
1053 attributes.link = NULL;
1059 WINE_FIXME("NIY non-break space/hyphen\n");
1071 WINE_TRACE("macro => %s\n", format + 3);
1072 HLPFILE_FreeLink(attributes.link);
1073 attributes.link = HLPFILE_AllocLink(hlp_link_macro, format + 3,
1074 0, !(*format & 4), -1);
1075 format += 3 + GET_USHORT(format, 1);
1080 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1088 HLPFILE_FreeLink(attributes.link);
1089 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1091 GET_UINT(format, 1),
1092 !(*format & 4), -1);
1101 char* ptr = format + 8;
1102 BYTE type = format[3];
1106 if (type == 1) wnd = *ptr++;
1107 if (type == 4 || type == 6)
1110 ptr += strlen(ptr) + 1;
1113 str = hlpfile->lpszPath;
1116 for (wnd = hlpfile->numWindows - 1; wnd >= 0; wnd--)
1118 if (!strcmp(ptr, hlpfile->windows[wnd].name)) break;
1121 WINE_WARN("Couldn't find window info for %s\n", ptr);
1123 HLPFILE_FreeLink(attributes.link);
1124 attributes.link = HLPFILE_AllocLink((*format & 4) ? hlp_link_link : hlp_link_popup,
1125 str, GET_UINT(format, 4),
1126 !(*format & 1), wnd);
1128 format += 3 + GET_USHORT(format, 1);
1132 WINE_WARN("format %02x\n", *format);
1137 if (text_end != buf + GET_UINT(buf, 0x10) + size)
1138 HeapFree(GetProcessHeap(), 0, text_end - size);
1142 /******************************************************************
1147 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1150 unsigned i, len, idx;
1151 unsigned face_num, dscr_num, face_offset, dscr_offset;
1154 if (!HLPFILE_FindSubFile("|FONT", &ref, &end))
1156 WINE_WARN("no subfile FONT\n");
1157 hlpfile->numFonts = 0;
1158 hlpfile->fonts = NULL;
1164 face_num = GET_USHORT(ref, 0);
1165 dscr_num = GET_USHORT(ref, 2);
1166 face_offset = GET_USHORT(ref, 4);
1167 dscr_offset = GET_USHORT(ref, 6);
1169 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1170 face_num, face_offset, dscr_num, dscr_offset);
1172 hlpfile->numFonts = dscr_num;
1173 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1175 len = (dscr_offset - face_offset) / face_num;
1176 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1177 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1178 for (i = 0; i < dscr_num; i++)
1180 flag = ref[dscr_offset + i * 11 + 0];
1181 family = ref[dscr_offset + i * 11 + 2];
1183 hlpfile->fonts[i].LogFont.lfHeight = -ref[dscr_offset + i * 11 + 1] / 2;
1184 hlpfile->fonts[i].LogFont.lfWidth = 0;
1185 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1186 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1187 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1188 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1189 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1190 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1191 hlpfile->fonts[i].LogFont.lfCharSet = ANSI_CHARSET;
1192 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1193 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1194 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1195 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1199 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1200 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1201 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1202 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1203 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1204 default: WINE_FIXME("Unknown family %u\n", family);
1206 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1210 strncpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1211 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1) + 1] = '\0';
1215 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1216 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1218 hlpfile->fonts[i].hFont = 0;
1219 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1220 ref[dscr_offset + i * 11 + 6],
1221 ref[dscr_offset + i * 11 + 7]);
1222 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1223 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1229 X(4, "dblUnderline"),
1231 ref[dscr_offset + i * 11 + 1],
1233 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1234 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1239 /***********************************************************************
1241 * HLPFILE_ReadFileToBuffer
1243 static BOOL HLPFILE_ReadFileToBuffer(HFILE hFile)
1245 BYTE header[16], dummy[1];
1248 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1251 if (GET_UINT(header, 0) != 0x00035F3F)
1252 {WINE_WARN("wrong header\n"); return FALSE;};
1254 size = GET_UINT(header, 12);
1255 file_buffer = HeapAlloc(GetProcessHeap(), 0, size + 1);
1256 if (!file_buffer) return FALSE;
1258 memcpy(file_buffer, header, 16);
1259 if (_hread(hFile, file_buffer + 16, size - 16) != size - 16)
1260 {WINE_WARN("filesize1\n"); return FALSE;};
1262 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1264 file_buffer[size] = '\0'; /* FIXME: was '0', sounds ackward to me */
1269 /***********************************************************************
1271 * HLPFILE_FindSubFile
1273 static BOOL HLPFILE_FindSubFile(LPCSTR name, BYTE **subbuf, BYTE **subend)
1275 BYTE *root = file_buffer + GET_UINT(file_buffer, 4);
1276 BYTE *end = file_buffer + GET_UINT(file_buffer, 12);
1287 /* FIXME: this should be using the EnumBTree functions from this file */
1288 pgsize = GET_USHORT(bth, 4);
1289 WINE_TRACE("%s => pgsize=%u #pg=%u rootpg=%u #lvl=%u\n",
1290 name, pgsize, GET_USHORT(bth, 30), GET_USHORT(bth, 26), GET_USHORT(bth, 32));
1292 ptr = bth + 38 + GET_USHORT(bth, 26) * pgsize;
1294 for (n = 1; n < GET_USHORT(bth, 32); n++)
1296 nentries = GET_USHORT(ptr, 2);
1297 pglast = GET_USHORT(ptr, 4);
1298 WINE_TRACE("[%u]: #entries=%u next=%u\n", n, nentries, pglast);
1301 for (i = 0; i < nentries; i++)
1303 WINE_TRACE("<= %s\n", ptr);
1304 if (strcmp(name, ptr) < 0) break;
1305 ptr += strlen(ptr) + 1;
1306 pglast = GET_USHORT(ptr, 0);
1309 ptr = bth + 38 + pglast * pgsize;
1312 nentries = GET_USHORT(ptr, 2);
1314 for (i = 0; i < nentries; i++)
1317 ptr += strlen(fname) + 1;
1318 WINE_TRACE("\\- %s\n", fname);
1319 if (strcmp(fname, name) == 0)
1321 *subbuf = file_buffer + GET_UINT(ptr, 0);
1322 *subend = *subbuf + GET_UINT(*subbuf, 0);
1323 if (file_buffer > *subbuf || *subbuf > *subend || *subend > end)
1325 WINE_WARN("size mismatch\n");
1336 /***********************************************************************
1338 * HLPFILE_SystemCommands
1340 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1342 BYTE *buf, *ptr, *end;
1343 HLPFILE_MACRO *macro, **m;
1345 unsigned short magic, minor, major, flags;
1347 hlpfile->lpszTitle = NULL;
1349 if (!HLPFILE_FindSubFile("|SYSTEM", &buf, &end)) return FALSE;
1351 magic = GET_USHORT(buf + 9, 0);
1352 minor = GET_USHORT(buf + 9, 2);
1353 major = GET_USHORT(buf + 9, 4);
1354 /* gen date on 4 bytes */
1355 flags = GET_USHORT(buf + 9, 10);
1356 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1357 magic, major, minor, flags);
1358 if (magic != 0x036C || major != 1)
1359 {WINE_WARN("Wrong system header\n"); return FALSE;}
1360 if (minor <= 16) {WINE_WARN("too old file format (NIY)\n"); return FALSE;}
1361 if (flags & 8) {WINE_WARN("Unsupported yet page size\n"); return FALSE;}
1363 hlpfile->version = minor;
1364 hlpfile->flags = flags;
1366 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1368 switch (GET_USHORT(ptr, 0))
1371 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1372 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(ptr + 4) + 1);
1373 if (!hlpfile->lpszTitle) return FALSE;
1374 lstrcpy(hlpfile->lpszTitle, ptr + 4);
1375 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1379 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1380 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(ptr + 4) + 1);
1381 if (!hlpfile->lpszCopyright) return FALSE;
1382 lstrcpy(hlpfile->lpszCopyright, ptr + 4);
1383 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1387 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1388 hlpfile->contents_start = GET_UINT(ptr, 4);
1389 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1393 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(ptr + 4) + 1);
1395 p = (char*)macro + sizeof(HLPFILE_MACRO);
1396 lstrcpy(p, (LPSTR)ptr + 4);
1397 macro->lpszMacro = p;
1399 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1404 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1406 if (hlpfile->windows)
1407 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1408 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1410 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1411 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1413 if (hlpfile->windows)
1415 unsigned flags = GET_USHORT(ptr, 4);
1416 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1418 if (flags & 0x0001) strcpy(wi->type, ptr + 6); else wi->type[0] = '\0';
1419 if (flags & 0x0002) strcpy(wi->name, ptr + 16); else wi->name[0] = '\0';
1420 if (flags & 0x0004) strcpy(wi->caption, ptr + 25); else strncpy(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1421 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1422 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1423 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1424 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1425 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1426 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1427 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1428 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",
1429 flags & 0x0001 ? 'T' : 't',
1430 flags & 0x0002 ? 'N' : 'n',
1431 flags & 0x0004 ? 'C' : 'c',
1432 flags & 0x0008 ? 'X' : 'x',
1433 flags & 0x0010 ? 'Y' : 'y',
1434 flags & 0x0020 ? 'W' : 'w',
1435 flags & 0x0040 ? 'H' : 'h',
1436 flags & 0x0080 ? 'S' : 's',
1437 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1438 wi->size.cx, wi->size.cy);
1442 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1448 /***********************************************************************
1450 * HLPFILE_UncompressedLZ77_Size
1452 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
1459 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1463 int code = GET_USHORT(ptr, 0);
1464 int len = 3 + (code >> 12);
1468 else newsize++, ptr++;
1475 /***********************************************************************
1477 * HLPFILE_UncompressLZ77
1479 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
1486 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1490 int code = GET_USHORT(ptr, 0);
1491 int len = 3 + (code >> 12);
1492 int offset = code & 0xfff;
1493 memcpy(newptr, newptr - offset - 1, len);
1497 else *newptr++ = *ptr++;
1504 /***********************************************************************
1506 * HLPFILE_UncompressLZ77_Phrases
1508 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
1510 UINT i, num, dec_size;
1513 if (!HLPFILE_FindSubFile("|Phrases", &buf, &end)) return FALSE;
1515 num = phrases.num = GET_USHORT(buf, 9);
1516 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
1518 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
1520 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1521 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1522 if (!phrases.offsets || !phrases.buffer) return FALSE;
1524 for (i = 0; i <= num; i++)
1525 phrases.offsets[i] = GET_USHORT(buf, 0x11 + 2 * i) - 2 * num - 2;
1527 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, phrases.buffer);
1529 hlpfile->hasPhrases = TRUE;
1533 /***********************************************************************
1535 * HLPFILE_Uncompress_Phrases40
1537 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
1539 UINT num, dec_size, cpr_size;
1540 BYTE *buf_idx, *end_idx;
1541 BYTE *buf_phs, *end_phs;
1543 long* ptr, mask = 0;
1546 if (!HLPFILE_FindSubFile("|PhrIndex", &buf_idx, &end_idx) ||
1547 !HLPFILE_FindSubFile("|PhrImage", &buf_phs, &end_phs)) return FALSE;
1549 ptr = (long*)(buf_idx + 9 + 28);
1550 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
1551 num = phrases.num = GET_USHORT(buf_idx, 9 + 4);
1553 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
1554 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
1555 GET_UINT(buf_idx, 9 + 0),
1556 GET_UINT(buf_idx, 9 + 4),
1557 GET_UINT(buf_idx, 9 + 8),
1558 GET_UINT(buf_idx, 9 + 12),
1559 GET_UINT(buf_idx, 9 + 16),
1560 GET_UINT(buf_idx, 9 + 20),
1561 GET_USHORT(buf_idx, 9 + 24),
1562 GET_USHORT(buf_idx, 9 + 26));
1564 dec_size = GET_UINT(buf_idx, 9 + 12);
1565 cpr_size = GET_UINT(buf_idx, 9 + 16);
1567 if (dec_size != cpr_size &&
1568 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
1570 WINE_WARN("size mismatch %u %u\n",
1571 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1572 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1575 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1576 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1577 if (!phrases.offsets || !phrases.buffer) return FALSE;
1579 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
1581 phrases.offsets[0] = 0;
1582 for (i = 0; i < num; i++)
1584 for (n = 1; getbit(); n += 1 << bc);
1586 if (bc > 1 && getbit()) n += 2;
1587 if (bc > 2 && getbit()) n += 4;
1588 if (bc > 3 && getbit()) n += 8;
1589 if (bc > 4 && getbit()) n += 16;
1590 phrases.offsets[i + 1] = phrases.offsets[i] + n;
1594 if (dec_size == cpr_size)
1595 memcpy(phrases.buffer, buf_phs + 9, dec_size);
1597 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, phrases.buffer);
1599 hlpfile->hasPhrases = FALSE;
1603 /***********************************************************************
1605 * HLPFILE_Uncompress_Topic
1607 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
1609 BYTE *buf, *ptr, *end, *newptr;
1612 if (!HLPFILE_FindSubFile("|TOPIC", &buf, &end))
1613 {WINE_WARN("topic0\n"); return FALSE;}
1615 switch (hlpfile->flags & (8|4))
1618 WINE_FIXME("Unsupported format\n");
1622 topic.wMapLen = (end - buf - 1) / 0x1000 + 1;
1624 for (i = 0; i < topic.wMapLen; i++)
1626 ptr = buf + i * 0x1000;
1628 /* I don't know why, it's necessary for printman.hlp */
1629 if (ptr + 0x44 > end) ptr = end - 0x44;
1631 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + 0x1000));
1634 topic.map = HeapAlloc(GetProcessHeap(), 0,
1635 topic.wMapLen * sizeof(topic.map[0]) + newsize);
1636 if (!topic.map) return FALSE;
1637 newptr = (char*)(topic.map + topic.wMapLen);
1638 topic.end = newptr + newsize;
1640 for (i = 0; i < topic.wMapLen; i++)
1642 ptr = buf + i * 0x1000;
1643 if (ptr + 0x44 > end) ptr = end - 0x44;
1645 topic.map[i] = newptr;
1646 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + 0x1000), newptr);
1650 /* basically, we need to copy the 0x1000 byte pages (removing the first 0x0C) in
1651 * one single are in memory
1653 #define DST_LEN (0x1000 - 0x0C)
1655 newsize = end - buf;
1656 /* number of destination pages */
1657 topic.wMapLen = (newsize - 1) / DST_LEN + 1;
1658 topic.map = HeapAlloc(GetProcessHeap(), 0,
1659 topic.wMapLen * (sizeof(topic.map[0]) + DST_LEN));
1660 if (!topic.map) return FALSE;
1661 newptr = (char*)(topic.map + topic.wMapLen);
1662 topic.end = newptr + newsize;
1664 for (i = 0; i < topic.wMapLen; i++)
1666 topic.map[i] = newptr + i * DST_LEN;
1667 memcpy(topic.map[i], buf + i * 0x1000 + 0x0C, DST_LEN);
1675 /***********************************************************************
1677 * HLPFILE_Uncompress2
1680 static void HLPFILE_Uncompress2(const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
1682 BYTE *phptr, *phend;
1686 while (ptr < end && newptr < newend)
1688 if (!*ptr || *ptr >= 0x10)
1692 code = 0x100 * ptr[0] + ptr[1];
1693 index = (code - 0x100) / 2;
1695 phptr = phrases.buffer + phrases.offsets[index];
1696 phend = phrases.buffer + phrases.offsets[index + 1];
1698 if (newptr + (phend - phptr) > newend)
1700 WINE_FIXME("buffer overflow %p > %p for %d bytes\n",
1701 newptr, newend, phend - phptr);
1704 memcpy(newptr, phptr, phend - phptr);
1705 newptr += phend - phptr;
1706 if (code & 1) *newptr++ = ' ';
1711 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
1714 /******************************************************************
1715 * HLPFILE_Uncompress3
1719 static BOOL HLPFILE_Uncompress3(char* dst, const char* dst_end,
1720 const BYTE* src, const BYTE* src_end)
1724 for (; src < src_end; src++)
1726 if ((*src & 1) == 0)
1729 if (idx > phrases.num)
1731 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1736 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1737 if (dst + len <= dst_end)
1738 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1741 else if ((*src & 0x03) == 0x01)
1743 idx = (*src + 1) * 64;
1745 if (idx > phrases.num)
1747 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1752 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1753 if (dst + len <= dst_end)
1754 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1757 else if ((*src & 0x07) == 0x03)
1759 len = (*src / 8) + 1;
1760 if (dst + len <= dst_end)
1761 memcpy(dst, src + 1, len);
1766 len = (*src / 16) + 1;
1767 if (dst + len <= dst_end)
1768 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
1773 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
1777 /******************************************************************
1778 * HLPFILE_UncompressRLE
1782 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
1785 BYTE* sdst = *dst + dstsz;
1790 if (!(ch & 0x7F)) continue;
1794 if ((*dst) + ch < sdst)
1795 memcpy(*dst, src, ch);
1800 if ((*dst) + ch < sdst)
1801 memset(*dst, (char)*src++, ch);
1806 WINE_WARN("Buffer X-flow: d(%u) instead of d(%u)\n",
1807 *dst - (sdst - dstsz), dstsz);
1810 /******************************************************************
1811 * HLPFILE_EnumBTreeLeaves
1815 static void HLPFILE_EnumBTreeLeaves(const BYTE* buf, const BYTE* end, unsigned (*fn)(const BYTE*, void*), void* user)
1817 unsigned psize, pnext;
1821 num = GET_UINT(buf, 9 + 34);
1822 psize = GET_USHORT(buf, 9 + 4);
1823 nlvl = GET_USHORT(buf, 9 + 32);
1824 pnext = GET_USHORT(buf, 9 + 26);
1826 WINE_TRACE("BTree: #entries=%u pagSize=%u #levels=%u #pages=%u root=%u struct%16s\n",
1827 num, psize, nlvl, GET_USHORT(buf, 9 + 30), pnext, buf + 9 + 6);
1832 ptr = (buf + 9 + 38) + pnext * psize;
1833 WINE_TRACE("BTree: (index[%u]) unused=%u #entries=%u <%u\n",
1834 pnext, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2), GET_USHORT(ptr, 4));
1835 pnext = GET_USHORT(ptr, 4);
1837 while (pnext != 0xFFFF)
1839 const BYTE* node_page;
1840 unsigned short limit;
1842 node_page = ptr = (buf + 9 + 38) + pnext * psize;
1843 limit = GET_USHORT(ptr, 2);
1844 WINE_TRACE("BTree: (leaf [%u]) unused=%u #entries=%u <%u >%u\n",
1845 pnext, GET_USHORT(ptr, 0), limit, GET_USHORT(ptr, 4), GET_USHORT(ptr, 6));
1848 ptr += (fn)(ptr, user);
1849 pnext = GET_USHORT(node_page, 6);
1858 static unsigned myfn(const BYTE* ptr, void* user)
1860 struct myfncb* m = user;
1862 m->hlpfile->Context[m->i].lHash = GET_UINT(ptr, 0);
1863 m->hlpfile->Context[m->i].offset = GET_UINT(ptr, 4);
1868 /***********************************************************************
1870 * HLPFILE_GetContext
1872 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
1878 if (!HLPFILE_FindSubFile("|CONTEXT", &cbuf, &cend)) {WINE_WARN("context0\n"); return FALSE;}
1880 clen = GET_UINT(cbuf, 0x2b);
1881 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen * sizeof(HLPFILE_CONTEXT));
1882 if (!hlpfile->Context) return FALSE;
1883 hlpfile->wContextLen = clen;
1885 m.hlpfile = hlpfile;
1887 HLPFILE_EnumBTreeLeaves(cbuf, cend, myfn, &m);
1892 /******************************************************************
1893 * HLPFILE_DeleteLink
1897 void HLPFILE_FreeLink(HLPFILE_LINK* link)
1899 if (link && !--link->wRefCount)
1900 HeapFree(GetProcessHeap(), 0, link);
1903 /***********************************************************************
1905 * HLPFILE_DeleteParagraph
1907 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
1909 HLPFILE_PARAGRAPH* next;
1913 next = paragraph->next;
1915 if (paragraph->cookie == para_metafile)
1916 DeleteMetaFile(paragraph->u.gfx.u.mf.hMetaFile);
1918 HLPFILE_FreeLink(paragraph->link);
1920 HeapFree(GetProcessHeap(), 0, paragraph);
1925 /***********************************************************************
1929 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
1931 HLPFILE_MACRO* next;
1936 HeapFree(GetProcessHeap(), 0, macro);
1941 /***********************************************************************
1945 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
1952 HLPFILE_DeleteParagraph(page->first_paragraph);
1953 HLPFILE_DeleteMacro(page->first_macro);
1954 HeapFree(GetProcessHeap(), 0, page);
1959 /***********************************************************************
1961 * HLPFILE_FreeHlpFile
1963 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
1967 if (!hlpfile || --hlpfile->wRefCount > 0) return;
1969 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
1970 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
1971 else first_hlpfile = hlpfile->next;
1973 if (hlpfile->numFonts)
1975 for (i = 0; i < hlpfile->numFonts; i++)
1977 DeleteObject(hlpfile->fonts[i].hFont);
1979 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
1982 if (hlpfile->numBmps)
1984 for (i = 0; i < hlpfile->numBmps; i++)
1986 DeleteObject(hlpfile->bmps[i]);
1988 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
1991 HLPFILE_DeletePage(hlpfile->first_page);
1992 HLPFILE_DeleteMacro(hlpfile->first_macro);
1994 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
1995 if (hlpfile->Context) HeapFree(GetProcessHeap(), 0, hlpfile->Context);
1996 if (hlpfile->lpszTitle) HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
1997 if (hlpfile->lpszCopyright) HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
1998 HeapFree(GetProcessHeap(), 0, hlpfile);