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(const BYTE *ptr, const BYTE *end);
59 static BYTE* HLPFILE_UncompressLZ77(const BYTE *ptr, const 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*, const BYTE*, const BYTE*, unsigned, unsigned);
67 static BOOL HLPFILE_SkipParagraph(HLPFILE*, const BYTE*, const 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, const BYTE *buf, const 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(const BYTE** ptr)
466 ret = (*(const unsigned long*)(*ptr) - 0x80000000L) / 2;
471 ret = (*(const unsigned short*)(*ptr) - 0x8000) / 2;
478 static unsigned long fetch_ulong(const BYTE** ptr)
484 ret = *(const unsigned long*)(*ptr) / 2;
489 ret = *(const unsigned short*)(*ptr) / 2;
495 static short fetch_short(const BYTE** ptr)
501 ret = (*(const unsigned short*)(*ptr) - 0x8000) / 2;
506 ret = (*(const unsigned char*)(*ptr) - 0x80) / 2;
512 static unsigned short fetch_ushort(const BYTE** ptr)
518 ret = *(const unsigned short*)(*ptr) / 2;
523 ret = *(const unsigned char*)(*ptr) / 2;
529 /***********************************************************************
531 * HLPFILE_SkipParagraph
533 static BOOL HLPFILE_SkipParagraph(HLPFILE *hlpfile, const BYTE *buf, const 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 const BYTE* HLPFILE_DecompressGfx(const BYTE* src, unsigned csz, unsigned sz, BYTE packing,
563 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
567 case 0: /* uncompressed */
569 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
574 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz);
575 if (!dst) return NULL;
576 HLPFILE_UncompressRLE(src, src + csz, *alloc, sz);
579 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
580 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz77);
581 if (!dst) return NULL;
582 HLPFILE_UncompressLZ77(src, src + csz, *alloc);
584 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
586 case 3: /* LZ77 then RLE */
587 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
588 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
589 if (!tmp) return FALSE;
590 HLPFILE_UncompressLZ77(src, src + csz, tmp);
591 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz);
594 HeapFree(GetProcessHeap(), 0, tmp);
597 HLPFILE_UncompressRLE(tmp, tmp + sz77, *alloc, sz);
598 HeapFree(GetProcessHeap(), 0, tmp);
601 WINE_FIXME("Unsupported packing %u\n", packing);
607 static BOOL HLPFILE_RtfAddRawString(struct RtfData* rd, const char* str, size_t sz)
609 if (rd->ptr + sz >= rd->data + rd->allocated)
611 char* new = HeapReAlloc(GetProcessHeap(), 0, rd->data, rd->allocated *= 2);
612 if (!new) return FALSE;
613 rd->ptr = new + (rd->ptr - rd->data);
616 memcpy(rd->ptr, str, sz);
622 static BOOL HLPFILE_RtfAddControl(struct RtfData* rd, const char* str)
624 if (*str == '\\' || *str == '{') rd->in_text = FALSE;
625 else if (*str == '}') rd->in_text = TRUE;
626 return HLPFILE_RtfAddRawString(rd, str, strlen(str));
629 static BOOL HLPFILE_RtfAddText(struct RtfData* rd, const char* str)
638 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
641 for (last = p = str; *p; p++)
643 if (*p < 0) /* escape non ASCII chars */
646 rlen = sprintf(xx, "\\'%x", *(const BYTE*)p);
651 case '{': rlen = 2; replace = "\\{"; break;
652 case '}': rlen = 2; replace = "\\}"; break;
653 case '\\': rlen = 2; replace = "\\\\"; break;
656 if ((p != last && !HLPFILE_RtfAddRawString(rd, last, p - last)) ||
657 !HLPFILE_RtfAddRawString(rd, replace, rlen)) return FALSE;
660 return HLPFILE_RtfAddRawString(rd, last, p - last);
663 /******************************************************************
667 static BOOL HLPFILE_RtfAddHexBytes(struct RtfData* rd, const void* _ptr, unsigned sz)
671 const BYTE* ptr = _ptr;
672 static const char* _2hex = "0123456789abcdef";
676 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
679 for (; sz; sz -= step)
682 for (i = 0; i < step; i++)
684 tmp[2 * i + 0] = _2hex[*ptr >> 4];
685 tmp[2 * i + 1] = _2hex[*ptr++ & 0xF];
687 if (!HLPFILE_RtfAddRawString(rd, tmp, 2 * step)) return FALSE;
692 /******************************************************************
693 * HLPFILE_RtfAddTransparentBitmap
695 * We'll transform a transparent bitmap into an metafile that
696 * we then transform into RTF
698 static BOOL HLPFILE_RtfAddTransparentBitmap(struct RtfData* rd, const BITMAPINFO* bi,
699 const void* pict, unsigned nc)
701 HDC hdc, hdcMask, hdcMem, hdcEMF;
702 HBITMAP hbm, hbmMask, hbmOldMask, hbmOldMem;
708 hbm = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
709 CBM_INIT, pict, bi, DIB_RGB_COLORS);
711 hdcMem = CreateCompatibleDC(hdc);
712 hbmOldMem = SelectObject(hdcMem, hbm);
714 /* create the mask bitmap from the main bitmap */
715 hdcMask = CreateCompatibleDC(hdc);
716 hbmMask = CreateBitmap(bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, 1, 1, NULL);
717 hbmOldMask = SelectObject(hdcMask, hbmMask);
719 RGB(bi->bmiColors[nc - 1].rgbRed,
720 bi->bmiColors[nc - 1].rgbGreen,
721 bi->bmiColors[nc - 1].rgbBlue));
722 BitBlt(hdcMask, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCCOPY);
724 /* sets to RGB(0,0,0) the transparent bits in main bitmap */
725 SetBkColor(hdcMem, RGB(0,0,0));
726 SetTextColor(hdcMem, RGB(255,255,255));
727 BitBlt(hdcMem, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMask, 0, 0, SRCAND);
729 SelectObject(hdcMask, hbmOldMask);
732 SelectObject(hdcMem, hbmOldMem);
735 /* we create the bitmap on the fly */
736 hdcEMF = CreateEnhMetaFile(NULL, NULL, NULL, NULL);
737 hdcMem = CreateCompatibleDC(hdcEMF);
739 /* sets to RGB(0,0,0) the transparent bits in final bitmap */
740 hbmOldMem = SelectObject(hdcMem, hbmMask);
741 SetBkColor(hdcEMF, RGB(255, 255, 255));
742 SetTextColor(hdcEMF, RGB(0, 0, 0));
743 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCAND);
745 /* and copy the remaining bits of main bitmap */
746 SelectObject(hdcMem, hbm);
747 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCPAINT);
748 SelectObject(hdcMem, hbmOldMem);
753 DeleteObject(hbmMask);
756 hEMF = CloseEnhMetaFile(hdcEMF);
758 /* generate rtf stream */
759 sz = GetEnhMetaFileBits(hEMF, 0, NULL);
760 if (sz && (data = HeapAlloc(GetProcessHeap(), 0, sz)))
762 if (sz == GetEnhMetaFileBits(hEMF, sz, data))
764 ret = HLPFILE_RtfAddControl(rd, "{\\pict\\emfblip") &&
765 HLPFILE_RtfAddHexBytes(rd, data, sz) &&
766 HLPFILE_RtfAddControl(rd, "}");
768 HeapFree(GetProcessHeap(), 0, data);
770 DeleteEnhMetaFile(hEMF);
775 /******************************************************************
776 * HLPFILE_RtfAddBitmap
779 static BOOL HLPFILE_RtfAddBitmap(struct RtfData* rd, const BYTE* beg, BYTE type, BYTE pack)
782 const BYTE* pict_beg;
785 unsigned long off, csz;
787 BOOL clrImportant = FALSE;
791 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
792 if (!bi) return FALSE;
794 ptr = beg + 2; /* for type and pack */
796 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
797 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
798 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
799 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
800 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
801 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
802 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
803 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
804 clrImportant = fetch_ulong(&ptr);
805 bi->bmiHeader.biClrImportant = (clrImportant > 1) ? clrImportant : 0;
806 bi->bmiHeader.biCompression = BI_RGB;
807 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
808 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
809 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
810 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
811 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
812 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
814 csz = fetch_ulong(&ptr);
815 fetch_ulong(&ptr); /* hotspot size */
817 off = GET_UINT(ptr, 0); ptr += 4;
818 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
820 /* now read palette info */
825 nc = bi->bmiHeader.biClrUsed;
826 /* not quite right, especially for bitfields type of compression */
827 if (!nc && bi->bmiHeader.biBitCount <= 8)
828 nc = 1 << bi->bmiHeader.biBitCount;
830 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
831 if (!bi) return FALSE;
832 for (i = 0; i < nc; i++)
834 bi->bmiColors[i].rgbBlue = ptr[0];
835 bi->bmiColors[i].rgbGreen = ptr[1];
836 bi->bmiColors[i].rgbRed = ptr[2];
837 bi->bmiColors[i].rgbReserved = 0;
841 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack, &alloc);
843 if (clrImportant == 1 && nc > 0)
845 ret = HLPFILE_RtfAddTransparentBitmap(rd, bi, pict_beg, nc);
848 if (!HLPFILE_RtfAddControl(rd, "{\\pict")) goto done;
851 sprintf(tmp, "\\dibitmap0\\picw%d\\pich%d",
852 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
853 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
854 if (!HLPFILE_RtfAddHexBytes(rd, bi, sizeof(*bi) + nc * sizeof(RGBQUAD))) goto done;
858 sprintf(tmp, "\\wbitmap0\\wbmbitspixel%d\\wbmplanes%d\\picw%d\\pich%d",
859 bi->bmiHeader.biBitCount, bi->bmiHeader.biPlanes,
860 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
861 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
863 if (!HLPFILE_RtfAddHexBytes(rd, pict_beg, bi->bmiHeader.biSizeImage)) goto done;
864 if (!HLPFILE_RtfAddControl(rd, "}")) goto done;
868 HeapFree(GetProcessHeap(), 0, bi);
869 HeapFree(GetProcessHeap(), 0, alloc);
874 /******************************************************************
875 * HLPFILE_RtfAddMetaFile
878 static BOOL HLPFILE_RtfAddMetaFile(struct RtfData* rd, const BYTE* beg, BYTE pack)
881 unsigned long size, csize;
882 unsigned long off, hsoff;
889 WINE_TRACE("Loading metafile\n");
891 ptr = beg + 2; /* for type and pack */
893 mm = fetch_ushort(&ptr); /* mapping mode */
894 sprintf(tmp, "{\\pict\\wmetafile%d\\picw%d\\pich%d",
895 mm, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2));
896 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
899 size = fetch_ulong(&ptr); /* decompressed size */
900 csize = fetch_ulong(&ptr); /* compressed size */
901 fetch_ulong(&ptr); /* hotspot size */
902 off = GET_UINT(ptr, 0);
903 hsoff = GET_UINT(ptr, 4);
906 WINE_TRACE("sz=%lu csz=%lu offs=%lu/%u,%lu\n",
907 size, csize, off, ptr - beg, hsoff);
909 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack, &alloc);
910 if (!bits) return FALSE;
912 ret = HLPFILE_RtfAddHexBytes(rd, bits, size) &&
913 HLPFILE_RtfAddControl(rd, "}");
915 HeapFree(GetProcessHeap(), 0, alloc);
920 /******************************************************************
921 * HLPFILE_RtfAddGfxByAddr
924 static BOOL HLPFILE_RtfAddGfxByAddr(struct RtfData* rd, HLPFILE *hlpfile,
925 const BYTE* ref, unsigned long size)
929 numpict = GET_USHORT(ref, 2);
930 WINE_TRACE("Got picture magic=%04x #=%d\n", GET_USHORT(ref, 0), numpict);
932 for (i = 0; i < numpict; i++)
938 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
939 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
946 case 5: /* device dependent bmp */
947 case 6: /* device independent bmp */
948 HLPFILE_RtfAddBitmap(rd, beg, type, pack);
951 HLPFILE_RtfAddMetaFile(rd, beg, pack);
953 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
956 /* FIXME: hotspots */
958 /* FIXME: implement support for multiple picture format */
959 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
965 /******************************************************************
966 * HLPFILE_RtfAddGfxByIndex
970 static BOOL HLPFILE_RtfAddGfxByIndex(struct RtfData* rd, HLPFILE *hlpfile,
976 WINE_TRACE("Loading picture #%d\n", index);
978 sprintf(tmp, "|bm%u", index);
980 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
983 return HLPFILE_RtfAddGfxByAddr(rd, hlpfile, ref, end - ref);
986 /******************************************************************
991 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
992 const char* str, unsigned len, LONG hash,
993 unsigned clrChange, unsigned wnd)
998 /* FIXME: should build a string table for the attributes.link.lpszPath
999 * they are reallocated for each link
1001 if (len == -1) len = strlen(str);
1002 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + len + 1);
1003 if (!link) return NULL;
1005 link->cookie = cookie;
1006 link->string = link_str = (char*)(link + 1);
1007 memcpy(link_str, str, len);
1008 link_str[len] = '\0';
1010 link->bClrChange = clrChange ? 1 : 0;
1012 link->next = rd->first_link;
1013 rd->first_link = link;
1014 link->cpMin = rd->char_pos;
1016 rd->force_color = clrChange;
1017 if (rd->current_link) WINE_FIXME("Pending link\n");
1018 rd->current_link = link;
1020 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
1021 link->cookie, link->string, link->hash, link->window);
1025 unsigned HLPFILE_HalfPointsToTwips(unsigned pts)
1027 static unsigned logPxY;
1030 HDC hdc = GetDC(NULL);
1031 logPxY = GetDeviceCaps(hdc, LOGPIXELSY);
1032 ReleaseDC(NULL, hdc);
1034 return MulDiv(pts, 72 * 10, logPxY);
1037 /***********************************************************************
1039 * HLPFILE_BrowseParagraph
1041 static BOOL HLPFILE_BrowseParagraph(HLPFILE_PAGE* page, struct RtfData* rd,
1042 BYTE *buf, BYTE* end, unsigned* parlen)
1045 const BYTE *format, *format_end;
1046 char *text, *text_base, *text_end;
1047 long size, blocksize, datalen;
1048 unsigned short bits;
1049 unsigned nc, ncol = 1;
1051 BOOL in_table = FALSE;
1055 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
1058 blocksize = GET_UINT(buf, 0);
1059 size = GET_UINT(buf, 0x4);
1060 datalen = GET_UINT(buf, 0x10);
1061 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
1062 if (!text) return FALSE;
1063 if (size > blocksize - datalen)
1065 /* need to decompress */
1066 if (page->file->hasPhrases)
1067 HLPFILE_Uncompress2(page->file, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
1068 else if (page->file->hasPhrases40)
1069 HLPFILE_Uncompress3(page->file, text, text + size, buf + datalen, end);
1072 WINE_FIXME("Text size is too long, splitting\n");
1073 size = blocksize - datalen;
1074 memcpy(text, buf + datalen, size);
1078 memcpy(text, buf + datalen, size);
1080 text_end = text + size;
1082 format = buf + 0x15;
1083 format_end = buf + GET_UINT(buf, 0x10);
1085 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
1087 fetch_long(&format);
1088 *parlen = fetch_ushort(&format);
1091 if (buf[0x14] == 0x23)
1098 if (!HLPFILE_RtfAddControl(rd, "\\trowd")) goto done;
1100 if (type == 0 || type == 2)
1102 table_width = GET_SHORT(format, 0);
1106 table_width = 32767;
1107 WINE_TRACE("New table: cols=%d type=%x width=%d\n",
1108 ncol, type, table_width);
1112 sprintf(tmp, "\\trgaph%d\\trleft%d",
1113 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6), table_width, 32767)),
1114 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)));
1115 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1116 pos = HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6) / 2, table_width, 32767));
1117 for (nc = 0; nc < ncol; nc++)
1119 WINE_TRACE("column(%d/%d) gap=%d width=%d\n",
1120 nc, ncol, GET_SHORT(format, nc*4),
1121 GET_SHORT(format, nc*4+2));
1122 pos += GET_SHORT(format, nc * 4) + GET_SHORT(format, nc * 4 + 2);
1123 sprintf(tmp, "\\cellx%d",
1124 HLPFILE_HalfPointsToTwips(MulDiv(pos, table_width, 32767)));
1125 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1130 WINE_TRACE("column(0/%d) gap=%d width=%d\n",
1131 ncol, GET_SHORT(format, 0), GET_SHORT(format, 2));
1132 sprintf(tmp, "\\trleft%d\\cellx%d ",
1133 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)),
1134 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0) + GET_SHORT(format, 2),
1135 table_width, 32767)));
1136 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1141 for (nc = 0; nc < ncol; /**/)
1143 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
1144 if (!HLPFILE_RtfAddControl(rd, "\\pard")) goto done;
1147 nc = GET_SHORT(format, 0);
1148 if (nc == -1) break;
1150 if (!HLPFILE_RtfAddControl(rd, "\\intbl")) goto done;
1153 if (buf[0x14] == 0x01)
1157 bits = GET_USHORT(format, 0); format += 2;
1158 if (bits & 0x0001) fetch_long(&format);
1161 sprintf(tmp, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1162 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1166 sprintf(tmp, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1167 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1171 sprintf(tmp, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1172 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1176 sprintf(tmp, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1177 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1181 sprintf(tmp, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1182 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1186 sprintf(tmp, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1187 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1191 BYTE brdr = *format++;
1194 if (brdr & 0x01 && !HLPFILE_RtfAddControl(rd, "\\box")) goto done;
1195 if (brdr & 0x02 && !HLPFILE_RtfAddControl(rd, "\\brdrt")) goto done;
1196 if (brdr & 0x04 && !HLPFILE_RtfAddControl(rd, "\\brdrl")) goto done;
1197 if (brdr & 0x08 && !HLPFILE_RtfAddControl(rd, "\\brdrb")) goto done;
1198 if (brdr & 0x10 && !HLPFILE_RtfAddControl(rd, "\\brdrr")) goto done;
1199 if (brdr & 0x20 && !HLPFILE_RtfAddControl(rd, "\\brdrth")) goto done;
1200 if (!(brdr & 0x20) && !HLPFILE_RtfAddControl(rd, "\\brdrs")) goto done;
1201 if (brdr & 0x40 && !HLPFILE_RtfAddControl(rd, "\\brdrdb")) goto done;
1204 w = GET_SHORT(format, 0); format += 2;
1207 sprintf(tmp, "\\brdrw%d", HLPFILE_HalfPointsToTwips(w));
1208 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1213 int i, ntab = fetch_short(&format);
1217 for (i = 0; i < ntab; i++)
1219 tab = fetch_ushort(&format);
1220 ts = (tab & 0x4000) ? fetch_ushort(&format) : 0 /* left */;
1223 default: WINE_FIXME("Unknown tab style %x\n", ts);
1225 case 0: kind = ""; break;
1226 case 1: kind = "\\tqr"; break;
1227 case 2: kind = "\\tqc"; break;
1229 /* FIXME: do kind */
1230 sprintf(tmp, "%s\\tx%d",
1231 kind, HLPFILE_HalfPointsToTwips(tab & 0x3FFF));
1232 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1235 switch (bits & 0xc00)
1237 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1238 case 0: if (!HLPFILE_RtfAddControl(rd, "\\ql")) goto done; break;
1239 case 0x400: if (!HLPFILE_RtfAddControl(rd, "\\qr")) goto done; break;
1240 case 0x800: if (!HLPFILE_RtfAddControl(rd, "\\qc")) goto done; break;
1243 /* 0x1000 doesn't need space */
1244 if ((bits & 0x1000) && !HLPFILE_RtfAddControl(rd, "\\keep")) goto done;
1245 if ((bits & 0xE080) != 0)
1246 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
1248 while (text < text_end && format < format_end)
1250 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
1251 textsize = strlen(text);
1254 if (rd->force_color)
1256 if ((rd->current_link->cookie == hlp_link_popup) ?
1257 !HLPFILE_RtfAddControl(rd, "{\\uld\\cf1") :
1258 !HLPFILE_RtfAddControl(rd, "{\\ul\\cf1")) goto done;
1260 if (!HLPFILE_RtfAddText(rd, text)) goto done;
1261 if (rd->force_color && !HLPFILE_RtfAddControl(rd, "}")) goto done;
1262 rd->char_pos += textsize;
1264 /* else: null text, keep on storing attributes */
1265 text += textsize + 1;
1267 if (*format == 0xff)
1273 WINE_TRACE("format=%02x\n", *format);
1277 WINE_FIXME("NIY20\n");
1282 WINE_FIXME("NIY21\n");
1288 unsigned font = GET_USHORT(format, 1);
1291 WINE_TRACE("Changing font to %d\n", font);
1293 /* Font size in hlpfile is given in the same units as
1294 rtf control word \fs uses (half-points). */
1295 switch (rd->font_scale)
1297 case 0: fs = page->file->fonts[font].LogFont.lfHeight - 4; break;
1299 case 1: fs = page->file->fonts[font].LogFont.lfHeight; break;
1300 case 2: fs = page->file->fonts[font].LogFont.lfHeight + 4; break;
1302 /* FIXME: missing at least colors, also bold attribute looses information */
1304 sprintf(tmp, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1306 page->file->fonts[font].LogFont.lfWeight > 400 ? "\\b" : "\\b0",
1307 page->file->fonts[font].LogFont.lfItalic ? "\\i" : "\\i0",
1308 page->file->fonts[font].LogFont.lfUnderline ? "\\ul" : "\\ul0",
1309 page->file->fonts[font].LogFont.lfStrikeOut ? "\\strike" : "\\strike0");
1310 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1315 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1323 if (format[1] != 0xFF)
1325 if (!HLPFILE_RtfAddControl(rd, "\\par\\intbl")) goto done;
1329 if (!HLPFILE_RtfAddControl(rd, "\\cell\\pard\\intbl")) goto done;
1332 else if (!HLPFILE_RtfAddControl(rd, "\\par")) goto done;
1338 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1353 BYTE type = format[1];
1356 /* FIXME: we don't use 'BYTE pos = (*format - 0x86);' for the image position */
1358 size = fetch_long(&format);
1363 fetch_ushort(&format); /* hot spot */
1366 switch (GET_SHORT(format, 0))
1369 HLPFILE_RtfAddGfxByIndex(rd, page->file, GET_SHORT(format, 2));
1373 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1374 GET_SHORT(format, 0),
1375 size, GET_SHORT(format, 2));
1376 HLPFILE_RtfAddGfxByAddr(rd, page->file, format + 2, size - 4);
1380 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1385 WINE_FIXME("Got an embedded element %s\n", format + 6);
1388 WINE_FIXME("Got a type %d picture\n", type);
1397 if (!rd->current_link)
1398 WINE_FIXME("No existing link\n");
1399 rd->current_link->cpMax = rd->char_pos;
1400 rd->current_link = NULL;
1401 rd->force_color = FALSE;
1405 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1411 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1412 /* FIXME: it could be that hypen is also in input stream !! */
1425 WINE_TRACE("macro => %s\n", format + 3);
1426 HLPFILE_AllocLink(rd, hlp_link_macro, (const char*)format + 3,
1427 GET_USHORT(format, 1), 0, !(*format & 4), -1);
1428 format += 3 + GET_USHORT(format, 1);
1433 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1434 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1435 page->file->lpszPath, -1, GET_UINT(format, 1)-16, 1, -1);
1445 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1446 page->file->lpszPath, -1, GET_UINT(format, 1),
1447 !(*format & 4), -1);
1456 char* ptr = (char*) format + 8;
1457 BYTE type = format[3];
1466 ptr = page->file->lpszPath;
1469 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1471 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1474 WINE_WARN("Couldn't find window info for %s\n", ptr);
1475 ptr += strlen(ptr) + 1;
1480 WINE_WARN("Unknown link type %d\n", type);
1483 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1484 ptr, -1, GET_UINT(format, 4), !(*format & 4), wnd);
1486 format += 3 + GET_USHORT(format, 1);
1490 WINE_WARN("format %02x\n", *format);
1497 if (!HLPFILE_RtfAddControl(rd, "\\row\\par\\pard\\plain")) goto done;
1503 HeapFree(GetProcessHeap(), 0, text_base);
1507 /******************************************************************
1508 * HLPFILE_BrowsePage
1511 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page, struct RtfData* rd,
1512 unsigned font_scale, unsigned relative)
1514 HLPFILE *hlpfile = page->file;
1516 DWORD ref = page->reference;
1517 unsigned index, old_index = -1, offset, count = 0, offs = 0;
1518 unsigned cpg, parlen;
1520 const char* ck = NULL;
1523 rd->data = rd->ptr = HeapAlloc(GetProcessHeap(), 0, rd->allocated = 32768);
1525 rd->first_link = rd->current_link = NULL;
1526 rd->force_color = FALSE;
1527 rd->font_scale = font_scale;
1528 rd->relative = relative;
1529 rd->char_pos_rel = 0;
1531 switch (hlpfile->charset)
1533 case DEFAULT_CHARSET:
1534 case ANSI_CHARSET: cpg = 1252; break;
1535 case SHIFTJIS_CHARSET: cpg = 932; break;
1536 case HANGEUL_CHARSET: cpg = 949; break;
1537 case GB2312_CHARSET: cpg = 936; break;
1538 case CHINESEBIG5_CHARSET: cpg = 950; break;
1539 case GREEK_CHARSET: cpg = 1253; break;
1540 case TURKISH_CHARSET: cpg = 1254; break;
1541 case HEBREW_CHARSET: cpg = 1255; break;
1542 case ARABIC_CHARSET: cpg = 1256; break;
1543 case BALTIC_CHARSET: cpg = 1257; break;
1544 case VIETNAMESE_CHARSET: cpg = 1258; break;
1545 case RUSSIAN_CHARSET: cpg = 1251; break;
1546 case EE_CHARSET: cpg = 1250; break;
1547 case THAI_CHARSET: cpg = 874; break;
1548 case JOHAB_CHARSET: cpg = 1361; break;
1549 case MAC_CHARSET: ck = "mac"; break;
1551 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1556 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1557 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1561 sprintf(tmp, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg);
1562 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1565 /* generate font table */
1566 if (!HLPFILE_RtfAddControl(rd, "{\\fonttbl")) return FALSE;
1567 for (index = 0; index < hlpfile->numFonts; index++)
1570 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1572 case FF_MODERN: family = "modern"; break;
1573 case FF_ROMAN: family = "roman"; break;
1574 case FF_SWISS: family = "swiss"; break;
1575 case FF_SCRIPT: family = "script"; break;
1576 case FF_DECORATIVE: family = "decor"; break;
1577 default: family = "nil"; break;
1579 sprintf(tmp, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1581 hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0x0F,
1582 hlpfile->fonts[index].LogFont.lfCharSet,
1583 hlpfile->fonts[index].LogFont.lfFaceName);
1584 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1586 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1587 /* generate color table */
1588 if (!HLPFILE_RtfAddControl(rd, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE;
1589 for (index = 0; index < hlpfile->numFonts; index++)
1592 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1594 case FF_MODERN: family = "modern"; break;
1595 case FF_ROMAN: family = "roman"; break;
1596 case FF_SWISS: family = "swiss"; break;
1597 case FF_SCRIPT: family = "script"; break;
1598 case FF_DECORATIVE: family = "decor"; break;
1599 default: family = "nil"; break;
1601 sprintf(tmp, "\\red%d\\green%d\\blue%d;",
1602 GetRValue(hlpfile->fonts[index].color),
1603 GetGValue(hlpfile->fonts[index].color),
1604 GetBValue(hlpfile->fonts[index].color));
1605 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1607 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1611 if (hlpfile->version <= 16)
1613 index = (ref - 0x0C) / hlpfile->dsize;
1614 offset = (ref - 0x0C) % hlpfile->dsize;
1618 index = (ref - 0x0C) >> 14;
1619 offset = (ref - 0x0C) & 0x3FFF;
1622 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
1624 /* we jumped to the next block, adjust pointers */
1629 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1630 buf = hlpfile->topic_map[index] + offset;
1631 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1632 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1633 if (index != old_index) {offs = 0; old_index = index;}
1638 if (count++) goto done;
1643 if (!HLPFILE_BrowseParagraph(page, rd, buf, end, &parlen)) return FALSE;
1644 if (relative > index * 0x8000 + offs)
1645 rd->char_pos_rel = rd->char_pos;
1649 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1651 if (hlpfile->version <= 16)
1653 ref += GET_UINT(buf, 0xc);
1654 if (GET_UINT(buf, 0xc) == 0)
1658 ref = GET_UINT(buf, 0xc);
1659 } while (ref != 0xffffffff);
1661 page->first_link = rd->first_link;
1662 return HLPFILE_RtfAddControl(rd, "}");
1665 /******************************************************************
1670 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1673 unsigned i, len, idx;
1674 unsigned face_num, dscr_num, face_offset, dscr_offset;
1677 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1679 WINE_WARN("no subfile FONT\n");
1680 hlpfile->numFonts = 0;
1681 hlpfile->fonts = NULL;
1687 face_num = GET_USHORT(ref, 0);
1688 dscr_num = GET_USHORT(ref, 2);
1689 face_offset = GET_USHORT(ref, 4);
1690 dscr_offset = GET_USHORT(ref, 6);
1692 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1693 face_num, face_offset, dscr_num, dscr_offset);
1695 hlpfile->numFonts = dscr_num;
1696 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1698 len = (dscr_offset - face_offset) / face_num;
1699 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1700 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1701 for (i = 0; i < dscr_num; i++)
1703 flag = ref[dscr_offset + i * 11 + 0];
1704 family = ref[dscr_offset + i * 11 + 2];
1706 hlpfile->fonts[i].LogFont.lfHeight = ref[dscr_offset + i * 11 + 1];
1707 hlpfile->fonts[i].LogFont.lfWidth = 0;
1708 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1709 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1710 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1711 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1712 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1713 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1714 hlpfile->fonts[i].LogFont.lfCharSet = hlpfile->charset;
1715 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1716 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1717 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1718 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1722 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1723 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1724 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1725 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1726 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1727 default: WINE_FIXME("Unknown family %u\n", family);
1729 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1733 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1734 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1738 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1739 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1741 hlpfile->fonts[i].hFont = 0;
1742 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1743 ref[dscr_offset + i * 11 + 6],
1744 ref[dscr_offset + i * 11 + 7]);
1745 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1746 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1752 X(4, "dblUnderline"),
1754 ref[dscr_offset + i * 11 + 1],
1756 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1757 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1762 /***********************************************************************
1764 * HLPFILE_ReadFileToBuffer
1766 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1768 BYTE header[16], dummy[1];
1770 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1773 if (GET_UINT(header, 0) != 0x00035F3F)
1774 {WINE_WARN("wrong header\n"); return FALSE;};
1776 hlpfile->file_buffer_size = GET_UINT(header, 12);
1777 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1778 if (!hlpfile->file_buffer) return FALSE;
1780 memcpy(hlpfile->file_buffer, header, 16);
1781 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1782 {WINE_WARN("filesize1\n"); return FALSE;};
1784 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1786 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1791 /**************************************************************************
1794 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1797 static int comp_FindSubFile(void *p, const void *key,
1798 int leaf, void** next)
1800 *next = (char *)p+strlen(p)+(leaf?5:3);
1801 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1802 return strcmp(p, key);
1805 /***********************************************************************
1807 * HLPFILE_FindSubFile
1809 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
1813 WINE_TRACE("looking for file '%s'\n", name);
1814 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
1815 name, comp_FindSubFile);
1816 if (!ptr) return FALSE;
1817 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
1818 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
1820 WINE_ERR("internal file %s does not fit\n", name);
1823 *subend = *subbuf + GET_UINT(*subbuf, 0);
1824 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
1826 WINE_ERR("internal file %s does not fit\n", name);
1829 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1831 WINE_ERR("invalid size provided for internal file %s\n", name);
1837 /***********************************************************************
1839 * HLPFILE_SystemCommands
1841 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1843 BYTE *buf, *ptr, *end;
1844 HLPFILE_MACRO *macro, **m;
1846 unsigned short magic, minor, major, flags;
1848 hlpfile->lpszTitle = NULL;
1850 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1852 magic = GET_USHORT(buf + 9, 0);
1853 minor = GET_USHORT(buf + 9, 2);
1854 major = GET_USHORT(buf + 9, 4);
1855 /* gen date on 4 bytes */
1856 flags = GET_USHORT(buf + 9, 10);
1857 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1858 magic, major, minor, flags);
1859 if (magic != 0x036C || major != 1)
1860 {WINE_WARN("Wrong system header\n"); return FALSE;}
1863 hlpfile->tbsize = 0x800;
1864 hlpfile->compressed = 0;
1866 else if (flags == 0)
1868 hlpfile->tbsize = 0x1000;
1869 hlpfile->compressed = 0;
1871 else if (flags == 4)
1873 hlpfile->tbsize = 0x1000;
1874 hlpfile->compressed = 1;
1878 hlpfile->tbsize = 0x800;
1879 hlpfile->compressed = 1;
1882 if (hlpfile->compressed)
1883 hlpfile->dsize = 0x4000;
1885 hlpfile->dsize = hlpfile->tbsize - 0x0C;
1887 hlpfile->version = minor;
1888 hlpfile->flags = flags;
1889 hlpfile->charset = DEFAULT_CHARSET;
1891 if (hlpfile->version <= 16)
1893 char *str = (char*)buf + 0x15;
1895 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1896 if (!hlpfile->lpszTitle) return FALSE;
1897 lstrcpy(hlpfile->lpszTitle, str);
1898 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1899 /* Nothing more to parse */
1902 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1904 char *str = (char*) ptr + 4;
1905 switch (GET_USHORT(ptr, 0))
1908 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1909 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1910 if (!hlpfile->lpszTitle) return FALSE;
1911 lstrcpy(hlpfile->lpszTitle, str);
1912 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1916 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1917 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1918 if (!hlpfile->lpszCopyright) return FALSE;
1919 lstrcpy(hlpfile->lpszCopyright, str);
1920 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1924 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1925 hlpfile->contents_start = GET_UINT(ptr, 4);
1926 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1930 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1932 p = (char*)macro + sizeof(HLPFILE_MACRO);
1934 macro->lpszMacro = p;
1936 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1941 if (GET_USHORT(ptr, 4 + 4) != 1)
1942 WINE_FIXME("More than one icon, picking up first\n");
1943 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
1944 hlpfile->hIcon = CreateIconFromResourceEx(ptr + 4 + 0x16,
1945 GET_USHORT(ptr, 2) - 0x16, TRUE,
1950 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1952 if (hlpfile->windows)
1953 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1954 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1956 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1957 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1959 if (hlpfile->windows)
1961 unsigned flags = GET_USHORT(ptr, 4);
1962 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1964 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1965 else wi->type[0] = '\0';
1966 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1967 else wi->name[0] = '\0';
1968 if (flags & 0x0004) strcpy(wi->caption, &str[21]);
1969 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1970 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1971 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1972 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1973 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1974 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1975 wi->win_style = WS_OVERLAPPEDWINDOW;
1976 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1977 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1978 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",
1979 flags & 0x0001 ? 'T' : 't',
1980 flags & 0x0002 ? 'N' : 'n',
1981 flags & 0x0004 ? 'C' : 'c',
1982 flags & 0x0008 ? 'X' : 'x',
1983 flags & 0x0010 ? 'Y' : 'y',
1984 flags & 0x0020 ? 'W' : 'w',
1985 flags & 0x0040 ? 'H' : 'h',
1986 flags & 0x0080 ? 'S' : 's',
1987 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1988 wi->size.cx, wi->size.cy);
1992 WINE_WARN("Citation: '%s'\n", ptr + 4);
1995 hlpfile->charset = ptr[4];
1996 WINE_TRACE("Charset: %d\n", hlpfile->charset);
1999 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
2002 if (!hlpfile->lpszTitle)
2003 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
2007 /***********************************************************************
2009 * HLPFILE_UncompressedLZ77_Size
2011 static INT HLPFILE_UncompressedLZ77_Size(const BYTE *ptr, const BYTE *end)
2018 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2022 int code = GET_USHORT(ptr, 0);
2023 int len = 3 + (code >> 12);
2027 else newsize++, ptr++;
2034 /***********************************************************************
2036 * HLPFILE_UncompressLZ77
2038 static BYTE *HLPFILE_UncompressLZ77(const BYTE *ptr, const BYTE *end, BYTE *newptr)
2045 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2049 int code = GET_USHORT(ptr, 0);
2050 int len = 3 + (code >> 12);
2051 int offset = code & 0xfff;
2053 * We must copy byte-by-byte here. We cannot use memcpy nor
2054 * memmove here. Just example:
2055 * a[]={1,2,3,4,5,6,7,8,9,10}
2059 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
2061 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
2064 else *newptr++ = *ptr++;
2071 /***********************************************************************
2073 * HLPFILE_UncompressLZ77_Phrases
2075 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
2077 UINT i, num, dec_size, head_size;
2080 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
2082 if (hlpfile->version <= 16)
2087 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
2088 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
2090 if (hlpfile->version <= 16)
2091 dec_size = end - buf - 15 - 2 * num;
2093 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
2095 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2096 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2097 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2099 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2100 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2104 for (i = 0; i <= num; i++)
2105 hlpfile->phrases_offsets[i] = GET_USHORT(buf, head_size + 2 * i) - 2 * num - 2;
2107 if (hlpfile->version <= 16)
2108 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
2110 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
2112 hlpfile->hasPhrases = TRUE;
2116 /***********************************************************************
2118 * HLPFILE_Uncompress_Phrases40
2120 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
2123 INT dec_size, cpr_size;
2124 BYTE *buf_idx, *end_idx;
2125 BYTE *buf_phs, *end_phs;
2126 long* ptr, mask = 0;
2128 unsigned short bc, n;
2130 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
2131 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
2133 ptr = (long*)(buf_idx + 9 + 28);
2134 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
2135 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
2137 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2138 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2139 GET_UINT(buf_idx, 9 + 0),
2140 GET_UINT(buf_idx, 9 + 4),
2141 GET_UINT(buf_idx, 9 + 8),
2142 GET_UINT(buf_idx, 9 + 12),
2143 GET_UINT(buf_idx, 9 + 16),
2144 GET_UINT(buf_idx, 9 + 20),
2145 GET_USHORT(buf_idx, 9 + 24),
2146 GET_USHORT(buf_idx, 9 + 26));
2148 dec_size = GET_UINT(buf_idx, 9 + 12);
2149 cpr_size = GET_UINT(buf_idx, 9 + 16);
2151 if (dec_size != cpr_size &&
2152 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
2154 WINE_WARN("size mismatch %u %u\n",
2155 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2156 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2159 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2160 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2161 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2163 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2164 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2168 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
2170 hlpfile->phrases_offsets[0] = 0;
2171 for (i = 0; i < num; i++)
2173 for (n = 1; getbit(); n += 1 << bc);
2175 if (bc > 1 && getbit()) n += 2;
2176 if (bc > 2 && getbit()) n += 4;
2177 if (bc > 3 && getbit()) n += 8;
2178 if (bc > 4 && getbit()) n += 16;
2179 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
2183 if (dec_size == cpr_size)
2184 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2186 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2188 hlpfile->hasPhrases40 = TRUE;
2192 /***********************************************************************
2194 * HLPFILE_Uncompress_Topic
2196 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
2198 BYTE *buf, *ptr, *end, *newptr;
2199 unsigned int i, newsize = 0;
2200 unsigned int topic_size;
2202 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
2203 {WINE_WARN("topic0\n"); return FALSE;}
2205 buf += 9; /* Skip file header */
2206 topic_size = end - buf;
2207 if (hlpfile->compressed)
2209 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2211 for (i = 0; i < hlpfile->topic_maplen; i++)
2213 ptr = buf + i * hlpfile->tbsize;
2215 /* I don't know why, it's necessary for printman.hlp */
2216 if (ptr + 0x44 > end) ptr = end - 0x44;
2218 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
2221 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2222 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
2223 if (!hlpfile->topic_map) return FALSE;
2224 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2225 hlpfile->topic_end = newptr + newsize;
2227 for (i = 0; i < hlpfile->topic_maplen; i++)
2229 ptr = buf + i * hlpfile->tbsize;
2230 if (ptr + 0x44 > end) ptr = end - 0x44;
2232 hlpfile->topic_map[i] = newptr;
2233 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
2238 /* basically, we need to copy the TopicBlockSize byte pages
2239 * (removing the first 0x0C) in one single area in memory
2241 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2242 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2243 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
2244 if (!hlpfile->topic_map) return FALSE;
2245 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2246 hlpfile->topic_end = newptr + topic_size;
2248 for (i = 0; i < hlpfile->topic_maplen; i++)
2250 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
2251 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
2257 /***********************************************************************
2259 * HLPFILE_Uncompress2
2262 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
2264 BYTE *phptr, *phend;
2268 while (ptr < end && newptr < newend)
2270 if (!*ptr || *ptr >= 0x10)
2274 code = 0x100 * ptr[0] + ptr[1];
2275 index = (code - 0x100) / 2;
2277 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
2278 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
2280 if (newptr + (phend - phptr) > newend)
2282 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
2283 newptr, newend, (SIZE_T)(phend - phptr));
2286 memcpy(newptr, phptr, phend - phptr);
2287 newptr += phend - phptr;
2288 if (code & 1) *newptr++ = ' ';
2293 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
2296 /******************************************************************
2297 * HLPFILE_Uncompress3
2301 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
2302 const BYTE* src, const BYTE* src_end)
2304 unsigned int idx, len;
2306 for (; src < src_end; src++)
2308 if ((*src & 1) == 0)
2311 if (idx > hlpfile->num_phrases)
2313 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2318 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2319 if (dst + len <= dst_end)
2320 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2323 else if ((*src & 0x03) == 0x01)
2325 idx = (*src + 1) * 64;
2327 if (idx > hlpfile->num_phrases)
2329 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2334 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2335 if (dst + len <= dst_end)
2336 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2339 else if ((*src & 0x07) == 0x03)
2341 len = (*src / 8) + 1;
2342 if (dst + len <= dst_end)
2343 memcpy(dst, src + 1, len);
2348 len = (*src / 16) + 1;
2349 if (dst + len <= dst_end)
2350 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
2355 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
2359 /******************************************************************
2360 * HLPFILE_UncompressRLE
2364 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE* dst, unsigned dstsz)
2367 BYTE* sdst = dst + dstsz;
2375 if (dst + ch <= sdst)
2376 memcpy(dst, src, ch);
2381 if (dst + ch <= sdst)
2382 memset(dst, (char)*src, ch);
2388 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
2389 (SIZE_T)(dst - (sdst - dstsz)), dstsz);
2392 /**************************************************************************
2393 * HLPFILE_BPTreeSearch
2395 * Searches for an element in B+ tree
2398 * buf [I] pointer to the embedded file structured as a B+ tree
2399 * key [I] pointer to data to find
2400 * comp [I] compare function
2403 * Pointer to block identified by key, or NULL if failure.
2406 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
2407 HLPFILE_BPTreeCompare comp)
2413 BYTE *pages, *ptr, *newptr;
2417 magic = GET_USHORT(buf, 9);
2418 if (magic != 0x293B)
2420 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2423 page_size = GET_USHORT(buf, 9+4);
2424 cur_page = GET_USHORT(buf, 9+26);
2425 level = GET_USHORT(buf, 9+32);
2426 pages = buf + 9 + 38;
2429 ptr = pages + cur_page*page_size;
2430 entries = GET_SHORT(ptr, 2);
2432 for (i = 0; i < entries; i++)
2434 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
2437 cur_page = GET_USHORT(ptr-2, 0);
2439 ptr = pages + cur_page*page_size;
2440 entries = GET_SHORT(ptr, 2);
2442 for (i = 0; i < entries; i++)
2444 ret = comp(ptr, key, 1, (void **)&newptr);
2445 if (ret == 0) return ptr;
2446 if (ret > 0) return NULL;
2452 /**************************************************************************
2453 * HLPFILE_BPTreeEnum
2455 * Enumerates elements in B+ tree.
2458 * buf [I] pointer to the embedded file structured as a B+ tree
2459 * cb [I] compare function
2460 * cookie [IO] cookie for cb function
2462 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
2468 BYTE *pages, *ptr, *newptr;
2471 magic = GET_USHORT(buf, 9);
2472 if (magic != 0x293B)
2474 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2477 page_size = GET_USHORT(buf, 9+4);
2478 cur_page = GET_USHORT(buf, 9+26);
2479 level = GET_USHORT(buf, 9+32);
2480 pages = buf + 9 + 38;
2483 ptr = pages + cur_page*page_size;
2484 cur_page = GET_USHORT(ptr, 4);
2486 while (cur_page != 0xFFFF)
2488 ptr = pages + cur_page*page_size;
2489 entries = GET_SHORT(ptr, 2);
2491 for (i = 0; i < entries; i++)
2493 cb(ptr, (void **)&newptr, cookie);
2496 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
2501 /***********************************************************************
2503 * HLPFILE_GetContext
2505 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2510 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2511 {WINE_WARN("context0\n"); return FALSE;}
2514 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2515 if (!hlpfile->Context) return FALSE;
2516 memcpy(hlpfile->Context, cbuf, clen);
2521 /***********************************************************************
2523 * HLPFILE_GetKeywords
2525 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2530 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2532 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2533 if (!hlpfile->kwbtree) return FALSE;
2534 memcpy(hlpfile->kwbtree, cbuf, clen);
2536 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2538 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2539 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2543 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2544 if (!hlpfile->kwdata)
2546 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2549 memcpy(hlpfile->kwdata, cbuf, clen);
2554 /***********************************************************************
2558 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2561 unsigned entries, i;
2563 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2564 {WINE_WARN("no map section\n"); return FALSE;}
2566 entries = GET_USHORT(cbuf, 9);
2567 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2568 if (!hlpfile->Map) return FALSE;
2569 hlpfile->wMapLen = entries;
2570 for (i = 0; i < entries; i++)
2572 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2573 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2578 /***********************************************************************
2582 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2584 HLPFILE_MACRO* next;
2589 HeapFree(GetProcessHeap(), 0, macro);
2594 /***********************************************************************
2598 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2605 HLPFILE_DeleteMacro(page->first_macro);
2606 HeapFree(GetProcessHeap(), 0, page);
2611 /***********************************************************************
2613 * HLPFILE_FreeHlpFile
2615 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2619 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2621 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2622 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2623 else first_hlpfile = hlpfile->next;
2625 if (hlpfile->numFonts)
2627 for (i = 0; i < hlpfile->numFonts; i++)
2629 DeleteObject(hlpfile->fonts[i].hFont);
2631 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2634 if (hlpfile->numBmps)
2636 for (i = 0; i < hlpfile->numBmps; i++)
2638 DeleteObject(hlpfile->bmps[i]);
2640 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2643 HLPFILE_DeletePage(hlpfile->first_page);
2644 HLPFILE_DeleteMacro(hlpfile->first_macro);
2646 DestroyIcon(hlpfile->hIcon);
2647 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2648 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2649 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2650 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2651 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2652 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2653 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2654 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2655 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2656 HeapFree(GetProcessHeap(), 0, hlpfile->help_on_file);
2657 HeapFree(GetProcessHeap(), 0, hlpfile);