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;
54 static BOOL HLPFILE_DoReadHlpFile(HLPFILE*, LPCSTR);
55 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE*, HFILE);
56 static BOOL HLPFILE_FindSubFile(HLPFILE*, LPCSTR, BYTE**, BYTE**);
57 static BOOL HLPFILE_SystemCommands(HLPFILE*);
58 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end);
59 static BYTE* HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr);
60 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE*);
61 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE*);
62 static BOOL HLPFILE_Uncompress_Topic(HLPFILE*);
63 static BOOL HLPFILE_GetContext(HLPFILE*);
64 static BOOL HLPFILE_GetKeywords(HLPFILE*);
65 static BOOL HLPFILE_GetMap(HLPFILE*);
66 static BOOL HLPFILE_AddPage(HLPFILE*, BYTE*, BYTE*, unsigned, unsigned);
67 static BOOL HLPFILE_SkipParagraph(HLPFILE*, BYTE *, BYTE*, unsigned*);
68 static void HLPFILE_Uncompress2(HLPFILE*, const BYTE*, const BYTE*, BYTE*, const BYTE*);
69 static BOOL HLPFILE_Uncompress3(HLPFILE*, char*, const char*, const BYTE*, const BYTE*);
70 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE* dst, unsigned dstsz);
71 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile);
73 /***********************************************************************
75 * HLPFILE_PageByNumber
77 static HLPFILE_PAGE *HLPFILE_PageByNumber(HLPFILE* hlpfile, UINT wNum)
82 WINE_TRACE("<%s>[%u]\n", hlpfile->lpszPath, wNum);
84 for (page = hlpfile->first_page; page && temp; page = page->next) temp--;
86 WINE_ERR("Page of number %u not found in file %s\n", wNum, hlpfile->lpszPath);
90 /******************************************************************
91 * HLPFILE_PageByOffset
95 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset, ULONG* relative)
100 if (!hlpfile) return 0;
102 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, offset);
104 if (offset == 0xFFFFFFFF) return NULL;
107 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
109 if (page->offset <= offset && (!found || found->offset < page->offset))
111 *relative = offset - page->offset;
116 WINE_ERR("Page of offset %u not found in file %s\n",
117 offset, hlpfile->lpszPath);
121 /**************************************************************************
124 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
127 static int comp_PageByHash(void *p, const void *key,
128 int leaf, void** next)
130 LONG lKey = (LONG_PTR)key;
131 LONG lTest = (INT)GET_UINT(p, 0);
133 *next = (char *)p+(leaf?8:6);
134 WINE_TRACE("Comparing '%d' with '%d'\n", lKey, lTest);
135 if (lTest < lKey) return -1;
136 if (lTest > lKey) return 1;
140 /***********************************************************************
144 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash, ULONG* relative)
148 if (!hlpfile) return NULL;
149 if (!lHash) return HLPFILE_Contents(hlpfile, relative);
151 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lHash);
153 /* For win 3.0 files hash values are really page numbers */
154 if (hlpfile->version <= 16)
157 return HLPFILE_PageByNumber(hlpfile, lHash);
160 ptr = HLPFILE_BPTreeSearch(hlpfile->Context, LongToPtr(lHash), comp_PageByHash);
163 WINE_ERR("Page of hash %x not found in file %s\n", lHash, hlpfile->lpszPath);
167 return HLPFILE_PageByOffset(hlpfile, GET_UINT(ptr, 4), relative);
170 /***********************************************************************
174 HLPFILE_PAGE *HLPFILE_PageByMap(HLPFILE* hlpfile, LONG lMap, ULONG* relative)
178 if (!hlpfile) return 0;
180 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lMap);
182 for (i = 0; i < hlpfile->wMapLen; i++)
184 if (hlpfile->Map[i].lMap == lMap)
185 return HLPFILE_PageByOffset(hlpfile, hlpfile->Map[i].offset, relative);
188 WINE_ERR("Page of Map %x not found in file %s\n", lMap, hlpfile->lpszPath);
192 /***********************************************************************
196 HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile, ULONG* relative)
198 HLPFILE_PAGE* page = NULL;
200 if (!hlpfile) return NULL;
202 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start, relative);
205 page = hlpfile->first_page;
211 /***********************************************************************
215 LONG HLPFILE_Hash(LPCSTR lpszContext)
220 while ((c = *lpszContext++))
223 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
224 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
225 if (c >= '1' && c <= '9') x = c - '0';
226 if (c == '0') x = 10;
227 if (c == '.') x = 12;
228 if (c == '_') x = 13;
229 if (x) lHash = lHash * 43 + x;
234 /***********************************************************************
236 * HLPFILE_ReadHlpFile
238 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
242 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
244 if (!strcmp(lpszPath, hlpfile->lpszPath))
246 hlpfile->wRefCount++;
251 hlpfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
252 sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
253 if (!hlpfile) return 0;
255 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
256 hlpfile->contents_start = 0xFFFFFFFF;
257 hlpfile->next = first_hlpfile;
258 hlpfile->wRefCount = 1;
260 strcpy(hlpfile->lpszPath, lpszPath);
262 first_hlpfile = hlpfile;
263 if (hlpfile->next) hlpfile->next->prev = hlpfile;
265 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
267 HLPFILE_FreeHlpFile(hlpfile);
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);
288 if (hFile == HFILE_ERROR) return FALSE;
290 ret = HLPFILE_ReadFileToBuffer(hlpfile, 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;
303 buf = hlpfile->topic_map[0];
310 if (hlpfile->version <= 16)
312 index = (ref - 0x0C) / hlpfile->dsize;
313 offset = (ref - 0x0C) % hlpfile->dsize;
317 index = (ref - 0x0C) >> 14;
318 offset = (ref - 0x0C) & 0x3FFF;
321 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
323 /* we jumped to the next block, adjust pointers */
328 WINE_TRACE("ref=%08x => [%u/%u]\n", ref, index, offset);
330 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
331 buf = hlpfile->topic_map[index] + offset;
332 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
333 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
334 if (index != old_index) {offs = 0; old_index = index;}
339 if (!HLPFILE_AddPage(hlpfile, buf, end, ref, index * 0x8000L + offs)) return FALSE;
345 if (!HLPFILE_SkipParagraph(hlpfile, buf, end, &len)) return FALSE;
350 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
353 if (hlpfile->version <= 16)
355 ref += GET_UINT(buf, 0xc);
356 if (GET_UINT(buf, 0xc) == 0)
360 ref = GET_UINT(buf, 0xc);
361 } while (ref != 0xffffffff);
363 HLPFILE_GetKeywords(hlpfile);
364 HLPFILE_GetMap(hlpfile);
365 if (hlpfile->version <= 16) return TRUE;
366 return HLPFILE_GetContext(hlpfile);
369 /***********************************************************************
373 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned ref, unsigned offset)
377 UINT titlesize, blocksize, datalen;
381 blocksize = GET_UINT(buf, 0);
382 datalen = GET_UINT(buf, 0x10);
383 title = buf + datalen;
384 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
386 titlesize = GET_UINT(buf, 4);
387 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
388 if (!page) return FALSE;
389 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
391 if (titlesize > blocksize - datalen)
393 /* need to decompress */
394 if (hlpfile->hasPhrases)
395 HLPFILE_Uncompress2(hlpfile, title, end, (BYTE*)page->lpszTitle, (BYTE*)page->lpszTitle + titlesize);
396 else if (hlpfile->hasPhrases40)
397 HLPFILE_Uncompress3(hlpfile, page->lpszTitle, page->lpszTitle + titlesize, title, end);
400 WINE_FIXME("Text size is too long, splitting\n");
401 titlesize = blocksize - datalen;
402 memcpy(page->lpszTitle, title, titlesize);
406 memcpy(page->lpszTitle, title, titlesize);
408 page->lpszTitle[titlesize] = '\0';
410 if (hlpfile->first_page)
412 hlpfile->last_page->next = page;
413 page->prev = hlpfile->last_page;
414 hlpfile->last_page = page;
418 hlpfile->first_page = page;
419 hlpfile->last_page = page;
423 page->file = hlpfile;
425 page->first_macro = NULL;
426 page->first_link = NULL;
427 page->wNumber = GET_UINT(buf, 0x21);
428 page->offset = offset;
429 page->reference = ref;
431 page->browse_bwd = GET_UINT(buf, 0x19);
432 page->browse_fwd = GET_UINT(buf, 0x1D);
434 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
435 page->wNumber, page->lpszTitle,
436 page->browse_bwd, page->offset, page->browse_fwd);
438 /* now load macros */
439 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
440 while (ptr < page->lpszTitle + titlesize)
442 unsigned len = strlen(ptr);
445 WINE_TRACE("macro: %s\n", ptr);
446 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
447 macro->lpszMacro = macro_str = (char*)(macro + 1);
448 memcpy(macro_str, ptr, len + 1);
449 /* FIXME: shall we really link macro in reverse order ??
450 * may produce strange results when played at page opening
452 macro->next = page->first_macro;
453 page->first_macro = macro;
460 static long fetch_long(BYTE** ptr)
466 ret = (*(unsigned long*)(*ptr) - 0x80000000L) / 2;
471 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
478 static unsigned long fetch_ulong(BYTE** ptr)
484 ret = *(unsigned long*)(*ptr) / 2;
489 ret = *(unsigned short*)(*ptr) / 2;
495 static short fetch_short(BYTE** ptr)
501 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
506 ret = (*(unsigned char*)(*ptr) - 0x80) / 2;
512 static unsigned short fetch_ushort(BYTE** ptr)
518 ret = *(unsigned short*)(*ptr) / 2;
523 ret = *(unsigned char*)(*ptr) / 2;
529 /***********************************************************************
531 * HLPFILE_SkipParagraph
533 static BOOL HLPFILE_SkipParagraph(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned* len)
537 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
538 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
541 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
544 *len = fetch_ushort(&tmp);
546 else *len = end-buf-15;
551 /******************************************************************
552 * HLPFILE_DecompressGfx
554 * Decompress the data part of a bitmap or a metafile
556 static BYTE* HLPFILE_DecompressGfx(BYTE* src, unsigned csz, unsigned sz, BYTE packing)
562 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
566 case 0: /* uncompressed */
568 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
572 dst = HeapAlloc(GetProcessHeap(), 0, sz);
573 if (!dst) return NULL;
574 HLPFILE_UncompressRLE(src, src + csz, dst, sz);
577 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
578 dst = HeapAlloc(GetProcessHeap(), 0, sz77);
579 if (!dst) return NULL;
580 HLPFILE_UncompressLZ77(src, src + csz, dst);
582 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
584 case 3: /* LZ77 then RLE */
585 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
586 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
587 if (!tmp) return FALSE;
588 HLPFILE_UncompressLZ77(src, src + csz, tmp);
589 dst = HeapAlloc(GetProcessHeap(), 0, sz);
592 HeapFree(GetProcessHeap(), 0, tmp);
595 HLPFILE_UncompressRLE(tmp, tmp + sz77, dst, sz);
596 HeapFree(GetProcessHeap(), 0, tmp);
599 WINE_FIXME("Unsupported packing %u\n", packing);
605 static BOOL HLPFILE_RtfAddRawString(struct RtfData* rd, const char* str, size_t sz)
607 if (rd->ptr + sz >= rd->data + rd->allocated)
609 char* new = HeapReAlloc(GetProcessHeap(), 0, rd->data, rd->allocated *= 2);
610 if (!new) return FALSE;
611 rd->ptr = new + (rd->ptr - rd->data);
614 memcpy(rd->ptr, str, sz);
620 static BOOL HLPFILE_RtfAddControl(struct RtfData* rd, const char* str)
622 if (*str == '\\' || *str == '{') rd->in_text = FALSE;
623 else if (*str == '}') rd->in_text = TRUE;
624 return HLPFILE_RtfAddRawString(rd, str, strlen(str));
627 static BOOL HLPFILE_RtfAddText(struct RtfData* rd, const char* str)
636 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
639 for (last = p = str; *p; p++)
641 if (*p < 0) /* escape non ASCII chars */
644 rlen = sprintf(xx, "\\'%x", *(const BYTE*)p);
649 case '{': rlen = 2; replace = "\\{"; break;
650 case '}': rlen = 2; replace = "\\}"; break;
651 case '\\': rlen = 2; replace = "\\\\"; break;
654 if ((p != last && !HLPFILE_RtfAddRawString(rd, last, p - last)) ||
655 !HLPFILE_RtfAddRawString(rd, replace, rlen)) return FALSE;
658 return HLPFILE_RtfAddRawString(rd, last, p - last);
661 /******************************************************************
665 static BOOL HLPFILE_RtfAddHexBytes(struct RtfData* rd, const void* _ptr, unsigned sz)
669 const BYTE* ptr = _ptr;
670 static const char* _2hex = "0123456789abcdef";
674 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
677 for (; sz; sz -= step)
680 for (i = 0; i < step; i++)
682 tmp[2 * i + 0] = _2hex[*ptr >> 4];
683 tmp[2 * i + 1] = _2hex[*ptr++ & 0xF];
685 if (!HLPFILE_RtfAddRawString(rd, tmp, 2 * step)) return FALSE;
690 /******************************************************************
691 * HLPFILE_RtfAddTransparentBitmap
693 * We'll transform a transparent bitmap into an metafile that
694 * we then transform into RTF
696 static BOOL HLPFILE_RtfAddTransparentBitmap(struct RtfData* rd, const BITMAPINFO* bi,
697 const void* pict, unsigned nc)
699 HDC hdc, hdcMask, hdcMem, hdcEMF;
700 HBITMAP hbm, hbmMask, hbmOldMask, hbmOldMem;
706 hbm = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
707 CBM_INIT, pict, bi, DIB_RGB_COLORS);
709 hdcMem = CreateCompatibleDC(hdc);
710 hbmOldMem = SelectObject(hdcMem, hbm);
712 /* create the mask bitmap from the main bitmap */
713 hdcMask = CreateCompatibleDC(hdc);
714 hbmMask = CreateBitmap(bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, 1, 1, NULL);
715 hbmOldMask = SelectObject(hdcMask, hbmMask);
717 RGB(bi->bmiColors[nc - 1].rgbRed,
718 bi->bmiColors[nc - 1].rgbGreen,
719 bi->bmiColors[nc - 1].rgbBlue));
720 BitBlt(hdcMask, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCCOPY);
722 /* sets to RGB(0,0,0) the transparent bits in main bitmap */
723 SetBkColor(hdcMem, RGB(0,0,0));
724 SetTextColor(hdcMem, RGB(255,255,255));
725 BitBlt(hdcMem, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMask, 0, 0, SRCAND);
727 SelectObject(hdcMask, hbmOldMask);
730 SelectObject(hdcMem, hbmOldMem);
733 /* we create the bitmap on the fly */
734 hdcEMF = CreateEnhMetaFile(NULL, NULL, NULL, NULL);
735 hdcMem = CreateCompatibleDC(hdcEMF);
737 /* sets to RGB(0,0,0) the transparent bits in final bitmap */
738 hbmOldMem = SelectObject(hdcMem, hbmMask);
739 SetBkColor(hdcEMF, RGB(255, 255, 255));
740 SetTextColor(hdcEMF, RGB(0, 0, 0));
741 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCAND);
743 /* and copy the remaining bits of main bitmap */
744 SelectObject(hdcMem, hbm);
745 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCPAINT);
746 SelectObject(hdcMem, hbmOldMem);
751 DeleteObject(hbmMask);
754 hEMF = CloseEnhMetaFile(hdcEMF);
756 /* generate rtf stream */
757 sz = GetEnhMetaFileBits(hEMF, 0, NULL);
758 if (sz && (data = HeapAlloc(GetProcessHeap(), 0, sz)))
760 if (sz == GetEnhMetaFileBits(hEMF, sz, data))
762 ret = HLPFILE_RtfAddControl(rd, "{\\pict\\emfblip") &&
763 HLPFILE_RtfAddHexBytes(rd, data, sz) &&
764 HLPFILE_RtfAddControl(rd, "}");
766 HeapFree(GetProcessHeap(), 0, data);
768 DeleteEnhMetaFile(hEMF);
773 /******************************************************************
774 * HLPFILE_RtfAddBitmap
777 static BOOL HLPFILE_RtfAddBitmap(struct RtfData* rd, BYTE* beg, BYTE type, BYTE pack)
782 unsigned long off, csz;
784 BOOL clrImportant = FALSE;
788 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
789 if (!bi) return FALSE;
791 ptr = beg + 2; /* for type and pack */
793 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
794 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
795 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
796 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
797 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
798 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
799 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
800 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
801 clrImportant = fetch_ulong(&ptr);
802 bi->bmiHeader.biClrImportant = (clrImportant > 1) ? clrImportant : 0;
803 bi->bmiHeader.biCompression = BI_RGB;
804 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
805 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
806 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
807 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
808 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
809 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
811 csz = fetch_ulong(&ptr);
812 fetch_ulong(&ptr); /* hotspot size */
814 off = GET_UINT(ptr, 0); ptr += 4;
815 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
817 /* now read palette info */
822 nc = bi->bmiHeader.biClrUsed;
823 /* not quite right, especially for bitfields type of compression */
824 if (!nc && bi->bmiHeader.biBitCount <= 8)
825 nc = 1 << bi->bmiHeader.biBitCount;
827 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
828 if (!bi) return FALSE;
829 for (i = 0; i < nc; i++)
831 bi->bmiColors[i].rgbBlue = ptr[0];
832 bi->bmiColors[i].rgbGreen = ptr[1];
833 bi->bmiColors[i].rgbRed = ptr[2];
834 bi->bmiColors[i].rgbReserved = 0;
838 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
840 if (clrImportant == 1 && nc > 0)
842 ret = HLPFILE_RtfAddTransparentBitmap(rd, bi, pict_beg, nc);
845 if (!HLPFILE_RtfAddControl(rd, "{\\pict")) goto done;
848 sprintf(tmp, "\\dibitmap0\\picw%d\\pich%d",
849 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
850 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
851 if (!HLPFILE_RtfAddHexBytes(rd, bi, sizeof(*bi) + nc * sizeof(RGBQUAD))) goto done;
855 sprintf(tmp, "\\wbitmap0\\wbmbitspixel%d\\wbmplanes%d\\picw%d\\pich%d",
856 bi->bmiHeader.biBitCount, bi->bmiHeader.biPlanes,
857 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
858 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
860 if (!HLPFILE_RtfAddHexBytes(rd, pict_beg, bi->bmiHeader.biSizeImage)) goto done;
861 if (!HLPFILE_RtfAddControl(rd, "}")) goto done;
865 HeapFree(GetProcessHeap(), 0, bi);
866 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
871 /******************************************************************
872 * HLPFILE_RtfAddMetaFile
875 static BOOL HLPFILE_RtfAddMetaFile(struct RtfData* rd, BYTE* beg, BYTE pack)
878 unsigned long size, csize;
879 unsigned long off, hsoff;
885 WINE_TRACE("Loading metafile\n");
887 ptr = beg + 2; /* for type and pack */
889 mm = fetch_ushort(&ptr); /* mapping mode */
890 sprintf(tmp, "{\\pict\\wmetafile%d\\picw%d\\pich%d",
891 mm, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2));
892 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
895 size = fetch_ulong(&ptr); /* decompressed size */
896 csize = fetch_ulong(&ptr); /* compressed size */
897 fetch_ulong(&ptr); /* hotspot size */
898 off = GET_UINT(ptr, 0);
899 hsoff = GET_UINT(ptr, 4);
902 WINE_TRACE("sz=%lu csz=%lu offs=%lu/%u,%lu\n",
903 size, csize, off, ptr - beg, hsoff);
905 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
906 if (!bits) return FALSE;
908 ret = HLPFILE_RtfAddHexBytes(rd, bits, size) &&
909 HLPFILE_RtfAddControl(rd, "}");
911 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
916 /******************************************************************
917 * HLPFILE_RtfAddGfxByAddr
920 static BOOL HLPFILE_RtfAddGfxByAddr(struct RtfData* rd, HLPFILE *hlpfile,
921 BYTE* ref, unsigned long size)
925 numpict = GET_USHORT(ref, 2);
926 WINE_TRACE("Got picture magic=%04x #=%d\n", GET_USHORT(ref, 0), numpict);
928 for (i = 0; i < numpict; i++)
934 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
935 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
942 case 5: /* device dependent bmp */
943 case 6: /* device independent bmp */
944 HLPFILE_RtfAddBitmap(rd, beg, type, pack);
947 HLPFILE_RtfAddMetaFile(rd, beg, pack);
949 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
952 /* FIXME: hotspots */
954 /* FIXME: implement support for multiple picture format */
955 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
961 /******************************************************************
962 * HLPFILE_RtfAddGfxByIndex
966 static BOOL HLPFILE_RtfAddGfxByIndex(struct RtfData* rd, HLPFILE *hlpfile,
972 WINE_TRACE("Loading picture #%d\n", index);
974 sprintf(tmp, "|bm%u", index);
976 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
979 return HLPFILE_RtfAddGfxByAddr(rd, hlpfile, ref, end - ref);
982 /******************************************************************
987 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
988 const char* str, unsigned len, LONG hash,
989 unsigned clrChange, unsigned wnd)
994 /* FIXME: should build a string table for the attributes.link.lpszPath
995 * they are reallocated for each link
997 if (len == -1) len = strlen(str);
998 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + len + 1);
999 if (!link) return NULL;
1001 link->cookie = cookie;
1002 link->string = link_str = (char*)(link + 1);
1003 memcpy(link_str, str, len);
1004 link_str[len] = '\0';
1006 link->bClrChange = clrChange ? 1 : 0;
1008 link->next = rd->first_link;
1009 rd->first_link = link;
1010 link->cpMin = rd->char_pos;
1012 rd->force_color = clrChange;
1013 if (rd->current_link) WINE_FIXME("Pending link\n");
1014 rd->current_link = link;
1016 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
1017 link->cookie, link->string, link->hash, link->window);
1021 unsigned HLPFILE_HalfPointsToTwips(unsigned pts)
1023 static unsigned logPxY;
1026 HDC hdc = GetDC(NULL);
1027 logPxY = GetDeviceCaps(hdc, LOGPIXELSY);
1028 ReleaseDC(NULL, hdc);
1030 return MulDiv(pts, 72 * 10, logPxY);
1033 /***********************************************************************
1035 * HLPFILE_BrowseParagraph
1037 static BOOL HLPFILE_BrowseParagraph(HLPFILE_PAGE* page, struct RtfData* rd,
1038 BYTE *buf, BYTE* end, unsigned* parlen)
1041 BYTE *format, *format_end;
1042 char *text, *text_base, *text_end;
1043 long size, blocksize, datalen;
1044 unsigned short bits;
1045 unsigned nc, ncol = 1;
1047 BOOL in_table = FALSE;
1051 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
1054 blocksize = GET_UINT(buf, 0);
1055 size = GET_UINT(buf, 0x4);
1056 datalen = GET_UINT(buf, 0x10);
1057 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
1058 if (!text) return FALSE;
1059 if (size > blocksize - datalen)
1061 /* need to decompress */
1062 if (page->file->hasPhrases)
1063 HLPFILE_Uncompress2(page->file, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
1064 else if (page->file->hasPhrases40)
1065 HLPFILE_Uncompress3(page->file, text, text + size, buf + datalen, end);
1068 WINE_FIXME("Text size is too long, splitting\n");
1069 size = blocksize - datalen;
1070 memcpy(text, buf + datalen, size);
1074 memcpy(text, buf + datalen, size);
1076 text_end = text + size;
1078 format = buf + 0x15;
1079 format_end = buf + GET_UINT(buf, 0x10);
1081 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
1083 fetch_long(&format);
1084 *parlen = fetch_ushort(&format);
1087 if (buf[0x14] == 0x23)
1094 if (!HLPFILE_RtfAddControl(rd, "\\trowd")) goto done;
1096 if (type == 0 || type == 2)
1098 table_width = GET_SHORT(format, 0);
1102 table_width = 32767;
1103 WINE_TRACE("New table: cols=%d type=%x width=%d\n",
1104 ncol, type, table_width);
1108 sprintf(tmp, "\\trgaph%d\\trleft%d",
1109 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6), table_width, 32767)),
1110 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)));
1111 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1112 pos = HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6) / 2, table_width, 32767));
1113 for (nc = 0; nc < ncol; nc++)
1115 WINE_TRACE("column(%d/%d) gap=%d width=%d\n",
1116 nc, ncol, GET_SHORT(format, nc*4),
1117 GET_SHORT(format, nc*4+2));
1118 pos += GET_SHORT(format, nc * 4) + GET_SHORT(format, nc * 4 + 2);
1119 sprintf(tmp, "\\cellx%d",
1120 HLPFILE_HalfPointsToTwips(MulDiv(pos, table_width, 32767)));
1121 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1126 WINE_TRACE("column(0/%d) gap=%d width=%d\n",
1127 ncol, GET_SHORT(format, 0), GET_SHORT(format, 2));
1128 sprintf(tmp, "\\trleft%d\\cellx%d ",
1129 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)),
1130 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0) + GET_SHORT(format, 2),
1131 table_width, 32767)));
1132 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1137 for (nc = 0; nc < ncol; /**/)
1139 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
1140 if (!HLPFILE_RtfAddControl(rd, "\\pard")) goto done;
1143 nc = GET_SHORT(format, 0);
1144 if (nc == -1) break;
1146 if (!HLPFILE_RtfAddControl(rd, "\\intbl")) goto done;
1149 if (buf[0x14] == 0x01)
1153 bits = GET_USHORT(format, 0); format += 2;
1154 if (bits & 0x0001) fetch_long(&format);
1157 sprintf(tmp, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1158 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1162 sprintf(tmp, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1163 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1167 sprintf(tmp, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1168 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1172 sprintf(tmp, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1173 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1177 sprintf(tmp, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1178 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1182 sprintf(tmp, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1183 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1187 BYTE brdr = *format++;
1190 if (brdr & 0x01 && !HLPFILE_RtfAddControl(rd, "\\box")) goto done;
1191 if (brdr & 0x02 && !HLPFILE_RtfAddControl(rd, "\\brdrt")) goto done;
1192 if (brdr & 0x04 && !HLPFILE_RtfAddControl(rd, "\\brdrl")) goto done;
1193 if (brdr & 0x08 && !HLPFILE_RtfAddControl(rd, "\\brdrb")) goto done;
1194 if (brdr & 0x10 && !HLPFILE_RtfAddControl(rd, "\\brdrr")) goto done;
1195 if (brdr & 0x20 && !HLPFILE_RtfAddControl(rd, "\\brdrth")) goto done;
1196 if (!(brdr & 0x20) && !HLPFILE_RtfAddControl(rd, "\\brdrs")) goto done;
1197 if (brdr & 0x40 && !HLPFILE_RtfAddControl(rd, "\\brdrdb")) goto done;
1200 w = GET_SHORT(format, 0); format += 2;
1203 sprintf(tmp, "\\brdrw%d", HLPFILE_HalfPointsToTwips(w));
1204 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1209 int i, ntab = fetch_short(&format);
1213 for (i = 0; i < ntab; i++)
1215 tab = fetch_ushort(&format);
1216 ts = (tab & 0x4000) ? fetch_ushort(&format) : 0 /* left */;
1219 default: WINE_FIXME("Unknown tab style %x\n", ts);
1221 case 0: kind = ""; break;
1222 case 1: kind = "\\tqr"; break;
1223 case 2: kind = "\\tqc"; break;
1225 /* FIXME: do kind */
1226 sprintf(tmp, "%s\\tx%d",
1227 kind, HLPFILE_HalfPointsToTwips(tab & 0x3FFF));
1228 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1231 switch (bits & 0xc00)
1233 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1234 case 0: if (!HLPFILE_RtfAddControl(rd, "\\ql")) goto done; break;
1235 case 0x400: if (!HLPFILE_RtfAddControl(rd, "\\qr")) goto done; break;
1236 case 0x800: if (!HLPFILE_RtfAddControl(rd, "\\qc")) goto done; break;
1239 /* 0x1000 doesn't need space */
1240 if ((bits & 0x1000) && !HLPFILE_RtfAddControl(rd, "\\keep")) goto done;
1241 if ((bits & 0xE080) != 0)
1242 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
1244 while (text < text_end && format < format_end)
1246 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
1247 textsize = strlen(text);
1250 if (rd->force_color)
1252 if ((rd->current_link->cookie == hlp_link_popup) ?
1253 !HLPFILE_RtfAddControl(rd, "{\\uld\\cf1") :
1254 !HLPFILE_RtfAddControl(rd, "{\\ul\\cf1")) goto done;
1256 if (!HLPFILE_RtfAddText(rd, text)) goto done;
1257 if (rd->force_color && !HLPFILE_RtfAddControl(rd, "}")) goto done;
1258 rd->char_pos += textsize;
1260 /* else: null text, keep on storing attributes */
1261 text += textsize + 1;
1263 if (*format == 0xff)
1269 WINE_TRACE("format=%02x\n", *format);
1273 WINE_FIXME("NIY20\n");
1278 WINE_FIXME("NIY21\n");
1284 unsigned font = GET_USHORT(format, 1);
1287 WINE_TRACE("Changing font to %d\n", font);
1289 switch (rd->font_scale)
1291 case 0: fs = (4 * page->file->fonts[font].LogFont.lfHeight - 13) / 5; break;
1293 case 1: fs = (4 * page->file->fonts[font].LogFont.lfHeight - 3) / 5; break;
1294 case 2: fs = (4 * page->file->fonts[font].LogFont.lfHeight + 17) / 5; break;
1296 /* FIXME: missing at least colors, also bold attribute looses information */
1298 sprintf(tmp, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1300 page->file->fonts[font].LogFont.lfWeight > 400 ? "\\b" : "\\b0",
1301 page->file->fonts[font].LogFont.lfItalic ? "\\i" : "\\i0",
1302 page->file->fonts[font].LogFont.lfUnderline ? "\\ul" : "\\ul0",
1303 page->file->fonts[font].LogFont.lfStrikeOut ? "\\strike" : "\\strike0");
1304 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1309 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1317 if (format[1] != 0xFF)
1319 if (!HLPFILE_RtfAddControl(rd, "\\par\\intbl")) goto done;
1323 if (!HLPFILE_RtfAddControl(rd, "\\cell\\pard\\intbl")) goto done;
1326 else if (!HLPFILE_RtfAddControl(rd, "\\par")) goto done;
1332 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1347 BYTE type = format[1];
1350 /* FIXME: we don't use 'BYTE pos = (*format - 0x86);' for the image position */
1352 size = fetch_long(&format);
1357 fetch_ushort(&format); /* hot spot */
1360 switch (GET_SHORT(format, 0))
1363 HLPFILE_RtfAddGfxByIndex(rd, page->file, GET_SHORT(format, 2));
1367 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1368 GET_SHORT(format, 0),
1369 size, GET_SHORT(format, 2));
1370 HLPFILE_RtfAddGfxByAddr(rd, page->file, format + 2, size - 4);
1374 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1379 WINE_FIXME("Got an embedded element %s\n", format + 6);
1382 WINE_FIXME("Got a type %d picture\n", type);
1391 if (!rd->current_link)
1392 WINE_FIXME("No existing link\n");
1393 rd->current_link->cpMax = rd->char_pos;
1394 rd->current_link = NULL;
1395 rd->force_color = FALSE;
1399 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1405 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1406 /* FIXME: it could be that hypen is also in input stream !! */
1419 WINE_TRACE("macro => %s\n", format + 3);
1420 HLPFILE_AllocLink(rd, hlp_link_macro, (const char*)format + 3,
1421 GET_USHORT(format, 1), 0, !(*format & 4), -1);
1422 format += 3 + GET_USHORT(format, 1);
1427 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1428 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1429 page->file->lpszPath, -1, GET_UINT(format, 1)-16, 1, -1);
1439 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1440 page->file->lpszPath, -1, GET_UINT(format, 1),
1441 !(*format & 4), -1);
1450 char* ptr = (char*) format + 8;
1451 BYTE type = format[3];
1460 ptr = page->file->lpszPath;
1463 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1465 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1468 WINE_WARN("Couldn't find window info for %s\n", ptr);
1469 ptr += strlen(ptr) + 1;
1474 WINE_WARN("Unknown link type %d\n", type);
1477 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1478 ptr, -1, GET_UINT(format, 4), !(*format & 4), wnd);
1480 format += 3 + GET_USHORT(format, 1);
1484 WINE_WARN("format %02x\n", *format);
1491 if (!HLPFILE_RtfAddControl(rd, "\\row\\par\\pard\\plain")) goto done;
1497 HeapFree(GetProcessHeap(), 0, text_base);
1501 /******************************************************************
1502 * HLPFILE_BrowsePage
1505 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page, struct RtfData* rd,
1506 unsigned font_scale, unsigned relative)
1508 HLPFILE *hlpfile = page->file;
1510 DWORD ref = page->reference;
1511 unsigned index, old_index = -1, offset, count = 0, offs = 0;
1512 unsigned cpg, parlen;
1514 const char* ck = NULL;
1517 rd->data = rd->ptr = HeapAlloc(GetProcessHeap(), 0, rd->allocated = 32768);
1519 rd->first_link = rd->current_link = NULL;
1520 rd->force_color = FALSE;
1521 rd->font_scale = font_scale;
1522 rd->relative = relative;
1523 rd->char_pos_rel = 0;
1525 switch (hlpfile->charset)
1527 case DEFAULT_CHARSET:
1528 case ANSI_CHARSET: cpg = 1252; break;
1529 case SHIFTJIS_CHARSET: cpg = 932; break;
1530 case HANGEUL_CHARSET: cpg = 949; break;
1531 case GB2312_CHARSET: cpg = 936; break;
1532 case CHINESEBIG5_CHARSET: cpg = 950; break;
1533 case GREEK_CHARSET: cpg = 1253; break;
1534 case TURKISH_CHARSET: cpg = 1254; break;
1535 case HEBREW_CHARSET: cpg = 1255; break;
1536 case ARABIC_CHARSET: cpg = 1256; break;
1537 case BALTIC_CHARSET: cpg = 1257; break;
1538 case VIETNAMESE_CHARSET: cpg = 1258; break;
1539 case RUSSIAN_CHARSET: cpg = 1251; break;
1540 case EE_CHARSET: cpg = 1250; break;
1541 case THAI_CHARSET: cpg = 874; break;
1542 case JOHAB_CHARSET: cpg = 1361; break;
1543 case MAC_CHARSET: ck = "mac"; break;
1545 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1550 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1551 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1555 sprintf(tmp, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg);
1556 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1559 /* generate font table */
1560 if (!HLPFILE_RtfAddControl(rd, "{\\fonttbl")) return FALSE;
1561 for (index = 0; index < hlpfile->numFonts; index++)
1564 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1566 case FF_MODERN: family = "modern"; break;
1567 case FF_ROMAN: family = "roman"; break;
1568 case FF_SWISS: family = "swiss"; break;
1569 case FF_SCRIPT: family = "script"; break;
1570 case FF_DECORATIVE: family = "decor"; break;
1571 default: family = "nil"; break;
1573 sprintf(tmp, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1575 hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0x0F,
1576 hlpfile->fonts[index].LogFont.lfCharSet,
1577 hlpfile->fonts[index].LogFont.lfFaceName);
1578 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1580 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1581 /* generate color table */
1582 if (!HLPFILE_RtfAddControl(rd, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE;
1583 for (index = 0; index < hlpfile->numFonts; index++)
1586 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1588 case FF_MODERN: family = "modern"; break;
1589 case FF_ROMAN: family = "roman"; break;
1590 case FF_SWISS: family = "swiss"; break;
1591 case FF_SCRIPT: family = "script"; break;
1592 case FF_DECORATIVE: family = "decor"; break;
1593 default: family = "nil"; break;
1595 sprintf(tmp, "\\red%d\\green%d\\blue%d;",
1596 GetRValue(hlpfile->fonts[index].color),
1597 GetGValue(hlpfile->fonts[index].color),
1598 GetBValue(hlpfile->fonts[index].color));
1599 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1601 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1605 if (hlpfile->version <= 16)
1607 index = (ref - 0x0C) / hlpfile->dsize;
1608 offset = (ref - 0x0C) % hlpfile->dsize;
1612 index = (ref - 0x0C) >> 14;
1613 offset = (ref - 0x0C) & 0x3FFF;
1616 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
1618 /* we jumped to the next block, adjust pointers */
1623 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1624 buf = hlpfile->topic_map[index] + offset;
1625 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1626 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1627 if (index != old_index) {offs = 0; old_index = index;}
1632 if (count++) goto done;
1637 if (!HLPFILE_BrowseParagraph(page, rd, buf, end, &parlen)) return FALSE;
1638 if (relative >= index * 0x8000 + offs)
1639 rd->char_pos_rel = rd->char_pos;
1643 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1645 if (hlpfile->version <= 16)
1647 ref += GET_UINT(buf, 0xc);
1648 if (GET_UINT(buf, 0xc) == 0)
1652 ref = GET_UINT(buf, 0xc);
1653 } while (ref != 0xffffffff);
1655 page->first_link = rd->first_link;
1656 return HLPFILE_RtfAddControl(rd, "}");
1659 /******************************************************************
1664 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1667 unsigned i, len, idx;
1668 unsigned face_num, dscr_num, face_offset, dscr_offset;
1671 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1673 WINE_WARN("no subfile FONT\n");
1674 hlpfile->numFonts = 0;
1675 hlpfile->fonts = NULL;
1681 face_num = GET_USHORT(ref, 0);
1682 dscr_num = GET_USHORT(ref, 2);
1683 face_offset = GET_USHORT(ref, 4);
1684 dscr_offset = GET_USHORT(ref, 6);
1686 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1687 face_num, face_offset, dscr_num, dscr_offset);
1689 hlpfile->numFonts = dscr_num;
1690 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1692 len = (dscr_offset - face_offset) / face_num;
1693 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1694 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1695 for (i = 0; i < dscr_num; i++)
1697 flag = ref[dscr_offset + i * 11 + 0];
1698 family = ref[dscr_offset + i * 11 + 2];
1700 hlpfile->fonts[i].LogFont.lfHeight = ref[dscr_offset + i * 11 + 1];
1701 hlpfile->fonts[i].LogFont.lfWidth = 0;
1702 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1703 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1704 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1705 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1706 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1707 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1708 hlpfile->fonts[i].LogFont.lfCharSet = hlpfile->charset;
1709 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1710 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1711 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1712 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1716 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1717 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1718 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1719 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1720 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1721 default: WINE_FIXME("Unknown family %u\n", family);
1723 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1727 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1728 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1732 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1733 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1735 hlpfile->fonts[i].hFont = 0;
1736 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1737 ref[dscr_offset + i * 11 + 6],
1738 ref[dscr_offset + i * 11 + 7]);
1739 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1740 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1746 X(4, "dblUnderline"),
1748 ref[dscr_offset + i * 11 + 1],
1750 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1751 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1756 /***********************************************************************
1758 * HLPFILE_ReadFileToBuffer
1760 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1762 BYTE header[16], dummy[1];
1764 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1767 if (GET_UINT(header, 0) != 0x00035F3F)
1768 {WINE_WARN("wrong header\n"); return FALSE;};
1770 hlpfile->file_buffer_size = GET_UINT(header, 12);
1771 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1772 if (!hlpfile->file_buffer) return FALSE;
1774 memcpy(hlpfile->file_buffer, header, 16);
1775 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1776 {WINE_WARN("filesize1\n"); return FALSE;};
1778 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1780 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1785 /**************************************************************************
1788 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1791 static int comp_FindSubFile(void *p, const void *key,
1792 int leaf, void** next)
1794 *next = (char *)p+strlen(p)+(leaf?5:3);
1795 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1796 return strcmp(p, key);
1799 /***********************************************************************
1801 * HLPFILE_FindSubFile
1803 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
1807 WINE_TRACE("looking for file '%s'\n", name);
1808 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
1809 name, comp_FindSubFile);
1810 if (!ptr) return FALSE;
1811 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
1812 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
1814 WINE_ERR("internal file %s does not fit\n", name);
1817 *subend = *subbuf + GET_UINT(*subbuf, 0);
1818 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
1820 WINE_ERR("internal file %s does not fit\n", name);
1823 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1825 WINE_ERR("invalid size provided for internal file %s\n", name);
1831 /***********************************************************************
1833 * HLPFILE_SystemCommands
1835 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1837 BYTE *buf, *ptr, *end;
1838 HLPFILE_MACRO *macro, **m;
1840 unsigned short magic, minor, major, flags;
1842 hlpfile->lpszTitle = NULL;
1844 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1846 magic = GET_USHORT(buf + 9, 0);
1847 minor = GET_USHORT(buf + 9, 2);
1848 major = GET_USHORT(buf + 9, 4);
1849 /* gen date on 4 bytes */
1850 flags = GET_USHORT(buf + 9, 10);
1851 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1852 magic, major, minor, flags);
1853 if (magic != 0x036C || major != 1)
1854 {WINE_WARN("Wrong system header\n"); return FALSE;}
1857 hlpfile->tbsize = 0x800;
1858 hlpfile->compressed = 0;
1860 else if (flags == 0)
1862 hlpfile->tbsize = 0x1000;
1863 hlpfile->compressed = 0;
1865 else if (flags == 4)
1867 hlpfile->tbsize = 0x1000;
1868 hlpfile->compressed = 1;
1872 hlpfile->tbsize = 0x800;
1873 hlpfile->compressed = 1;
1876 if (hlpfile->compressed)
1877 hlpfile->dsize = 0x4000;
1879 hlpfile->dsize = hlpfile->tbsize - 0x0C;
1881 hlpfile->version = minor;
1882 hlpfile->flags = flags;
1883 hlpfile->charset = DEFAULT_CHARSET;
1885 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1887 char *str = (char*) ptr + 4;
1888 switch (GET_USHORT(ptr, 0))
1891 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1892 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1893 if (!hlpfile->lpszTitle) return FALSE;
1894 lstrcpy(hlpfile->lpszTitle, str);
1895 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1899 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1900 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1901 if (!hlpfile->lpszCopyright) return FALSE;
1902 lstrcpy(hlpfile->lpszCopyright, str);
1903 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1907 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1908 hlpfile->contents_start = GET_UINT(ptr, 4);
1909 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1913 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1915 p = (char*)macro + sizeof(HLPFILE_MACRO);
1917 macro->lpszMacro = p;
1919 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1924 if (GET_USHORT(ptr, 4 + 4) != 1)
1925 WINE_FIXME("More than one icon, picking up first\n");
1926 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
1927 hlpfile->hIcon = CreateIconFromResourceEx(ptr + 4 + 0x16,
1928 GET_USHORT(ptr, 2) - 0x16, TRUE,
1933 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1935 if (hlpfile->windows)
1936 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1937 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1939 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1940 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1942 if (hlpfile->windows)
1944 unsigned flags = GET_USHORT(ptr, 4);
1945 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1947 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1948 else wi->type[0] = '\0';
1949 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1950 else wi->name[0] = '\0';
1951 if (flags & 0x0004) strcpy(wi->caption, &str[21]);
1952 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1953 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1954 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1955 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1956 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1957 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1958 wi->win_style = WS_OVERLAPPEDWINDOW;
1959 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1960 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1961 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",
1962 flags & 0x0001 ? 'T' : 't',
1963 flags & 0x0002 ? 'N' : 'n',
1964 flags & 0x0004 ? 'C' : 'c',
1965 flags & 0x0008 ? 'X' : 'x',
1966 flags & 0x0010 ? 'Y' : 'y',
1967 flags & 0x0020 ? 'W' : 'w',
1968 flags & 0x0040 ? 'H' : 'h',
1969 flags & 0x0080 ? 'S' : 's',
1970 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1971 wi->size.cx, wi->size.cy);
1975 WINE_WARN("Citation: '%s'\n", ptr + 4);
1978 hlpfile->charset = ptr[4];
1979 WINE_TRACE("Charset: %d\n", hlpfile->charset);
1982 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1985 if (!hlpfile->lpszTitle)
1986 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
1990 /***********************************************************************
1992 * HLPFILE_UncompressedLZ77_Size
1994 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
2001 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2005 int code = GET_USHORT(ptr, 0);
2006 int len = 3 + (code >> 12);
2010 else newsize++, ptr++;
2017 /***********************************************************************
2019 * HLPFILE_UncompressLZ77
2021 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
2028 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2032 int code = GET_USHORT(ptr, 0);
2033 int len = 3 + (code >> 12);
2034 int offset = code & 0xfff;
2036 * We must copy byte-by-byte here. We cannot use memcpy nor
2037 * memmove here. Just example:
2038 * a[]={1,2,3,4,5,6,7,8,9,10}
2042 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
2044 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
2047 else *newptr++ = *ptr++;
2054 /***********************************************************************
2056 * HLPFILE_UncompressLZ77_Phrases
2058 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
2060 UINT i, num, dec_size, head_size;
2063 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
2065 if (hlpfile->version <= 16)
2070 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
2071 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
2073 if (hlpfile->version <= 16)
2074 dec_size = end - buf - 15 - 2 * num;
2076 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
2078 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2079 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2080 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2082 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2083 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2087 for (i = 0; i <= num; i++)
2088 hlpfile->phrases_offsets[i] = GET_USHORT(buf, head_size + 2 * i) - 2 * num - 2;
2090 if (hlpfile->version <= 16)
2091 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
2093 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
2095 hlpfile->hasPhrases = TRUE;
2099 /***********************************************************************
2101 * HLPFILE_Uncompress_Phrases40
2103 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
2106 INT dec_size, cpr_size;
2107 BYTE *buf_idx, *end_idx;
2108 BYTE *buf_phs, *end_phs;
2109 long* ptr, mask = 0;
2111 unsigned short bc, n;
2113 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
2114 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
2116 ptr = (long*)(buf_idx + 9 + 28);
2117 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
2118 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
2120 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2121 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2122 GET_UINT(buf_idx, 9 + 0),
2123 GET_UINT(buf_idx, 9 + 4),
2124 GET_UINT(buf_idx, 9 + 8),
2125 GET_UINT(buf_idx, 9 + 12),
2126 GET_UINT(buf_idx, 9 + 16),
2127 GET_UINT(buf_idx, 9 + 20),
2128 GET_USHORT(buf_idx, 9 + 24),
2129 GET_USHORT(buf_idx, 9 + 26));
2131 dec_size = GET_UINT(buf_idx, 9 + 12);
2132 cpr_size = GET_UINT(buf_idx, 9 + 16);
2134 if (dec_size != cpr_size &&
2135 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
2137 WINE_WARN("size mismatch %u %u\n",
2138 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2139 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2142 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2143 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2144 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2146 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2147 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2151 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
2153 hlpfile->phrases_offsets[0] = 0;
2154 for (i = 0; i < num; i++)
2156 for (n = 1; getbit(); n += 1 << bc);
2158 if (bc > 1 && getbit()) n += 2;
2159 if (bc > 2 && getbit()) n += 4;
2160 if (bc > 3 && getbit()) n += 8;
2161 if (bc > 4 && getbit()) n += 16;
2162 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
2166 if (dec_size == cpr_size)
2167 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2169 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2171 hlpfile->hasPhrases40 = TRUE;
2175 /***********************************************************************
2177 * HLPFILE_Uncompress_Topic
2179 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
2181 BYTE *buf, *ptr, *end, *newptr;
2182 unsigned int i, newsize = 0;
2183 unsigned int topic_size;
2185 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
2186 {WINE_WARN("topic0\n"); return FALSE;}
2188 buf += 9; /* Skip file header */
2189 topic_size = end - buf;
2190 if (hlpfile->compressed)
2192 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2194 for (i = 0; i < hlpfile->topic_maplen; i++)
2196 ptr = buf + i * hlpfile->tbsize;
2198 /* I don't know why, it's necessary for printman.hlp */
2199 if (ptr + 0x44 > end) ptr = end - 0x44;
2201 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
2204 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2205 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
2206 if (!hlpfile->topic_map) return FALSE;
2207 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2208 hlpfile->topic_end = newptr + newsize;
2210 for (i = 0; i < hlpfile->topic_maplen; i++)
2212 ptr = buf + i * hlpfile->tbsize;
2213 if (ptr + 0x44 > end) ptr = end - 0x44;
2215 hlpfile->topic_map[i] = newptr;
2216 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
2221 /* basically, we need to copy the TopicBlockSize byte pages
2222 * (removing the first 0x0C) in one single area in memory
2224 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2225 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2226 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
2227 if (!hlpfile->topic_map) return FALSE;
2228 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2229 hlpfile->topic_end = newptr + topic_size;
2231 for (i = 0; i < hlpfile->topic_maplen; i++)
2233 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
2234 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
2240 /***********************************************************************
2242 * HLPFILE_Uncompress2
2245 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
2247 BYTE *phptr, *phend;
2251 while (ptr < end && newptr < newend)
2253 if (!*ptr || *ptr >= 0x10)
2257 code = 0x100 * ptr[0] + ptr[1];
2258 index = (code - 0x100) / 2;
2260 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
2261 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
2263 if (newptr + (phend - phptr) > newend)
2265 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
2266 newptr, newend, (SIZE_T)(phend - phptr));
2269 memcpy(newptr, phptr, phend - phptr);
2270 newptr += phend - phptr;
2271 if (code & 1) *newptr++ = ' ';
2276 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
2279 /******************************************************************
2280 * HLPFILE_Uncompress3
2284 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
2285 const BYTE* src, const BYTE* src_end)
2287 unsigned int idx, len;
2289 for (; src < src_end; src++)
2291 if ((*src & 1) == 0)
2294 if (idx > hlpfile->num_phrases)
2296 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2301 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2302 if (dst + len <= dst_end)
2303 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2306 else if ((*src & 0x03) == 0x01)
2308 idx = (*src + 1) * 64;
2310 if (idx > hlpfile->num_phrases)
2312 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2317 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2318 if (dst + len <= dst_end)
2319 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2322 else if ((*src & 0x07) == 0x03)
2324 len = (*src / 8) + 1;
2325 if (dst + len <= dst_end)
2326 memcpy(dst, src + 1, len);
2331 len = (*src / 16) + 1;
2332 if (dst + len <= dst_end)
2333 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
2338 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
2342 /******************************************************************
2343 * HLPFILE_UncompressRLE
2347 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE* dst, unsigned dstsz)
2350 BYTE* sdst = dst + dstsz;
2358 if (dst + ch <= sdst)
2359 memcpy(dst, src, ch);
2364 if (dst + ch <= sdst)
2365 memset(dst, (char)*src, ch);
2371 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
2372 (SIZE_T)(dst - (sdst - dstsz)), dstsz);
2375 /**************************************************************************
2376 * HLPFILE_BPTreeSearch
2378 * Searches for an element in B+ tree
2381 * buf [I] pointer to the embedded file structured as a B+ tree
2382 * key [I] pointer to data to find
2383 * comp [I] compare function
2386 * Pointer to block identified by key, or NULL if failure.
2389 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
2390 HLPFILE_BPTreeCompare comp)
2396 BYTE *pages, *ptr, *newptr;
2400 magic = GET_USHORT(buf, 9);
2401 if (magic != 0x293B)
2403 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2406 page_size = GET_USHORT(buf, 9+4);
2407 cur_page = GET_USHORT(buf, 9+26);
2408 level = GET_USHORT(buf, 9+32);
2409 pages = buf + 9 + 38;
2412 ptr = pages + cur_page*page_size;
2413 entries = GET_SHORT(ptr, 2);
2415 for (i = 0; i < entries; i++)
2417 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
2420 cur_page = GET_USHORT(ptr-2, 0);
2422 ptr = pages + cur_page*page_size;
2423 entries = GET_SHORT(ptr, 2);
2425 for (i = 0; i < entries; i++)
2427 ret = comp(ptr, key, 1, (void **)&newptr);
2428 if (ret == 0) return ptr;
2429 if (ret > 0) return NULL;
2435 /**************************************************************************
2436 * HLPFILE_BPTreeEnum
2438 * Enumerates elements in B+ tree.
2441 * buf [I] pointer to the embedded file structured as a B+ tree
2442 * cb [I] compare function
2443 * cookie [IO] cookie for cb function
2445 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
2451 BYTE *pages, *ptr, *newptr;
2454 magic = GET_USHORT(buf, 9);
2455 if (magic != 0x293B)
2457 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2460 page_size = GET_USHORT(buf, 9+4);
2461 cur_page = GET_USHORT(buf, 9+26);
2462 level = GET_USHORT(buf, 9+32);
2463 pages = buf + 9 + 38;
2466 ptr = pages + cur_page*page_size;
2467 cur_page = GET_USHORT(ptr, 4);
2469 while (cur_page != 0xFFFF)
2471 ptr = pages + cur_page*page_size;
2472 entries = GET_SHORT(ptr, 2);
2474 for (i = 0; i < entries; i++)
2476 cb(ptr, (void **)&newptr, cookie);
2479 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
2484 /***********************************************************************
2486 * HLPFILE_GetContext
2488 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2493 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2494 {WINE_WARN("context0\n"); return FALSE;}
2497 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2498 if (!hlpfile->Context) return FALSE;
2499 memcpy(hlpfile->Context, cbuf, clen);
2504 /***********************************************************************
2506 * HLPFILE_GetKeywords
2508 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2513 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2515 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2516 if (!hlpfile->kwbtree) return FALSE;
2517 memcpy(hlpfile->kwbtree, cbuf, clen);
2519 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2521 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2522 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2526 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2527 if (!hlpfile->kwdata)
2529 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2532 memcpy(hlpfile->kwdata, cbuf, clen);
2537 /***********************************************************************
2541 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2544 unsigned entries, i;
2546 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2547 {WINE_WARN("no map section\n"); return FALSE;}
2549 entries = GET_USHORT(cbuf, 9);
2550 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2551 if (!hlpfile->Map) return FALSE;
2552 hlpfile->wMapLen = entries;
2553 for (i = 0; i < entries; i++)
2555 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2556 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2561 /***********************************************************************
2565 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2567 HLPFILE_MACRO* next;
2572 HeapFree(GetProcessHeap(), 0, macro);
2577 /***********************************************************************
2581 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2588 HLPFILE_DeleteMacro(page->first_macro);
2589 HeapFree(GetProcessHeap(), 0, page);
2594 /***********************************************************************
2596 * HLPFILE_FreeHlpFile
2598 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2602 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2604 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2605 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2606 else first_hlpfile = hlpfile->next;
2608 if (hlpfile->numFonts)
2610 for (i = 0; i < hlpfile->numFonts; i++)
2612 DeleteObject(hlpfile->fonts[i].hFont);
2614 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2617 if (hlpfile->numBmps)
2619 for (i = 0; i < hlpfile->numBmps; i++)
2621 DeleteObject(hlpfile->bmps[i]);
2623 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2626 HLPFILE_DeletePage(hlpfile->first_page);
2627 HLPFILE_DeleteMacro(hlpfile->first_macro);
2629 DestroyIcon(hlpfile->hIcon);
2630 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2631 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2632 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2633 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2634 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2635 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2636 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2637 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2638 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2639 HeapFree(GetProcessHeap(), 0, hlpfile->help_on_file);
2640 HeapFree(GetProcessHeap(), 0, hlpfile);