push 1439a81556dec7242b4effc273edbc16b1d3ce68
[wine/hacks.git] / programs / winhlp32 / hlpfile.c
blob7f1a931c12f7f3bf8a2619798f7f93a324dd7731
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 switch (rd->font_scale)
1295 case 0: fs = (4 * page->file->fonts[font].LogFont.lfHeight - 13) / 5; break;
1296 default:
1297 case 1: fs = (4 * page->file->fonts[font].LogFont.lfHeight - 3) / 5; break;
1298 case 2: fs = (4 * page->file->fonts[font].LogFont.lfHeight + 17) / 5; break;
1300 /* FIXME: missing at least colors, also bold attribute looses information */
1302 sprintf(tmp, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1303 font, font + 2, fs,
1304 page->file->fonts[font].LogFont.lfWeight > 400 ? "\\b" : "\\b0",
1305 page->file->fonts[font].LogFont.lfItalic ? "\\i" : "\\i0",
1306 page->file->fonts[font].LogFont.lfUnderline ? "\\ul" : "\\ul0",
1307 page->file->fonts[font].LogFont.lfStrikeOut ? "\\strike" : "\\strike0");
1308 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1310 break;
1312 case 0x81:
1313 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1314 format += 1;
1315 rd->char_pos++;
1316 break;
1318 case 0x82:
1319 if (in_table)
1321 if (format[1] != 0xFF)
1323 if (!HLPFILE_RtfAddControl(rd, "\\par\\intbl")) goto done;
1325 else
1327 if (!HLPFILE_RtfAddControl(rd, "\\cell\\pard\\intbl")) goto done;
1330 else if (!HLPFILE_RtfAddControl(rd, "\\par")) goto done;
1331 format += 1;
1332 rd->char_pos++;
1333 break;
1335 case 0x83:
1336 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1337 format += 1;
1338 rd->char_pos++;
1339 break;
1341 #if 0
1342 case 0x84:
1343 format += 3;
1344 break;
1345 #endif
1347 case 0x86:
1348 case 0x87:
1349 case 0x88:
1351 BYTE type = format[1];
1352 long size;
1354 /* FIXME: we don't use 'BYTE pos = (*format - 0x86);' for the image position */
1355 format += 2;
1356 size = fetch_long(&format);
1358 switch (type)
1360 case 0x22:
1361 fetch_ushort(&format); /* hot spot */
1362 /* fall thru */
1363 case 0x03:
1364 switch (GET_SHORT(format, 0))
1366 case 0:
1367 HLPFILE_RtfAddGfxByIndex(rd, page->file, GET_SHORT(format, 2));
1368 rd->char_pos++;
1369 break;
1370 case 1:
1371 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1372 GET_SHORT(format, 0),
1373 size, GET_SHORT(format, 2));
1374 HLPFILE_RtfAddGfxByAddr(rd, page->file, format + 2, size - 4);
1375 rd->char_pos++;
1376 break;
1377 default:
1378 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1379 break;
1381 break;
1382 case 0x05:
1383 WINE_FIXME("Got an embedded element %s\n", format + 6);
1384 break;
1385 default:
1386 WINE_FIXME("Got a type %d picture\n", type);
1387 break;
1389 format += size;
1391 break;
1393 case 0x89:
1394 format += 1;
1395 if (!rd->current_link)
1396 WINE_FIXME("No existing link\n");
1397 rd->current_link->cpMax = rd->char_pos;
1398 rd->current_link = NULL;
1399 rd->force_color = FALSE;
1400 break;
1402 case 0x8B:
1403 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1404 format += 1;
1405 rd->char_pos++;
1406 break;
1408 case 0x8C:
1409 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1410 /* FIXME: it could be that hypen is also in input stream !! */
1411 format += 1;
1412 rd->char_pos++;
1413 break;
1415 #if 0
1416 case 0xA9:
1417 format += 2;
1418 break;
1419 #endif
1421 case 0xC8:
1422 case 0xCC:
1423 WINE_TRACE("macro => %s\n", format + 3);
1424 HLPFILE_AllocLink(rd, hlp_link_macro, (const char*)format + 3,
1425 GET_USHORT(format, 1), 0, !(*format & 4), -1);
1426 format += 3 + GET_USHORT(format, 1);
1427 break;
1429 case 0xE0:
1430 case 0xE1:
1431 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1432 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1433 page->file->lpszPath, -1, GET_UINT(format, 1)-16, 1, -1);
1436 format += 5;
1437 break;
1439 case 0xE2:
1440 case 0xE3:
1441 case 0xE6:
1442 case 0xE7:
1443 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1444 page->file->lpszPath, -1, GET_UINT(format, 1),
1445 !(*format & 4), -1);
1446 format += 5;
1447 break;
1449 case 0xEA:
1450 case 0xEB:
1451 case 0xEE:
1452 case 0xEF:
1454 char* ptr = (char*) format + 8;
1455 BYTE type = format[3];
1456 int wnd = -1;
1458 switch (type)
1460 case 1:
1461 wnd = *ptr;
1462 /* fall through */
1463 case 0:
1464 ptr = page->file->lpszPath;
1465 break;
1466 case 6:
1467 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1469 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1471 if (wnd == -1)
1472 WINE_WARN("Couldn't find window info for %s\n", ptr);
1473 ptr += strlen(ptr) + 1;
1474 /* fall through */
1475 case 4:
1476 break;
1477 default:
1478 WINE_WARN("Unknown link type %d\n", type);
1479 break;
1481 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1482 ptr, -1, GET_UINT(format, 4), !(*format & 4), wnd);
1484 format += 3 + GET_USHORT(format, 1);
1485 break;
1487 default:
1488 WINE_WARN("format %02x\n", *format);
1489 format++;
1493 if (in_table)
1495 if (!HLPFILE_RtfAddControl(rd, "\\row\\par\\pard\\plain")) goto done;
1496 rd->char_pos += 2;
1498 ret = TRUE;
1499 done:
1501 HeapFree(GetProcessHeap(), 0, text_base);
1502 return ret;
1505 /******************************************************************
1506 * HLPFILE_BrowsePage
1509 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page, struct RtfData* rd,
1510 unsigned font_scale, unsigned relative)
1512 HLPFILE *hlpfile = page->file;
1513 BYTE *buf, *end;
1514 DWORD ref = page->reference;
1515 unsigned index, old_index = -1, offset, count = 0, offs = 0;
1516 unsigned cpg, parlen;
1517 char tmp[1024];
1518 const char* ck = NULL;
1520 rd->in_text = TRUE;
1521 rd->data = rd->ptr = HeapAlloc(GetProcessHeap(), 0, rd->allocated = 32768);
1522 rd->char_pos = 0;
1523 rd->first_link = rd->current_link = NULL;
1524 rd->force_color = FALSE;
1525 rd->font_scale = font_scale;
1526 rd->relative = relative;
1527 rd->char_pos_rel = 0;
1529 switch (hlpfile->charset)
1531 case DEFAULT_CHARSET:
1532 case ANSI_CHARSET: cpg = 1252; break;
1533 case SHIFTJIS_CHARSET: cpg = 932; break;
1534 case HANGEUL_CHARSET: cpg = 949; break;
1535 case GB2312_CHARSET: cpg = 936; break;
1536 case CHINESEBIG5_CHARSET: cpg = 950; break;
1537 case GREEK_CHARSET: cpg = 1253; break;
1538 case TURKISH_CHARSET: cpg = 1254; break;
1539 case HEBREW_CHARSET: cpg = 1255; break;
1540 case ARABIC_CHARSET: cpg = 1256; break;
1541 case BALTIC_CHARSET: cpg = 1257; break;
1542 case VIETNAMESE_CHARSET: cpg = 1258; break;
1543 case RUSSIAN_CHARSET: cpg = 1251; break;
1544 case EE_CHARSET: cpg = 1250; break;
1545 case THAI_CHARSET: cpg = 874; break;
1546 case JOHAB_CHARSET: cpg = 1361; break;
1547 case MAC_CHARSET: ck = "mac"; break;
1548 default:
1549 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1550 cpg = 1252;
1552 if (ck)
1554 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1555 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1557 else
1559 sprintf(tmp, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg);
1560 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1563 /* generate font table */
1564 if (!HLPFILE_RtfAddControl(rd, "{\\fonttbl")) return FALSE;
1565 for (index = 0; index < hlpfile->numFonts; index++)
1567 const char* family;
1568 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1570 case FF_MODERN: family = "modern"; break;
1571 case FF_ROMAN: family = "roman"; break;
1572 case FF_SWISS: family = "swiss"; break;
1573 case FF_SCRIPT: family = "script"; break;
1574 case FF_DECORATIVE: family = "decor"; break;
1575 default: family = "nil"; break;
1577 sprintf(tmp, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1578 index, family,
1579 hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0x0F,
1580 hlpfile->fonts[index].LogFont.lfCharSet,
1581 hlpfile->fonts[index].LogFont.lfFaceName);
1582 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1584 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1585 /* generate color table */
1586 if (!HLPFILE_RtfAddControl(rd, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE;
1587 for (index = 0; index < hlpfile->numFonts; index++)
1589 const char* family;
1590 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1592 case FF_MODERN: family = "modern"; break;
1593 case FF_ROMAN: family = "roman"; break;
1594 case FF_SWISS: family = "swiss"; break;
1595 case FF_SCRIPT: family = "script"; break;
1596 case FF_DECORATIVE: family = "decor"; break;
1597 default: family = "nil"; break;
1599 sprintf(tmp, "\\red%d\\green%d\\blue%d;",
1600 GetRValue(hlpfile->fonts[index].color),
1601 GetGValue(hlpfile->fonts[index].color),
1602 GetBValue(hlpfile->fonts[index].color));
1603 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1605 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1609 if (hlpfile->version <= 16)
1611 index = (ref - 0x0C) / hlpfile->dsize;
1612 offset = (ref - 0x0C) % hlpfile->dsize;
1614 else
1616 index = (ref - 0x0C) >> 14;
1617 offset = (ref - 0x0C) & 0x3FFF;
1620 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
1622 /* we jumped to the next block, adjust pointers */
1623 ref -= 12;
1624 offset -= 12;
1627 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1628 buf = hlpfile->topic_map[index] + offset;
1629 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1630 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1631 if (index != old_index) {offs = 0; old_index = index;}
1633 switch (buf[0x14])
1635 case 0x02:
1636 if (count++) goto done;
1637 break;
1638 case 0x01:
1639 case 0x20:
1640 case 0x23:
1641 if (!HLPFILE_BrowseParagraph(page, rd, buf, end, &parlen)) return FALSE;
1642 if (relative > index * 0x8000 + offs)
1643 rd->char_pos_rel = rd->char_pos;
1644 offs += parlen;
1645 break;
1646 default:
1647 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1649 if (hlpfile->version <= 16)
1651 ref += GET_UINT(buf, 0xc);
1652 if (GET_UINT(buf, 0xc) == 0)
1653 break;
1655 else
1656 ref = GET_UINT(buf, 0xc);
1657 } while (ref != 0xffffffff);
1658 done:
1659 page->first_link = rd->first_link;
1660 return HLPFILE_RtfAddControl(rd, "}");
1663 /******************************************************************
1664 * HLPFILE_ReadFont
1668 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1670 BYTE *ref, *end;
1671 unsigned i, len, idx;
1672 unsigned face_num, dscr_num, face_offset, dscr_offset;
1673 BYTE flag, family;
1675 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1677 WINE_WARN("no subfile FONT\n");
1678 hlpfile->numFonts = 0;
1679 hlpfile->fonts = NULL;
1680 return FALSE;
1683 ref += 9;
1685 face_num = GET_USHORT(ref, 0);
1686 dscr_num = GET_USHORT(ref, 2);
1687 face_offset = GET_USHORT(ref, 4);
1688 dscr_offset = GET_USHORT(ref, 6);
1690 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1691 face_num, face_offset, dscr_num, dscr_offset);
1693 hlpfile->numFonts = dscr_num;
1694 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1696 len = (dscr_offset - face_offset) / face_num;
1697 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1698 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1699 for (i = 0; i < dscr_num; i++)
1701 flag = ref[dscr_offset + i * 11 + 0];
1702 family = ref[dscr_offset + i * 11 + 2];
1704 hlpfile->fonts[i].LogFont.lfHeight = ref[dscr_offset + i * 11 + 1];
1705 hlpfile->fonts[i].LogFont.lfWidth = 0;
1706 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1707 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1708 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1709 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1710 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1711 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1712 hlpfile->fonts[i].LogFont.lfCharSet = hlpfile->charset;
1713 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1714 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1715 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1716 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1718 switch (family)
1720 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1721 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1722 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1723 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1724 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1725 default: WINE_FIXME("Unknown family %u\n", family);
1727 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1729 if (idx < face_num)
1731 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1732 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1734 else
1736 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1737 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1739 hlpfile->fonts[i].hFont = 0;
1740 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1741 ref[dscr_offset + i * 11 + 6],
1742 ref[dscr_offset + i * 11 + 7]);
1743 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1744 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1745 i, flag,
1746 X(0, "bold"),
1747 X(1, "italic"),
1748 X(2, "underline"),
1749 X(3, "strikeOut"),
1750 X(4, "dblUnderline"),
1751 X(5, "smallCaps"),
1752 ref[dscr_offset + i * 11 + 1],
1753 family,
1754 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1755 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1757 return TRUE;
1760 /***********************************************************************
1762 * HLPFILE_ReadFileToBuffer
1764 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1766 BYTE header[16], dummy[1];
1768 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1770 /* sanity checks */
1771 if (GET_UINT(header, 0) != 0x00035F3F)
1772 {WINE_WARN("wrong header\n"); return FALSE;};
1774 hlpfile->file_buffer_size = GET_UINT(header, 12);
1775 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1776 if (!hlpfile->file_buffer) return FALSE;
1778 memcpy(hlpfile->file_buffer, header, 16);
1779 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1780 {WINE_WARN("filesize1\n"); return FALSE;};
1782 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1784 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1786 return TRUE;
1789 /**************************************************************************
1790 * comp_FindSubFile
1792 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1795 static int comp_FindSubFile(void *p, const void *key,
1796 int leaf, void** next)
1798 *next = (char *)p+strlen(p)+(leaf?5:3);
1799 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1800 return strcmp(p, key);
1803 /***********************************************************************
1805 * HLPFILE_FindSubFile
1807 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
1809 BYTE *ptr;
1811 WINE_TRACE("looking for file '%s'\n", name);
1812 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
1813 name, comp_FindSubFile);
1814 if (!ptr) return FALSE;
1815 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
1816 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
1818 WINE_ERR("internal file %s does not fit\n", name);
1819 return FALSE;
1821 *subend = *subbuf + GET_UINT(*subbuf, 0);
1822 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
1824 WINE_ERR("internal file %s does not fit\n", name);
1825 return FALSE;
1827 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1829 WINE_ERR("invalid size provided for internal file %s\n", name);
1830 return FALSE;
1832 return TRUE;
1835 /***********************************************************************
1837 * HLPFILE_SystemCommands
1839 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1841 BYTE *buf, *ptr, *end;
1842 HLPFILE_MACRO *macro, **m;
1843 LPSTR p;
1844 unsigned short magic, minor, major, flags;
1846 hlpfile->lpszTitle = NULL;
1848 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1850 magic = GET_USHORT(buf + 9, 0);
1851 minor = GET_USHORT(buf + 9, 2);
1852 major = GET_USHORT(buf + 9, 4);
1853 /* gen date on 4 bytes */
1854 flags = GET_USHORT(buf + 9, 10);
1855 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1856 magic, major, minor, flags);
1857 if (magic != 0x036C || major != 1)
1858 {WINE_WARN("Wrong system header\n"); return FALSE;}
1859 if (minor <= 16)
1861 hlpfile->tbsize = 0x800;
1862 hlpfile->compressed = 0;
1864 else if (flags == 0)
1866 hlpfile->tbsize = 0x1000;
1867 hlpfile->compressed = 0;
1869 else if (flags == 4)
1871 hlpfile->tbsize = 0x1000;
1872 hlpfile->compressed = 1;
1874 else
1876 hlpfile->tbsize = 0x800;
1877 hlpfile->compressed = 1;
1880 if (hlpfile->compressed)
1881 hlpfile->dsize = 0x4000;
1882 else
1883 hlpfile->dsize = hlpfile->tbsize - 0x0C;
1885 hlpfile->version = minor;
1886 hlpfile->flags = flags;
1887 hlpfile->charset = DEFAULT_CHARSET;
1889 if (hlpfile->version <= 16)
1891 char *str = (char*)buf + 0x15;
1893 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1894 if (!hlpfile->lpszTitle) return FALSE;
1895 lstrcpy(hlpfile->lpszTitle, str);
1896 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1897 /* Nothing more to parse */
1898 return TRUE;
1900 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1902 char *str = (char*) ptr + 4;
1903 switch (GET_USHORT(ptr, 0))
1905 case 1:
1906 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1907 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1908 if (!hlpfile->lpszTitle) return FALSE;
1909 lstrcpy(hlpfile->lpszTitle, str);
1910 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1911 break;
1913 case 2:
1914 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1915 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1916 if (!hlpfile->lpszCopyright) return FALSE;
1917 lstrcpy(hlpfile->lpszCopyright, str);
1918 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1919 break;
1921 case 3:
1922 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1923 hlpfile->contents_start = GET_UINT(ptr, 4);
1924 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1925 break;
1927 case 4:
1928 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1929 if (!macro) break;
1930 p = (char*)macro + sizeof(HLPFILE_MACRO);
1931 lstrcpy(p, str);
1932 macro->lpszMacro = p;
1933 macro->next = 0;
1934 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1935 *m = macro;
1936 break;
1938 case 5:
1939 if (GET_USHORT(ptr, 4 + 4) != 1)
1940 WINE_FIXME("More than one icon, picking up first\n");
1941 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
1942 hlpfile->hIcon = CreateIconFromResourceEx(ptr + 4 + 0x16,
1943 GET_USHORT(ptr, 2) - 0x16, TRUE,
1944 0x30000, 0, 0, 0);
1945 break;
1947 case 6:
1948 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1950 if (hlpfile->windows)
1951 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1952 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1953 else
1954 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1955 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1957 if (hlpfile->windows)
1959 unsigned flags = GET_USHORT(ptr, 4);
1960 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1962 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1963 else wi->type[0] = '\0';
1964 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1965 else wi->name[0] = '\0';
1966 if (flags & 0x0004) strcpy(wi->caption, &str[21]);
1967 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1968 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1969 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1970 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1971 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1972 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1973 wi->win_style = WS_OVERLAPPEDWINDOW;
1974 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1975 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1976 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",
1977 flags & 0x0001 ? 'T' : 't',
1978 flags & 0x0002 ? 'N' : 'n',
1979 flags & 0x0004 ? 'C' : 'c',
1980 flags & 0x0008 ? 'X' : 'x',
1981 flags & 0x0010 ? 'Y' : 'y',
1982 flags & 0x0020 ? 'W' : 'w',
1983 flags & 0x0040 ? 'H' : 'h',
1984 flags & 0x0080 ? 'S' : 's',
1985 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1986 wi->size.cx, wi->size.cy);
1988 break;
1989 case 8:
1990 WINE_WARN("Citation: '%s'\n", ptr + 4);
1991 break;
1992 case 11:
1993 hlpfile->charset = ptr[4];
1994 WINE_TRACE("Charset: %d\n", hlpfile->charset);
1995 break;
1996 default:
1997 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
2000 if (!hlpfile->lpszTitle)
2001 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
2002 return TRUE;
2005 /***********************************************************************
2007 * HLPFILE_UncompressedLZ77_Size
2009 static INT HLPFILE_UncompressedLZ77_Size(const BYTE *ptr, const BYTE *end)
2011 int i, newsize = 0;
2013 while (ptr < end)
2015 int mask = *ptr++;
2016 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2018 if (mask & 1)
2020 int code = GET_USHORT(ptr, 0);
2021 int len = 3 + (code >> 12);
2022 newsize += len;
2023 ptr += 2;
2025 else newsize++, ptr++;
2029 return newsize;
2032 /***********************************************************************
2034 * HLPFILE_UncompressLZ77
2036 static BYTE *HLPFILE_UncompressLZ77(const BYTE *ptr, const BYTE *end, BYTE *newptr)
2038 int i;
2040 while (ptr < end)
2042 int mask = *ptr++;
2043 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2045 if (mask & 1)
2047 int code = GET_USHORT(ptr, 0);
2048 int len = 3 + (code >> 12);
2049 int offset = code & 0xfff;
2051 * We must copy byte-by-byte here. We cannot use memcpy nor
2052 * memmove here. Just example:
2053 * a[]={1,2,3,4,5,6,7,8,9,10}
2054 * newptr=a+2;
2055 * offset=1;
2056 * We expect:
2057 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
2059 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
2060 ptr += 2;
2062 else *newptr++ = *ptr++;
2066 return newptr;
2069 /***********************************************************************
2071 * HLPFILE_UncompressLZ77_Phrases
2073 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
2075 UINT i, num, dec_size, head_size;
2076 BYTE *buf, *end;
2078 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
2080 if (hlpfile->version <= 16)
2081 head_size = 13;
2082 else
2083 head_size = 17;
2085 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
2086 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
2088 if (hlpfile->version <= 16)
2089 dec_size = end - buf - 15 - 2 * num;
2090 else
2091 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
2093 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2094 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2095 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2097 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2098 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2099 return FALSE;
2102 for (i = 0; i <= num; i++)
2103 hlpfile->phrases_offsets[i] = GET_USHORT(buf, head_size + 2 * i) - 2 * num - 2;
2105 if (hlpfile->version <= 16)
2106 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
2107 else
2108 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
2110 hlpfile->hasPhrases = TRUE;
2111 return TRUE;
2114 /***********************************************************************
2116 * HLPFILE_Uncompress_Phrases40
2118 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
2120 UINT num;
2121 INT dec_size, cpr_size;
2122 BYTE *buf_idx, *end_idx;
2123 BYTE *buf_phs, *end_phs;
2124 long* ptr, mask = 0;
2125 unsigned int i;
2126 unsigned short bc, n;
2128 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
2129 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
2131 ptr = (long*)(buf_idx + 9 + 28);
2132 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
2133 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
2135 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2136 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2137 GET_UINT(buf_idx, 9 + 0),
2138 GET_UINT(buf_idx, 9 + 4),
2139 GET_UINT(buf_idx, 9 + 8),
2140 GET_UINT(buf_idx, 9 + 12),
2141 GET_UINT(buf_idx, 9 + 16),
2142 GET_UINT(buf_idx, 9 + 20),
2143 GET_USHORT(buf_idx, 9 + 24),
2144 GET_USHORT(buf_idx, 9 + 26));
2146 dec_size = GET_UINT(buf_idx, 9 + 12);
2147 cpr_size = GET_UINT(buf_idx, 9 + 16);
2149 if (dec_size != cpr_size &&
2150 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
2152 WINE_WARN("size mismatch %u %u\n",
2153 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2154 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2157 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2158 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2159 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2161 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2162 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2163 return FALSE;
2166 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
2168 hlpfile->phrases_offsets[0] = 0;
2169 for (i = 0; i < num; i++)
2171 for (n = 1; getbit(); n += 1 << bc);
2172 if (getbit()) n++;
2173 if (bc > 1 && getbit()) n += 2;
2174 if (bc > 2 && getbit()) n += 4;
2175 if (bc > 3 && getbit()) n += 8;
2176 if (bc > 4 && getbit()) n += 16;
2177 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
2179 #undef getbit
2181 if (dec_size == cpr_size)
2182 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2183 else
2184 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2186 hlpfile->hasPhrases40 = TRUE;
2187 return TRUE;
2190 /***********************************************************************
2192 * HLPFILE_Uncompress_Topic
2194 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
2196 BYTE *buf, *ptr, *end, *newptr;
2197 unsigned int i, newsize = 0;
2198 unsigned int topic_size;
2200 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
2201 {WINE_WARN("topic0\n"); return FALSE;}
2203 buf += 9; /* Skip file header */
2204 topic_size = end - buf;
2205 if (hlpfile->compressed)
2207 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2209 for (i = 0; i < hlpfile->topic_maplen; i++)
2211 ptr = buf + i * hlpfile->tbsize;
2213 /* I don't know why, it's necessary for printman.hlp */
2214 if (ptr + 0x44 > end) ptr = end - 0x44;
2216 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
2219 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2220 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
2221 if (!hlpfile->topic_map) return FALSE;
2222 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2223 hlpfile->topic_end = newptr + newsize;
2225 for (i = 0; i < hlpfile->topic_maplen; i++)
2227 ptr = buf + i * hlpfile->tbsize;
2228 if (ptr + 0x44 > end) ptr = end - 0x44;
2230 hlpfile->topic_map[i] = newptr;
2231 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
2234 else
2236 /* basically, we need to copy the TopicBlockSize byte pages
2237 * (removing the first 0x0C) in one single area in memory
2239 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2240 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2241 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
2242 if (!hlpfile->topic_map) return FALSE;
2243 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2244 hlpfile->topic_end = newptr + topic_size;
2246 for (i = 0; i < hlpfile->topic_maplen; i++)
2248 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
2249 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
2252 return TRUE;
2255 /***********************************************************************
2257 * HLPFILE_Uncompress2
2260 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
2262 BYTE *phptr, *phend;
2263 UINT code;
2264 UINT index;
2266 while (ptr < end && newptr < newend)
2268 if (!*ptr || *ptr >= 0x10)
2269 *newptr++ = *ptr++;
2270 else
2272 code = 0x100 * ptr[0] + ptr[1];
2273 index = (code - 0x100) / 2;
2275 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
2276 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
2278 if (newptr + (phend - phptr) > newend)
2280 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
2281 newptr, newend, (SIZE_T)(phend - phptr));
2282 return;
2284 memcpy(newptr, phptr, phend - phptr);
2285 newptr += phend - phptr;
2286 if (code & 1) *newptr++ = ' ';
2288 ptr += 2;
2291 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
2294 /******************************************************************
2295 * HLPFILE_Uncompress3
2299 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
2300 const BYTE* src, const BYTE* src_end)
2302 unsigned int idx, len;
2304 for (; src < src_end; src++)
2306 if ((*src & 1) == 0)
2308 idx = *src / 2;
2309 if (idx > hlpfile->num_phrases)
2311 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2312 len = 0;
2314 else
2316 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2317 if (dst + len <= dst_end)
2318 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2321 else if ((*src & 0x03) == 0x01)
2323 idx = (*src + 1) * 64;
2324 idx += *++src;
2325 if (idx > hlpfile->num_phrases)
2327 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2328 len = 0;
2330 else
2332 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2333 if (dst + len <= dst_end)
2334 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2337 else if ((*src & 0x07) == 0x03)
2339 len = (*src / 8) + 1;
2340 if (dst + len <= dst_end)
2341 memcpy(dst, src + 1, len);
2342 src += len;
2344 else
2346 len = (*src / 16) + 1;
2347 if (dst + len <= dst_end)
2348 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
2350 dst += len;
2353 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
2354 return TRUE;
2357 /******************************************************************
2358 * HLPFILE_UncompressRLE
2362 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE* dst, unsigned dstsz)
2364 BYTE ch;
2365 BYTE* sdst = dst + dstsz;
2367 while (src < end)
2369 ch = *src++;
2370 if (ch & 0x80)
2372 ch &= 0x7F;
2373 if (dst + ch <= sdst)
2374 memcpy(dst, src, ch);
2375 src += ch;
2377 else
2379 if (dst + ch <= sdst)
2380 memset(dst, (char)*src, ch);
2381 src++;
2383 dst += ch;
2385 if (dst != sdst)
2386 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
2387 (SIZE_T)(dst - (sdst - dstsz)), dstsz);
2390 /**************************************************************************
2391 * HLPFILE_BPTreeSearch
2393 * Searches for an element in B+ tree
2395 * PARAMS
2396 * buf [I] pointer to the embedded file structured as a B+ tree
2397 * key [I] pointer to data to find
2398 * comp [I] compare function
2400 * RETURNS
2401 * Pointer to block identified by key, or NULL if failure.
2404 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
2405 HLPFILE_BPTreeCompare comp)
2407 unsigned magic;
2408 unsigned page_size;
2409 unsigned cur_page;
2410 unsigned level;
2411 BYTE *pages, *ptr, *newptr;
2412 int i, entries;
2413 int ret;
2415 magic = GET_USHORT(buf, 9);
2416 if (magic != 0x293B)
2418 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2419 return NULL;
2421 page_size = GET_USHORT(buf, 9+4);
2422 cur_page = GET_USHORT(buf, 9+26);
2423 level = GET_USHORT(buf, 9+32);
2424 pages = buf + 9 + 38;
2425 while (--level > 0)
2427 ptr = pages + cur_page*page_size;
2428 entries = GET_SHORT(ptr, 2);
2429 ptr += 6;
2430 for (i = 0; i < entries; i++)
2432 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
2433 ptr = newptr;
2435 cur_page = GET_USHORT(ptr-2, 0);
2437 ptr = pages + cur_page*page_size;
2438 entries = GET_SHORT(ptr, 2);
2439 ptr += 8;
2440 for (i = 0; i < entries; i++)
2442 ret = comp(ptr, key, 1, (void **)&newptr);
2443 if (ret == 0) return ptr;
2444 if (ret > 0) return NULL;
2445 ptr = newptr;
2447 return NULL;
2450 /**************************************************************************
2451 * HLPFILE_BPTreeEnum
2453 * Enumerates elements in B+ tree.
2455 * PARAMS
2456 * buf [I] pointer to the embedded file structured as a B+ tree
2457 * cb [I] compare function
2458 * cookie [IO] cookie for cb function
2460 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
2462 unsigned magic;
2463 unsigned page_size;
2464 unsigned cur_page;
2465 unsigned level;
2466 BYTE *pages, *ptr, *newptr;
2467 int i, entries;
2469 magic = GET_USHORT(buf, 9);
2470 if (magic != 0x293B)
2472 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2473 return;
2475 page_size = GET_USHORT(buf, 9+4);
2476 cur_page = GET_USHORT(buf, 9+26);
2477 level = GET_USHORT(buf, 9+32);
2478 pages = buf + 9 + 38;
2479 while (--level > 0)
2481 ptr = pages + cur_page*page_size;
2482 cur_page = GET_USHORT(ptr, 4);
2484 while (cur_page != 0xFFFF)
2486 ptr = pages + cur_page*page_size;
2487 entries = GET_SHORT(ptr, 2);
2488 ptr += 8;
2489 for (i = 0; i < entries; i++)
2491 cb(ptr, (void **)&newptr, cookie);
2492 ptr = newptr;
2494 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
2499 /***********************************************************************
2501 * HLPFILE_GetContext
2503 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2505 BYTE *cbuf, *cend;
2506 unsigned clen;
2508 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2509 {WINE_WARN("context0\n"); return FALSE;}
2511 clen = cend - cbuf;
2512 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2513 if (!hlpfile->Context) return FALSE;
2514 memcpy(hlpfile->Context, cbuf, clen);
2516 return TRUE;
2519 /***********************************************************************
2521 * HLPFILE_GetKeywords
2523 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2525 BYTE *cbuf, *cend;
2526 unsigned clen;
2528 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2529 clen = cend - cbuf;
2530 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2531 if (!hlpfile->kwbtree) return FALSE;
2532 memcpy(hlpfile->kwbtree, cbuf, clen);
2534 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2536 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2537 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2538 return FALSE;
2540 clen = cend - cbuf;
2541 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2542 if (!hlpfile->kwdata)
2544 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2545 return FALSE;
2547 memcpy(hlpfile->kwdata, cbuf, clen);
2549 return TRUE;
2552 /***********************************************************************
2554 * HLPFILE_GetMap
2556 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2558 BYTE *cbuf, *cend;
2559 unsigned entries, i;
2561 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2562 {WINE_WARN("no map section\n"); return FALSE;}
2564 entries = GET_USHORT(cbuf, 9);
2565 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2566 if (!hlpfile->Map) return FALSE;
2567 hlpfile->wMapLen = entries;
2568 for (i = 0; i < entries; i++)
2570 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2571 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2573 return TRUE;
2576 /***********************************************************************
2578 * DeleteMacro
2580 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2582 HLPFILE_MACRO* next;
2584 while (macro)
2586 next = macro->next;
2587 HeapFree(GetProcessHeap(), 0, macro);
2588 macro = next;
2592 /***********************************************************************
2594 * DeletePage
2596 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2598 HLPFILE_PAGE* next;
2600 while (page)
2602 next = page->next;
2603 HLPFILE_DeleteMacro(page->first_macro);
2604 HeapFree(GetProcessHeap(), 0, page);
2605 page = next;
2609 /***********************************************************************
2611 * HLPFILE_FreeHlpFile
2613 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2615 unsigned i;
2617 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2619 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2620 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2621 else first_hlpfile = hlpfile->next;
2623 if (hlpfile->numFonts)
2625 for (i = 0; i < hlpfile->numFonts; i++)
2627 DeleteObject(hlpfile->fonts[i].hFont);
2629 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2632 if (hlpfile->numBmps)
2634 for (i = 0; i < hlpfile->numBmps; i++)
2636 DeleteObject(hlpfile->bmps[i]);
2638 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2641 HLPFILE_DeletePage(hlpfile->first_page);
2642 HLPFILE_DeleteMacro(hlpfile->first_macro);
2644 DestroyIcon(hlpfile->hIcon);
2645 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2646 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2647 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2648 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2649 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2650 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2651 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2652 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2653 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2654 HeapFree(GetProcessHeap(), 0, hlpfile->help_on_file);
2655 HeapFree(GetProcessHeap(), 0, hlpfile);