winhelp: Added support for relative offsets in richedit rendered pages.
[wine/wine64.git] / programs / winhelp / hlpfile.c
blob046d25a9e150cff251b7bc7201d21cd3ebe6f9f8
1 /*
2 * Help Viewer
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
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winhelp.h"
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)
79 HLPFILE_PAGE *page;
80 UINT temp = wNum;
82 WINE_TRACE("<%s>[%u]\n", hlpfile->lpszPath, wNum);
84 for (page = hlpfile->first_page; page && temp; page = page->next) temp--;
85 if (!page)
86 WINE_ERR("Page of number %u not found in file %s\n", wNum, hlpfile->lpszPath);
87 return page;
90 /******************************************************************
91 * HLPFILE_PageByOffset
95 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset, ULONG* relative)
97 HLPFILE_PAGE* page;
98 HLPFILE_PAGE* found;
100 if (!hlpfile) return 0;
102 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, offset);
104 if (offset == 0xFFFFFFFF) return NULL;
105 page = 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;
112 found = page;
115 if (!found)
116 WINE_ERR("Page of offset %u not found in file %s\n",
117 offset, hlpfile->lpszPath);
118 return found;
121 /**************************************************************************
122 * comp_PageByHash
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;
137 return 0;
140 /***********************************************************************
142 * HLPFILE_PageByHash
144 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash, ULONG* relative)
146 BYTE *ptr;
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)
156 *relative = 0;
157 return HLPFILE_PageByNumber(hlpfile, lHash);
160 ptr = HLPFILE_BPTreeSearch(hlpfile->Context, LongToPtr(lHash), comp_PageByHash);
161 if (!ptr)
163 WINE_ERR("Page of hash %x not found in file %s\n", lHash, hlpfile->lpszPath);
164 return NULL;
167 return HLPFILE_PageByOffset(hlpfile, GET_UINT(ptr, 4), relative);
170 /***********************************************************************
172 * HLPFILE_PageByMap
174 HLPFILE_PAGE *HLPFILE_PageByMap(HLPFILE* hlpfile, LONG lMap, ULONG* relative)
176 unsigned int i;
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);
189 return NULL;
192 /***********************************************************************
194 * HLPFILE_Contents
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);
203 if (!page)
205 page = hlpfile->first_page;
206 *relative = 0;
208 return page;
211 /***********************************************************************
213 * HLPFILE_Hash
215 LONG HLPFILE_Hash(LPCSTR lpszContext)
217 LONG lHash = 0;
218 CHAR c;
220 while ((c = *lpszContext++))
222 CHAR x = 0;
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;
231 return lHash;
234 /***********************************************************************
236 * HLPFILE_ReadHlpFile
238 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
240 HLPFILE* hlpfile;
242 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
244 if (!strcmp(lpszPath, hlpfile->lpszPath))
246 hlpfile->wRefCount++;
247 return hlpfile;
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);
268 hlpfile = 0;
271 return hlpfile;
274 /***********************************************************************
276 * HLPFILE_DoReadHlpFile
278 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
280 BOOL ret;
281 HFILE hFile;
282 OFSTRUCT ofs;
283 BYTE* buf;
284 DWORD ref = 0x0C;
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);
291 _lclose(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];
304 old_index = -1;
305 offs = 0;
308 BYTE* end;
310 if (hlpfile->version <= 16)
312 index = (ref - 0x0C) / hlpfile->dsize;
313 offset = (ref - 0x0C) % hlpfile->dsize;
315 else
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 */
324 ref -= 12;
325 offset -= 12;
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;}
336 switch (buf[0x14])
338 case 0x02:
339 if (!HLPFILE_AddPage(hlpfile, buf, end, ref, index * 0x8000L + offs)) return FALSE;
340 break;
342 case 0x01:
343 case 0x20:
344 case 0x23:
345 if (!HLPFILE_SkipParagraph(hlpfile, buf, end, &len)) return FALSE;
346 offs += len;
347 break;
349 default:
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)
357 break;
359 else
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 /***********************************************************************
371 * HLPFILE_AddPage
373 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned ref, unsigned offset)
375 HLPFILE_PAGE* page;
376 BYTE* title;
377 UINT titlesize, blocksize, datalen;
378 char* ptr;
379 HLPFILE_MACRO*macro;
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);
398 else
400 WINE_FIXME("Text size is too long, splitting\n");
401 titlesize = blocksize - datalen;
402 memcpy(page->lpszTitle, title, titlesize);
405 else
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;
416 else
418 hlpfile->first_page = page;
419 hlpfile->last_page = page;
420 page->prev = NULL;
423 page->file = hlpfile;
424 page->next = NULL;
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);
443 char* macro_str;
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;
454 ptr += len + 1;
457 return TRUE;
460 static long fetch_long(BYTE** ptr)
462 long ret;
464 if (*(*ptr) & 1)
466 ret = (*(unsigned long*)(*ptr) - 0x80000000L) / 2;
467 (*ptr) += 4;
469 else
471 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
472 (*ptr) += 2;
475 return ret;
478 static unsigned long fetch_ulong(BYTE** ptr)
480 unsigned long ret;
482 if (*(*ptr) & 1)
484 ret = *(unsigned long*)(*ptr) / 2;
485 (*ptr) += 4;
487 else
489 ret = *(unsigned short*)(*ptr) / 2;
490 (*ptr) += 2;
492 return ret;
495 static short fetch_short(BYTE** ptr)
497 short ret;
499 if (*(*ptr) & 1)
501 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
502 (*ptr) += 2;
504 else
506 ret = (*(unsigned char*)(*ptr) - 0x80) / 2;
507 (*ptr)++;
509 return ret;
512 static unsigned short fetch_ushort(BYTE** ptr)
514 unsigned short ret;
516 if (*(*ptr) & 1)
518 ret = *(unsigned short*)(*ptr) / 2;
519 (*ptr) += 2;
521 else
523 ret = *(unsigned char*)(*ptr) / 2;
524 (*ptr)++;
526 return ret;
529 /***********************************************************************
531 * HLPFILE_SkipParagraph
533 static BOOL HLPFILE_SkipParagraph(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned* len)
535 BYTE *tmp;
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;};
540 tmp = buf + 0x15;
541 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
543 fetch_long(&tmp);
544 *len = fetch_ushort(&tmp);
546 else *len = end-buf-15;
548 return TRUE;
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)
558 BYTE* dst;
559 BYTE* tmp;
560 BYTE* tmp2;
561 unsigned sz77;
563 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
565 switch (packing)
567 case 0: /* uncompressed */
568 if (sz != csz)
569 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
570 dst = src;
571 break;
572 case 1: /* RunLen */
573 tmp = dst = HeapAlloc(GetProcessHeap(), 0, sz);
574 if (!dst) return NULL;
575 HLPFILE_UncompressRLE(src, src + csz, &tmp, sz);
576 if (tmp - dst != sz)
577 WINE_WARN("Bogus gfx sizes (RunLen): %lu/%u\n", (SIZE_T)(tmp - dst), sz);
578 break;
579 case 2: /* LZ77 */
580 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
581 dst = HeapAlloc(GetProcessHeap(), 0, sz77);
582 if (!dst) return NULL;
583 HLPFILE_UncompressLZ77(src, src + csz, dst);
584 if (sz77 != sz)
585 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
586 break;
587 case 3: /* LZ77 then RLE */
588 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
589 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
590 if (!tmp) return FALSE;
591 HLPFILE_UncompressLZ77(src, src + csz, tmp);
592 dst = tmp2 = HeapAlloc(GetProcessHeap(), 0, sz);
593 if (!dst)
595 HeapFree(GetProcessHeap(), 0, tmp);
596 return FALSE;
598 HLPFILE_UncompressRLE(tmp, tmp + sz77, &tmp2, sz);
599 if (tmp2 - dst != sz)
600 WINE_WARN("Bogus gfx sizes (LZ77+RunLen): %lu / %u\n", (SIZE_T)(tmp2 - dst), sz);
601 HeapFree(GetProcessHeap(), 0, tmp);
602 break;
603 default:
604 WINE_FIXME("Unsupported packing %u\n", packing);
605 return NULL;
607 return dst;
610 static BOOL HLPFILE_RtfAddRawString(struct RtfData* rd, const char* str, size_t sz)
612 if (rd->ptr + sz >= rd->data + rd->allocated)
614 char* new = HeapReAlloc(GetProcessHeap(), 0, rd->data, rd->allocated *= 2);
615 if (!new) return FALSE;
616 rd->ptr = new + (rd->ptr - rd->data);
617 rd->data = new;
619 memcpy(rd->ptr, str, sz);
620 rd->ptr += sz;
622 return TRUE;
625 static BOOL HLPFILE_RtfAddControl(struct RtfData* rd, const char* str)
627 if (*str == '\\' || *str == '{') rd->in_text = FALSE;
628 else if (*str == '}') rd->in_text = TRUE;
629 return HLPFILE_RtfAddRawString(rd, str, strlen(str));
632 static BOOL HLPFILE_RtfAddText(struct RtfData* rd, const char* str)
634 const char* p;
635 const char* last;
636 const char* replace;
637 unsigned rlen;
639 if (!rd->in_text)
641 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
642 rd->in_text = TRUE;
644 for (last = p = str; *p; p++)
646 if (*p < 0) /* escape non ASCII chars */
648 static char xx[8];
649 rlen = sprintf(xx, "\\'%x", *(const BYTE*)p);
650 replace = xx;
652 else switch (*p)
654 case '{': rlen = 2; replace = "\\{"; break;
655 case '}': rlen = 2; replace = "\\}"; break;
656 case '\\': rlen = 2; replace = "\\\\"; break;
657 default: continue;
659 if ((p != last && !HLPFILE_RtfAddRawString(rd, last, p - last)) ||
660 !HLPFILE_RtfAddRawString(rd, replace, rlen)) return FALSE;
661 last = p + 1;
663 return HLPFILE_RtfAddRawString(rd, last, p - last);
666 /******************************************************************
667 * RtfAddHexBytes
670 static BOOL HLPFILE_RtfAddHexBytes(struct RtfData* rd, const void* _ptr, unsigned sz)
672 char tmp[512];
673 unsigned i, step;
674 const BYTE* ptr = _ptr;
675 static const char* _2hex = "0123456789abcdef";
677 if (!rd->in_text)
679 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
680 rd->in_text = TRUE;
682 for (; sz; sz -= step)
684 step = min(256, sz);
685 for (i = 0; i < step; i++)
687 tmp[2 * i + 0] = _2hex[*ptr >> 4];
688 tmp[2 * i + 1] = _2hex[*ptr++ & 0xF];
690 if (!HLPFILE_RtfAddRawString(rd, tmp, 2 * step)) return FALSE;
692 return TRUE;
695 /******************************************************************
696 * HLPFILE_RtfAddTransparentBitmap
698 * We'll transform a transparent bitmap into an metafile that
699 * we then transform into RTF
701 static BOOL HLPFILE_RtfAddTransparentBitmap(struct RtfData* rd, const BITMAPINFO* bi,
702 const void* pict, unsigned nc)
704 HDC hdc, hdcMask, hdcMem, hdcEMF;
705 HBITMAP hbm, hbmMask, hbmOldMask, hbmOldMem;
706 HENHMETAFILE hEMF;
707 BOOL ret = FALSE;
708 void* data;
709 UINT sz;
711 hbm = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
712 CBM_INIT, pict, bi, DIB_RGB_COLORS);
714 hdcMem = CreateCompatibleDC(hdc);
715 hbmOldMem = SelectObject(hdcMem, hbm);
717 /* create the mask bitmap from the main bitmap */
718 hdcMask = CreateCompatibleDC(hdc);
719 hbmMask = CreateBitmap(bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, 1, 1, NULL);
720 hbmOldMask = SelectObject(hdcMask, hbmMask);
721 SetBkColor(hdcMem,
722 RGB(bi->bmiColors[nc - 1].rgbRed,
723 bi->bmiColors[nc - 1].rgbGreen,
724 bi->bmiColors[nc - 1].rgbBlue));
725 BitBlt(hdcMask, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCCOPY);
727 /* sets to RGB(0,0,0) the transparent bits in main bitmap */
728 SetBkColor(hdcMem, RGB(0,0,0));
729 SetTextColor(hdcMem, RGB(255,255,255));
730 BitBlt(hdcMem, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMask, 0, 0, SRCAND);
732 SelectObject(hdcMask, hbmOldMask);
733 DeleteDC(hdcMask);
735 SelectObject(hdcMem, hbmOldMem);
736 DeleteDC(hdcMem);
738 /* we create the bitmap on the fly */
739 hdcEMF = CreateEnhMetaFile(NULL, NULL, NULL, NULL);
740 hdcMem = CreateCompatibleDC(hdcEMF);
742 /* sets to RGB(0,0,0) the transparent bits in final bitmap */
743 hbmOldMem = SelectObject(hdcMem, hbmMask);
744 SetBkColor(hdcEMF, RGB(255, 255, 255));
745 SetTextColor(hdcEMF, RGB(0, 0, 0));
746 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCAND);
748 /* and copy the remaining bits of main bitmap */
749 SelectObject(hdcMem, hbm);
750 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCPAINT);
751 SelectObject(hdcMem, hbmOldMem);
752 DeleteDC(hdcMem);
754 /* do the cleanup */
755 ReleaseDC(0, hdc);
756 DeleteObject(hbmMask);
757 DeleteObject(hbm);
759 hEMF = CloseEnhMetaFile(hdcEMF);
761 /* generate rtf stream */
762 sz = GetEnhMetaFileBits(hEMF, 0, NULL);
763 if (sz && (data = HeapAlloc(GetProcessHeap(), 0, sz)))
765 if (sz == GetEnhMetaFileBits(hEMF, sz, data))
767 ret = HLPFILE_RtfAddControl(rd, "{\\pict\\emfblip") &&
768 HLPFILE_RtfAddHexBytes(rd, data, sz) &&
769 HLPFILE_RtfAddControl(rd, "}");
771 HeapFree(GetProcessHeap(), 0, data);
773 DeleteEnhMetaFile(hEMF);
775 return ret;
778 /******************************************************************
779 * HLPFILE_RtfAddBitmap
782 static BOOL HLPFILE_RtfAddBitmap(struct RtfData* rd, BYTE* beg, BYTE type, BYTE pack)
784 BYTE* ptr;
785 BYTE* pict_beg;
786 BITMAPINFO* bi;
787 unsigned long off, csz;
788 unsigned nc = 0;
789 BOOL clrImportant = FALSE;
790 BOOL ret = FALSE;
791 char tmp[256];
793 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
794 if (!bi) return FALSE;
796 ptr = beg + 2; /* for type and pack */
798 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
799 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
800 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
801 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
802 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
803 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
804 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
805 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
806 clrImportant = fetch_ulong(&ptr);
807 bi->bmiHeader.biClrImportant = (clrImportant > 1) ? clrImportant : 0;
808 bi->bmiHeader.biCompression = BI_RGB;
809 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
810 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
811 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
812 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
813 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
814 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
816 csz = fetch_ulong(&ptr);
817 fetch_ulong(&ptr); /* hotspot size */
819 off = GET_UINT(ptr, 0); ptr += 4;
820 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
822 /* now read palette info */
823 if (type == 0x06)
825 unsigned i;
827 nc = bi->bmiHeader.biClrUsed;
828 /* not quite right, especially for bitfields type of compression */
829 if (!nc && bi->bmiHeader.biBitCount <= 8)
830 nc = 1 << bi->bmiHeader.biBitCount;
832 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
833 if (!bi) return FALSE;
834 for (i = 0; i < nc; i++)
836 bi->bmiColors[i].rgbBlue = ptr[0];
837 bi->bmiColors[i].rgbGreen = ptr[1];
838 bi->bmiColors[i].rgbRed = ptr[2];
839 bi->bmiColors[i].rgbReserved = 0;
840 ptr += 4;
843 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
845 if (clrImportant == 1 && nc > 0)
847 ret = HLPFILE_RtfAddTransparentBitmap(rd, bi, pict_beg, nc);
848 goto done;
850 if (!HLPFILE_RtfAddControl(rd, "{\\pict")) goto done;
851 if (type == 0x06)
853 sprintf(tmp, "\\dibitmap0\\picw%d\\pich%d",
854 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
855 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
856 if (!HLPFILE_RtfAddHexBytes(rd, bi, sizeof(*bi) + nc * sizeof(RGBQUAD))) goto done;
858 else
860 sprintf(tmp, "\\wbitmap0\\wbmbitspixel%d\\wbmplanes%d\\picw%d\\pich%d",
861 bi->bmiHeader.biBitCount, bi->bmiHeader.biPlanes,
862 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
863 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
865 if (!HLPFILE_RtfAddHexBytes(rd, pict_beg, bi->bmiHeader.biSizeImage)) goto done;
866 if (!HLPFILE_RtfAddControl(rd, "}")) goto done;
868 ret = TRUE;
869 done:
870 HeapFree(GetProcessHeap(), 0, bi);
871 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
873 return ret;
876 /******************************************************************
877 * HLPFILE_RtfAddMetaFile
880 static BOOL HLPFILE_RtfAddMetaFile(struct RtfData* rd, BYTE* beg, BYTE pack)
882 BYTE* ptr;
883 unsigned long size, csize;
884 unsigned long off, hsoff;
885 BYTE* bits;
886 char tmp[256];
887 unsigned mm;
888 BOOL ret;
890 WINE_TRACE("Loading metafile\n");
892 ptr = beg + 2; /* for type and pack */
894 mm = fetch_ushort(&ptr); /* mapping mode */
895 sprintf(tmp, "{\\pict\\wmetafile%d\\picw%d\\pich%d",
896 mm, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2));
897 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
898 ptr += 4;
900 size = fetch_ulong(&ptr); /* decompressed size */
901 csize = fetch_ulong(&ptr); /* compressed size */
902 fetch_ulong(&ptr); /* hotspot size */
903 off = GET_UINT(ptr, 0);
904 hsoff = GET_UINT(ptr, 4);
905 ptr += 8;
907 WINE_TRACE("sz=%lu csz=%lu offs=%lu/%u,%lu\n",
908 size, csize, off, ptr - beg, hsoff);
910 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
911 if (!bits) return FALSE;
913 ret = HLPFILE_RtfAddHexBytes(rd, bits, size) &&
914 HLPFILE_RtfAddControl(rd, "}");
916 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
918 return ret;
921 /******************************************************************
922 * HLPFILE_RtfAddGfxByAddr
925 static BOOL HLPFILE_RtfAddGfxByAddr(struct RtfData* rd, HLPFILE *hlpfile,
926 BYTE* ref, unsigned long size)
928 unsigned i, numpict;
930 numpict = GET_USHORT(ref, 2);
931 WINE_TRACE("Got picture magic=%04x #=%d\n", GET_USHORT(ref, 0), numpict);
933 for (i = 0; i < numpict; i++)
935 BYTE* beg;
936 BYTE* ptr;
937 BYTE type, pack;
939 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
940 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
942 type = *ptr++;
943 pack = *ptr++;
945 switch (type)
947 case 5: /* device dependent bmp */
948 case 6: /* device independent bmp */
949 HLPFILE_RtfAddBitmap(rd, beg, type, pack);
950 break;
951 case 8:
952 HLPFILE_RtfAddMetaFile(rd, beg, pack);
953 break;
954 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
957 /* FIXME: hotspots */
959 /* FIXME: implement support for multiple picture format */
960 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
961 break;
963 return TRUE;
966 /******************************************************************
967 * HLPFILE_RtfAddGfxByIndex
971 static BOOL HLPFILE_RtfAddGfxByIndex(struct RtfData* rd, HLPFILE *hlpfile,
972 unsigned index)
974 char tmp[16];
975 BYTE *ref, *end;
977 WINE_TRACE("Loading picture #%d\n", index);
979 sprintf(tmp, "|bm%u", index);
981 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
983 ref += 9;
984 return HLPFILE_RtfAddGfxByAddr(rd, hlpfile, ref, end - ref);
987 /******************************************************************
988 * HLPFILE_AllocLink
992 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
993 const char* str, unsigned len, LONG hash,
994 unsigned clrChange, unsigned wnd)
996 HLPFILE_LINK* link;
997 char* link_str;
999 /* FIXME: should build a string table for the attributes.link.lpszPath
1000 * they are reallocated for each link
1002 if (len == -1) len = strlen(str);
1003 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + len + 1);
1004 if (!link) return NULL;
1006 link->cookie = cookie;
1007 link->string = link_str = (char*)(link + 1);
1008 memcpy(link_str, str, len);
1009 link_str[len] = '\0';
1010 link->hash = hash;
1011 link->bClrChange = clrChange ? 1 : 0;
1012 link->window = wnd;
1013 link->next = rd->first_link;
1014 rd->first_link = link;
1015 link->cpMin = rd->char_pos;
1016 link->cpMax = 0;
1017 rd->force_color = clrChange;
1018 if (rd->current_link) WINE_FIXME("Pending link\n");
1019 rd->current_link = link;
1021 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
1022 link->cookie, link->string, link->hash, link->window);
1023 return link;
1026 unsigned HLPFILE_HalfPointsToTwips(unsigned pts)
1028 static unsigned logPxY;
1029 if (!logPxY)
1031 HDC hdc = GetDC(NULL);
1032 logPxY = GetDeviceCaps(hdc, LOGPIXELSY);
1033 ReleaseDC(NULL, hdc);
1035 return MulDiv(pts, 72 * 10, logPxY);
1038 /***********************************************************************
1040 * HLPFILE_BrowseParagraph
1042 static BOOL HLPFILE_BrowseParagraph(HLPFILE_PAGE* page, struct RtfData* rd,
1043 BYTE *buf, BYTE* end, unsigned* parlen)
1045 UINT textsize;
1046 BYTE *format, *format_end;
1047 char *text, *text_base, *text_end;
1048 long size, blocksize, datalen;
1049 unsigned short bits;
1050 unsigned nc, ncol = 1;
1051 short table_width;
1052 BOOL in_table = FALSE;
1053 char tmp[256];
1054 BOOL ret = FALSE;
1056 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
1058 *parlen = 0;
1059 blocksize = GET_UINT(buf, 0);
1060 size = GET_UINT(buf, 0x4);
1061 datalen = GET_UINT(buf, 0x10);
1062 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
1063 if (!text) return FALSE;
1064 if (size > blocksize - datalen)
1066 /* need to decompress */
1067 if (page->file->hasPhrases)
1068 HLPFILE_Uncompress2(page->file, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
1069 else if (page->file->hasPhrases40)
1070 HLPFILE_Uncompress3(page->file, text, text + size, buf + datalen, end);
1071 else
1073 WINE_FIXME("Text size is too long, splitting\n");
1074 size = blocksize - datalen;
1075 memcpy(text, buf + datalen, size);
1078 else
1079 memcpy(text, buf + datalen, size);
1081 text_end = text + size;
1083 format = buf + 0x15;
1084 format_end = buf + GET_UINT(buf, 0x10);
1086 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
1088 fetch_long(&format);
1089 *parlen = fetch_ushort(&format);
1092 if (buf[0x14] == 0x23)
1094 char type;
1096 in_table = TRUE;
1097 ncol = *format++;
1099 if (!HLPFILE_RtfAddControl(rd, "\\trowd")) goto done;
1100 type = *format++;
1101 if (type == 0 || type == 2)
1103 table_width = GET_SHORT(format, 0);
1104 format += 2;
1106 else
1107 table_width = 32767;
1108 WINE_TRACE("New table: cols=%d type=%x width=%d\n",
1109 ncol, type, table_width);
1110 if (ncol > 1)
1112 int pos;
1113 sprintf(tmp, "\\trgaph%d\\trleft%d",
1114 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6), table_width, 32767)),
1115 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)));
1116 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1117 pos = HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6) / 2, table_width, 32767));
1118 for (nc = 0; nc < ncol; nc++)
1120 WINE_TRACE("column(%d/%d) gap=%d width=%d\n",
1121 nc, ncol, GET_SHORT(format, nc*4),
1122 GET_SHORT(format, nc*4+2));
1123 pos += GET_SHORT(format, nc * 4) + GET_SHORT(format, nc * 4 + 2);
1124 sprintf(tmp, "\\cellx%d",
1125 HLPFILE_HalfPointsToTwips(MulDiv(pos, table_width, 32767)));
1126 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1129 else
1131 WINE_TRACE("column(0/%d) gap=%d width=%d\n",
1132 ncol, GET_SHORT(format, 0), GET_SHORT(format, 2));
1133 sprintf(tmp, "\\trleft%d\\cellx%d ",
1134 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)),
1135 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0) + GET_SHORT(format, 2),
1136 table_width, 32767)));
1137 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1139 format += ncol * 4;
1142 for (nc = 0; nc < ncol; /**/)
1144 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
1145 if (!HLPFILE_RtfAddControl(rd, "\\pard")) goto done;
1146 if (in_table)
1148 nc = GET_SHORT(format, 0);
1149 if (nc == -1) break;
1150 format += 5;
1151 if (!HLPFILE_RtfAddControl(rd, "\\intbl")) goto done;
1153 else nc++;
1154 if (buf[0x14] == 0x01)
1155 format += 6;
1156 else
1157 format += 4;
1158 bits = GET_USHORT(format, 0); format += 2;
1159 if (bits & 0x0001) fetch_long(&format);
1160 if (bits & 0x0002)
1162 sprintf(tmp, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1163 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1165 if (bits & 0x0004)
1167 sprintf(tmp, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1168 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1170 if (bits & 0x0008)
1172 sprintf(tmp, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1173 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1175 if (bits & 0x0010)
1177 sprintf(tmp, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1178 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1180 if (bits & 0x0020)
1182 sprintf(tmp, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1183 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1185 if (bits & 0x0040)
1187 sprintf(tmp, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1188 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1190 if (bits & 0x0100)
1192 BYTE brdr = *format++;
1193 short w;
1195 if (brdr & 0x01 && !HLPFILE_RtfAddControl(rd, "\\box")) goto done;
1196 if (brdr & 0x02 && !HLPFILE_RtfAddControl(rd, "\\brdrt")) goto done;
1197 if (brdr & 0x04 && !HLPFILE_RtfAddControl(rd, "\\brdrl")) goto done;
1198 if (brdr & 0x08 && !HLPFILE_RtfAddControl(rd, "\\brdrb")) goto done;
1199 if (brdr & 0x10 && !HLPFILE_RtfAddControl(rd, "\\brdrr")) goto done;
1200 if (brdr & 0x20 && !HLPFILE_RtfAddControl(rd, "\\brdrth")) goto done;
1201 if (!(brdr & 0x20) && !HLPFILE_RtfAddControl(rd, "\\brdrs")) goto done;
1202 if (brdr & 0x40 && !HLPFILE_RtfAddControl(rd, "\\brdrdb")) goto done;
1203 /* 0x80: unknown */
1205 w = GET_SHORT(format, 0); format += 2;
1206 if (w)
1208 sprintf(tmp, "\\brdrw%d", HLPFILE_HalfPointsToTwips(w));
1209 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1212 if (bits & 0x0200)
1214 int i, ntab = fetch_short(&format);
1215 unsigned tab, ts;
1216 const char* kind;
1218 for (i = 0; i < ntab; i++)
1220 tab = fetch_ushort(&format);
1221 ts = (tab & 0x4000) ? fetch_ushort(&format) : 0 /* left */;
1222 switch (ts)
1224 default: WINE_FIXME("Unknown tab style %x\n", ts);
1225 /* fall through */
1226 case 0: kind = ""; break;
1227 case 1: kind = "\\tqr"; break;
1228 case 2: kind = "\\tqc"; break;
1230 /* FIXME: do kind */
1231 sprintf(tmp, "%s\\tx%d",
1232 kind, HLPFILE_HalfPointsToTwips(tab & 0x3FFF));
1233 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1236 switch (bits & 0xc00)
1238 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1239 case 0: if (!HLPFILE_RtfAddControl(rd, "\\ql")) goto done; break;
1240 case 0x400: if (!HLPFILE_RtfAddControl(rd, "\\qr")) goto done; break;
1241 case 0x800: if (!HLPFILE_RtfAddControl(rd, "\\qc")) goto done; break;
1244 /* 0x1000 doesn't need space */
1245 if ((bits & 0x1000) && !HLPFILE_RtfAddControl(rd, "\\keep")) goto done;
1246 if ((bits & 0xE080) != 0)
1247 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
1249 while (text < text_end && format < format_end)
1251 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
1252 textsize = strlen(text);
1253 if (textsize)
1255 if (rd->force_color)
1257 if ((rd->current_link->cookie == hlp_link_popup) ?
1258 !HLPFILE_RtfAddControl(rd, "{\\uld\\cf1") :
1259 !HLPFILE_RtfAddControl(rd, "{\\ul\\cf1")) goto done;
1261 if (!HLPFILE_RtfAddText(rd, text)) goto done;
1262 if (rd->force_color && !HLPFILE_RtfAddControl(rd, "}")) goto done;
1263 rd->char_pos += textsize;
1265 /* else: null text, keep on storing attributes */
1266 text += textsize + 1;
1268 if (*format == 0xff)
1270 format++;
1271 break;
1274 WINE_TRACE("format=%02x\n", *format);
1275 switch (*format)
1277 case 0x20:
1278 WINE_FIXME("NIY20\n");
1279 format += 5;
1280 break;
1282 case 0x21:
1283 WINE_FIXME("NIY21\n");
1284 format += 3;
1285 break;
1287 case 0x80:
1289 unsigned font = GET_USHORT(format, 1);
1290 unsigned fs;
1292 WINE_TRACE("Changing font to %d\n", font);
1293 format += 3;
1294 switch (rd->font_scale)
1296 case 0: fs = (4 * page->file->fonts[font].LogFont.lfHeight - 13) / 5; break;
1297 default:
1298 case 1: fs = (4 * page->file->fonts[font].LogFont.lfHeight - 3) / 5; break;
1299 case 2: fs = (4 * page->file->fonts[font].LogFont.lfHeight + 17) / 5; break;
1301 /* FIXME: missing at least colors, also bold attribute looses information */
1303 sprintf(tmp, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1304 font, font + 2, fs,
1305 page->file->fonts[font].LogFont.lfWeight > 400 ? "\\b" : "\\b0",
1306 page->file->fonts[font].LogFont.lfItalic ? "\\i" : "\\i0",
1307 page->file->fonts[font].LogFont.lfUnderline ? "\\ul" : "\\ul0",
1308 page->file->fonts[font].LogFont.lfStrikeOut ? "\\strike" : "\\strike0");
1309 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1311 break;
1313 case 0x81:
1314 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1315 format += 1;
1316 rd->char_pos++;
1317 break;
1319 case 0x82:
1320 if (in_table)
1322 if (format[1] != 0xFF)
1324 if (!HLPFILE_RtfAddControl(rd, "\\par\\intbl")) goto done;
1326 else
1328 if (!HLPFILE_RtfAddControl(rd, "\\cell\\pard\\intbl")) goto done;
1331 else if (!HLPFILE_RtfAddControl(rd, "\\par")) goto done;
1332 format += 1;
1333 rd->char_pos++;
1334 break;
1336 case 0x83:
1337 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1338 format += 1;
1339 rd->char_pos++;
1340 break;
1342 #if 0
1343 case 0x84:
1344 format += 3;
1345 break;
1346 #endif
1348 case 0x86:
1349 case 0x87:
1350 case 0x88:
1352 BYTE type = format[1];
1353 long size;
1355 /* FIXME: we don't use 'BYTE pos = (*format - 0x86);' for the image position */
1356 format += 2;
1357 size = fetch_long(&format);
1359 switch (type)
1361 case 0x22:
1362 fetch_ushort(&format); /* hot spot */
1363 /* fall thru */
1364 case 0x03:
1365 switch (GET_SHORT(format, 0))
1367 case 0:
1368 HLPFILE_RtfAddGfxByIndex(rd, page->file, GET_SHORT(format, 2));
1369 rd->char_pos++;
1370 break;
1371 case 1:
1372 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1373 GET_SHORT(format, 0),
1374 size, GET_SHORT(format, 2));
1375 HLPFILE_RtfAddGfxByAddr(rd, page->file, format + 2, size - 4);
1376 rd->char_pos++;
1377 break;
1378 default:
1379 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1380 break;
1382 break;
1383 case 0x05:
1384 WINE_FIXME("Got an embedded element %s\n", format + 6);
1385 break;
1386 default:
1387 WINE_FIXME("Got a type %d picture\n", type);
1388 break;
1390 format += size;
1392 break;
1394 case 0x89:
1395 format += 1;
1396 if (!rd->current_link)
1397 WINE_FIXME("No existing link\n");
1398 rd->current_link->cpMax = rd->char_pos;
1399 rd->current_link = NULL;
1400 rd->force_color = FALSE;
1401 break;
1403 case 0x8B:
1404 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1405 format += 1;
1406 rd->char_pos++;
1407 break;
1409 case 0x8C:
1410 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1411 /* FIXME: it could be that hypen is also in input stream !! */
1412 format += 1;
1413 rd->char_pos++;
1414 break;
1416 #if 0
1417 case 0xA9:
1418 format += 2;
1419 break;
1420 #endif
1422 case 0xC8:
1423 case 0xCC:
1424 WINE_TRACE("macro => %s\n", format + 3);
1425 HLPFILE_AllocLink(rd, hlp_link_macro, (const char*)format + 3,
1426 GET_USHORT(format, 1), 0, !(*format & 4), -1);
1427 format += 3 + GET_USHORT(format, 1);
1428 break;
1430 case 0xE0:
1431 case 0xE1:
1432 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1433 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1434 page->file->lpszPath, -1, GET_UINT(format, 1)-16, 1, -1);
1437 format += 5;
1438 break;
1440 case 0xE2:
1441 case 0xE3:
1442 case 0xE6:
1443 case 0xE7:
1444 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1445 page->file->lpszPath, -1, GET_UINT(format, 1),
1446 !(*format & 4), -1);
1447 format += 5;
1448 break;
1450 case 0xEA:
1451 case 0xEB:
1452 case 0xEE:
1453 case 0xEF:
1455 char* ptr = (char*) format + 8;
1456 BYTE type = format[3];
1457 int wnd = -1;
1459 switch (type)
1461 case 1:
1462 wnd = *ptr;
1463 /* fall through */
1464 case 0:
1465 ptr = page->file->lpszPath;
1466 break;
1467 case 6:
1468 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1470 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1472 if (wnd == -1)
1473 WINE_WARN("Couldn't find window info for %s\n", ptr);
1474 ptr += strlen(ptr) + 1;
1475 /* fall through */
1476 case 4:
1477 break;
1478 default:
1479 WINE_WARN("Unknown link type %d\n", type);
1480 break;
1482 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1483 ptr, -1, GET_UINT(format, 4), !(*format & 4), wnd);
1485 format += 3 + GET_USHORT(format, 1);
1486 break;
1488 default:
1489 WINE_WARN("format %02x\n", *format);
1490 format++;
1494 if (in_table)
1496 if (!HLPFILE_RtfAddControl(rd, "\\row\\par\\pard\\plain")) goto done;
1497 rd->char_pos += 2;
1499 ret = TRUE;
1500 done:
1502 HeapFree(GetProcessHeap(), 0, text_base);
1503 return ret;
1506 /******************************************************************
1507 * HLPFILE_BrowsePage
1510 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page, struct RtfData* rd,
1511 unsigned font_scale, unsigned relative)
1513 HLPFILE *hlpfile = page->file;
1514 BYTE *buf, *end;
1515 DWORD ref = page->reference;
1516 unsigned index, old_index = -1, offset, count = 0, offs = 0;
1517 unsigned cpg, parlen;
1518 char tmp[1024];
1519 const char* ck = NULL;
1521 rd->in_text = TRUE;
1522 rd->data = rd->ptr = HeapAlloc(GetProcessHeap(), 0, rd->allocated = 32768);
1523 rd->char_pos = 0;
1524 rd->first_link = rd->current_link = NULL;
1525 rd->force_color = FALSE;
1526 rd->font_scale = font_scale;
1527 rd->relative = relative;
1528 rd->char_pos_rel = 0;
1530 switch (hlpfile->charset)
1532 case DEFAULT_CHARSET:
1533 case ANSI_CHARSET: cpg = 1252; break;
1534 case SHIFTJIS_CHARSET: cpg = 932; break;
1535 case HANGEUL_CHARSET: cpg = 949; break;
1536 case GB2312_CHARSET: cpg = 936; break;
1537 case CHINESEBIG5_CHARSET: cpg = 950; break;
1538 case GREEK_CHARSET: cpg = 1253; break;
1539 case TURKISH_CHARSET: cpg = 1254; break;
1540 case HEBREW_CHARSET: cpg = 1255; break;
1541 case ARABIC_CHARSET: cpg = 1256; break;
1542 case BALTIC_CHARSET: cpg = 1257; break;
1543 case VIETNAMESE_CHARSET: cpg = 1258; break;
1544 case RUSSIAN_CHARSET: cpg = 1251; break;
1545 case EE_CHARSET: cpg = 1250; break;
1546 case THAI_CHARSET: cpg = 874; break;
1547 case JOHAB_CHARSET: cpg = 1361; break;
1548 case MAC_CHARSET: ck = "mac"; break;
1549 default:
1550 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1551 cpg = 1252;
1553 if (ck)
1555 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1556 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1558 else
1560 sprintf(tmp, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg);
1561 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1564 /* generate font table */
1565 if (!HLPFILE_RtfAddControl(rd, "{\\fonttbl")) return FALSE;
1566 for (index = 0; index < hlpfile->numFonts; index++)
1568 const char* family;
1569 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1571 case FF_MODERN: family = "modern"; break;
1572 case FF_ROMAN: family = "roman"; break;
1573 case FF_SWISS: family = "swiss"; break;
1574 case FF_SCRIPT: family = "script"; break;
1575 case FF_DECORATIVE: family = "decor"; break;
1576 default: family = "nil"; break;
1578 sprintf(tmp, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1579 index, family,
1580 hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0x0F,
1581 hlpfile->fonts[index].LogFont.lfCharSet,
1582 hlpfile->fonts[index].LogFont.lfFaceName);
1583 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1585 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1586 /* generate color table */
1587 if (!HLPFILE_RtfAddControl(rd, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE;
1588 for (index = 0; index < hlpfile->numFonts; index++)
1590 const char* family;
1591 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1593 case FF_MODERN: family = "modern"; break;
1594 case FF_ROMAN: family = "roman"; break;
1595 case FF_SWISS: family = "swiss"; break;
1596 case FF_SCRIPT: family = "script"; break;
1597 case FF_DECORATIVE: family = "decor"; break;
1598 default: family = "nil"; break;
1600 sprintf(tmp, "\\red%d\\green%d\\blue%d;",
1601 GetRValue(hlpfile->fonts[index].color),
1602 GetGValue(hlpfile->fonts[index].color),
1603 GetBValue(hlpfile->fonts[index].color));
1604 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1606 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1610 if (hlpfile->version <= 16)
1612 index = (ref - 0x0C) / hlpfile->dsize;
1613 offset = (ref - 0x0C) % hlpfile->dsize;
1615 else
1617 index = (ref - 0x0C) >> 14;
1618 offset = (ref - 0x0C) & 0x3FFF;
1621 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
1623 /* we jumped to the next block, adjust pointers */
1624 ref -= 12;
1625 offset -= 12;
1628 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1629 buf = hlpfile->topic_map[index] + offset;
1630 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1631 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1632 if (index != old_index) {offs = 0; old_index = index;}
1634 switch (buf[0x14])
1636 case 0x02:
1637 if (count++) goto done;
1638 break;
1639 case 0x01:
1640 case 0x20:
1641 case 0x23:
1642 if (!HLPFILE_BrowseParagraph(page, rd, buf, end, &parlen)) return FALSE;
1643 if (relative >= index * 0x8000 + offs)
1644 rd->char_pos_rel = rd->char_pos;
1645 offs += parlen;
1646 break;
1647 default:
1648 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1650 if (hlpfile->version <= 16)
1652 ref += GET_UINT(buf, 0xc);
1653 if (GET_UINT(buf, 0xc) == 0)
1654 break;
1656 else
1657 ref = GET_UINT(buf, 0xc);
1658 } while (ref != 0xffffffff);
1659 done:
1660 page->first_link = rd->first_link;
1661 return HLPFILE_RtfAddControl(rd, "}");
1664 /******************************************************************
1665 * HLPFILE_ReadFont
1669 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1671 BYTE *ref, *end;
1672 unsigned i, len, idx;
1673 unsigned face_num, dscr_num, face_offset, dscr_offset;
1674 BYTE flag, family;
1676 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1678 WINE_WARN("no subfile FONT\n");
1679 hlpfile->numFonts = 0;
1680 hlpfile->fonts = NULL;
1681 return FALSE;
1684 ref += 9;
1686 face_num = GET_USHORT(ref, 0);
1687 dscr_num = GET_USHORT(ref, 2);
1688 face_offset = GET_USHORT(ref, 4);
1689 dscr_offset = GET_USHORT(ref, 6);
1691 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1692 face_num, face_offset, dscr_num, dscr_offset);
1694 hlpfile->numFonts = dscr_num;
1695 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1697 len = (dscr_offset - face_offset) / face_num;
1698 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1699 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1700 for (i = 0; i < dscr_num; i++)
1702 flag = ref[dscr_offset + i * 11 + 0];
1703 family = ref[dscr_offset + i * 11 + 2];
1705 hlpfile->fonts[i].LogFont.lfHeight = ref[dscr_offset + i * 11 + 1];
1706 hlpfile->fonts[i].LogFont.lfWidth = 0;
1707 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1708 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1709 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1710 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1711 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1712 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1713 hlpfile->fonts[i].LogFont.lfCharSet = hlpfile->charset;
1714 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1715 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1716 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1717 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1719 switch (family)
1721 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1722 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1723 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1724 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1725 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1726 default: WINE_FIXME("Unknown family %u\n", family);
1728 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1730 if (idx < face_num)
1732 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1733 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1735 else
1737 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1738 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1740 hlpfile->fonts[i].hFont = 0;
1741 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1742 ref[dscr_offset + i * 11 + 6],
1743 ref[dscr_offset + i * 11 + 7]);
1744 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1745 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1746 i, flag,
1747 X(0, "bold"),
1748 X(1, "italic"),
1749 X(2, "underline"),
1750 X(3, "strikeOut"),
1751 X(4, "dblUnderline"),
1752 X(5, "smallCaps"),
1753 ref[dscr_offset + i * 11 + 1],
1754 family,
1755 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1756 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1758 return TRUE;
1761 /***********************************************************************
1763 * HLPFILE_ReadFileToBuffer
1765 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1767 BYTE header[16], dummy[1];
1769 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1771 /* sanity checks */
1772 if (GET_UINT(header, 0) != 0x00035F3F)
1773 {WINE_WARN("wrong header\n"); return FALSE;};
1775 hlpfile->file_buffer_size = GET_UINT(header, 12);
1776 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1777 if (!hlpfile->file_buffer) return FALSE;
1779 memcpy(hlpfile->file_buffer, header, 16);
1780 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1781 {WINE_WARN("filesize1\n"); return FALSE;};
1783 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1785 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1787 return TRUE;
1790 /**************************************************************************
1791 * comp_FindSubFile
1793 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1796 static int comp_FindSubFile(void *p, const void *key,
1797 int leaf, void** next)
1799 *next = (char *)p+strlen(p)+(leaf?5:3);
1800 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1801 return strcmp(p, key);
1804 /***********************************************************************
1806 * HLPFILE_FindSubFile
1808 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
1810 BYTE *ptr;
1812 WINE_TRACE("looking for file '%s'\n", name);
1813 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
1814 name, comp_FindSubFile);
1815 if (!ptr) return FALSE;
1816 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
1817 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
1819 WINE_ERR("internal file %s does not fit\n", name);
1820 return FALSE;
1822 *subend = *subbuf + GET_UINT(*subbuf, 0);
1823 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
1825 WINE_ERR("internal file %s does not fit\n", name);
1826 return FALSE;
1828 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1830 WINE_ERR("invalid size provided for internal file %s\n", name);
1831 return FALSE;
1833 return TRUE;
1836 /***********************************************************************
1838 * HLPFILE_SystemCommands
1840 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1842 BYTE *buf, *ptr, *end;
1843 HLPFILE_MACRO *macro, **m;
1844 LPSTR p;
1845 unsigned short magic, minor, major, flags;
1847 hlpfile->lpszTitle = NULL;
1849 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1851 magic = GET_USHORT(buf + 9, 0);
1852 minor = GET_USHORT(buf + 9, 2);
1853 major = GET_USHORT(buf + 9, 4);
1854 /* gen date on 4 bytes */
1855 flags = GET_USHORT(buf + 9, 10);
1856 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1857 magic, major, minor, flags);
1858 if (magic != 0x036C || major != 1)
1859 {WINE_WARN("Wrong system header\n"); return FALSE;}
1860 if (minor <= 16)
1862 hlpfile->tbsize = 0x800;
1863 hlpfile->compressed = 0;
1865 else if (flags == 0)
1867 hlpfile->tbsize = 0x1000;
1868 hlpfile->compressed = 0;
1870 else if (flags == 4)
1872 hlpfile->tbsize = 0x1000;
1873 hlpfile->compressed = 1;
1875 else
1877 hlpfile->tbsize = 0x800;
1878 hlpfile->compressed = 1;
1881 if (hlpfile->compressed)
1882 hlpfile->dsize = 0x4000;
1883 else
1884 hlpfile->dsize = hlpfile->tbsize - 0x0C;
1886 hlpfile->version = minor;
1887 hlpfile->flags = flags;
1888 hlpfile->charset = DEFAULT_CHARSET;
1890 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1892 char *str = (char*) ptr + 4;
1893 switch (GET_USHORT(ptr, 0))
1895 case 1:
1896 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1897 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1898 if (!hlpfile->lpszTitle) return FALSE;
1899 lstrcpy(hlpfile->lpszTitle, str);
1900 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1901 break;
1903 case 2:
1904 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1905 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1906 if (!hlpfile->lpszCopyright) return FALSE;
1907 lstrcpy(hlpfile->lpszCopyright, str);
1908 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1909 break;
1911 case 3:
1912 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1913 hlpfile->contents_start = GET_UINT(ptr, 4);
1914 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1915 break;
1917 case 4:
1918 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1919 if (!macro) break;
1920 p = (char*)macro + sizeof(HLPFILE_MACRO);
1921 lstrcpy(p, str);
1922 macro->lpszMacro = p;
1923 macro->next = 0;
1924 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1925 *m = macro;
1926 break;
1928 case 5:
1929 if (GET_USHORT(ptr, 4 + 4) != 1)
1930 WINE_FIXME("More than one icon, picking up first\n");
1931 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
1932 hlpfile->hIcon = CreateIconFromResourceEx(ptr + 4 + 0x16,
1933 GET_USHORT(ptr, 2) - 0x16, TRUE,
1934 0x30000, 0, 0, 0);
1935 break;
1937 case 6:
1938 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1940 if (hlpfile->windows)
1941 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1942 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1943 else
1944 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1945 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1947 if (hlpfile->windows)
1949 unsigned flags = GET_USHORT(ptr, 4);
1950 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1952 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1953 else wi->type[0] = '\0';
1954 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1955 else wi->name[0] = '\0';
1956 if (flags & 0x0004) strcpy(wi->caption, &str[21]);
1957 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1958 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1959 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1960 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1961 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1962 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1963 wi->win_style = WS_OVERLAPPEDWINDOW;
1964 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1965 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1966 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",
1967 flags & 0x0001 ? 'T' : 't',
1968 flags & 0x0002 ? 'N' : 'n',
1969 flags & 0x0004 ? 'C' : 'c',
1970 flags & 0x0008 ? 'X' : 'x',
1971 flags & 0x0010 ? 'Y' : 'y',
1972 flags & 0x0020 ? 'W' : 'w',
1973 flags & 0x0040 ? 'H' : 'h',
1974 flags & 0x0080 ? 'S' : 's',
1975 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1976 wi->size.cx, wi->size.cy);
1978 break;
1979 case 8:
1980 WINE_WARN("Citation: '%s'\n", ptr + 4);
1981 break;
1982 case 11:
1983 hlpfile->charset = ptr[4];
1984 WINE_TRACE("Charset: %d\n", hlpfile->charset);
1985 break;
1986 default:
1987 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1990 if (!hlpfile->lpszTitle)
1991 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
1992 return TRUE;
1995 /***********************************************************************
1997 * HLPFILE_UncompressedLZ77_Size
1999 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
2001 int i, newsize = 0;
2003 while (ptr < end)
2005 int mask = *ptr++;
2006 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2008 if (mask & 1)
2010 int code = GET_USHORT(ptr, 0);
2011 int len = 3 + (code >> 12);
2012 newsize += len;
2013 ptr += 2;
2015 else newsize++, ptr++;
2019 return newsize;
2022 /***********************************************************************
2024 * HLPFILE_UncompressLZ77
2026 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
2028 int i;
2030 while (ptr < end)
2032 int mask = *ptr++;
2033 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2035 if (mask & 1)
2037 int code = GET_USHORT(ptr, 0);
2038 int len = 3 + (code >> 12);
2039 int offset = code & 0xfff;
2041 * We must copy byte-by-byte here. We cannot use memcpy nor
2042 * memmove here. Just example:
2043 * a[]={1,2,3,4,5,6,7,8,9,10}
2044 * newptr=a+2;
2045 * offset=1;
2046 * We expect:
2047 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
2049 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
2050 ptr += 2;
2052 else *newptr++ = *ptr++;
2056 return newptr;
2059 /***********************************************************************
2061 * HLPFILE_UncompressLZ77_Phrases
2063 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
2065 UINT i, num, dec_size, head_size;
2066 BYTE *buf, *end;
2068 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
2070 if (hlpfile->version <= 16)
2071 head_size = 13;
2072 else
2073 head_size = 17;
2075 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
2076 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
2078 if (hlpfile->version <= 16)
2079 dec_size = end - buf - 15 - 2 * num;
2080 else
2081 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
2083 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2084 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2085 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2087 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2088 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2089 return FALSE;
2092 for (i = 0; i <= num; i++)
2093 hlpfile->phrases_offsets[i] = GET_USHORT(buf, head_size + 2 * i) - 2 * num - 2;
2095 if (hlpfile->version <= 16)
2096 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
2097 else
2098 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
2100 hlpfile->hasPhrases = TRUE;
2101 return TRUE;
2104 /***********************************************************************
2106 * HLPFILE_Uncompress_Phrases40
2108 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
2110 UINT num;
2111 INT dec_size, cpr_size;
2112 BYTE *buf_idx, *end_idx;
2113 BYTE *buf_phs, *end_phs;
2114 long* ptr, mask = 0;
2115 unsigned int i;
2116 unsigned short bc, n;
2118 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
2119 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
2121 ptr = (long*)(buf_idx + 9 + 28);
2122 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
2123 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
2125 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2126 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2127 GET_UINT(buf_idx, 9 + 0),
2128 GET_UINT(buf_idx, 9 + 4),
2129 GET_UINT(buf_idx, 9 + 8),
2130 GET_UINT(buf_idx, 9 + 12),
2131 GET_UINT(buf_idx, 9 + 16),
2132 GET_UINT(buf_idx, 9 + 20),
2133 GET_USHORT(buf_idx, 9 + 24),
2134 GET_USHORT(buf_idx, 9 + 26));
2136 dec_size = GET_UINT(buf_idx, 9 + 12);
2137 cpr_size = GET_UINT(buf_idx, 9 + 16);
2139 if (dec_size != cpr_size &&
2140 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
2142 WINE_WARN("size mismatch %u %u\n",
2143 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2144 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2147 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2148 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2149 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2151 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2152 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2153 return FALSE;
2156 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
2158 hlpfile->phrases_offsets[0] = 0;
2159 for (i = 0; i < num; i++)
2161 for (n = 1; getbit(); n += 1 << bc);
2162 if (getbit()) n++;
2163 if (bc > 1 && getbit()) n += 2;
2164 if (bc > 2 && getbit()) n += 4;
2165 if (bc > 3 && getbit()) n += 8;
2166 if (bc > 4 && getbit()) n += 16;
2167 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
2169 #undef getbit
2171 if (dec_size == cpr_size)
2172 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2173 else
2174 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2176 hlpfile->hasPhrases40 = TRUE;
2177 return TRUE;
2180 /***********************************************************************
2182 * HLPFILE_Uncompress_Topic
2184 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
2186 BYTE *buf, *ptr, *end, *newptr;
2187 unsigned int i, newsize = 0;
2188 unsigned int topic_size;
2190 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
2191 {WINE_WARN("topic0\n"); return FALSE;}
2193 buf += 9; /* Skip file header */
2194 topic_size = end - buf;
2195 if (hlpfile->compressed)
2197 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2199 for (i = 0; i < hlpfile->topic_maplen; i++)
2201 ptr = buf + i * hlpfile->tbsize;
2203 /* I don't know why, it's necessary for printman.hlp */
2204 if (ptr + 0x44 > end) ptr = end - 0x44;
2206 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
2209 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2210 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
2211 if (!hlpfile->topic_map) return FALSE;
2212 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2213 hlpfile->topic_end = newptr + newsize;
2215 for (i = 0; i < hlpfile->topic_maplen; i++)
2217 ptr = buf + i * hlpfile->tbsize;
2218 if (ptr + 0x44 > end) ptr = end - 0x44;
2220 hlpfile->topic_map[i] = newptr;
2221 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
2224 else
2226 /* basically, we need to copy the TopicBlockSize byte pages
2227 * (removing the first 0x0C) in one single area in memory
2229 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2230 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2231 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
2232 if (!hlpfile->topic_map) return FALSE;
2233 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2234 hlpfile->topic_end = newptr + topic_size;
2236 for (i = 0; i < hlpfile->topic_maplen; i++)
2238 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
2239 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
2242 return TRUE;
2245 /***********************************************************************
2247 * HLPFILE_Uncompress2
2250 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
2252 BYTE *phptr, *phend;
2253 UINT code;
2254 UINT index;
2256 while (ptr < end && newptr < newend)
2258 if (!*ptr || *ptr >= 0x10)
2259 *newptr++ = *ptr++;
2260 else
2262 code = 0x100 * ptr[0] + ptr[1];
2263 index = (code - 0x100) / 2;
2265 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
2266 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
2268 if (newptr + (phend - phptr) > newend)
2270 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
2271 newptr, newend, (SIZE_T)(phend - phptr));
2272 return;
2274 memcpy(newptr, phptr, phend - phptr);
2275 newptr += phend - phptr;
2276 if (code & 1) *newptr++ = ' ';
2278 ptr += 2;
2281 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
2284 /******************************************************************
2285 * HLPFILE_Uncompress3
2289 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
2290 const BYTE* src, const BYTE* src_end)
2292 unsigned int idx, len;
2294 for (; src < src_end; src++)
2296 if ((*src & 1) == 0)
2298 idx = *src / 2;
2299 if (idx > hlpfile->num_phrases)
2301 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2302 len = 0;
2304 else
2306 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2307 if (dst + len <= dst_end)
2308 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2311 else if ((*src & 0x03) == 0x01)
2313 idx = (*src + 1) * 64;
2314 idx += *++src;
2315 if (idx > hlpfile->num_phrases)
2317 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2318 len = 0;
2320 else
2322 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2323 if (dst + len <= dst_end)
2324 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2327 else if ((*src & 0x07) == 0x03)
2329 len = (*src / 8) + 1;
2330 if (dst + len <= dst_end)
2331 memcpy(dst, src + 1, len);
2332 src += len;
2334 else
2336 len = (*src / 16) + 1;
2337 if (dst + len <= dst_end)
2338 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
2340 dst += len;
2343 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
2344 return TRUE;
2347 /******************************************************************
2348 * HLPFILE_UncompressRLE
2352 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
2354 BYTE ch;
2355 BYTE* sdst = *dst + dstsz;
2357 while (src < end)
2359 ch = *src++;
2360 if (ch & 0x80)
2362 ch &= 0x7F;
2363 if ((*dst) + ch <= sdst)
2364 memcpy(*dst, src, ch);
2365 src += ch;
2367 else
2369 if ((*dst) + ch <= sdst)
2370 memset(*dst, (char)*src, ch);
2371 src++;
2373 *dst += ch;
2375 if (*dst != sdst)
2376 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
2377 (SIZE_T)(*dst - (sdst - dstsz)), dstsz);
2380 /**************************************************************************
2381 * HLPFILE_BPTreeSearch
2383 * Searches for an element in B+ tree
2385 * PARAMS
2386 * buf [I] pointer to the embedded file structured as a B+ tree
2387 * key [I] pointer to data to find
2388 * comp [I] compare function
2390 * RETURNS
2391 * Pointer to block identified by key, or NULL if failure.
2394 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
2395 HLPFILE_BPTreeCompare comp)
2397 unsigned magic;
2398 unsigned page_size;
2399 unsigned cur_page;
2400 unsigned level;
2401 BYTE *pages, *ptr, *newptr;
2402 int i, entries;
2403 int ret;
2405 magic = GET_USHORT(buf, 9);
2406 if (magic != 0x293B)
2408 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2409 return NULL;
2411 page_size = GET_USHORT(buf, 9+4);
2412 cur_page = GET_USHORT(buf, 9+26);
2413 level = GET_USHORT(buf, 9+32);
2414 pages = buf + 9 + 38;
2415 while (--level > 0)
2417 ptr = pages + cur_page*page_size;
2418 entries = GET_SHORT(ptr, 2);
2419 ptr += 6;
2420 for (i = 0; i < entries; i++)
2422 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
2423 ptr = newptr;
2425 cur_page = GET_USHORT(ptr-2, 0);
2427 ptr = pages + cur_page*page_size;
2428 entries = GET_SHORT(ptr, 2);
2429 ptr += 8;
2430 for (i = 0; i < entries; i++)
2432 ret = comp(ptr, key, 1, (void **)&newptr);
2433 if (ret == 0) return ptr;
2434 if (ret > 0) return NULL;
2435 ptr = newptr;
2437 return NULL;
2440 /**************************************************************************
2441 * HLPFILE_BPTreeEnum
2443 * Enumerates elements in B+ tree.
2445 * PARAMS
2446 * buf [I] pointer to the embedded file structured as a B+ tree
2447 * cb [I] compare function
2448 * cookie [IO] cookie for cb function
2450 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
2452 unsigned magic;
2453 unsigned page_size;
2454 unsigned cur_page;
2455 unsigned level;
2456 BYTE *pages, *ptr, *newptr;
2457 int i, entries;
2459 magic = GET_USHORT(buf, 9);
2460 if (magic != 0x293B)
2462 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2463 return;
2465 page_size = GET_USHORT(buf, 9+4);
2466 cur_page = GET_USHORT(buf, 9+26);
2467 level = GET_USHORT(buf, 9+32);
2468 pages = buf + 9 + 38;
2469 while (--level > 0)
2471 ptr = pages + cur_page*page_size;
2472 cur_page = GET_USHORT(ptr, 4);
2474 while (cur_page != 0xFFFF)
2476 ptr = pages + cur_page*page_size;
2477 entries = GET_SHORT(ptr, 2);
2478 ptr += 8;
2479 for (i = 0; i < entries; i++)
2481 cb(ptr, (void **)&newptr, cookie);
2482 ptr = newptr;
2484 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
2489 /***********************************************************************
2491 * HLPFILE_GetContext
2493 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2495 BYTE *cbuf, *cend;
2496 unsigned clen;
2498 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2499 {WINE_WARN("context0\n"); return FALSE;}
2501 clen = cend - cbuf;
2502 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2503 if (!hlpfile->Context) return FALSE;
2504 memcpy(hlpfile->Context, cbuf, clen);
2506 return TRUE;
2509 /***********************************************************************
2511 * HLPFILE_GetKeywords
2513 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2515 BYTE *cbuf, *cend;
2516 unsigned clen;
2518 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2519 clen = cend - cbuf;
2520 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2521 if (!hlpfile->kwbtree) return FALSE;
2522 memcpy(hlpfile->kwbtree, cbuf, clen);
2524 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2526 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2527 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2528 return FALSE;
2530 clen = cend - cbuf;
2531 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2532 if (!hlpfile->kwdata)
2534 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2535 return FALSE;
2537 memcpy(hlpfile->kwdata, cbuf, clen);
2539 return TRUE;
2542 /***********************************************************************
2544 * HLPFILE_GetMap
2546 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2548 BYTE *cbuf, *cend;
2549 unsigned entries, i;
2551 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2552 {WINE_WARN("no map section\n"); return FALSE;}
2554 entries = GET_USHORT(cbuf, 9);
2555 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2556 if (!hlpfile->Map) return FALSE;
2557 hlpfile->wMapLen = entries;
2558 for (i = 0; i < entries; i++)
2560 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2561 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2563 return TRUE;
2566 /***********************************************************************
2568 * DeleteMacro
2570 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2572 HLPFILE_MACRO* next;
2574 while (macro)
2576 next = macro->next;
2577 HeapFree(GetProcessHeap(), 0, macro);
2578 macro = next;
2582 /***********************************************************************
2584 * DeletePage
2586 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2588 HLPFILE_PAGE* next;
2590 while (page)
2592 next = page->next;
2593 HLPFILE_DeleteMacro(page->first_macro);
2594 HeapFree(GetProcessHeap(), 0, page);
2595 page = next;
2599 /***********************************************************************
2601 * HLPFILE_FreeHlpFile
2603 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2605 unsigned i;
2607 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2609 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2610 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2611 else first_hlpfile = hlpfile->next;
2613 if (hlpfile->numFonts)
2615 for (i = 0; i < hlpfile->numFonts; i++)
2617 DeleteObject(hlpfile->fonts[i].hFont);
2619 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2622 if (hlpfile->numBmps)
2624 for (i = 0; i < hlpfile->numBmps; i++)
2626 DeleteObject(hlpfile->bmps[i]);
2628 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2631 HLPFILE_DeletePage(hlpfile->first_page);
2632 HLPFILE_DeleteMacro(hlpfile->first_macro);
2634 DestroyIcon(hlpfile->hIcon);
2635 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2636 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2637 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2638 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2639 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2640 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2641 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2642 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2643 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2644 HeapFree(GetProcessHeap(), 0, hlpfile);