push 535a8035db5d709ef8f05977281c2f1a0c4cce0a
[wine/hacks.git] / programs / winhlp32 / hlpfile.c
blob05d5347cef0a7116aaa5f5ec79f74e6bb6cf3080
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(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)
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, const BYTE *buf, const BYTE *end, unsigned ref, unsigned offset)
375 HLPFILE_PAGE* page;
376 const 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(const BYTE** ptr)
462 long ret;
464 if (*(*ptr) & 1)
466 ret = (*(const unsigned long*)(*ptr) - 0x80000000L) / 2;
467 (*ptr) += 4;
469 else
471 ret = (*(const unsigned short*)(*ptr) - 0x8000) / 2;
472 (*ptr) += 2;
475 return ret;
478 static unsigned long fetch_ulong(const BYTE** ptr)
480 unsigned long ret;
482 if (*(*ptr) & 1)
484 ret = *(const unsigned long*)(*ptr) / 2;
485 (*ptr) += 4;
487 else
489 ret = *(const unsigned short*)(*ptr) / 2;
490 (*ptr) += 2;
492 return ret;
495 static short fetch_short(const BYTE** ptr)
497 short ret;
499 if (*(*ptr) & 1)
501 ret = (*(const unsigned short*)(*ptr) - 0x8000) / 2;
502 (*ptr) += 2;
504 else
506 ret = (*(const unsigned char*)(*ptr) - 0x80) / 2;
507 (*ptr)++;
509 return ret;
512 static unsigned short fetch_ushort(const BYTE** ptr)
514 unsigned short ret;
516 if (*(*ptr) & 1)
518 ret = *(const unsigned short*)(*ptr) / 2;
519 (*ptr) += 2;
521 else
523 ret = *(const unsigned char*)(*ptr) / 2;
524 (*ptr)++;
526 return ret;
529 /***********************************************************************
531 * HLPFILE_SkipParagraph
533 static BOOL HLPFILE_SkipParagraph(HLPFILE *hlpfile, const BYTE *buf, const BYTE *end, unsigned* len)
535 const 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 const BYTE* HLPFILE_DecompressGfx(const BYTE* src, unsigned csz, unsigned sz, BYTE packing,
557 BYTE** alloc)
559 const BYTE* dst;
560 BYTE* tmp;
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 *alloc = NULL;
572 break;
573 case 1: /* RunLen */
574 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz);
575 if (!dst) return NULL;
576 HLPFILE_UncompressRLE(src, src + csz, *alloc, sz);
577 break;
578 case 2: /* LZ77 */
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);
583 if (sz77 != sz)
584 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
585 break;
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);
592 if (!dst)
594 HeapFree(GetProcessHeap(), 0, tmp);
595 return FALSE;
597 HLPFILE_UncompressRLE(tmp, tmp + sz77, *alloc, sz);
598 HeapFree(GetProcessHeap(), 0, tmp);
599 break;
600 default:
601 WINE_FIXME("Unsupported packing %u\n", packing);
602 return NULL;
604 return dst;
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);
614 rd->data = new;
616 memcpy(rd->ptr, str, sz);
617 rd->ptr += sz;
619 return TRUE;
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)
631 const char* p;
632 const char* last;
633 const char* replace;
634 unsigned rlen;
636 if (!rd->in_text)
638 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
639 rd->in_text = TRUE;
641 for (last = p = str; *p; p++)
643 if (*p < 0) /* escape non ASCII chars */
645 static char xx[8];
646 rlen = sprintf(xx, "\\'%x", *(const BYTE*)p);
647 replace = xx;
649 else switch (*p)
651 case '{': rlen = 2; replace = "\\{"; break;
652 case '}': rlen = 2; replace = "\\}"; break;
653 case '\\': rlen = 2; replace = "\\\\"; break;
654 default: continue;
656 if ((p != last && !HLPFILE_RtfAddRawString(rd, last, p - last)) ||
657 !HLPFILE_RtfAddRawString(rd, replace, rlen)) return FALSE;
658 last = p + 1;
660 return HLPFILE_RtfAddRawString(rd, last, p - last);
663 /******************************************************************
664 * RtfAddHexBytes
667 static BOOL HLPFILE_RtfAddHexBytes(struct RtfData* rd, const void* _ptr, unsigned sz)
669 char tmp[512];
670 unsigned i, step;
671 const BYTE* ptr = _ptr;
672 static const char* _2hex = "0123456789abcdef";
674 if (!rd->in_text)
676 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
677 rd->in_text = TRUE;
679 for (; sz; sz -= step)
681 step = min(256, sz);
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;
689 return TRUE;
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;
703 HENHMETAFILE hEMF;
704 BOOL ret = FALSE;
705 void* data;
706 UINT sz;
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);
718 SetBkColor(hdcMem,
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);
730 DeleteDC(hdcMask);
732 SelectObject(hdcMem, hbmOldMem);
733 DeleteDC(hdcMem);
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);
749 DeleteDC(hdcMem);
751 /* do the cleanup */
752 ReleaseDC(0, hdc);
753 DeleteObject(hbmMask);
754 DeleteObject(hbm);
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);
772 return ret;
775 /******************************************************************
776 * HLPFILE_RtfAddBitmap
779 static BOOL HLPFILE_RtfAddBitmap(struct RtfData* rd, const BYTE* beg, BYTE type, BYTE pack)
781 const BYTE* ptr;
782 const BYTE* pict_beg;
783 BYTE* alloc = NULL;
784 BITMAPINFO* bi;
785 unsigned long off, csz;
786 unsigned nc = 0;
787 BOOL clrImportant = FALSE;
788 BOOL ret = FALSE;
789 char tmp[256];
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 */
821 if (type == 0x06)
823 unsigned i;
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;
838 ptr += 4;
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);
846 goto done;
848 if (!HLPFILE_RtfAddControl(rd, "{\\pict")) goto done;
849 if (type == 0x06)
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;
856 else
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;
866 ret = TRUE;
867 done:
868 HeapFree(GetProcessHeap(), 0, bi);
869 HeapFree(GetProcessHeap(), 0, alloc);
871 return ret;
874 /******************************************************************
875 * HLPFILE_RtfAddMetaFile
878 static BOOL HLPFILE_RtfAddMetaFile(struct RtfData* rd, const BYTE* beg, BYTE pack)
880 const BYTE* ptr;
881 unsigned long size, csize;
882 unsigned long off, hsoff;
883 const BYTE* bits;
884 BYTE* alloc = NULL;
885 char tmp[256];
886 unsigned mm;
887 BOOL ret;
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;
897 ptr += 4;
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);
904 ptr += 8;
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);
917 return ret;
920 /******************************************************************
921 * HLPFILE_RtfAddGfxByAddr
924 static BOOL HLPFILE_RtfAddGfxByAddr(struct RtfData* rd, HLPFILE *hlpfile,
925 const BYTE* ref, unsigned long size)
927 unsigned i, numpict;
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++)
934 const BYTE* beg;
935 const BYTE* ptr;
936 BYTE type, pack;
938 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
939 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
941 type = *ptr++;
942 pack = *ptr++;
944 switch (type)
946 case 5: /* device dependent bmp */
947 case 6: /* device independent bmp */
948 HLPFILE_RtfAddBitmap(rd, beg, type, pack);
949 break;
950 case 8:
951 HLPFILE_RtfAddMetaFile(rd, beg, pack);
952 break;
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");
960 break;
962 return TRUE;
965 /******************************************************************
966 * HLPFILE_RtfAddGfxByIndex
970 static BOOL HLPFILE_RtfAddGfxByIndex(struct RtfData* rd, HLPFILE *hlpfile,
971 unsigned index)
973 char tmp[16];
974 BYTE *ref, *end;
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;}
982 ref += 9;
983 return HLPFILE_RtfAddGfxByAddr(rd, hlpfile, ref, end - ref);
986 /******************************************************************
987 * HLPFILE_AllocLink
991 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
992 const char* str, unsigned len, LONG hash,
993 unsigned clrChange, unsigned wnd)
995 HLPFILE_LINK* link;
996 char* link_str;
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';
1009 link->hash = hash;
1010 link->bClrChange = clrChange ? 1 : 0;
1011 link->window = wnd;
1012 link->next = rd->first_link;
1013 rd->first_link = link;
1014 link->cpMin = rd->char_pos;
1015 link->cpMax = 0;
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);
1022 return link;
1025 unsigned HLPFILE_HalfPointsToTwips(unsigned pts)
1027 static unsigned logPxY;
1028 if (!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)
1044 UINT textsize;
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;
1050 short table_width;
1051 BOOL in_table = FALSE;
1052 char tmp[256];
1053 BOOL ret = FALSE;
1055 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
1057 *parlen = 0;
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);
1070 else
1072 WINE_FIXME("Text size is too long, splitting\n");
1073 size = blocksize - datalen;
1074 memcpy(text, buf + datalen, size);
1077 else
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)
1093 char type;
1095 in_table = TRUE;
1096 ncol = *format++;
1098 if (!HLPFILE_RtfAddControl(rd, "\\trowd")) goto done;
1099 type = *format++;
1100 if (type == 0 || type == 2)
1102 table_width = GET_SHORT(format, 0);
1103 format += 2;
1105 else
1106 table_width = 32767;
1107 WINE_TRACE("New table: cols=%d type=%x width=%d\n",
1108 ncol, type, table_width);
1109 if (ncol > 1)
1111 int pos;
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;
1128 else
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;
1138 format += ncol * 4;
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;
1145 if (in_table)
1147 nc = GET_SHORT(format, 0);
1148 if (nc == -1) break;
1149 format += 5;
1150 if (!HLPFILE_RtfAddControl(rd, "\\intbl")) goto done;
1152 else nc++;
1153 if (buf[0x14] == 0x01)
1154 format += 6;
1155 else
1156 format += 4;
1157 bits = GET_USHORT(format, 0); format += 2;
1158 if (bits & 0x0001) fetch_long(&format);
1159 if (bits & 0x0002)
1161 sprintf(tmp, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1162 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1164 if (bits & 0x0004)
1166 sprintf(tmp, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1167 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1169 if (bits & 0x0008)
1171 sprintf(tmp, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1172 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1174 if (bits & 0x0010)
1176 sprintf(tmp, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1177 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1179 if (bits & 0x0020)
1181 sprintf(tmp, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1182 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1184 if (bits & 0x0040)
1186 sprintf(tmp, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1187 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1189 if (bits & 0x0100)
1191 BYTE brdr = *format++;
1192 short w;
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;
1202 /* 0x80: unknown */
1204 w = GET_SHORT(format, 0); format += 2;
1205 if (w)
1207 sprintf(tmp, "\\brdrw%d", HLPFILE_HalfPointsToTwips(w));
1208 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1211 if (bits & 0x0200)
1213 int i, ntab = fetch_short(&format);
1214 unsigned tab, ts;
1215 const char* kind;
1217 for (i = 0; i < ntab; i++)
1219 tab = fetch_ushort(&format);
1220 ts = (tab & 0x4000) ? fetch_ushort(&format) : 0 /* left */;
1221 switch (ts)
1223 default: WINE_FIXME("Unknown tab style %x\n", ts);
1224 /* fall through */
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);
1252 if (textsize)
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)
1269 format++;
1270 break;
1273 WINE_TRACE("format=%02x\n", *format);
1274 switch (*format)
1276 case 0x20:
1277 WINE_FIXME("NIY20\n");
1278 format += 5;
1279 break;
1281 case 0x21:
1282 WINE_FIXME("NIY21\n");
1283 format += 3;
1284 break;
1286 case 0x80:
1288 unsigned font = GET_USHORT(format, 1);
1289 unsigned fs;
1291 WINE_TRACE("Changing font to %d\n", font);
1292 format += 3;
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;
1298 default:
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",
1305 font, font + 2, fs,
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;
1312 break;
1314 case 0x81:
1315 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1316 format += 1;
1317 rd->char_pos++;
1318 break;
1320 case 0x82:
1321 if (in_table)
1323 if (format[1] != 0xFF)
1325 if (!HLPFILE_RtfAddControl(rd, "\\par\\intbl")) goto done;
1327 else
1329 if (!HLPFILE_RtfAddControl(rd, "\\cell\\pard\\intbl")) goto done;
1332 else if (!HLPFILE_RtfAddControl(rd, "\\par")) goto done;
1333 format += 1;
1334 rd->char_pos++;
1335 break;
1337 case 0x83:
1338 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1339 format += 1;
1340 rd->char_pos++;
1341 break;
1343 #if 0
1344 case 0x84:
1345 format += 3;
1346 break;
1347 #endif
1349 case 0x86:
1350 case 0x87:
1351 case 0x88:
1353 BYTE type = format[1];
1354 long size;
1356 /* FIXME: we don't use 'BYTE pos = (*format - 0x86);' for the image position */
1357 format += 2;
1358 size = fetch_long(&format);
1360 switch (type)
1362 case 0x22:
1363 fetch_ushort(&format); /* hot spot */
1364 /* fall thru */
1365 case 0x03:
1366 switch (GET_SHORT(format, 0))
1368 case 0:
1369 HLPFILE_RtfAddGfxByIndex(rd, page->file, GET_SHORT(format, 2));
1370 rd->char_pos++;
1371 break;
1372 case 1:
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);
1377 rd->char_pos++;
1378 break;
1379 default:
1380 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1381 break;
1383 break;
1384 case 0x05:
1385 WINE_FIXME("Got an embedded element %s\n", format + 6);
1386 break;
1387 default:
1388 WINE_FIXME("Got a type %d picture\n", type);
1389 break;
1391 format += size;
1393 break;
1395 case 0x89:
1396 format += 1;
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;
1402 break;
1404 case 0x8B:
1405 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1406 format += 1;
1407 rd->char_pos++;
1408 break;
1410 case 0x8C:
1411 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1412 /* FIXME: it could be that hypen is also in input stream !! */
1413 format += 1;
1414 rd->char_pos++;
1415 break;
1417 #if 0
1418 case 0xA9:
1419 format += 2;
1420 break;
1421 #endif
1423 case 0xC8:
1424 case 0xCC:
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);
1429 break;
1431 case 0xE0:
1432 case 0xE1:
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);
1438 format += 5;
1439 break;
1441 case 0xE2:
1442 case 0xE3:
1443 case 0xE6:
1444 case 0xE7:
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);
1448 format += 5;
1449 break;
1451 case 0xEA:
1452 case 0xEB:
1453 case 0xEE:
1454 case 0xEF:
1456 char* ptr = (char*) format + 8;
1457 BYTE type = format[3];
1458 int wnd = -1;
1460 switch (type)
1462 case 1:
1463 wnd = *ptr;
1464 /* fall through */
1465 case 0:
1466 ptr = page->file->lpszPath;
1467 break;
1468 case 6:
1469 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1471 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1473 if (wnd == -1)
1474 WINE_WARN("Couldn't find window info for %s\n", ptr);
1475 ptr += strlen(ptr) + 1;
1476 /* fall through */
1477 case 4:
1478 break;
1479 default:
1480 WINE_WARN("Unknown link type %d\n", type);
1481 break;
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);
1487 break;
1489 default:
1490 WINE_WARN("format %02x\n", *format);
1491 format++;
1495 if (in_table)
1497 if (!HLPFILE_RtfAddControl(rd, "\\row\\par\\pard\\plain")) goto done;
1498 rd->char_pos += 2;
1500 ret = TRUE;
1501 done:
1503 HeapFree(GetProcessHeap(), 0, text_base);
1504 return ret;
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;
1515 BYTE *buf, *end;
1516 DWORD ref = page->reference;
1517 unsigned index, old_index = -1, offset, count = 0, offs = 0;
1518 unsigned cpg, parlen;
1519 char tmp[1024];
1520 const char* ck = NULL;
1522 rd->in_text = TRUE;
1523 rd->data = rd->ptr = HeapAlloc(GetProcessHeap(), 0, rd->allocated = 32768);
1524 rd->char_pos = 0;
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;
1550 default:
1551 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1552 cpg = 1252;
1554 if (ck)
1556 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1557 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1559 else
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++)
1569 const char* family;
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;}",
1580 index, family,
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++)
1591 const char* family;
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;
1616 else
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 */
1625 ref -= 12;
1626 offset -= 12;
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;}
1635 switch (buf[0x14])
1637 case 0x02:
1638 if (count++) goto done;
1639 break;
1640 case 0x01:
1641 case 0x20:
1642 case 0x23:
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;
1646 offs += parlen;
1647 break;
1648 default:
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)
1655 break;
1657 else
1658 ref = GET_UINT(buf, 0xc);
1659 } while (ref != 0xffffffff);
1660 done:
1661 page->first_link = rd->first_link;
1662 return HLPFILE_RtfAddControl(rd, "}");
1665 /******************************************************************
1666 * HLPFILE_ReadFont
1670 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1672 BYTE *ref, *end;
1673 unsigned i, len, idx;
1674 unsigned face_num, dscr_num, face_offset, dscr_offset;
1675 BYTE flag, family;
1677 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1679 WINE_WARN("no subfile FONT\n");
1680 hlpfile->numFonts = 0;
1681 hlpfile->fonts = NULL;
1682 return FALSE;
1685 ref += 9;
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;
1720 switch (family)
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);
1731 if (idx < face_num)
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';
1736 else
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",
1747 i, flag,
1748 X(0, "bold"),
1749 X(1, "italic"),
1750 X(2, "underline"),
1751 X(3, "strikeOut"),
1752 X(4, "dblUnderline"),
1753 X(5, "smallCaps"),
1754 ref[dscr_offset + i * 11 + 1],
1755 family,
1756 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1757 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1759 return TRUE;
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;};
1772 /* sanity checks */
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 */
1788 return TRUE;
1791 /**************************************************************************
1792 * comp_FindSubFile
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)
1811 BYTE *ptr;
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);
1821 return FALSE;
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);
1827 return FALSE;
1829 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1831 WINE_ERR("invalid size provided for internal file %s\n", name);
1832 return FALSE;
1834 return TRUE;
1837 /***********************************************************************
1839 * HLPFILE_SystemCommands
1841 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1843 BYTE *buf, *ptr, *end;
1844 HLPFILE_MACRO *macro, **m;
1845 LPSTR p;
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;}
1861 if (minor <= 16)
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;
1876 else
1878 hlpfile->tbsize = 0x800;
1879 hlpfile->compressed = 1;
1882 if (hlpfile->compressed)
1883 hlpfile->dsize = 0x4000;
1884 else
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 */
1900 return TRUE;
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))
1907 case 1:
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);
1913 break;
1915 case 2:
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);
1921 break;
1923 case 3:
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);
1927 break;
1929 case 4:
1930 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1931 if (!macro) break;
1932 p = (char*)macro + sizeof(HLPFILE_MACRO);
1933 lstrcpy(p, str);
1934 macro->lpszMacro = p;
1935 macro->next = 0;
1936 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1937 *m = macro;
1938 break;
1940 case 5:
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,
1946 0x30000, 0, 0, 0);
1947 break;
1949 case 6:
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);
1955 else
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);
1990 break;
1991 case 8:
1992 WINE_WARN("Citation: '%s'\n", ptr + 4);
1993 break;
1994 case 11:
1995 hlpfile->charset = ptr[4];
1996 WINE_TRACE("Charset: %d\n", hlpfile->charset);
1997 break;
1998 default:
1999 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
2002 if (!hlpfile->lpszTitle)
2003 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
2004 return TRUE;
2007 /***********************************************************************
2009 * HLPFILE_UncompressedLZ77_Size
2011 static INT HLPFILE_UncompressedLZ77_Size(const BYTE *ptr, const BYTE *end)
2013 int i, newsize = 0;
2015 while (ptr < end)
2017 int mask = *ptr++;
2018 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2020 if (mask & 1)
2022 int code = GET_USHORT(ptr, 0);
2023 int len = 3 + (code >> 12);
2024 newsize += len;
2025 ptr += 2;
2027 else newsize++, ptr++;
2031 return newsize;
2034 /***********************************************************************
2036 * HLPFILE_UncompressLZ77
2038 static BYTE *HLPFILE_UncompressLZ77(const BYTE *ptr, const BYTE *end, BYTE *newptr)
2040 int i;
2042 while (ptr < end)
2044 int mask = *ptr++;
2045 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2047 if (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}
2056 * newptr=a+2;
2057 * offset=1;
2058 * We expect:
2059 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
2061 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
2062 ptr += 2;
2064 else *newptr++ = *ptr++;
2068 return newptr;
2071 /***********************************************************************
2073 * HLPFILE_UncompressLZ77_Phrases
2075 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
2077 UINT i, num, dec_size, head_size;
2078 BYTE *buf, *end;
2080 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
2082 if (hlpfile->version <= 16)
2083 head_size = 13;
2084 else
2085 head_size = 17;
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;
2092 else
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);
2101 return FALSE;
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);
2109 else
2110 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
2112 hlpfile->hasPhrases = TRUE;
2113 return TRUE;
2116 /***********************************************************************
2118 * HLPFILE_Uncompress_Phrases40
2120 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
2122 UINT num;
2123 INT dec_size, cpr_size;
2124 BYTE *buf_idx, *end_idx;
2125 BYTE *buf_phs, *end_phs;
2126 long* ptr, mask = 0;
2127 unsigned int i;
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);
2165 return FALSE;
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);
2174 if (getbit()) n++;
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;
2181 #undef getbit
2183 if (dec_size == cpr_size)
2184 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2185 else
2186 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2188 hlpfile->hasPhrases40 = TRUE;
2189 return 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);
2236 else
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);
2254 return TRUE;
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;
2265 UINT code;
2266 UINT index;
2268 while (ptr < end && newptr < newend)
2270 if (!*ptr || *ptr >= 0x10)
2271 *newptr++ = *ptr++;
2272 else
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));
2284 return;
2286 memcpy(newptr, phptr, phend - phptr);
2287 newptr += phend - phptr;
2288 if (code & 1) *newptr++ = ' ';
2290 ptr += 2;
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)
2310 idx = *src / 2;
2311 if (idx > hlpfile->num_phrases)
2313 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2314 len = 0;
2316 else
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;
2326 idx += *++src;
2327 if (idx > hlpfile->num_phrases)
2329 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2330 len = 0;
2332 else
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);
2344 src += len;
2346 else
2348 len = (*src / 16) + 1;
2349 if (dst + len <= dst_end)
2350 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
2352 dst += len;
2355 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
2356 return TRUE;
2359 /******************************************************************
2360 * HLPFILE_UncompressRLE
2364 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE* dst, unsigned dstsz)
2366 BYTE ch;
2367 BYTE* sdst = dst + dstsz;
2369 while (src < end)
2371 ch = *src++;
2372 if (ch & 0x80)
2374 ch &= 0x7F;
2375 if (dst + ch <= sdst)
2376 memcpy(dst, src, ch);
2377 src += ch;
2379 else
2381 if (dst + ch <= sdst)
2382 memset(dst, (char)*src, ch);
2383 src++;
2385 dst += ch;
2387 if (dst != sdst)
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
2397 * PARAMS
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
2402 * RETURNS
2403 * Pointer to block identified by key, or NULL if failure.
2406 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
2407 HLPFILE_BPTreeCompare comp)
2409 unsigned magic;
2410 unsigned page_size;
2411 unsigned cur_page;
2412 unsigned level;
2413 BYTE *pages, *ptr, *newptr;
2414 int i, entries;
2415 int ret;
2417 magic = GET_USHORT(buf, 9);
2418 if (magic != 0x293B)
2420 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2421 return NULL;
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;
2427 while (--level > 0)
2429 ptr = pages + cur_page*page_size;
2430 entries = GET_SHORT(ptr, 2);
2431 ptr += 6;
2432 for (i = 0; i < entries; i++)
2434 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
2435 ptr = newptr;
2437 cur_page = GET_USHORT(ptr-2, 0);
2439 ptr = pages + cur_page*page_size;
2440 entries = GET_SHORT(ptr, 2);
2441 ptr += 8;
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;
2447 ptr = newptr;
2449 return NULL;
2452 /**************************************************************************
2453 * HLPFILE_BPTreeEnum
2455 * Enumerates elements in B+ tree.
2457 * PARAMS
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)
2464 unsigned magic;
2465 unsigned page_size;
2466 unsigned cur_page;
2467 unsigned level;
2468 BYTE *pages, *ptr, *newptr;
2469 int i, entries;
2471 magic = GET_USHORT(buf, 9);
2472 if (magic != 0x293B)
2474 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2475 return;
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;
2481 while (--level > 0)
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);
2490 ptr += 8;
2491 for (i = 0; i < entries; i++)
2493 cb(ptr, (void **)&newptr, cookie);
2494 ptr = newptr;
2496 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
2501 /***********************************************************************
2503 * HLPFILE_GetContext
2505 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2507 BYTE *cbuf, *cend;
2508 unsigned clen;
2510 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2511 {WINE_WARN("context0\n"); return FALSE;}
2513 clen = cend - cbuf;
2514 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2515 if (!hlpfile->Context) return FALSE;
2516 memcpy(hlpfile->Context, cbuf, clen);
2518 return TRUE;
2521 /***********************************************************************
2523 * HLPFILE_GetKeywords
2525 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2527 BYTE *cbuf, *cend;
2528 unsigned clen;
2530 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2531 clen = cend - cbuf;
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);
2540 return FALSE;
2542 clen = cend - cbuf;
2543 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2544 if (!hlpfile->kwdata)
2546 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2547 return FALSE;
2549 memcpy(hlpfile->kwdata, cbuf, clen);
2551 return TRUE;
2554 /***********************************************************************
2556 * HLPFILE_GetMap
2558 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2560 BYTE *cbuf, *cend;
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);
2575 return TRUE;
2578 /***********************************************************************
2580 * DeleteMacro
2582 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2584 HLPFILE_MACRO* next;
2586 while (macro)
2588 next = macro->next;
2589 HeapFree(GetProcessHeap(), 0, macro);
2590 macro = next;
2594 /***********************************************************************
2596 * DeletePage
2598 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2600 HLPFILE_PAGE* next;
2602 while (page)
2604 next = page->next;
2605 HLPFILE_DeleteMacro(page->first_macro);
2606 HeapFree(GetProcessHeap(), 0, page);
2607 page = next;
2611 /***********************************************************************
2613 * HLPFILE_FreeHlpFile
2615 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2617 unsigned i;
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);