4 * Copyright 1996 Ulrich Schmid
5 * 2002, 2008 Eric Pouech
6 * 2007 Kirill K. Smirnov
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
37 static inline unsigned short GET_USHORT(const BYTE* buffer, unsigned i)
39 return (BYTE)buffer[i] + 0x100 * (BYTE)buffer[i + 1];
42 static inline short GET_SHORT(const BYTE* buffer, unsigned i)
44 return (BYTE)buffer[i] + 0x100 * (signed char)buffer[i+1];
47 static inline unsigned GET_UINT(const BYTE* buffer, unsigned i)
49 return GET_USHORT(buffer, i) + 0x10000 * GET_USHORT(buffer, i + 2);
52 static HLPFILE *first_hlpfile = 0;
63 static BOOL HLPFILE_DoReadHlpFile(HLPFILE*, LPCSTR);
64 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE*, HFILE);
65 static BOOL HLPFILE_FindSubFile(HLPFILE*, LPCSTR, BYTE**, BYTE**);
66 static BOOL HLPFILE_SystemCommands(HLPFILE*);
67 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end);
68 static BYTE* HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr);
69 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE*);
70 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE*);
71 static BOOL HLPFILE_Uncompress_Topic(HLPFILE*);
72 static BOOL HLPFILE_GetContext(HLPFILE*);
73 static BOOL HLPFILE_GetKeywords(HLPFILE*);
74 static BOOL HLPFILE_GetMap(HLPFILE*);
75 static BOOL HLPFILE_AddPage(HLPFILE*, BYTE*, BYTE*, unsigned, unsigned);
76 static BOOL HLPFILE_SkipParagraph(HLPFILE*, BYTE *, BYTE*, unsigned*);
77 static void HLPFILE_Uncompress2(HLPFILE*, const BYTE*, const BYTE*, BYTE*, const BYTE*);
78 static BOOL HLPFILE_Uncompress3(HLPFILE*, char*, const char*, const BYTE*, const BYTE*);
79 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz);
80 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile);
82 /***********************************************************************
84 * HLPFILE_PageByNumber
86 static HLPFILE_PAGE *HLPFILE_PageByNumber(HLPFILE* hlpfile, UINT wNum)
91 WINE_TRACE("<%s>[%u]\n", hlpfile->lpszPath, wNum);
93 for (page = hlpfile->first_page; page && temp; page = page->next) temp--;
95 WINE_ERR("Page of number %u not found in file %s\n", wNum, hlpfile->lpszPath);
99 /******************************************************************
100 * HLPFILE_PageByOffset
104 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset, ULONG* relative)
109 if (!hlpfile) return 0;
111 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, offset);
113 if (offset == 0xFFFFFFFF) return NULL;
116 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
118 if (page->offset <= offset && (!found || found->offset < page->offset))
120 *relative = offset - page->offset;
125 WINE_ERR("Page of offset %u not found in file %s\n",
126 offset, hlpfile->lpszPath);
130 /**************************************************************************
133 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
136 static int comp_PageByHash(void *p, const void *key,
137 int leaf, void** next)
139 LONG lKey = (LONG_PTR)key;
140 LONG lTest = (INT)GET_UINT(p, 0);
142 *next = (char *)p+(leaf?8:6);
143 WINE_TRACE("Comparing '%d' with '%d'\n", lKey, lTest);
144 if (lTest < lKey) return -1;
145 if (lTest > lKey) return 1;
149 /***********************************************************************
153 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash, ULONG* relative)
157 if (!hlpfile) return NULL;
158 if (!lHash) return HLPFILE_Contents(hlpfile, relative);
160 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lHash);
162 /* For win 3.0 files hash values are really page numbers */
163 if (hlpfile->version <= 16)
166 return HLPFILE_PageByNumber(hlpfile, lHash);
169 ptr = HLPFILE_BPTreeSearch(hlpfile->Context, LongToPtr(lHash), comp_PageByHash);
172 WINE_ERR("Page of hash %x not found in file %s\n", lHash, hlpfile->lpszPath);
176 return HLPFILE_PageByOffset(hlpfile, GET_UINT(ptr, 4), relative);
179 /***********************************************************************
183 HLPFILE_PAGE *HLPFILE_PageByMap(HLPFILE* hlpfile, LONG lMap, ULONG* relative)
187 if (!hlpfile) return 0;
189 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lMap);
191 for (i = 0; i < hlpfile->wMapLen; i++)
193 if (hlpfile->Map[i].lMap == lMap)
194 return HLPFILE_PageByOffset(hlpfile, hlpfile->Map[i].offset, relative);
197 WINE_ERR("Page of Map %x not found in file %s\n", lMap, hlpfile->lpszPath);
201 /***********************************************************************
205 HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile, ULONG* relative)
207 HLPFILE_PAGE* page = NULL;
209 if (!hlpfile) return NULL;
211 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start, relative);
214 page = hlpfile->first_page;
220 /***********************************************************************
224 LONG HLPFILE_Hash(LPCSTR lpszContext)
229 while ((c = *lpszContext++))
232 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
233 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
234 if (c >= '1' && c <= '9') x = c - '0';
235 if (c == '0') x = 10;
236 if (c == '.') x = 12;
237 if (c == '_') x = 13;
238 if (x) lHash = lHash * 43 + x;
243 /***********************************************************************
245 * HLPFILE_ReadHlpFile
247 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
251 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
253 if (!strcmp(lpszPath, hlpfile->lpszPath))
255 hlpfile->wRefCount++;
260 hlpfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
261 sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
262 if (!hlpfile) return 0;
264 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
265 hlpfile->contents_start = 0xFFFFFFFF;
266 hlpfile->next = first_hlpfile;
267 hlpfile->wRefCount = 1;
269 strcpy(hlpfile->lpszPath, lpszPath);
271 first_hlpfile = hlpfile;
272 if (hlpfile->next) hlpfile->next->prev = hlpfile;
274 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
276 HLPFILE_FreeHlpFile(hlpfile);
283 /***********************************************************************
285 * HLPFILE_DoReadHlpFile
287 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
294 unsigned index, old_index, offset, len, offs;
296 hFile = OpenFile(lpszPath, &ofs, OF_READ);
297 if (hFile == HFILE_ERROR) return FALSE;
299 ret = HLPFILE_ReadFileToBuffer(hlpfile, hFile);
301 if (!ret) return FALSE;
303 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
305 /* load phrases support */
306 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
307 HLPFILE_Uncompress_Phrases40(hlpfile);
309 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
310 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
312 buf = hlpfile->topic_map[0];
319 if (hlpfile->version <= 16)
321 index = (ref - 0x0C) / hlpfile->dsize;
322 offset = (ref - 0x0C) % hlpfile->dsize;
326 index = (ref - 0x0C) >> 14;
327 offset = (ref - 0x0C) & 0x3FFF;
330 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
332 /* we jumped to the next block, adjust pointers */
337 WINE_TRACE("ref=%08x => [%u/%u]\n", ref, index, offset);
339 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
340 buf = hlpfile->topic_map[index] + offset;
341 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
342 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
343 if (index != old_index) {offs = 0; old_index = index;}
348 if (!HLPFILE_AddPage(hlpfile, buf, end, ref, index * 0x8000L + offs)) return FALSE;
354 if (!HLPFILE_SkipParagraph(hlpfile, buf, end, &len)) return FALSE;
359 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
362 if (hlpfile->version <= 16)
364 ref += GET_UINT(buf, 0xc);
365 if (GET_UINT(buf, 0xc) == 0)
369 ref = GET_UINT(buf, 0xc);
370 } while (ref != 0xffffffff);
372 HLPFILE_GetKeywords(hlpfile);
373 HLPFILE_GetMap(hlpfile);
374 if (hlpfile->version <= 16) return TRUE;
375 return HLPFILE_GetContext(hlpfile);
378 /***********************************************************************
382 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned ref, unsigned offset)
386 UINT titlesize, blocksize, datalen;
390 blocksize = GET_UINT(buf, 0);
391 datalen = GET_UINT(buf, 0x10);
392 title = buf + datalen;
393 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
395 titlesize = GET_UINT(buf, 4);
396 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
397 if (!page) return FALSE;
398 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
400 if (titlesize > blocksize - datalen)
402 /* need to decompress */
403 if (hlpfile->hasPhrases)
404 HLPFILE_Uncompress2(hlpfile, title, end, (BYTE*)page->lpszTitle, (BYTE*)page->lpszTitle + titlesize);
405 else if (hlpfile->hasPhrases40)
406 HLPFILE_Uncompress3(hlpfile, page->lpszTitle, page->lpszTitle + titlesize, title, end);
409 WINE_FIXME("Text size is too long, splitting\n");
410 titlesize = blocksize - datalen;
411 memcpy(page->lpszTitle, title, titlesize);
415 memcpy(page->lpszTitle, title, titlesize);
417 page->lpszTitle[titlesize] = '\0';
419 if (hlpfile->first_page)
421 hlpfile->last_page->next = page;
422 page->prev = hlpfile->last_page;
423 hlpfile->last_page = page;
427 hlpfile->first_page = page;
428 hlpfile->last_page = page;
432 page->file = hlpfile;
434 page->first_paragraph = NULL;
435 page->first_macro = NULL;
436 page->first_link = NULL;
437 page->wNumber = GET_UINT(buf, 0x21);
438 page->offset = offset;
439 page->reference = ref;
441 page->browse_bwd = GET_UINT(buf, 0x19);
442 page->browse_fwd = GET_UINT(buf, 0x1D);
444 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
445 page->wNumber, page->lpszTitle,
446 page->browse_bwd, page->offset, page->browse_fwd);
448 memset(&attributes, 0, sizeof(attributes));
450 /* now load macros */
451 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
452 while (ptr < page->lpszTitle + titlesize)
454 unsigned len = strlen(ptr);
457 WINE_TRACE("macro: %s\n", ptr);
458 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
459 macro->lpszMacro = macro_str = (char*)(macro + 1);
460 memcpy(macro_str, ptr, len + 1);
461 /* FIXME: shall we really link macro in reverse order ??
462 * may produce strange results when played at page opening
464 macro->next = page->first_macro;
465 page->first_macro = macro;
472 static long fetch_long(BYTE** ptr)
478 ret = (*(unsigned long*)(*ptr) - 0x80000000L) / 2;
483 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
490 static unsigned long fetch_ulong(BYTE** ptr)
496 ret = *(unsigned long*)(*ptr) / 2;
501 ret = *(unsigned short*)(*ptr) / 2;
507 static short fetch_short(BYTE** ptr)
513 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
518 ret = (*(unsigned char*)(*ptr) - 0x80) / 2;
524 static unsigned short fetch_ushort(BYTE** ptr)
530 ret = *(unsigned short*)(*ptr) / 2;
535 ret = *(unsigned char*)(*ptr) / 2;
541 /***********************************************************************
543 * HLPFILE_SkipParagraph
545 static BOOL HLPFILE_SkipParagraph(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned* len)
549 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
550 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
553 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
556 *len = fetch_ushort(&tmp);
558 else *len = end-buf-15;
563 /******************************************************************
564 * HLPFILE_DecompressGfx
566 * Decompress the data part of a bitmap or a metafile
568 static BYTE* HLPFILE_DecompressGfx(BYTE* src, unsigned csz, unsigned sz, BYTE packing)
575 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
579 case 0: /* uncompressed */
581 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
585 tmp = dst = HeapAlloc(GetProcessHeap(), 0, sz);
586 if (!dst) return NULL;
587 HLPFILE_UncompressRLE(src, src + csz, &tmp, sz);
589 WINE_WARN("Bogus gfx sizes (RunLen): %lu/%u\n", (SIZE_T)(tmp - dst), sz);
592 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
593 dst = HeapAlloc(GetProcessHeap(), 0, sz77);
594 if (!dst) return NULL;
595 HLPFILE_UncompressLZ77(src, src + csz, dst);
597 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
599 case 3: /* LZ77 then RLE */
600 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
601 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
602 if (!tmp) return FALSE;
603 HLPFILE_UncompressLZ77(src, src + csz, tmp);
604 dst = tmp2 = HeapAlloc(GetProcessHeap(), 0, sz);
607 HeapFree(GetProcessHeap(), 0, tmp);
610 HLPFILE_UncompressRLE(tmp, tmp + sz77, &tmp2, sz);
611 if (tmp2 - dst != sz)
612 WINE_WARN("Bogus gfx sizes (LZ77+RunLen): %lu / %u\n", (SIZE_T)(tmp2 - dst), sz);
613 HeapFree(GetProcessHeap(), 0, tmp);
616 WINE_FIXME("Unsupported packing %u\n", packing);
622 static BOOL HLPFILE_RtfAddRawString(struct RtfData* rd, const char* str, size_t sz)
624 if (!rd) return TRUE; /* FIXME: TEMP */
625 if (rd->ptr + sz >= rd->data + rd->allocated)
627 char* new = HeapReAlloc(GetProcessHeap(), 0, rd->data, rd->allocated *= 2);
628 if (!new) return FALSE;
629 rd->ptr = new + (rd->ptr - rd->data);
632 memcpy(rd->ptr, str, sz);
638 static BOOL HLPFILE_RtfAddControl(struct RtfData* rd, const char* str)
640 if (!rd) return TRUE; /* FIXME: TEMP */
641 if (*str == '\\' || *str == '{') rd->in_text = FALSE;
642 else if (*str == '}') rd->in_text = TRUE;
643 return HLPFILE_RtfAddRawString(rd, str, strlen(str));
646 static BOOL HLPFILE_RtfAddText(struct RtfData* rd, const char* str)
653 if (!rd) return TRUE; /* FIXME: TEMP */
656 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
659 for (last = p = str; *p; p++)
661 if (*p < 0) /* escape non ASCII chars */
664 rlen = sprintf(xx, "\\'%x", *(const BYTE*)p);
669 case '{': rlen = 2; replace = "\\{"; break;
670 case '}': rlen = 2; replace = "\\}"; break;
671 case '\\': rlen = 2; replace = "\\\\"; break;
674 if ((p != last && !HLPFILE_RtfAddRawString(rd, last, p - last)) ||
675 !HLPFILE_RtfAddRawString(rd, replace, rlen)) return FALSE;
678 return HLPFILE_RtfAddRawString(rd, last, p - last);
681 /******************************************************************
685 static BOOL HLPFILE_RtfAddHexBytes(struct RtfData* rd, const void* _ptr, unsigned sz)
689 const BYTE* ptr = _ptr;
690 static const char* _2hex = "0123456789abcdef";
692 if (!rd) return TRUE; /* FIXME: TEMP */
695 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
698 for (; sz; sz -= step)
701 for (i = 0; i < step; i++)
703 tmp[2 * i + 0] = _2hex[*ptr >> 4];
704 tmp[2 * i + 1] = _2hex[*ptr++ & 0xF];
706 if (!HLPFILE_RtfAddRawString(rd, tmp, 2 * step)) return FALSE;
711 /******************************************************************
712 * HLPFILE_RtfAddBitmap
715 static BOOL HLPFILE_RtfAddBitmap(struct RtfData* rd, BYTE* beg, BYTE type, BYTE pack)
720 unsigned long off, csz;
725 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
726 if (!bi) return FALSE;
728 ptr = beg + 2; /* for type and pack */
730 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
731 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
732 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
733 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
734 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
735 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
736 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
737 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
738 bi->bmiHeader.biClrImportant = fetch_ulong(&ptr);
739 bi->bmiHeader.biCompression = BI_RGB;
740 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
741 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
742 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
743 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
744 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
745 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
747 csz = fetch_ulong(&ptr);
748 fetch_ulong(&ptr); /* hotspot size */
750 off = GET_UINT(ptr, 0); ptr += 4;
751 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
753 /* now read palette info */
758 nc = bi->bmiHeader.biClrUsed;
759 /* not quite right, especially for bitfields type of compression */
760 if (!nc && bi->bmiHeader.biBitCount <= 8)
761 nc = 1 << bi->bmiHeader.biBitCount;
763 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
764 if (!bi) return FALSE;
765 for (i = 0; i < nc; i++)
767 bi->bmiColors[i].rgbBlue = ptr[0];
768 bi->bmiColors[i].rgbGreen = ptr[1];
769 bi->bmiColors[i].rgbRed = ptr[2];
770 bi->bmiColors[i].rgbReserved = 0;
774 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
777 if (!HLPFILE_RtfAddControl(rd, "{\\pict")) goto done;
780 sprintf(tmp, "\\dibitmap0\\picw%d\\pich%d",
781 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
782 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
783 if (!HLPFILE_RtfAddHexBytes(rd, bi, sizeof(*bi) + nc * sizeof(RGBQUAD))) goto done;
787 sprintf(tmp, "\\wbitmap0\\wbmbitspixel%d\\wbmplanes%d\\picw%d\\pich%d",
788 bi->bmiHeader.biBitCount, bi->bmiHeader.biPlanes,
789 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
790 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
792 if (!HLPFILE_RtfAddHexBytes(rd, pict_beg, bi->bmiHeader.biSizeImage)) goto done;
793 if (!HLPFILE_RtfAddControl(rd, "}")) goto done;
797 HeapFree(GetProcessHeap(), 0, bi);
798 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
803 /******************************************************************
804 * HLPFILE_RtfAddMetaFile
807 static BOOL HLPFILE_RtfAddMetaFile(struct RtfData* rd, BYTE* beg, BYTE pack)
810 unsigned long size, csize;
811 unsigned long off, hsoff;
817 WINE_TRACE("Loading metafile\n");
819 ptr = beg + 2; /* for type and pack */
821 mm = fetch_ushort(&ptr); /* mapping mode */
822 sprintf(tmp, "{\\pict\\wmetafile%d\\picw%d\\pich%d",
823 mm, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2));
824 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
827 size = fetch_ulong(&ptr); /* decompressed size */
828 csize = fetch_ulong(&ptr); /* compressed size */
829 fetch_ulong(&ptr); /* hotspot size */
830 off = GET_UINT(ptr, 0);
831 hsoff = GET_UINT(ptr, 4);
834 WINE_TRACE("sz=%lu csz=%lu offs=%lu/%u,%lu\n",
835 size, csize, off, ptr - beg, hsoff);
837 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
838 if (!bits) return FALSE;
840 ret = HLPFILE_RtfAddHexBytes(rd, bits, size) &&
841 HLPFILE_RtfAddControl(rd, "}");
843 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
848 /******************************************************************
849 * HLPFILE_RtfAddGfxByAddr
852 static BOOL HLPFILE_RtfAddGfxByAddr(struct RtfData* rd, HLPFILE *hlpfile,
853 BYTE* ref, unsigned long size)
857 numpict = GET_USHORT(ref, 2);
858 WINE_TRACE("Got picture magic=%04x #=%d\n", GET_USHORT(ref, 0), numpict);
860 for (i = 0; i < numpict; i++)
866 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
867 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
874 case 5: /* device dependent bmp */
875 case 6: /* device independent bmp */
876 HLPFILE_RtfAddBitmap(rd, beg, type, pack);
879 HLPFILE_RtfAddMetaFile(rd, beg, pack);
881 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
884 /* FIXME: hotspots */
886 /* FIXME: implement support for multiple picture format */
887 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
893 /******************************************************************
894 * HLPFILE_RtfAddGfxByIndex
898 static BOOL HLPFILE_RtfAddGfxByIndex(struct RtfData* rd, HLPFILE *hlpfile,
904 WINE_TRACE("Loading picture #%d\n", index);
906 sprintf(tmp, "|bm%u", index);
908 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
911 return HLPFILE_RtfAddGfxByAddr(rd, hlpfile, ref, end - ref);
914 /******************************************************************
919 static BOOL HLPFILE_LoadBitmap(BYTE* beg, BYTE type, BYTE pack,
920 HLPFILE_PARAGRAPH* paragraph)
925 unsigned long off, csz;
928 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
929 if (!bi) return FALSE;
931 ptr = beg + 2; /* for type and pack */
933 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
934 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
935 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
936 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
937 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
938 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
939 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
940 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
941 bi->bmiHeader.biClrImportant = fetch_ulong(&ptr);
942 bi->bmiHeader.biCompression = BI_RGB;
943 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
944 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
945 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
946 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
947 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
948 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
950 csz = fetch_ulong(&ptr);
951 fetch_ulong(&ptr); /* hotspot size */
953 off = GET_UINT(ptr, 0); ptr += 4;
954 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
956 /* now read palette info */
959 unsigned nc = bi->bmiHeader.biClrUsed;
962 /* not quite right, especially for bitfields type of compression */
963 if (!nc && bi->bmiHeader.biBitCount <= 8)
964 nc = 1 << bi->bmiHeader.biBitCount;
966 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
967 if (!bi) return FALSE;
968 for (i = 0; i < nc; i++)
970 bi->bmiColors[i].rgbBlue = ptr[0];
971 bi->bmiColors[i].rgbGreen = ptr[1];
972 bi->bmiColors[i].rgbRed = ptr[2];
973 bi->bmiColors[i].rgbReserved = 0;
977 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
979 paragraph->u.gfx.u.bmp.hBitmap = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
983 if (!paragraph->u.gfx.u.bmp.hBitmap)
984 WINE_ERR("Couldn't create bitmap\n");
986 HeapFree(GetProcessHeap(), 0, bi);
987 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
992 /******************************************************************
993 * HLPFILE_LoadMetaFile
997 static BOOL HLPFILE_LoadMetaFile(BYTE* beg, BYTE pack, HLPFILE_PARAGRAPH* paragraph)
1000 unsigned long size, csize;
1001 unsigned long off, hsoff;
1003 LPMETAFILEPICT lpmfp;
1005 WINE_TRACE("Loading metafile\n");
1007 ptr = beg + 2; /* for type and pack */
1009 lpmfp = ¶graph->u.gfx.u.mfp;
1010 lpmfp->mm = fetch_ushort(&ptr); /* mapping mode */
1012 lpmfp->xExt = GET_USHORT(ptr, 0);
1013 lpmfp->yExt = GET_USHORT(ptr, 2);
1016 size = fetch_ulong(&ptr); /* decompressed size */
1017 csize = fetch_ulong(&ptr); /* compressed size */
1018 fetch_ulong(&ptr); /* hotspot size */
1019 off = GET_UINT(ptr, 0);
1020 hsoff = GET_UINT(ptr, 4);
1023 WINE_TRACE("sz=%lu csz=%lu (%d,%d) offs=%lu/%lu,%lu\n",
1024 size, csize, lpmfp->xExt, lpmfp->yExt, off, (SIZE_T)(ptr - beg), hsoff);
1026 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
1027 if (!bits) return FALSE;
1029 paragraph->cookie = para_metafile;
1031 lpmfp->hMF = SetMetaFileBitsEx(size, bits);
1034 WINE_FIXME("Couldn't load metafile\n");
1036 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
1041 /******************************************************************
1042 * HLPFILE_LoadGfxByAddr
1046 static BOOL HLPFILE_LoadGfxByAddr(HLPFILE *hlpfile, BYTE* ref,
1048 HLPFILE_PARAGRAPH* paragraph)
1050 unsigned i, numpict;
1052 numpict = GET_USHORT(ref, 2);
1053 WINE_TRACE("Got picture magic=%04x #=%d\n",
1054 GET_USHORT(ref, 0), numpict);
1056 for (i = 0; i < numpict; i++)
1062 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
1063 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
1070 case 5: /* device dependent bmp */
1071 case 6: /* device independent bmp */
1072 HLPFILE_LoadBitmap(beg, type, pack, paragraph);
1075 HLPFILE_LoadMetaFile(beg, pack, paragraph);
1077 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
1080 /* FIXME: hotspots */
1082 /* FIXME: implement support for multiple picture format */
1083 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
1089 /******************************************************************
1090 * HLPFILE_LoadGfxByIndex
1094 static BOOL HLPFILE_LoadGfxByIndex(HLPFILE *hlpfile, unsigned index,
1095 HLPFILE_PARAGRAPH* paragraph)
1101 WINE_TRACE("Loading picture #%d\n", index);
1103 if (index < hlpfile->numBmps && hlpfile->bmps[index] != NULL)
1105 paragraph->u.gfx.u.bmp.hBitmap = hlpfile->bmps[index];
1109 sprintf(tmp, "|bm%u", index);
1111 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
1115 ret = HLPFILE_LoadGfxByAddr(hlpfile, ref, end - ref, paragraph);
1118 if (ret && paragraph->cookie == para_bitmap)
1120 if (index >= hlpfile->numBmps)
1122 hlpfile->numBmps = index + 1;
1124 hlpfile->bmps = HeapReAlloc(GetProcessHeap(), 0, hlpfile->bmps,
1125 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
1127 hlpfile->bmps = HeapAlloc(GetProcessHeap(), 0,
1128 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
1131 hlpfile->bmps[index] = paragraph->u.gfx.u.bmp.hBitmap;
1136 /******************************************************************
1141 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
1142 const char* str, unsigned len, LONG hash,
1143 unsigned clrChange, unsigned wnd)
1148 /* FIXME: should build a string table for the attributes.link.lpszPath
1149 * they are reallocated for each link
1151 if (len == -1) len = strlen(str);
1152 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + len + 1);
1153 if (!link) return NULL;
1155 link->cookie = cookie;
1156 link->string = link_str = (char*)(link + 1);
1157 memcpy(link_str, str, len);
1158 link_str[len] = '\0';
1160 link->bClrChange = clrChange ? 1 : 0;
1162 link->wRefCount = 1;
1164 link->next = rd->first_link;
1165 rd->first_link = link;
1166 link->cpMin = rd->char_pos;
1168 rd->force_color = clrChange;
1170 if (rd->current_link) WINE_FIXME("Pending link\n");
1171 rd->current_link = link;
1174 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
1175 link->cookie, link->string, link->hash, link->window);
1179 unsigned HLPFILE_HalfPointsToTwips(unsigned pts)
1181 static unsigned logPxY;
1184 HDC hdc = GetDC(NULL);
1185 logPxY = GetDeviceCaps(hdc, LOGPIXELSY);
1186 ReleaseDC(NULL, hdc);
1188 return MulDiv(pts, 72 * 10, logPxY);
1191 /***********************************************************************
1193 * HLPFILE_BrowseParagraph
1195 static BOOL HLPFILE_BrowseParagraph(HLPFILE_PAGE* page, struct RtfData* rd, BYTE *buf, BYTE* end)
1197 HLPFILE_PARAGRAPH *paragraph, **paragraphptr;
1199 BYTE *format, *format_end;
1200 char *text, *text_base, *text_end;
1201 long size, blocksize, datalen;
1202 unsigned short bits;
1203 unsigned nc, ncol = 1;
1204 BOOL in_table = FALSE;
1208 for (paragraphptr = &page->first_paragraph; *paragraphptr;
1209 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
1211 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
1213 blocksize = GET_UINT(buf, 0);
1214 size = GET_UINT(buf, 0x4);
1215 datalen = GET_UINT(buf, 0x10);
1216 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
1217 if (!text) return FALSE;
1218 if (size > blocksize - datalen)
1220 /* need to decompress */
1221 if (page->file->hasPhrases)
1222 HLPFILE_Uncompress2(page->file, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
1223 else if (page->file->hasPhrases40)
1224 HLPFILE_Uncompress3(page->file, text, text + size, buf + datalen, end);
1227 WINE_FIXME("Text size is too long, splitting\n");
1228 size = blocksize - datalen;
1229 memcpy(text, buf + datalen, size);
1233 memcpy(text, buf + datalen, size);
1235 text_end = text + size;
1237 format = buf + 0x15;
1238 format_end = buf + GET_UINT(buf, 0x10);
1240 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
1242 fetch_long(&format);
1243 fetch_ushort(&format);
1246 if (buf[0x14] == 0x23)
1253 WINE_TRACE("#cols %u\n", ncol);
1255 if (type == 0 || type == 2)
1260 for (nc = 0; nc < ncol; /**/)
1262 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
1265 nc = GET_SHORT(format, 0);
1266 if (nc == -1) break;
1270 if (buf[0x14] == 0x01)
1274 bits = GET_USHORT(format, 0); format += 2;
1275 if (bits & 0x0001) fetch_long(&format);
1278 sprintf(tmp, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1279 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1283 sprintf(tmp, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1284 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1288 sprintf(tmp, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1289 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1293 sprintf(tmp, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1294 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1298 sprintf(tmp, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1299 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1303 sprintf(tmp, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1304 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1308 BYTE brdr = *format++;
1311 if (brdr & 0x01 && !HLPFILE_RtfAddControl(rd, "\\box")) goto done;
1312 if (brdr & 0x02 && !HLPFILE_RtfAddControl(rd, "\\brdrt")) goto done;
1313 if (brdr & 0x04 && !HLPFILE_RtfAddControl(rd, "\\brdrl")) goto done;
1314 if (brdr & 0x08 && !HLPFILE_RtfAddControl(rd, "\\brdrb")) goto done;
1315 if (brdr & 0x10 && !HLPFILE_RtfAddControl(rd, "\\brdrr")) goto done;
1316 if (brdr & 0x20 && !HLPFILE_RtfAddControl(rd, "\\brdrth")) goto done;
1317 if (!(brdr & 0x20) && !HLPFILE_RtfAddControl(rd, "\\brdrs")) goto done;
1318 if (brdr & 0x40 && !HLPFILE_RtfAddControl(rd, "\\brdrdb")) goto done;
1321 w = GET_SHORT(format, 0); format += 2;
1324 sprintf(tmp, "\\brdrw%d", HLPFILE_HalfPointsToTwips(w));
1325 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1330 int i, ntab = fetch_short(&format);
1334 for (i = 0; i < ntab; i++)
1336 tab = fetch_ushort(&format);
1337 ts = (tab & 0x4000) ? fetch_ushort(&format) : 0 /* left */;
1340 default: WINE_FIXME("Unknown tab style %x\n", ts);
1342 case 0: kind = ""; break;
1343 case 1: kind = "\\tqr"; break;
1344 case 2: kind = "\\tqc"; break;
1346 /* FIXME: do kind */
1347 sprintf(tmp, "%s\\tx%d",
1348 kind, HLPFILE_HalfPointsToTwips(tab & 0x3FFF));
1349 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1352 switch (bits & 0xc00)
1354 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1355 case 0: if (!HLPFILE_RtfAddControl(rd, "\\ql")) goto done; break;
1356 case 0x400: if (!HLPFILE_RtfAddControl(rd, "\\qr")) goto done; break;
1357 case 0x800: if (!HLPFILE_RtfAddControl(rd, "\\qc")) goto done; break;
1360 /* 0x1000 doesn't need space */
1361 if ((bits & 0x1000) && !HLPFILE_RtfAddControl(rd, "\\keep")) goto done;
1362 if ((bits & 0xE080) != 0)
1363 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
1365 while (text < text_end && format < format_end)
1367 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
1368 textsize = strlen(text);
1371 paragraph = HeapAlloc(GetProcessHeap(), 0,
1372 sizeof(HLPFILE_PARAGRAPH) + textsize + 1);
1373 if (!paragraph) return FALSE;
1374 *paragraphptr = paragraph;
1375 paragraphptr = ¶graph->next;
1377 paragraph->next = NULL;
1380 paragraph->link = attributes.link;
1381 if (paragraph->link) paragraph->link->wRefCount++;
1383 else paragraph->link = NULL;
1384 paragraph->cookie = para_normal_text;
1385 paragraph->u.text.wFont = attributes.wFont;
1386 paragraph->u.text.wVSpace = attributes.wVSpace;
1387 paragraph->u.text.wHSpace = attributes.wHSpace;
1388 paragraph->u.text.wIndent = attributes.wIndent;
1389 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
1390 strcpy(paragraph->u.text.lpszText, text);
1392 attributes.wVSpace = 0;
1393 attributes.wHSpace = 0;
1394 if (rd) /* FIXME: TEMP */ {
1395 if (rd->force_color)
1397 if ((rd->current_link->cookie == hlp_link_popup) ?
1398 !HLPFILE_RtfAddControl(rd, "{\\uld\\cf1") :
1399 !HLPFILE_RtfAddControl(rd, "{\\ul\\cf1")) goto done;
1401 if (!HLPFILE_RtfAddText(rd, text)) goto done;
1402 if (rd->force_color && !HLPFILE_RtfAddControl(rd, "}")) goto done;
1403 rd->char_pos += textsize;
1406 /* else: null text, keep on storing attributes */
1407 text += textsize + 1;
1409 if (*format == 0xff)
1415 WINE_TRACE("format=%02x\n", *format);
1419 WINE_FIXME("NIY20\n");
1424 WINE_FIXME("NIY21\n");
1430 unsigned font = GET_USHORT(format, 1);
1432 attributes.wFont = font;
1433 WINE_TRACE("Changing font to %d\n", attributes.wFont);
1435 fs = (4 * page->file->fonts[font].LogFont.lfHeight - 3) / 5;
1436 /* FIXME: missing at least colors, also bold attribute looses information */
1438 sprintf(tmp, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1440 page->file->fonts[font].LogFont.lfWeight > 400 ? "\\b" : "\\b0",
1441 page->file->fonts[font].LogFont.lfItalic ? "\\i" : "\\i0",
1442 page->file->fonts[font].LogFont.lfUnderline ? "\\ul" : "\\ul0",
1443 page->file->fonts[font].LogFont.lfStrikeOut ? "\\strike" : "\\strike0");
1444 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1449 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1450 attributes.wVSpace++;
1452 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1456 if (!HLPFILE_RtfAddControl(rd, "\\par\\pard")) goto done;
1457 attributes.wVSpace++;
1458 attributes.wIndent = 0;
1460 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1464 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1465 attributes.wIndent++;
1467 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1480 BYTE pos = (*format - 0x86);
1481 BYTE type = format[1];
1485 size = fetch_long(&format);
1487 paragraph = HeapAlloc(GetProcessHeap(), 0,
1488 sizeof(HLPFILE_PARAGRAPH) + textsize);
1489 if (!paragraph) return FALSE;
1490 *paragraphptr = paragraph;
1491 paragraphptr = ¶graph->next;
1493 paragraph->next = NULL;
1495 paragraph->link = attributes.link;
1496 if (paragraph->link) paragraph->link->wRefCount++;
1498 else paragraph->link = NULL;
1499 paragraph->cookie = para_bitmap;
1500 paragraph->u.gfx.pos = pos;
1504 fetch_ushort(&format); /* hot spot */
1507 switch (GET_SHORT(format, 0))
1510 HLPFILE_LoadGfxByIndex(page->file, GET_SHORT(format, 2),
1512 if (rd) { /* FIXME: TEMP */
1513 HLPFILE_RtfAddGfxByIndex(rd, page->file, GET_SHORT(format, 2));
1518 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1519 GET_SHORT(format, 0),
1520 size, GET_SHORT(format, 2));
1521 HLPFILE_LoadGfxByAddr(page->file, format + 2, size - 4,
1523 if (rd) { /* FIXME: TEMP */
1524 HLPFILE_RtfAddGfxByAddr(rd, page->file, format + 2, size - 4);
1529 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1534 WINE_FIXME("Got an embedded element %s\n", format + 6);
1537 WINE_FIXME("Got a type %d picture\n", type);
1540 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1547 HLPFILE_FreeLink(attributes.link);
1548 attributes.link = NULL;
1551 if (!rd->current_link)
1552 WINE_FIXME("No existing link\n");
1554 rd->current_link->cpMax = rd->char_pos;
1555 rd->current_link = NULL;
1556 rd->force_color = FALSE;
1561 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1563 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1567 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1568 /* FIXME: it could be that hypen is also in input stream !! */
1570 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1581 WINE_TRACE("macro => %s\n", format + 3);
1582 HLPFILE_FreeLink(attributes.link);
1583 attributes.link = HLPFILE_AllocLink(rd, hlp_link_macro, (const char*)format + 3,
1584 GET_USHORT(format, 1), 0, !(*format & 4), -1);
1585 format += 3 + GET_USHORT(format, 1);
1590 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1591 HLPFILE_FreeLink(attributes.link);
1592 attributes.link = HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1593 page->file->lpszPath, -1,
1594 GET_UINT(format, 1)-16,
1605 attributes.link = HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1606 page->file->lpszPath, -1,
1607 GET_UINT(format, 1),
1608 !(*format & 4), -1);
1617 char* ptr = (char*) format + 8;
1618 BYTE type = format[3];
1627 ptr = page->file->lpszPath;
1630 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1632 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1635 WINE_WARN("Couldn't find window info for %s\n", ptr);
1636 ptr += strlen(ptr) + 1;
1641 WINE_WARN("Unknown link type %d\n", type);
1644 HLPFILE_FreeLink(attributes.link);
1645 attributes.link = HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1646 ptr, -1, GET_UINT(format, 4),
1647 !(*format & 4), wnd);
1649 format += 3 + GET_USHORT(format, 1);
1653 WINE_WARN("format %02x\n", *format);
1660 HeapFree(GetProcessHeap(), 0, text_base);
1664 /******************************************************************
1665 * HLPFILE_BrowsePage
1668 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page, struct RtfData* rd)
1670 HLPFILE *hlpfile = page->file;
1672 DWORD ref = page->reference;
1673 unsigned index, old_index = -1, offset, count = 0, cpg;
1675 const char* ck = NULL;
1677 if (rd) { /* FIXME: TEMP */
1679 rd->data = rd->ptr = HeapAlloc(GetProcessHeap(), 0, rd->allocated = 32768);
1681 rd->first_link = rd->current_link = NULL;
1682 rd->force_color = FALSE;
1685 switch (hlpfile->charset)
1687 case DEFAULT_CHARSET:
1688 case ANSI_CHARSET: cpg = 1252; break;
1689 case SHIFTJIS_CHARSET: cpg = 932; break;
1690 case HANGEUL_CHARSET: cpg = 949; break;
1691 case GB2312_CHARSET: cpg = 936; break;
1692 case CHINESEBIG5_CHARSET: cpg = 950; break;
1693 case GREEK_CHARSET: cpg = 1253; break;
1694 case TURKISH_CHARSET: cpg = 1254; break;
1695 case HEBREW_CHARSET: cpg = 1255; break;
1696 case ARABIC_CHARSET: cpg = 1256; break;
1697 case BALTIC_CHARSET: cpg = 1257; break;
1698 case VIETNAMESE_CHARSET: cpg = 1258; break;
1699 case RUSSIAN_CHARSET: cpg = 1251; break;
1700 case EE_CHARSET: cpg = 1250; break;
1701 case THAI_CHARSET: cpg = 874; break;
1702 case JOHAB_CHARSET: cpg = 1361; break;
1703 case MAC_CHARSET: ck = "mac"; break;
1705 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1710 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1711 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1715 sprintf(tmp, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg);
1716 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1719 /* generate font table */
1720 if (!HLPFILE_RtfAddControl(rd, "{\\fonttbl")) return FALSE;
1721 for (index = 0; index < hlpfile->numFonts; index++)
1724 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1726 case FF_MODERN: family = "modern"; break;
1727 case FF_ROMAN: family = "roman"; break;
1728 case FF_SWISS: family = "swiss"; break;
1729 case FF_SCRIPT: family = "script"; break;
1730 case FF_DECORATIVE: family = "decor"; break;
1731 default: family = "nil"; break;
1733 sprintf(tmp, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1735 hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0x0F,
1736 hlpfile->fonts[index].LogFont.lfCharSet,
1737 hlpfile->fonts[index].LogFont.lfFaceName);
1738 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1740 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1741 /* generate color table */
1742 if (!HLPFILE_RtfAddControl(rd, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE;
1743 for (index = 0; index < hlpfile->numFonts; index++)
1746 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1748 case FF_MODERN: family = "modern"; break;
1749 case FF_ROMAN: family = "roman"; break;
1750 case FF_SWISS: family = "swiss"; break;
1751 case FF_SCRIPT: family = "script"; break;
1752 case FF_DECORATIVE: family = "decor"; break;
1753 default: family = "nil"; break;
1755 sprintf(tmp, "\\red%d\\green%d\\blue%d;",
1756 GetRValue(hlpfile->fonts[index].color),
1757 GetGValue(hlpfile->fonts[index].color),
1758 GetBValue(hlpfile->fonts[index].color));
1759 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1761 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1765 if (hlpfile->version <= 16)
1767 index = (ref - 0x0C) / hlpfile->dsize;
1768 offset = (ref - 0x0C) % hlpfile->dsize;
1772 index = (ref - 0x0C) >> 14;
1773 offset = (ref - 0x0C) & 0x3FFF;
1776 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
1778 /* we jumped to the next block, adjust pointers */
1783 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1784 buf = hlpfile->topic_map[index] + offset;
1785 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1786 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1787 if (index != old_index) {old_index = index;}
1792 if (count++) goto done;
1797 if (!HLPFILE_BrowseParagraph(page, rd, buf, end)) return FALSE;
1800 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1802 if (hlpfile->version <= 16)
1804 ref += GET_UINT(buf, 0xc);
1805 if (GET_UINT(buf, 0xc) == 0)
1809 ref = GET_UINT(buf, 0xc);
1810 } while (ref != 0xffffffff);
1813 page->first_link = rd->first_link;
1814 return HLPFILE_RtfAddControl(rd, "}");
1817 /******************************************************************
1822 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1825 unsigned i, len, idx;
1826 unsigned face_num, dscr_num, face_offset, dscr_offset;
1829 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1831 WINE_WARN("no subfile FONT\n");
1832 hlpfile->numFonts = 0;
1833 hlpfile->fonts = NULL;
1839 face_num = GET_USHORT(ref, 0);
1840 dscr_num = GET_USHORT(ref, 2);
1841 face_offset = GET_USHORT(ref, 4);
1842 dscr_offset = GET_USHORT(ref, 6);
1844 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1845 face_num, face_offset, dscr_num, dscr_offset);
1847 hlpfile->numFonts = dscr_num;
1848 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1850 len = (dscr_offset - face_offset) / face_num;
1851 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1852 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1853 for (i = 0; i < dscr_num; i++)
1855 flag = ref[dscr_offset + i * 11 + 0];
1856 family = ref[dscr_offset + i * 11 + 2];
1858 hlpfile->fonts[i].LogFont.lfHeight = ref[dscr_offset + i * 11 + 1];
1859 hlpfile->fonts[i].LogFont.lfWidth = 0;
1860 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1861 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1862 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1863 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1864 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1865 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1866 hlpfile->fonts[i].LogFont.lfCharSet = hlpfile->charset;
1867 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1868 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1869 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1870 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1874 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1875 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1876 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1877 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1878 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1879 default: WINE_FIXME("Unknown family %u\n", family);
1881 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1885 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1886 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1890 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1891 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1893 hlpfile->fonts[i].hFont = 0;
1894 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1895 ref[dscr_offset + i * 11 + 6],
1896 ref[dscr_offset + i * 11 + 7]);
1897 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1898 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1904 X(4, "dblUnderline"),
1906 ref[dscr_offset + i * 11 + 1],
1908 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1909 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1914 /***********************************************************************
1916 * HLPFILE_ReadFileToBuffer
1918 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1920 BYTE header[16], dummy[1];
1922 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1925 if (GET_UINT(header, 0) != 0x00035F3F)
1926 {WINE_WARN("wrong header\n"); return FALSE;};
1928 hlpfile->file_buffer_size = GET_UINT(header, 12);
1929 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1930 if (!hlpfile->file_buffer) return FALSE;
1932 memcpy(hlpfile->file_buffer, header, 16);
1933 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1934 {WINE_WARN("filesize1\n"); return FALSE;};
1936 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1938 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1943 /**************************************************************************
1946 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1949 static int comp_FindSubFile(void *p, const void *key,
1950 int leaf, void** next)
1952 *next = (char *)p+strlen(p)+(leaf?5:3);
1953 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1954 return strcmp(p, key);
1957 /***********************************************************************
1959 * HLPFILE_FindSubFile
1961 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
1965 WINE_TRACE("looking for file '%s'\n", name);
1966 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
1967 name, comp_FindSubFile);
1968 if (!ptr) return FALSE;
1969 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
1970 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
1972 WINE_ERR("internal file %s does not fit\n", name);
1975 *subend = *subbuf + GET_UINT(*subbuf, 0);
1976 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
1978 WINE_ERR("internal file %s does not fit\n", name);
1981 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1983 WINE_ERR("invalid size provided for internal file %s\n", name);
1989 /***********************************************************************
1991 * HLPFILE_SystemCommands
1993 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1995 BYTE *buf, *ptr, *end;
1996 HLPFILE_MACRO *macro, **m;
1998 unsigned short magic, minor, major, flags;
2000 hlpfile->lpszTitle = NULL;
2002 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
2004 magic = GET_USHORT(buf + 9, 0);
2005 minor = GET_USHORT(buf + 9, 2);
2006 major = GET_USHORT(buf + 9, 4);
2007 /* gen date on 4 bytes */
2008 flags = GET_USHORT(buf + 9, 10);
2009 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
2010 magic, major, minor, flags);
2011 if (magic != 0x036C || major != 1)
2012 {WINE_WARN("Wrong system header\n"); return FALSE;}
2015 hlpfile->tbsize = 0x800;
2016 hlpfile->compressed = 0;
2018 else if (flags == 0)
2020 hlpfile->tbsize = 0x1000;
2021 hlpfile->compressed = 0;
2023 else if (flags == 4)
2025 hlpfile->tbsize = 0x1000;
2026 hlpfile->compressed = 1;
2030 hlpfile->tbsize = 0x800;
2031 hlpfile->compressed = 1;
2034 if (hlpfile->compressed)
2035 hlpfile->dsize = 0x4000;
2037 hlpfile->dsize = hlpfile->tbsize - 0x0C;
2039 hlpfile->version = minor;
2040 hlpfile->flags = flags;
2041 hlpfile->charset = DEFAULT_CHARSET;
2043 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
2045 char *str = (char*) ptr + 4;
2046 switch (GET_USHORT(ptr, 0))
2049 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
2050 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
2051 if (!hlpfile->lpszTitle) return FALSE;
2052 lstrcpy(hlpfile->lpszTitle, str);
2053 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
2057 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
2058 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
2059 if (!hlpfile->lpszCopyright) return FALSE;
2060 lstrcpy(hlpfile->lpszCopyright, str);
2061 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
2065 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
2066 hlpfile->contents_start = GET_UINT(ptr, 4);
2067 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
2071 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
2073 p = (char*)macro + sizeof(HLPFILE_MACRO);
2075 macro->lpszMacro = p;
2077 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
2082 if (GET_USHORT(ptr, 4 + 4) != 1)
2083 WINE_FIXME("More than one icon, picking up first\n");
2084 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
2085 hlpfile->hIcon = CreateIconFromResourceEx(ptr + 4 + 0x16,
2086 GET_USHORT(ptr, 2) - 0x16, TRUE,
2091 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
2093 if (hlpfile->windows)
2094 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
2095 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
2097 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
2098 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
2100 if (hlpfile->windows)
2102 unsigned flags = GET_USHORT(ptr, 4);
2103 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
2105 if (flags & 0x0001) strcpy(wi->type, &str[2]);
2106 else wi->type[0] = '\0';
2107 if (flags & 0x0002) strcpy(wi->name, &str[12]);
2108 else wi->name[0] = '\0';
2109 if (flags & 0x0004) strcpy(wi->caption, &str[21]);
2110 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
2111 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
2112 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
2113 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
2114 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
2115 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
2116 wi->win_style = WS_OVERLAPPEDWINDOW;
2117 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
2118 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
2119 WINE_TRACE("System-Window: flags=%c%c%c%c%c%c%c%c type=%s name=%s caption=%s (%d,%d)x(%d,%d)\n",
2120 flags & 0x0001 ? 'T' : 't',
2121 flags & 0x0002 ? 'N' : 'n',
2122 flags & 0x0004 ? 'C' : 'c',
2123 flags & 0x0008 ? 'X' : 'x',
2124 flags & 0x0010 ? 'Y' : 'y',
2125 flags & 0x0020 ? 'W' : 'w',
2126 flags & 0x0040 ? 'H' : 'h',
2127 flags & 0x0080 ? 'S' : 's',
2128 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
2129 wi->size.cx, wi->size.cy);
2133 WINE_WARN("Citation: '%s'\n", ptr + 4);
2136 hlpfile->charset = ptr[4];
2137 WINE_TRACE("Charset: %d\n", hlpfile->charset);
2140 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
2143 if (!hlpfile->lpszTitle)
2144 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
2148 /***********************************************************************
2150 * HLPFILE_UncompressedLZ77_Size
2152 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
2159 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2163 int code = GET_USHORT(ptr, 0);
2164 int len = 3 + (code >> 12);
2168 else newsize++, ptr++;
2175 /***********************************************************************
2177 * HLPFILE_UncompressLZ77
2179 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
2186 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2190 int code = GET_USHORT(ptr, 0);
2191 int len = 3 + (code >> 12);
2192 int offset = code & 0xfff;
2194 * We must copy byte-by-byte here. We cannot use memcpy nor
2195 * memmove here. Just example:
2196 * a[]={1,2,3,4,5,6,7,8,9,10}
2200 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
2202 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
2205 else *newptr++ = *ptr++;
2212 /***********************************************************************
2214 * HLPFILE_UncompressLZ77_Phrases
2216 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
2218 UINT i, num, dec_size, head_size;
2221 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
2223 if (hlpfile->version <= 16)
2228 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
2229 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
2231 if (hlpfile->version <= 16)
2232 dec_size = end - buf - 15 - 2 * num;
2234 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
2236 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2237 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2238 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2240 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2241 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2245 for (i = 0; i <= num; i++)
2246 hlpfile->phrases_offsets[i] = GET_USHORT(buf, head_size + 2 * i) - 2 * num - 2;
2248 if (hlpfile->version <= 16)
2249 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
2251 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
2253 hlpfile->hasPhrases = TRUE;
2257 /***********************************************************************
2259 * HLPFILE_Uncompress_Phrases40
2261 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
2264 INT dec_size, cpr_size;
2265 BYTE *buf_idx, *end_idx;
2266 BYTE *buf_phs, *end_phs;
2267 long* ptr, mask = 0;
2269 unsigned short bc, n;
2271 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
2272 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
2274 ptr = (long*)(buf_idx + 9 + 28);
2275 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
2276 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
2278 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2279 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2280 GET_UINT(buf_idx, 9 + 0),
2281 GET_UINT(buf_idx, 9 + 4),
2282 GET_UINT(buf_idx, 9 + 8),
2283 GET_UINT(buf_idx, 9 + 12),
2284 GET_UINT(buf_idx, 9 + 16),
2285 GET_UINT(buf_idx, 9 + 20),
2286 GET_USHORT(buf_idx, 9 + 24),
2287 GET_USHORT(buf_idx, 9 + 26));
2289 dec_size = GET_UINT(buf_idx, 9 + 12);
2290 cpr_size = GET_UINT(buf_idx, 9 + 16);
2292 if (dec_size != cpr_size &&
2293 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
2295 WINE_WARN("size mismatch %u %u\n",
2296 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2297 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2300 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2301 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2302 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2304 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2305 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2309 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
2311 hlpfile->phrases_offsets[0] = 0;
2312 for (i = 0; i < num; i++)
2314 for (n = 1; getbit(); n += 1 << bc);
2316 if (bc > 1 && getbit()) n += 2;
2317 if (bc > 2 && getbit()) n += 4;
2318 if (bc > 3 && getbit()) n += 8;
2319 if (bc > 4 && getbit()) n += 16;
2320 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
2324 if (dec_size == cpr_size)
2325 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2327 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2329 hlpfile->hasPhrases40 = TRUE;
2333 /***********************************************************************
2335 * HLPFILE_Uncompress_Topic
2337 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
2339 BYTE *buf, *ptr, *end, *newptr;
2340 unsigned int i, newsize = 0;
2341 unsigned int topic_size;
2343 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
2344 {WINE_WARN("topic0\n"); return FALSE;}
2346 buf += 9; /* Skip file header */
2347 topic_size = end - buf;
2348 if (hlpfile->compressed)
2350 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2352 for (i = 0; i < hlpfile->topic_maplen; i++)
2354 ptr = buf + i * hlpfile->tbsize;
2356 /* I don't know why, it's necessary for printman.hlp */
2357 if (ptr + 0x44 > end) ptr = end - 0x44;
2359 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
2362 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2363 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
2364 if (!hlpfile->topic_map) return FALSE;
2365 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2366 hlpfile->topic_end = newptr + newsize;
2368 for (i = 0; i < hlpfile->topic_maplen; i++)
2370 ptr = buf + i * hlpfile->tbsize;
2371 if (ptr + 0x44 > end) ptr = end - 0x44;
2373 hlpfile->topic_map[i] = newptr;
2374 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
2379 /* basically, we need to copy the TopicBlockSize byte pages
2380 * (removing the first 0x0C) in one single area in memory
2382 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2383 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2384 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
2385 if (!hlpfile->topic_map) return FALSE;
2386 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2387 hlpfile->topic_end = newptr + topic_size;
2389 for (i = 0; i < hlpfile->topic_maplen; i++)
2391 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
2392 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
2398 /***********************************************************************
2400 * HLPFILE_Uncompress2
2403 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
2405 BYTE *phptr, *phend;
2409 while (ptr < end && newptr < newend)
2411 if (!*ptr || *ptr >= 0x10)
2415 code = 0x100 * ptr[0] + ptr[1];
2416 index = (code - 0x100) / 2;
2418 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
2419 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
2421 if (newptr + (phend - phptr) > newend)
2423 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
2424 newptr, newend, (SIZE_T)(phend - phptr));
2427 memcpy(newptr, phptr, phend - phptr);
2428 newptr += phend - phptr;
2429 if (code & 1) *newptr++ = ' ';
2434 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
2437 /******************************************************************
2438 * HLPFILE_Uncompress3
2442 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
2443 const BYTE* src, const BYTE* src_end)
2445 unsigned int idx, len;
2447 for (; src < src_end; src++)
2449 if ((*src & 1) == 0)
2452 if (idx > hlpfile->num_phrases)
2454 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2459 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2460 if (dst + len <= dst_end)
2461 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2464 else if ((*src & 0x03) == 0x01)
2466 idx = (*src + 1) * 64;
2468 if (idx > hlpfile->num_phrases)
2470 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2475 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2476 if (dst + len <= dst_end)
2477 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2480 else if ((*src & 0x07) == 0x03)
2482 len = (*src / 8) + 1;
2483 if (dst + len <= dst_end)
2484 memcpy(dst, src + 1, len);
2489 len = (*src / 16) + 1;
2490 if (dst + len <= dst_end)
2491 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
2496 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
2500 /******************************************************************
2501 * HLPFILE_UncompressRLE
2505 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
2508 BYTE* sdst = *dst + dstsz;
2516 if ((*dst) + ch <= sdst)
2517 memcpy(*dst, src, ch);
2522 if ((*dst) + ch <= sdst)
2523 memset(*dst, (char)*src, ch);
2529 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
2530 (SIZE_T)(*dst - (sdst - dstsz)), dstsz);
2533 /**************************************************************************
2534 * HLPFILE_BPTreeSearch
2536 * Searches for an element in B+ tree
2539 * buf [I] pointer to the embedded file structured as a B+ tree
2540 * key [I] pointer to data to find
2541 * comp [I] compare function
2544 * Pointer to block identified by key, or NULL if failure.
2547 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
2548 HLPFILE_BPTreeCompare comp)
2554 BYTE *pages, *ptr, *newptr;
2558 magic = GET_USHORT(buf, 9);
2559 if (magic != 0x293B)
2561 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2564 page_size = GET_USHORT(buf, 9+4);
2565 cur_page = GET_USHORT(buf, 9+26);
2566 level = GET_USHORT(buf, 9+32);
2567 pages = buf + 9 + 38;
2570 ptr = pages + cur_page*page_size;
2571 entries = GET_SHORT(ptr, 2);
2573 for (i = 0; i < entries; i++)
2575 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
2578 cur_page = GET_USHORT(ptr-2, 0);
2580 ptr = pages + cur_page*page_size;
2581 entries = GET_SHORT(ptr, 2);
2583 for (i = 0; i < entries; i++)
2585 ret = comp(ptr, key, 1, (void **)&newptr);
2586 if (ret == 0) return ptr;
2587 if (ret > 0) return NULL;
2593 /**************************************************************************
2594 * HLPFILE_BPTreeEnum
2596 * Enumerates elements in B+ tree.
2599 * buf [I] pointer to the embedded file structured as a B+ tree
2600 * cb [I] compare function
2601 * cookie [IO] cookie for cb function
2603 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
2609 BYTE *pages, *ptr, *newptr;
2612 magic = GET_USHORT(buf, 9);
2613 if (magic != 0x293B)
2615 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2618 page_size = GET_USHORT(buf, 9+4);
2619 cur_page = GET_USHORT(buf, 9+26);
2620 level = GET_USHORT(buf, 9+32);
2621 pages = buf + 9 + 38;
2624 ptr = pages + cur_page*page_size;
2625 cur_page = GET_USHORT(ptr, 4);
2627 while (cur_page != 0xFFFF)
2629 ptr = pages + cur_page*page_size;
2630 entries = GET_SHORT(ptr, 2);
2632 for (i = 0; i < entries; i++)
2634 cb(ptr, (void **)&newptr, cookie);
2637 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
2642 /***********************************************************************
2644 * HLPFILE_GetContext
2646 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2651 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2652 {WINE_WARN("context0\n"); return FALSE;}
2655 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2656 if (!hlpfile->Context) return FALSE;
2657 memcpy(hlpfile->Context, cbuf, clen);
2662 /***********************************************************************
2664 * HLPFILE_GetKeywords
2666 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2671 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2673 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2674 if (!hlpfile->kwbtree) return FALSE;
2675 memcpy(hlpfile->kwbtree, cbuf, clen);
2677 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2679 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2680 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2684 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2685 if (!hlpfile->kwdata)
2687 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2690 memcpy(hlpfile->kwdata, cbuf, clen);
2695 /***********************************************************************
2699 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2702 unsigned entries, i;
2704 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2705 {WINE_WARN("no map section\n"); return FALSE;}
2707 entries = GET_USHORT(cbuf, 9);
2708 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2709 if (!hlpfile->Map) return FALSE;
2710 hlpfile->wMapLen = entries;
2711 for (i = 0; i < entries; i++)
2713 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2714 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2719 /******************************************************************
2720 * HLPFILE_DeleteLink
2724 void HLPFILE_FreeLink(HLPFILE_LINK* link)
2726 if (link && !--link->wRefCount)
2728 HeapFree(GetProcessHeap(), 0, link);
2732 /***********************************************************************
2734 * HLPFILE_DeleteParagraph
2736 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
2738 HLPFILE_PARAGRAPH* next;
2742 next = paragraph->next;
2744 if (paragraph->cookie == para_metafile)
2745 DeleteMetaFile(paragraph->u.gfx.u.mfp.hMF);
2747 HLPFILE_FreeLink(paragraph->link);
2749 HeapFree(GetProcessHeap(), 0, paragraph);
2754 /***********************************************************************
2758 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2760 HLPFILE_MACRO* next;
2765 HeapFree(GetProcessHeap(), 0, macro);
2770 /***********************************************************************
2774 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2781 HLPFILE_DeleteParagraph(page->first_paragraph);
2782 HLPFILE_DeleteMacro(page->first_macro);
2783 HeapFree(GetProcessHeap(), 0, page);
2788 /***********************************************************************
2790 * HLPFILE_FreeHlpFile
2792 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2796 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2798 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2799 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2800 else first_hlpfile = hlpfile->next;
2802 if (hlpfile->numFonts)
2804 for (i = 0; i < hlpfile->numFonts; i++)
2806 DeleteObject(hlpfile->fonts[i].hFont);
2808 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2811 if (hlpfile->numBmps)
2813 for (i = 0; i < hlpfile->numBmps; i++)
2815 DeleteObject(hlpfile->bmps[i]);
2817 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2820 HLPFILE_DeletePage(hlpfile->first_page);
2821 HLPFILE_DeleteMacro(hlpfile->first_macro);
2823 DestroyIcon(hlpfile->hIcon);
2824 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2825 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2826 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2827 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2828 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2829 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2830 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2831 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2832 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2833 HeapFree(GetProcessHeap(), 0, hlpfile);