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
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
33 static inline unsigned short GET_USHORT(const BYTE* buffer, unsigned i)
35 return (BYTE)buffer[i] + 0x100 * (BYTE)buffer[i + 1];
38 static inline short GET_SHORT(const BYTE* buffer, unsigned i)
40 return (BYTE)buffer[i] + 0x100 * (signed char)buffer[i+1];
43 static inline unsigned GET_UINT(const BYTE* buffer, unsigned i)
45 return GET_USHORT(buffer, i) + 0x10000 * GET_USHORT(buffer, i + 2);
48 static HLPFILE *first_hlpfile = 0;
49 static BYTE *file_buffer;
74 static BOOL HLPFILE_DoReadHlpFile(HLPFILE*, LPCSTR);
75 static BOOL HLPFILE_ReadFileToBuffer(HFILE);
76 static BOOL HLPFILE_FindSubFile(LPCSTR name, BYTE**, BYTE**);
77 static BOOL HLPFILE_SystemCommands(HLPFILE*);
78 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end);
79 static BYTE* HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr);
80 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE*);
81 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE*);
82 static BOOL HLPFILE_Uncompress_Topic(HLPFILE*);
83 static BOOL HLPFILE_GetContext(HLPFILE*);
84 static BOOL HLPFILE_AddPage(HLPFILE*, BYTE*, BYTE*, unsigned);
85 static BOOL HLPFILE_AddParagraph(HLPFILE*, BYTE *, BYTE*, unsigned*);
86 static void HLPFILE_Uncompress2(const BYTE*, const BYTE*, BYTE*, const BYTE*);
87 static BOOL HLPFILE_Uncompress3(char*, const char*, const BYTE*, const BYTE*);
88 static void HLPFILE_UncompressRLE(const BYTE* src, unsigned sz, BYTE** dst);
89 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile);
91 /***********************************************************************
93 * HLPFILE_PageByNumber
95 HLPFILE_PAGE *HLPFILE_PageByNumber(LPCSTR lpszPath, UINT wNum)
98 HLPFILE *hlpfile = HLPFILE_ReadHlpFile(lpszPath);
100 if (!hlpfile) return 0;
102 WINE_TRACE("[%s/%u]\n", lpszPath, wNum);
104 for (page = hlpfile->first_page; page && wNum; page = page->next) wNum--;
106 /* HLPFILE_FreeHlpFile(lpszPath); */
112 * this finds the page containing the offset. The offset can either
113 * refer to the top of the page (offset == page->offset), or
114 * to some paragraph inside the page...
115 * As of today, we only return the page... we should also return
116 * a paragraph, and then, while opening a new page, compute the
117 * y-offset of the paragraph to be shown and scroll the window
120 /******************************************************************
121 * HLPFILE_PageByOffset
125 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset)
130 if (!hlpfile) return 0;
132 WINE_TRACE("<%s>[%lx]\n", hlpfile->lpszPath, offset);
134 if (offset == 0xFFFFFFFF) return NULL;
137 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
139 if (page->offset <= offset && (!found || found->offset < page->offset))
143 WINE_ERR("Page of offset %lu not found in file %s\n",
144 offset, hlpfile->lpszPath);
148 /***********************************************************************
150 * HLPFILE_HlpFilePageByHash
152 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash)
156 if (!hlpfile) return 0;
158 WINE_TRACE("<%s>[%lx]\n", hlpfile->lpszPath, lHash);
160 for (i = 0; i < hlpfile->wContextLen; i++)
162 if (hlpfile->Context[i].lHash == lHash)
163 return HLPFILE_PageByOffset(hlpfile, hlpfile->Context[i].offset);
166 WINE_ERR("Page of hash %lx not found in file %s\n", lHash, hlpfile->lpszPath);
170 /***********************************************************************
174 HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile)
176 HLPFILE_PAGE* page = NULL;
178 if (!hlpfile) return NULL;
180 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start);
181 if (!page) page = hlpfile->first_page;
185 /***********************************************************************
189 LONG HLPFILE_Hash(LPCSTR lpszContext)
194 while ((c = *lpszContext++))
197 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
198 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
199 if (c >= '1' && c <= '9') x = c - '0';
200 if (c == '0') x = 10;
201 if (c == '.') x = 12;
202 if (c == '_') x = 13;
203 if (x) lHash = lHash * 43 + x;
208 /***********************************************************************
210 * HLPFILE_ReadHlpFile
212 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
216 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
218 if (!strcmp(lpszPath, hlpfile->lpszPath))
220 hlpfile->wRefCount++;
225 hlpfile = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
226 if (!hlpfile) return 0;
228 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
229 hlpfile->lpszTitle = NULL;
230 hlpfile->lpszCopyright = NULL;
231 hlpfile->first_page = NULL;
232 hlpfile->first_macro = NULL;
233 hlpfile->wContextLen = 0;
234 hlpfile->Context = NULL;
235 hlpfile->contents_start = 0xFFFFFFFF;
236 hlpfile->prev = NULL;
237 hlpfile->next = first_hlpfile;
238 hlpfile->wRefCount = 1;
240 hlpfile->numBmps = 0;
241 hlpfile->bmps = NULL;
243 hlpfile->numFonts = 0;
244 hlpfile->fonts = NULL;
246 hlpfile->numWindows = 0;
247 hlpfile->windows = NULL;
249 strcpy(hlpfile->lpszPath, lpszPath);
251 first_hlpfile = hlpfile;
252 if (hlpfile->next) hlpfile->next->prev = hlpfile;
254 phrases.offsets = NULL;
255 phrases.buffer = NULL;
260 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
262 HLPFILE_FreeHlpFile(hlpfile);
266 if (phrases.offsets) HeapFree(GetProcessHeap(), 0, phrases.offsets);
267 if (phrases.buffer) HeapFree(GetProcessHeap(), 0, phrases.buffer);
268 if (topic.map) HeapFree(GetProcessHeap(), 0, topic.map);
269 if (file_buffer) HeapFree(GetProcessHeap(), 0, file_buffer);
274 /***********************************************************************
276 * HLPFILE_DoReadHlpFile
278 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
285 unsigned index, old_index, offset, len, offs;
287 hFile = OpenFile(lpszPath, &ofs, OF_READ | OF_SEARCH);
288 if (hFile == HFILE_ERROR) return FALSE;
290 ret = HLPFILE_ReadFileToBuffer(hFile);
292 if (!ret) return FALSE;
294 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
296 /* load phrases support */
297 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
298 HLPFILE_Uncompress_Phrases40(hlpfile);
300 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
301 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
310 /* FIXME this depends on the blocksize, can be 2k in some cases */
311 index = (ref - 0x0C) >> 14;
312 offset = (ref - 0x0C) & 0x3fff;
314 WINE_TRACE("ref=%08lx => [%u/%u]\n", ref, index, offset);
316 if (index >= topic.wMapLen) {WINE_WARN("maplen\n"); break;}
317 buf = topic.map[index] + offset;
318 if (buf + 0x15 >= topic.end) {WINE_WARN("extra\n"); break;}
319 end = min(buf + GET_UINT(buf, 0), topic.end);
320 if (index != old_index) {offs = 0; old_index = index;}
325 if (!HLPFILE_AddPage(hlpfile, buf, end, index * 0x8000L + offs)) return FALSE;
329 if (!HLPFILE_AddParagraph(hlpfile, buf, end, &len)) return FALSE;
334 if (!HLPFILE_AddParagraph(hlpfile, buf, end, &len)) return FALSE;
339 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
342 ref = GET_UINT(buf, 0xc);
343 } while (ref != 0xffffffff);
345 return HLPFILE_GetContext(hlpfile);
348 /***********************************************************************
352 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned offset)
360 if (buf + 0x31 > end) {WINE_WARN("page1\n"); return FALSE;};
361 title = buf + GET_UINT(buf, 0x10);
362 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
364 titlesize = GET_UINT(buf, 4);
365 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
366 if (!page) return FALSE;
367 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
369 if (hlpfile->hasPhrases)
371 HLPFILE_Uncompress2(title, end, page->lpszTitle, page->lpszTitle + titlesize);
375 if (GET_UINT(buf, 0x4) > GET_UINT(buf, 0) - GET_UINT(buf, 0x10))
377 /* need to decompress */
378 HLPFILE_Uncompress3(page->lpszTitle, page->lpszTitle + titlesize,
383 memcpy(page->lpszTitle, title, titlesize);
387 page->lpszTitle[titlesize] = '\0';
389 if (hlpfile->first_page)
393 for (p = hlpfile->first_page; p->next; p = p->next);
399 hlpfile->first_page = page;
403 page->file = hlpfile;
405 page->first_paragraph = NULL;
406 page->first_macro = NULL;
407 page->wNumber = GET_UINT(buf, 0x21);
408 page->offset = offset;
410 page->browse_bwd = GET_UINT(buf, 0x19);
411 page->browse_fwd = GET_UINT(buf, 0x1D);
413 WINE_TRACE("Added page[%d]: title='%s' %08lx << %08x >> %08lx\n",
414 page->wNumber, page->lpszTitle,
415 page->browse_bwd, page->offset, page->browse_fwd);
417 memset(&attributes, 0, sizeof(attributes));
419 /* now load macros */
420 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
421 while (ptr < page->lpszTitle + titlesize)
423 unsigned len = strlen(ptr);
424 WINE_TRACE("macro: %s\n", ptr);
425 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
426 macro->lpszMacro = (char*)(macro + 1);
427 memcpy((char*)macro->lpszMacro, ptr, len + 1);
428 /* FIXME: shall we really link macro in reverse order ??
429 * may produce strange results when played at page opening
431 macro->next = page->first_macro;
432 page->first_macro = macro;
439 static long fetch_long(BYTE** ptr)
445 ret = (*(unsigned long*)(*ptr) - 0x80000000L) / 2;
450 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
457 static unsigned long fetch_ulong(BYTE** ptr)
463 ret = *(unsigned long*)(*ptr) / 2;
468 ret = *(unsigned short*)(*ptr) / 2;
474 static short fetch_short(BYTE** ptr)
480 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
485 ret = (*(unsigned char*)(*ptr) - 0x80) / 2;
491 static unsigned short fetch_ushort(BYTE** ptr)
497 ret = *(unsigned short*)(*ptr) / 2;
502 ret = *(unsigned char*)(*ptr) / 2;
508 /******************************************************************
509 * HLPFILE_DecompressGfx
511 * Decompress the data part of a bitmap or a metafile
513 static BYTE* HLPFILE_DecompressGfx(BYTE* src, unsigned csz, unsigned sz, BYTE packing)
520 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
524 case 0: /* uncompressed */
526 WINE_WARN("Bogus gfx sizes: %u / %u\n", sz, csz);
530 tmp = dst = HeapAlloc(GetProcessHeap(), 0, sz);
531 if (!dst) return NULL;
532 HLPFILE_UncompressRLE(src, csz, &tmp);
534 WINE_FIXME("Bogus gfx sizes: %u/%u\n", tmp - dst, sz);
537 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
538 dst = HeapAlloc(GetProcessHeap(), 0, sz77);
539 if (!dst) return NULL;
540 HLPFILE_UncompressLZ77(src, src + csz, dst);
542 WINE_WARN("Bogus gfx sizes: %u / %u\n", sz77, sz);
544 case 3: /* LZ77 then RLE */
545 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
546 tmp = HeapAlloc(GetProcessHeap(), 0, sz/*sz77*/);
547 if (!tmp) return FALSE;
548 HLPFILE_UncompressLZ77(src, src + csz, tmp);
549 dst = tmp2 = HeapAlloc(GetProcessHeap(), 0, sz);
550 if (!dst) return FALSE;
551 HLPFILE_UncompressRLE(tmp, sz77, &tmp2);
552 if (tmp2 - dst != sz)
553 WINE_WARN("Bogus gfx: %u / %u\n", tmp2 - dst, sz);
554 HeapFree(GetProcessHeap(), 0, tmp);
557 WINE_FIXME("Unsupported packing %u\n", packing);
563 /******************************************************************
568 static BOOL HLPFILE_LoadBitmap(BYTE* beg, BYTE type, BYTE pack,
569 HLPFILE_PARAGRAPH* paragraph)
574 unsigned long off, csz;
577 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
578 if (!bi) return FALSE;
580 ptr = beg + 2; /* for type and pack */
582 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
583 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
584 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
585 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
586 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
587 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
588 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
589 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
590 bi->bmiHeader.biClrImportant = fetch_ulong(&ptr);
591 bi->bmiHeader.biCompression = BI_RGB;
592 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
593 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
594 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
596 csz = fetch_ulong(&ptr);
597 fetch_ulong(&ptr); /* hotspot size */
599 off = GET_UINT(ptr, 0); ptr += 4;
600 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
602 /* now read palette info */
605 unsigned nc = bi->bmiHeader.biClrUsed;
608 /* not quite right, especially for bitfields type of compression */
609 if (!nc && bi->bmiHeader.biBitCount <= 8)
610 nc = 1 << bi->bmiHeader.biBitCount;
612 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
613 if (!bi) return FALSE;
614 for (i = 0; i < nc; i++)
616 bi->bmiColors[i].rgbBlue = ptr[0];
617 bi->bmiColors[i].rgbGreen = ptr[1];
618 bi->bmiColors[i].rgbRed = ptr[2];
619 bi->bmiColors[i].rgbReserved = 0;
623 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
625 paragraph->u.gfx.u.bmp.hBitmap = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
629 if (!paragraph->u.gfx.u.bmp.hBitmap)
630 WINE_ERR("Couldn't create bitmap\n");
632 HeapFree(GetProcessHeap(), 0, bi);
633 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
638 /******************************************************************
639 * HLPFILE_LoadMetaFile
643 static BOOL HLPFILE_LoadMetaFile(BYTE* beg, BYTE pack, HLPFILE_PARAGRAPH* paragraph)
646 unsigned long size, csize;
647 unsigned long off, hsoff;
651 WINE_TRACE("Loading metafile\n");
653 ptr = beg + 2; /* for type and pack */
655 mfp.mm = fetch_ushort(&ptr); /* mapping mode */
657 mfp.xExt = GET_USHORT(ptr, 0);
658 mfp.yExt = GET_USHORT(ptr, 2);
661 size = fetch_ulong(&ptr); /* decompressed size */
662 csize = fetch_ulong(&ptr); /* compressed size */
663 fetch_ulong(&ptr); /* hotspot size */
664 off = GET_UINT(ptr, 0);
665 hsoff = GET_UINT(ptr, 4);
668 WINE_FIXME("sz=%lu csz=%lu (%ld,%ld) offs=%lu/%u,%lu\n",
669 size, csize, mfp.xExt, mfp.yExt, off, ptr - beg, hsoff);
671 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
672 if (!bits) return FALSE;
674 paragraph->cookie = para_metafile;
678 paragraph->u.gfx.u.mf.hMetaFile = SetMetaFileBitsEx(size, bits);
680 if (!paragraph->u.gfx.u.mf.hMetaFile)
681 WINE_FIXME("Couldn't load metafile\n");
683 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
685 paragraph->u.gfx.u.mf.mfSize.cx = mfp.xExt;
686 paragraph->u.gfx.u.mf.mfSize.cy = mfp.yExt;
691 /******************************************************************
692 * HLPFILE_LoadGfxByAddr
696 static BOOL HLPFILE_LoadGfxByAddr(HLPFILE *hlpfile, BYTE* ref,
698 HLPFILE_PARAGRAPH* paragraph)
702 numpict = GET_USHORT(ref, 2);
703 WINE_TRACE("Got picture magic=%04x #=%d\n",
704 GET_USHORT(ref, 0), numpict);
706 for (i = 0; i < numpict; i++)
712 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
713 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
720 case 5: /* device dependent bmp */
721 case 6: /* device independent bmp */
722 HLPFILE_LoadBitmap(beg, type, pack, paragraph);
725 HLPFILE_LoadMetaFile(beg, pack, paragraph);
727 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
730 /* FIXME: hotspots */
732 /* FIXME: implement support for multiple picture format */
733 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
739 /******************************************************************
740 * HLPFILE_LoadGfxByIndex
744 static BOOL HLPFILE_LoadGfxByIndex(HLPFILE *hlpfile, unsigned index,
745 HLPFILE_PARAGRAPH* paragraph)
751 WINE_TRACE("Loading picture #%d\n", index);
753 if (index < hlpfile->numBmps && hlpfile->bmps[index] != NULL)
755 paragraph->u.gfx.u.bmp.hBitmap = hlpfile->bmps[index];
759 sprintf(tmp, "|bm%u", index);
761 if (!HLPFILE_FindSubFile(tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
765 ret = HLPFILE_LoadGfxByAddr(hlpfile, ref, end - ref, paragraph);
768 if (ret && paragraph->cookie == para_bitmap)
770 if (index >= hlpfile->numBmps)
772 hlpfile->numBmps = index + 1;
773 hlpfile->bmps = HeapReAlloc(GetProcessHeap(), 0, hlpfile->bmps,
774 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
776 hlpfile->bmps[index] = paragraph->u.gfx.u.bmp.hBitmap;
781 /******************************************************************
786 static HLPFILE_LINK* HLPFILE_AllocLink(int cookie, const char* str, LONG hash,
787 BOOL clrChange, unsigned wnd)
791 /* FIXME: should build a string table for the attributes.link.lpszPath
792 * they are reallocated for each link
794 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + strlen(str) + 1);
795 if (!link) return NULL;
797 link->cookie = cookie;
798 link->lpszString = (char*)link + sizeof(HLPFILE_LINK);
799 strcpy((char*)link->lpszString, str);
801 link->bClrChange = clrChange ? 1 : 0;
805 WINE_TRACE("Link[%d] to %s@%08lx:%d\n",
806 link->cookie, link->lpszString,
807 link->lHash, link->window);
811 /***********************************************************************
813 * HLPFILE_AddParagraph
815 static BOOL HLPFILE_AddParagraph(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned* len)
818 HLPFILE_PARAGRAPH *paragraph, **paragraphptr;
820 BYTE *format, *format_end, *text, *text_end;
823 unsigned nc, ncol = 1;
825 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
827 for (page = hlpfile->first_page; page->next; page = page->next) /* Nothing */;
828 for (paragraphptr = &page->first_paragraph; *paragraphptr;
829 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
831 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
833 size = GET_UINT(buf, 0x4);
834 text = HeapAlloc(GetProcessHeap(), 0, size);
835 if (!text) return FALSE;
836 if (hlpfile->hasPhrases)
838 HLPFILE_Uncompress2(buf + GET_UINT(buf, 0x10), end, text, text + size);
842 if (GET_UINT(buf, 0x4) > GET_UINT(buf, 0) - GET_UINT(buf, 0x10))
844 /* block is compressed */
845 HLPFILE_Uncompress3(text, text + size, buf + GET_UINT(buf, 0x10), end);
849 text = buf + GET_UINT(buf, 0x10);
852 text_end = text + size;
855 format_end = buf + GET_UINT(buf, 0x10);
858 *len = fetch_ushort(&format);
860 if (buf[0x14] == 0x23)
866 WINE_TRACE("#cols %u\n", ncol);
868 if (type == 0 || type == 2)
873 for (nc = 0; nc < ncol; nc++)
875 WINE_TRACE("looking for format at offset %u for column %d\n", format - (buf + 0x15), nc);
876 if (buf[0x14] == 0x23)
879 bits = GET_USHORT(format, 0); format += 2;
880 if (bits & 0x0001) fetch_long(&format);
881 if (bits & 0x0002) fetch_short(&format);
882 if (bits & 0x0004) fetch_short(&format);
883 if (bits & 0x0008) fetch_short(&format);
884 if (bits & 0x0010) fetch_short(&format);
885 if (bits & 0x0020) fetch_short(&format);
886 if (bits & 0x0040) fetch_short(&format);
887 if (bits & 0x0100) format += 3;
890 int ntab = fetch_short(&format);
895 ts = fetch_ushort(&format);
896 if (ts & 0x4000) fetch_ushort(&format);
899 /* 0x0400, 0x0800 and 0x1000 don't need space */
900 if ((bits & 0xE080) != 0)
901 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
903 while (text < text_end && format < format_end)
905 WINE_TRACE("Got text: '%s' (%p/%p - %p/%p)\n", text, text, text_end, format, format_end);
906 textsize = strlen(text) + 1;
909 paragraph = HeapAlloc(GetProcessHeap(), 0,
910 sizeof(HLPFILE_PARAGRAPH) + textsize);
911 if (!paragraph) return FALSE;
912 *paragraphptr = paragraph;
913 paragraphptr = ¶graph->next;
915 paragraph->next = NULL;
916 paragraph->link = attributes.link;
917 if (paragraph->link) paragraph->link->wRefCount++;
918 paragraph->cookie = para_normal_text;
919 paragraph->u.text.wFont = attributes.wFont;
920 paragraph->u.text.wVSpace = attributes.wVSpace;
921 paragraph->u.text.wHSpace = attributes.wHSpace;
922 paragraph->u.text.wIndent = attributes.wIndent;
923 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
924 strcpy(paragraph->u.text.lpszText, text);
926 attributes.wVSpace = 0;
927 attributes.wHSpace = 0;
929 /* else: null text, keep on storing attributes */
938 WINE_TRACE("format=%02x\n", *format);
942 WINE_FIXME("NIY20\n");
947 WINE_FIXME("NIY21\n");
952 attributes.wFont = GET_USHORT(format, 1);
953 WINE_TRACE("Changing font to %d\n", attributes.wFont);
958 attributes.wVSpace++;
963 attributes.wVSpace++;
964 attributes.wIndent = 0;
969 attributes.wIndent++;
983 BYTE pos = (*format - 0x86);
984 BYTE type = format[1];
988 size = fetch_long(&format);
990 paragraph = HeapAlloc(GetProcessHeap(), 0,
991 sizeof(HLPFILE_PARAGRAPH) + textsize);
992 if (!paragraph) return FALSE;
993 *paragraphptr = paragraph;
994 paragraphptr = ¶graph->next;
996 paragraph->next = NULL;
997 paragraph->link = attributes.link;
998 if (paragraph->link) paragraph->link->wRefCount++;
999 paragraph->cookie = para_bitmap;
1000 paragraph->u.gfx.pos = pos;
1004 fetch_ushort(&format); /* hot spot */
1007 switch (GET_SHORT(format, 0))
1010 HLPFILE_LoadGfxByIndex(hlpfile, GET_SHORT(format, 2),
1014 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1015 GET_SHORT(format, 0),
1016 size, GET_SHORT(format, 2));
1017 HLPFILE_LoadGfxByAddr(hlpfile, format + 2, size - 4,
1021 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1026 WINE_FIXME("Got an embedded element %s\n", format + 6);
1029 WINE_FIXME("Got a type %d picture\n", type);
1032 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1039 HLPFILE_FreeLink(attributes.link);
1040 attributes.link = NULL;
1046 WINE_FIXME("NIY non-break space/hyphen\n");
1058 WINE_TRACE("macro => %s\n", format + 3);
1059 HLPFILE_FreeLink(attributes.link);
1060 attributes.link = HLPFILE_AllocLink(hlp_link_macro, format + 3,
1061 0, !(*format & 4), -1);
1062 format += 3 + GET_USHORT(format, 1);
1067 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1075 HLPFILE_FreeLink(attributes.link);
1076 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1078 GET_UINT(format, 1),
1079 !(*format & 4), -1);
1088 char* ptr = format + 8;
1089 BYTE type = format[3];
1093 if (type == 1) wnd = *ptr++;
1094 if (type == 4 || type == 6)
1097 ptr += strlen(ptr) + 1;
1100 str = hlpfile->lpszPath;
1103 for (wnd = hlpfile->numWindows - 1; wnd >= 0; wnd--)
1105 if (!strcmp(ptr, hlpfile->windows[wnd].name)) break;
1108 WINE_WARN("Couldn't find window info for %s\n", ptr);
1110 HLPFILE_FreeLink(attributes.link);
1111 attributes.link = HLPFILE_AllocLink((*format & 4) ? hlp_link_link : hlp_link_popup,
1112 str, GET_UINT(format, 4),
1113 !(*format & 1), wnd);
1115 format += 3 + GET_USHORT(format, 1);
1119 WINE_WARN("format %02x\n", *format);
1124 if (text_end != buf + GET_UINT(buf, 0x10) + size)
1125 HeapFree(GetProcessHeap(), 0, text_end - size);
1129 /******************************************************************
1134 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1137 unsigned i, len, idx;
1138 unsigned face_num, dscr_num, face_offset, dscr_offset;
1141 if (!HLPFILE_FindSubFile("|FONT", &ref, &end))
1143 WINE_WARN("no subfile FONT\n");
1144 hlpfile->numFonts = 0;
1145 hlpfile->fonts = NULL;
1151 face_num = GET_USHORT(ref, 0);
1152 dscr_num = GET_USHORT(ref, 2);
1153 face_offset = GET_USHORT(ref, 4);
1154 dscr_offset = GET_USHORT(ref, 6);
1156 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1157 face_num, face_offset, dscr_num, dscr_offset);
1159 hlpfile->numFonts = dscr_num;
1160 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1162 len = (dscr_offset - face_offset) / face_num;
1163 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1164 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1165 for (i = 0; i < dscr_num; i++)
1167 flag = ref[dscr_offset + i * 11 + 0];
1168 family = ref[dscr_offset + i * 11 + 2];
1170 hlpfile->fonts[i].LogFont.lfHeight = -ref[dscr_offset + i * 11 + 1] / 2;
1171 hlpfile->fonts[i].LogFont.lfWidth = 0;
1172 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1173 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1174 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1175 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1176 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1177 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1178 hlpfile->fonts[i].LogFont.lfCharSet = ANSI_CHARSET;
1179 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1180 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1181 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1182 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1186 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1187 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1188 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1189 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1190 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1191 default: WINE_FIXME("Unknown family %u\n", family);
1193 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1197 strncpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1198 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1) + 1] = '\0';
1202 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1203 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1205 hlpfile->fonts[i].hFont = 0;
1206 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1207 ref[dscr_offset + i * 11 + 6],
1208 ref[dscr_offset + i * 11 + 7]);
1209 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1210 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1216 X(4, "dblUnderline"),
1218 ref[dscr_offset + i * 11 + 1],
1220 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1221 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1226 /***********************************************************************
1228 * HLPFILE_ReadFileToBuffer
1230 static BOOL HLPFILE_ReadFileToBuffer(HFILE hFile)
1232 BYTE header[16], dummy[1];
1235 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1238 if (GET_UINT(header, 0) != 0x00035F3F)
1239 {WINE_WARN("wrong header\n"); return FALSE;};
1241 size = GET_UINT(header, 12);
1242 file_buffer = HeapAlloc(GetProcessHeap(), 0, size + 1);
1243 if (!file_buffer) return FALSE;
1245 memcpy(file_buffer, header, 16);
1246 if (_hread(hFile, file_buffer + 16, size - 16) != size - 16)
1247 {WINE_WARN("filesize1\n"); return FALSE;};
1249 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1251 file_buffer[size] = '\0'; /* FIXME: was '0', sounds ackward to me */
1256 /***********************************************************************
1258 * HLPFILE_FindSubFile
1260 static BOOL HLPFILE_FindSubFile(LPCSTR name, BYTE **subbuf, BYTE **subend)
1262 BYTE *root = file_buffer + GET_UINT(file_buffer, 4);
1263 BYTE *end = file_buffer + GET_UINT(file_buffer, 12);
1274 /* FIXME: this should be using the EnumBTree functions from this file */
1275 pgsize = GET_USHORT(bth, 4);
1276 WINE_TRACE("%s => pgsize=%u #pg=%u rootpg=%u #lvl=%u\n",
1277 name, pgsize, GET_USHORT(bth, 30), GET_USHORT(bth, 26), GET_USHORT(bth, 32));
1279 ptr = bth + 38 + GET_USHORT(bth, 26) * pgsize;
1281 for (n = 1; n < GET_USHORT(bth, 32); n++)
1283 nentries = GET_USHORT(ptr, 2);
1284 pglast = GET_USHORT(ptr, 4);
1285 WINE_TRACE("[%u]: #entries=%u next=%u\n", n, nentries, pglast);
1288 for (i = 0; i < nentries; i++)
1290 WINE_TRACE("<= %s\n", ptr);
1291 if (strcmp(name, ptr) < 0) break;
1292 ptr += strlen(ptr) + 1;
1293 pglast = GET_USHORT(ptr, 0);
1296 ptr = bth + 38 + pglast * pgsize;
1299 nentries = GET_USHORT(ptr, 2);
1301 for (i = 0; i < nentries; i++)
1304 ptr += strlen(fname) + 1;
1305 WINE_TRACE("\\- %s\n", fname);
1306 if (strcmp(fname, name) == 0)
1308 *subbuf = file_buffer + GET_UINT(ptr, 0);
1309 *subend = *subbuf + GET_UINT(*subbuf, 0);
1310 if (file_buffer > *subbuf || *subbuf > *subend || *subend > end)
1312 WINE_WARN("size mismatch\n");
1323 /***********************************************************************
1325 * HLPFILE_SystemCommands
1327 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1329 BYTE *buf, *ptr, *end;
1330 HLPFILE_MACRO *macro, **m;
1332 unsigned short magic, minor, major, flags;
1334 hlpfile->lpszTitle = NULL;
1336 if (!HLPFILE_FindSubFile("|SYSTEM", &buf, &end)) return FALSE;
1338 magic = GET_USHORT(buf + 9, 0);
1339 minor = GET_USHORT(buf + 9, 2);
1340 major = GET_USHORT(buf + 9, 4);
1341 /* gen date on 4 bytes */
1342 flags = GET_USHORT(buf + 9, 10);
1343 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1344 magic, major, minor, flags);
1345 if (magic != 0x036C || major != 1)
1346 {WINE_WARN("Wrong system header\n"); return FALSE;}
1347 if (minor <= 16) {WINE_WARN("too old file format (NIY)\n"); return FALSE;}
1348 if (flags & 8) {WINE_WARN("Unsupported yet page size\n"); return FALSE;}
1350 hlpfile->version = minor;
1351 hlpfile->flags = flags;
1353 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1355 switch (GET_USHORT(ptr, 0))
1358 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1359 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(ptr + 4) + 1);
1360 if (!hlpfile->lpszTitle) return FALSE;
1361 lstrcpy(hlpfile->lpszTitle, ptr + 4);
1362 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1366 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1367 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(ptr + 4) + 1);
1368 if (!hlpfile->lpszCopyright) return FALSE;
1369 lstrcpy(hlpfile->lpszCopyright, ptr + 4);
1370 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1374 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1375 hlpfile->contents_start = GET_UINT(ptr, 4);
1376 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1380 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(ptr + 4) + 1);
1382 p = (char*)macro + sizeof(HLPFILE_MACRO);
1383 lstrcpy(p, (LPSTR)ptr + 4);
1384 macro->lpszMacro = p;
1386 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1391 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1392 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1393 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1394 if (hlpfile->windows)
1396 unsigned flags = GET_USHORT(ptr, 4);
1397 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1399 if (flags & 0x0001) strcpy(wi->type, ptr + 6); else wi->type[0] = '\0';
1400 if (flags & 0x0002) strcpy(wi->name, ptr + 16); else wi->name[0] = '\0';
1401 if (flags & 0x0004) strcpy(wi->caption, ptr + 25); else strncpy(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1402 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1403 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1404 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1405 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1406 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1407 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1408 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1409 WINE_FIXME("System-Window: flags=%c%c%c%c%c%c%c%c type=%s name=%s caption=%s (%ld,%ld)x(%ld,%ld)\n",
1410 flags & 0x0001 ? 'T' : 't',
1411 flags & 0x0002 ? 'N' : 'n',
1412 flags & 0x0004 ? 'C' : 'c',
1413 flags & 0x0008 ? 'X' : 'x',
1414 flags & 0x0010 ? 'Y' : 'y',
1415 flags & 0x0020 ? 'W' : 'w',
1416 flags & 0x0040 ? 'H' : 'h',
1417 flags & 0x0080 ? 'S' : 's',
1418 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1419 wi->size.cx, wi->size.cy);
1423 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1429 /***********************************************************************
1431 * HLPFILE_UncompressedLZ77_Size
1433 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
1440 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1444 int code = GET_USHORT(ptr, 0);
1445 int len = 3 + (code >> 12);
1449 else newsize++, ptr++;
1456 /***********************************************************************
1458 * HLPFILE_UncompressLZ77
1460 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
1467 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1471 int code = GET_USHORT(ptr, 0);
1472 int len = 3 + (code >> 12);
1473 int offset = code & 0xfff;
1474 memcpy(newptr, newptr - offset - 1, len);
1478 else *newptr++ = *ptr++;
1485 /***********************************************************************
1487 * HLPFILE_UncompressLZ77_Phrases
1489 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
1491 UINT i, num, dec_size;
1494 if (!HLPFILE_FindSubFile("|Phrases", &buf, &end)) return FALSE;
1496 num = phrases.num = GET_USHORT(buf, 9);
1497 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
1499 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
1501 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1502 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1503 if (!phrases.offsets || !phrases.buffer) return FALSE;
1505 for (i = 0; i <= num; i++)
1506 phrases.offsets[i] = GET_USHORT(buf, 0x11 + 2 * i) - 2 * num - 2;
1508 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, phrases.buffer);
1510 hlpfile->hasPhrases = TRUE;
1514 /***********************************************************************
1516 * HLPFILE_Uncompress_Phrases40
1518 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
1520 UINT num, dec_size, cpr_size;
1521 BYTE *buf_idx, *end_idx;
1522 BYTE *buf_phs, *end_phs;
1524 long* ptr, mask = 0;
1527 if (!HLPFILE_FindSubFile("|PhrIndex", &buf_idx, &end_idx) ||
1528 !HLPFILE_FindSubFile("|PhrImage", &buf_phs, &end_phs)) return FALSE;
1530 ptr = (long*)(buf_idx + 9 + 28);
1531 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
1532 num = phrases.num = GET_USHORT(buf_idx, 9 + 4);
1534 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
1535 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
1536 GET_UINT(buf_idx, 9 + 0),
1537 GET_UINT(buf_idx, 9 + 4),
1538 GET_UINT(buf_idx, 9 + 8),
1539 GET_UINT(buf_idx, 9 + 12),
1540 GET_UINT(buf_idx, 9 + 16),
1541 GET_UINT(buf_idx, 9 + 20),
1542 GET_USHORT(buf_idx, 9 + 24),
1543 GET_USHORT(buf_idx, 9 + 26));
1545 dec_size = GET_UINT(buf_idx, 9 + 12);
1546 cpr_size = GET_UINT(buf_idx, 9 + 16);
1548 if (dec_size != cpr_size &&
1549 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
1551 WINE_WARN("size mismatch %u %u\n",
1552 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1553 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1556 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1557 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1558 if (!phrases.offsets || !phrases.buffer) return FALSE;
1560 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
1562 phrases.offsets[0] = 0;
1563 for (i = 0; i < num; i++)
1565 for (n = 1; getbit(); n += 1 << bc);
1567 if (bc > 1 && getbit()) n += 2;
1568 if (bc > 2 && getbit()) n += 4;
1569 if (bc > 3 && getbit()) n += 8;
1570 if (bc > 4 && getbit()) n += 16;
1571 phrases.offsets[i + 1] = phrases.offsets[i] + n;
1575 if (dec_size == cpr_size)
1576 memcpy(phrases.buffer, buf_phs + 9, dec_size);
1578 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, phrases.buffer);
1580 hlpfile->hasPhrases = FALSE;
1584 /***********************************************************************
1586 * HLPFILE_Uncompress_Topic
1588 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
1590 BYTE *buf, *ptr, *end, *newptr;
1593 if (!HLPFILE_FindSubFile("|TOPIC", &buf, &end))
1594 {WINE_WARN("topic0\n"); return FALSE;}
1596 switch (hlpfile->flags & (8|4))
1599 WINE_FIXME("Unsupported format\n");
1603 topic.wMapLen = (end - buf - 1) / 0x1000 + 1;
1605 for (i = 0; i < topic.wMapLen; i++)
1607 ptr = buf + i * 0x1000;
1609 /* I don't know why, it's necessary for printman.hlp */
1610 if (ptr + 0x44 > end) ptr = end - 0x44;
1612 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + 0x1000));
1615 topic.map = HeapAlloc(GetProcessHeap(), 0,
1616 topic.wMapLen * sizeof(topic.map[0]) + newsize);
1617 if (!topic.map) return FALSE;
1618 newptr = (char*)(topic.map + topic.wMapLen);
1619 topic.end = newptr + newsize;
1621 for (i = 0; i < topic.wMapLen; i++)
1623 ptr = buf + i * 0x1000;
1624 if (ptr + 0x44 > end) ptr = end - 0x44;
1626 topic.map[i] = newptr;
1627 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + 0x1000), newptr);
1631 /* basically, we need to copy the 0x1000 byte pages (removing the first 0x0C) in
1632 * one single are in memory
1634 #define DST_LEN (0x1000 - 0x0C)
1636 newsize = end - buf;
1637 /* number of destination pages */
1638 topic.wMapLen = (newsize - 1) / DST_LEN + 1;
1639 topic.map = HeapAlloc(GetProcessHeap(), 0,
1640 topic.wMapLen * (sizeof(topic.map[0]) + DST_LEN));
1641 if (!topic.map) return FALSE;
1642 newptr = (char*)(topic.map + topic.wMapLen);
1643 topic.end = newptr + newsize;
1645 for (i = 0; i < topic.wMapLen; i++)
1647 topic.map[i] = newptr + i * DST_LEN;
1648 memcpy(topic.map[i], buf + i * 0x1000 + 0x0C, DST_LEN);
1656 /***********************************************************************
1658 * HLPFILE_Uncompress2
1661 static void HLPFILE_Uncompress2(const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
1663 BYTE *phptr, *phend;
1667 while (ptr < end && newptr < newend)
1669 if (!*ptr || *ptr >= 0x10)
1673 code = 0x100 * ptr[0] + ptr[1];
1674 index = (code - 0x100) / 2;
1676 phptr = phrases.buffer + phrases.offsets[index];
1677 phend = phrases.buffer + phrases.offsets[index + 1];
1679 if (newptr + (phend - phptr) > newend)
1681 WINE_FIXME("buffer overflow %p > %p for %d bytes\n",
1682 newptr, newend, phend - phptr);
1685 memcpy(newptr, phptr, phend - phptr);
1686 newptr += phend - phptr;
1687 if (code & 1) *newptr++ = ' ';
1692 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
1695 /******************************************************************
1696 * HLPFILE_Uncompress3
1700 static BOOL HLPFILE_Uncompress3(char* dst, const char* dst_end,
1701 const BYTE* src, const BYTE* src_end)
1705 for (; src < src_end; src++)
1707 if ((*src & 1) == 0)
1710 if (idx > phrases.num)
1712 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1717 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1718 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1721 else if ((*src & 0x03) == 0x01)
1723 idx = (*src + 1) * 64;
1725 if (idx > phrases.num)
1727 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1732 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1733 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1736 else if ((*src & 0x07) == 0x03)
1738 len = (*src / 8) + 1;
1739 memcpy(dst, src + 1, len);
1744 len = (*src / 16) + 1;
1745 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
1750 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
1754 /******************************************************************
1755 * HLPFILE_UncompressRLE
1759 static void HLPFILE_UncompressRLE(const BYTE* src, unsigned sz, BYTE** dst)
1764 for (i = 0; i < sz; i++)
1770 memcpy(*dst, src + i + 1, ch);
1775 memset(*dst, (char)src[i + 1], ch);
1782 /******************************************************************
1783 * HLPFILE_EnumBTreeLeaves
1787 static void HLPFILE_EnumBTreeLeaves(const BYTE* buf, const BYTE* end, unsigned (*fn)(const BYTE*, void*), void* user)
1789 unsigned psize, pnext;
1793 num = GET_UINT(buf, 9 + 34);
1794 psize = GET_USHORT(buf, 9 + 4);
1795 nlvl = GET_USHORT(buf, 9 + 32);
1796 pnext = GET_USHORT(buf, 9 + 26);
1798 WINE_TRACE("BTree: #entries=%u pagSize=%u #levels=%u #pages=%u root=%u struct%16s\n",
1799 num, psize, nlvl, GET_USHORT(buf, 9 + 30), pnext, buf + 9 + 6);
1804 ptr = (buf + 9 + 38) + pnext * psize;
1805 WINE_TRACE("BTree: (index[%u]) unused=%u #entries=%u <%u\n",
1806 pnext, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2), GET_USHORT(ptr, 4));
1807 pnext = GET_USHORT(ptr, 4);
1809 while (pnext != 0xFFFF)
1811 const BYTE* node_page;
1812 unsigned short limit;
1814 node_page = ptr = (buf + 9 + 38) + pnext * psize;
1815 limit = GET_USHORT(ptr, 2);
1816 WINE_TRACE("BTree: (leaf [%u]) unused=%u #entries=%u <%u >%u\n",
1817 pnext, GET_USHORT(ptr, 0), limit, GET_USHORT(ptr, 4), GET_USHORT(ptr, 6));
1820 ptr += (fn)(ptr, user);
1821 pnext = GET_USHORT(node_page, 6);
1830 static unsigned myfn(const BYTE* ptr, void* user)
1832 struct myfncb* m = user;
1834 m->hlpfile->Context[m->i].lHash = GET_UINT(ptr, 0);
1835 m->hlpfile->Context[m->i].offset = GET_UINT(ptr, 4);
1840 /***********************************************************************
1842 * HLPFILE_GetContext
1844 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
1850 if (!HLPFILE_FindSubFile("|CONTEXT", &cbuf, &cend)) {WINE_WARN("context0\n"); return FALSE;}
1852 clen = GET_UINT(cbuf, 0x2b);
1853 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen * sizeof(HLPFILE_CONTEXT));
1854 if (!hlpfile->Context) return FALSE;
1855 hlpfile->wContextLen = clen;
1857 m.hlpfile = hlpfile;
1859 HLPFILE_EnumBTreeLeaves(cbuf, cend, myfn, &m);
1864 /******************************************************************
1865 * HLPFILE_DeleteLink
1869 void HLPFILE_FreeLink(HLPFILE_LINK* link)
1871 if (link && !--link->wRefCount)
1872 HeapFree(GetProcessHeap(), 0, link);
1875 /***********************************************************************
1877 * HLPFILE_DeleteParagraph
1879 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
1881 HLPFILE_PARAGRAPH* next;
1885 next = paragraph->next;
1887 if (paragraph->cookie == para_metafile)
1888 DeleteMetaFile(paragraph->u.gfx.u.mf.hMetaFile);
1890 HLPFILE_FreeLink(paragraph->link);
1892 HeapFree(GetProcessHeap(), 0, paragraph);
1897 /***********************************************************************
1901 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
1903 HLPFILE_MACRO* next;
1908 HeapFree(GetProcessHeap(), 0, macro);
1913 /***********************************************************************
1917 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
1924 HLPFILE_DeleteParagraph(page->first_paragraph);
1925 HLPFILE_DeleteMacro(page->first_macro);
1926 HeapFree(GetProcessHeap(), 0, page);
1931 /***********************************************************************
1933 * HLPFILE_FreeHlpFile
1935 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
1939 if (!hlpfile || --hlpfile->wRefCount > 0) return;
1941 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
1942 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
1943 else first_hlpfile = hlpfile->next;
1945 if (hlpfile->numFonts)
1947 for (i = 0; i < hlpfile->numFonts; i++)
1949 DeleteObject(hlpfile->fonts[i].hFont);
1951 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
1954 if (hlpfile->numBmps)
1956 for (i = 0; i < hlpfile->numBmps; i++)
1958 DeleteObject(hlpfile->bmps[i]);
1960 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
1963 HLPFILE_DeletePage(hlpfile->first_page);
1964 HLPFILE_DeleteMacro(hlpfile->first_macro);
1966 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
1967 if (hlpfile->Context) HeapFree(GetProcessHeap(), 0, hlpfile->Context);
1968 if (hlpfile->lpszTitle) HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
1969 if (hlpfile->lpszCopyright) HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
1970 HeapFree(GetProcessHeap(), 0, hlpfile);