comctl32: toolbar: Calculate a correct size for empty buttons with the BTNS_AUTOSIZE...
[wine/multimedia.git] / programs / winhlp32 / hlpfile.c
blob99043ac7b5b4468cdd9016daa95e66bbdfdeb4c6
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_GetTOMap(HLPFILE*);
67 static BOOL HLPFILE_AddPage(HLPFILE*, const BYTE*, const BYTE*, unsigned, unsigned);
68 static BOOL HLPFILE_SkipParagraph(HLPFILE*, const BYTE*, const BYTE*, unsigned*);
69 static void HLPFILE_Uncompress2(HLPFILE*, const BYTE*, const BYTE*, BYTE*, const BYTE*);
70 static BOOL HLPFILE_Uncompress3(HLPFILE*, char*, const char*, const BYTE*, const BYTE*);
71 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE* dst, unsigned dstsz);
72 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile);
74 /******************************************************************
75 * HLPFILE_PageByOffset
79 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset, ULONG* relative)
81 HLPFILE_PAGE* page;
82 HLPFILE_PAGE* found;
84 if (!hlpfile) return 0;
86 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, offset);
88 if (offset == 0xFFFFFFFF) return NULL;
89 page = NULL;
91 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
93 if (page->offset <= offset && (!found || found->offset < page->offset))
95 *relative = offset - page->offset;
96 found = page;
99 if (!found)
100 WINE_ERR("Page of offset %u not found in file %s\n",
101 offset, hlpfile->lpszPath);
102 return found;
105 /**************************************************************************
106 * comp_PageByHash
108 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
111 static int comp_PageByHash(void *p, const void *key,
112 int leaf, void** next)
114 LONG lKey = (LONG_PTR)key;
115 LONG lTest = (INT)GET_UINT(p, 0);
117 *next = (char *)p+(leaf?8:6);
118 WINE_TRACE("Comparing '%d' with '%d'\n", lKey, lTest);
119 if (lTest < lKey) return -1;
120 if (lTest > lKey) return 1;
121 return 0;
124 /***********************************************************************
126 * HLPFILE_PageByHash
128 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash, ULONG* relative)
130 BYTE *ptr;
132 if (!hlpfile) return NULL;
133 if (!lHash) return HLPFILE_Contents(hlpfile, relative);
135 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lHash);
137 /* For win 3.0 files hash values are really page numbers */
138 if (hlpfile->version <= 16)
140 if (lHash >= hlpfile->wTOMapLen) return NULL;
141 return HLPFILE_PageByOffset(hlpfile, hlpfile->TOMap[lHash], relative);
144 ptr = HLPFILE_BPTreeSearch(hlpfile->Context, LongToPtr(lHash), comp_PageByHash);
145 if (!ptr)
147 WINE_ERR("Page of hash %x not found in file %s\n", lHash, hlpfile->lpszPath);
148 return NULL;
151 return HLPFILE_PageByOffset(hlpfile, GET_UINT(ptr, 4), relative);
154 /***********************************************************************
156 * HLPFILE_PageByMap
158 HLPFILE_PAGE *HLPFILE_PageByMap(HLPFILE* hlpfile, LONG lMap, ULONG* relative)
160 unsigned int i;
162 if (!hlpfile) return 0;
164 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lMap);
166 for (i = 0; i < hlpfile->wMapLen; i++)
168 if (hlpfile->Map[i].lMap == lMap)
169 return HLPFILE_PageByOffset(hlpfile, hlpfile->Map[i].offset, relative);
172 WINE_ERR("Page of Map %x not found in file %s\n", lMap, hlpfile->lpszPath);
173 return NULL;
176 /***********************************************************************
178 * HLPFILE_Contents
180 HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile, ULONG* relative)
182 HLPFILE_PAGE* page = NULL;
184 if (!hlpfile) return NULL;
186 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start, relative);
187 if (!page)
189 page = hlpfile->first_page;
190 *relative = 0;
192 return page;
195 /***********************************************************************
197 * HLPFILE_Hash
199 LONG HLPFILE_Hash(LPCSTR lpszContext)
201 LONG lHash = 0;
202 CHAR c;
204 while ((c = *lpszContext++))
206 CHAR x = 0;
207 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
208 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
209 if (c >= '1' && c <= '9') x = c - '0';
210 if (c == '0') x = 10;
211 if (c == '.') x = 12;
212 if (c == '_') x = 13;
213 if (x) lHash = lHash * 43 + x;
215 return lHash;
218 /***********************************************************************
220 * HLPFILE_ReadHlpFile
222 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
224 HLPFILE* hlpfile;
226 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
228 if (!strcmp(lpszPath, hlpfile->lpszPath))
230 hlpfile->wRefCount++;
231 return hlpfile;
235 hlpfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
236 sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
237 if (!hlpfile) return 0;
239 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
240 hlpfile->contents_start = 0xFFFFFFFF;
241 hlpfile->next = first_hlpfile;
242 hlpfile->wRefCount = 1;
244 strcpy(hlpfile->lpszPath, lpszPath);
246 first_hlpfile = hlpfile;
247 if (hlpfile->next) hlpfile->next->prev = hlpfile;
249 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
251 HLPFILE_FreeHlpFile(hlpfile);
252 hlpfile = 0;
255 return hlpfile;
258 /***********************************************************************
260 * HLPFILE_DoReadHlpFile
262 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
264 BOOL ret;
265 HFILE hFile;
266 OFSTRUCT ofs;
267 BYTE* buf;
268 DWORD ref = 0x0C;
269 unsigned index, old_index, offset, len, offs, topicoffset;
271 hFile = OpenFile(lpszPath, &ofs, OF_READ);
272 if (hFile == HFILE_ERROR) return FALSE;
274 ret = HLPFILE_ReadFileToBuffer(hlpfile, hFile);
275 _lclose(hFile);
276 if (!ret) return FALSE;
278 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
280 if (hlpfile->version <= 16 && !HLPFILE_GetTOMap(hlpfile)) return FALSE;
282 /* load phrases support */
283 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
284 HLPFILE_Uncompress_Phrases40(hlpfile);
286 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
287 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
289 buf = hlpfile->topic_map[0];
290 old_index = -1;
291 offs = 0;
294 BYTE* end;
296 if (hlpfile->version <= 16)
298 index = (ref - 0x0C) / hlpfile->dsize;
299 offset = (ref - 0x0C) % hlpfile->dsize;
301 else
303 index = (ref - 0x0C) >> 14;
304 offset = (ref - 0x0C) & 0x3FFF;
307 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
309 /* we jumped to the next block, adjust pointers */
310 ref -= 12;
311 offset -= 12;
314 WINE_TRACE("ref=%08x => [%u/%u]\n", ref, index, offset);
316 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
317 buf = hlpfile->topic_map[index] + offset;
318 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
319 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
320 if (index != old_index) {offs = 0; old_index = index;}
322 switch (buf[0x14])
324 case 0x02:
325 if (hlpfile->version <= 16)
326 topicoffset = ref + index * 12;
327 else
328 topicoffset = index * 0x8000 + offs;
329 if (!HLPFILE_AddPage(hlpfile, buf, end, ref, topicoffset)) return FALSE;
330 break;
332 case 0x01:
333 case 0x20:
334 case 0x23:
335 if (!HLPFILE_SkipParagraph(hlpfile, buf, end, &len)) return FALSE;
336 offs += len;
337 break;
339 default:
340 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
343 if (hlpfile->version <= 16)
345 ref += GET_UINT(buf, 0xc);
346 if (GET_UINT(buf, 0xc) == 0)
347 break;
349 else
350 ref = GET_UINT(buf, 0xc);
351 } while (ref != 0xffffffff);
353 HLPFILE_GetKeywords(hlpfile);
354 HLPFILE_GetMap(hlpfile);
355 if (hlpfile->version <= 16) return TRUE;
356 return HLPFILE_GetContext(hlpfile);
359 /***********************************************************************
361 * HLPFILE_AddPage
363 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, const BYTE *buf, const BYTE *end, unsigned ref, unsigned offset)
365 HLPFILE_PAGE* page;
366 const BYTE* title;
367 UINT titlesize, blocksize, datalen;
368 char* ptr;
369 HLPFILE_MACRO*macro;
371 blocksize = GET_UINT(buf, 0);
372 datalen = GET_UINT(buf, 0x10);
373 title = buf + datalen;
374 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
376 titlesize = GET_UINT(buf, 4);
377 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
378 if (!page) return FALSE;
379 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
381 if (titlesize > blocksize - datalen)
383 /* need to decompress */
384 if (hlpfile->hasPhrases)
385 HLPFILE_Uncompress2(hlpfile, title, end, (BYTE*)page->lpszTitle, (BYTE*)page->lpszTitle + titlesize);
386 else if (hlpfile->hasPhrases40)
387 HLPFILE_Uncompress3(hlpfile, page->lpszTitle, page->lpszTitle + titlesize, title, end);
388 else
390 WINE_FIXME("Text size is too long, splitting\n");
391 titlesize = blocksize - datalen;
392 memcpy(page->lpszTitle, title, titlesize);
395 else
396 memcpy(page->lpszTitle, title, titlesize);
398 page->lpszTitle[titlesize] = '\0';
400 if (hlpfile->first_page)
402 hlpfile->last_page->next = page;
403 page->prev = hlpfile->last_page;
404 hlpfile->last_page = page;
406 else
408 hlpfile->first_page = page;
409 hlpfile->last_page = page;
410 page->prev = NULL;
413 page->file = hlpfile;
414 page->next = NULL;
415 page->first_macro = NULL;
416 page->first_link = NULL;
417 page->wNumber = GET_UINT(buf, 0x21);
418 page->offset = offset;
419 page->reference = ref;
421 page->browse_bwd = GET_UINT(buf, 0x19);
422 page->browse_fwd = GET_UINT(buf, 0x1D);
424 if (hlpfile->version <= 16)
426 if (page->browse_bwd == 0xFFFF || page->browse_bwd == 0xFFFFFFFF)
427 page->browse_bwd = 0xFFFFFFFF;
428 else
429 page->browse_bwd = hlpfile->TOMap[page->browse_bwd];
431 if (page->browse_fwd == 0xFFFF || page->browse_fwd == 0xFFFFFFFF)
432 page->browse_fwd = 0xFFFFFFFF;
433 else
434 page->browse_fwd = hlpfile->TOMap[page->browse_fwd];
437 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
438 page->wNumber, page->lpszTitle,
439 page->browse_bwd, page->offset, page->browse_fwd);
441 /* now load macros */
442 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
443 while (ptr < page->lpszTitle + titlesize)
445 unsigned len = strlen(ptr);
446 char* macro_str;
448 WINE_TRACE("macro: %s\n", ptr);
449 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
450 macro->lpszMacro = macro_str = (char*)(macro + 1);
451 memcpy(macro_str, ptr, len + 1);
452 /* FIXME: shall we really link macro in reverse order ??
453 * may produce strange results when played at page opening
455 macro->next = page->first_macro;
456 page->first_macro = macro;
457 ptr += len + 1;
460 return TRUE;
463 static long fetch_long(const BYTE** ptr)
465 long ret;
467 if (*(*ptr) & 1)
469 ret = (*(const unsigned long*)(*ptr) - 0x80000000L) / 2;
470 (*ptr) += 4;
472 else
474 ret = (*(const unsigned short*)(*ptr) - 0x8000) / 2;
475 (*ptr) += 2;
478 return ret;
481 static unsigned long fetch_ulong(const BYTE** ptr)
483 unsigned long ret;
485 if (*(*ptr) & 1)
487 ret = *(const unsigned long*)(*ptr) / 2;
488 (*ptr) += 4;
490 else
492 ret = *(const unsigned short*)(*ptr) / 2;
493 (*ptr) += 2;
495 return ret;
498 static short fetch_short(const BYTE** ptr)
500 short ret;
502 if (*(*ptr) & 1)
504 ret = (*(const unsigned short*)(*ptr) - 0x8000) / 2;
505 (*ptr) += 2;
507 else
509 ret = (*(const unsigned char*)(*ptr) - 0x80) / 2;
510 (*ptr)++;
512 return ret;
515 static unsigned short fetch_ushort(const BYTE** ptr)
517 unsigned short ret;
519 if (*(*ptr) & 1)
521 ret = *(const unsigned short*)(*ptr) / 2;
522 (*ptr) += 2;
524 else
526 ret = *(const unsigned char*)(*ptr) / 2;
527 (*ptr)++;
529 return ret;
532 /***********************************************************************
534 * HLPFILE_SkipParagraph
536 static BOOL HLPFILE_SkipParagraph(HLPFILE *hlpfile, const BYTE *buf, const BYTE *end, unsigned* len)
538 const BYTE *tmp;
540 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
541 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
543 tmp = buf + 0x15;
544 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
546 fetch_long(&tmp);
547 *len = fetch_ushort(&tmp);
549 else *len = end-buf-15;
551 return TRUE;
554 /******************************************************************
555 * HLPFILE_DecompressGfx
557 * Decompress the data part of a bitmap or a metafile
559 static const BYTE* HLPFILE_DecompressGfx(const BYTE* src, unsigned csz, unsigned sz, BYTE packing,
560 BYTE** alloc)
562 const BYTE* dst;
563 BYTE* tmp;
564 unsigned sz77;
566 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
568 switch (packing)
570 case 0: /* uncompressed */
571 if (sz != csz)
572 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
573 dst = src;
574 *alloc = NULL;
575 break;
576 case 1: /* RunLen */
577 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz);
578 if (!dst) return NULL;
579 HLPFILE_UncompressRLE(src, src + csz, *alloc, sz);
580 break;
581 case 2: /* LZ77 */
582 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
583 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz77);
584 if (!dst) return NULL;
585 HLPFILE_UncompressLZ77(src, src + csz, *alloc);
586 if (sz77 != sz)
587 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
588 break;
589 case 3: /* LZ77 then RLE */
590 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
591 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
592 if (!tmp) return FALSE;
593 HLPFILE_UncompressLZ77(src, src + csz, tmp);
594 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz);
595 if (!dst)
597 HeapFree(GetProcessHeap(), 0, tmp);
598 return FALSE;
600 HLPFILE_UncompressRLE(tmp, tmp + sz77, *alloc, sz);
601 HeapFree(GetProcessHeap(), 0, tmp);
602 break;
603 default:
604 WINE_FIXME("Unsupported packing %u\n", packing);
605 return NULL;
607 return dst;
610 static BOOL HLPFILE_RtfAddRawString(struct RtfData* rd, const char* str, size_t sz)
612 if (rd->ptr + sz >= rd->data + rd->allocated)
614 char* new = HeapReAlloc(GetProcessHeap(), 0, rd->data, rd->allocated *= 2);
615 if (!new) return FALSE;
616 rd->ptr = new + (rd->ptr - rd->data);
617 rd->data = new;
619 memcpy(rd->ptr, str, sz);
620 rd->ptr += sz;
622 return TRUE;
625 static BOOL HLPFILE_RtfAddControl(struct RtfData* rd, const char* str)
627 if (*str == '\\' || *str == '{') rd->in_text = FALSE;
628 else if (*str == '}') rd->in_text = TRUE;
629 return HLPFILE_RtfAddRawString(rd, str, strlen(str));
632 static BOOL HLPFILE_RtfAddText(struct RtfData* rd, const char* str)
634 const char* p;
635 const char* last;
636 const char* replace;
637 unsigned rlen;
639 if (!rd->in_text)
641 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
642 rd->in_text = TRUE;
644 for (last = p = str; *p; p++)
646 if (*p < 0) /* escape non ASCII chars */
648 static char xx[8];
649 rlen = sprintf(xx, "\\'%x", *(const BYTE*)p);
650 replace = xx;
652 else switch (*p)
654 case '{': rlen = 2; replace = "\\{"; break;
655 case '}': rlen = 2; replace = "\\}"; break;
656 case '\\': rlen = 2; replace = "\\\\"; break;
657 default: continue;
659 if ((p != last && !HLPFILE_RtfAddRawString(rd, last, p - last)) ||
660 !HLPFILE_RtfAddRawString(rd, replace, rlen)) return FALSE;
661 last = p + 1;
663 return HLPFILE_RtfAddRawString(rd, last, p - last);
666 /******************************************************************
667 * RtfAddHexBytes
670 static BOOL HLPFILE_RtfAddHexBytes(struct RtfData* rd, const void* _ptr, unsigned sz)
672 char tmp[512];
673 unsigned i, step;
674 const BYTE* ptr = _ptr;
675 static const char* _2hex = "0123456789abcdef";
677 if (!rd->in_text)
679 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
680 rd->in_text = TRUE;
682 for (; sz; sz -= step)
684 step = min(256, sz);
685 for (i = 0; i < step; i++)
687 tmp[2 * i + 0] = _2hex[*ptr >> 4];
688 tmp[2 * i + 1] = _2hex[*ptr++ & 0xF];
690 if (!HLPFILE_RtfAddRawString(rd, tmp, 2 * step)) return FALSE;
692 return TRUE;
695 /******************************************************************
696 * HLPFILE_RtfAddTransparentBitmap
698 * We'll transform a transparent bitmap into an metafile that
699 * we then transform into RTF
701 static BOOL HLPFILE_RtfAddTransparentBitmap(struct RtfData* rd, const BITMAPINFO* bi,
702 const void* pict, unsigned nc)
704 HDC hdc, hdcMask, hdcMem, hdcEMF;
705 HBITMAP hbm, hbmMask, hbmOldMask, hbmOldMem;
706 HENHMETAFILE hEMF;
707 BOOL ret = FALSE;
708 void* data;
709 UINT sz;
711 hbm = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
712 CBM_INIT, pict, bi, DIB_RGB_COLORS);
714 hdcMem = CreateCompatibleDC(hdc);
715 hbmOldMem = SelectObject(hdcMem, hbm);
717 /* create the mask bitmap from the main bitmap */
718 hdcMask = CreateCompatibleDC(hdc);
719 hbmMask = CreateBitmap(bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, 1, 1, NULL);
720 hbmOldMask = SelectObject(hdcMask, hbmMask);
721 SetBkColor(hdcMem,
722 RGB(bi->bmiColors[nc - 1].rgbRed,
723 bi->bmiColors[nc - 1].rgbGreen,
724 bi->bmiColors[nc - 1].rgbBlue));
725 BitBlt(hdcMask, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCCOPY);
727 /* sets to RGB(0,0,0) the transparent bits in main bitmap */
728 SetBkColor(hdcMem, RGB(0,0,0));
729 SetTextColor(hdcMem, RGB(255,255,255));
730 BitBlt(hdcMem, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMask, 0, 0, SRCAND);
732 SelectObject(hdcMask, hbmOldMask);
733 DeleteDC(hdcMask);
735 SelectObject(hdcMem, hbmOldMem);
736 DeleteDC(hdcMem);
738 /* we create the bitmap on the fly */
739 hdcEMF = CreateEnhMetaFile(NULL, NULL, NULL, NULL);
740 hdcMem = CreateCompatibleDC(hdcEMF);
742 /* sets to RGB(0,0,0) the transparent bits in final bitmap */
743 hbmOldMem = SelectObject(hdcMem, hbmMask);
744 SetBkColor(hdcEMF, RGB(255, 255, 255));
745 SetTextColor(hdcEMF, RGB(0, 0, 0));
746 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCAND);
748 /* and copy the remaining bits of main bitmap */
749 SelectObject(hdcMem, hbm);
750 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCPAINT);
751 SelectObject(hdcMem, hbmOldMem);
752 DeleteDC(hdcMem);
754 /* do the cleanup */
755 ReleaseDC(0, hdc);
756 DeleteObject(hbmMask);
757 DeleteObject(hbm);
759 hEMF = CloseEnhMetaFile(hdcEMF);
761 /* generate rtf stream */
762 sz = GetEnhMetaFileBits(hEMF, 0, NULL);
763 if (sz && (data = HeapAlloc(GetProcessHeap(), 0, sz)))
765 if (sz == GetEnhMetaFileBits(hEMF, sz, data))
767 ret = HLPFILE_RtfAddControl(rd, "{\\pict\\emfblip") &&
768 HLPFILE_RtfAddHexBytes(rd, data, sz) &&
769 HLPFILE_RtfAddControl(rd, "}");
771 HeapFree(GetProcessHeap(), 0, data);
773 DeleteEnhMetaFile(hEMF);
775 return ret;
778 /******************************************************************
779 * HLPFILE_RtfAddBitmap
782 static BOOL HLPFILE_RtfAddBitmap(struct RtfData* rd, const BYTE* beg, BYTE type, BYTE pack)
784 const BYTE* ptr;
785 const BYTE* pict_beg;
786 BYTE* alloc = NULL;
787 BITMAPINFO* bi;
788 unsigned long off, csz;
789 unsigned nc = 0;
790 BOOL clrImportant = FALSE;
791 BOOL ret = FALSE;
792 char tmp[256];
794 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
795 if (!bi) return FALSE;
797 ptr = beg + 2; /* for type and pack */
799 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
800 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
801 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
802 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
803 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
804 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
805 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
806 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
807 clrImportant = fetch_ulong(&ptr);
808 bi->bmiHeader.biClrImportant = (clrImportant > 1) ? clrImportant : 0;
809 bi->bmiHeader.biCompression = BI_RGB;
810 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
811 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
812 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
813 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
814 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
815 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
817 csz = fetch_ulong(&ptr);
818 fetch_ulong(&ptr); /* hotspot size */
820 off = GET_UINT(ptr, 0); ptr += 4;
821 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
823 /* now read palette info */
824 if (type == 0x06)
826 unsigned i;
828 nc = bi->bmiHeader.biClrUsed;
829 /* not quite right, especially for bitfields type of compression */
830 if (!nc && bi->bmiHeader.biBitCount <= 8)
831 nc = 1 << bi->bmiHeader.biBitCount;
833 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
834 if (!bi) return FALSE;
835 for (i = 0; i < nc; i++)
837 bi->bmiColors[i].rgbBlue = ptr[0];
838 bi->bmiColors[i].rgbGreen = ptr[1];
839 bi->bmiColors[i].rgbRed = ptr[2];
840 bi->bmiColors[i].rgbReserved = 0;
841 ptr += 4;
844 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack, &alloc);
846 if (clrImportant == 1 && nc > 0)
848 ret = HLPFILE_RtfAddTransparentBitmap(rd, bi, pict_beg, nc);
849 goto done;
851 if (!HLPFILE_RtfAddControl(rd, "{\\pict")) goto done;
852 if (type == 0x06)
854 sprintf(tmp, "\\dibitmap0\\picw%d\\pich%d",
855 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
856 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
857 if (!HLPFILE_RtfAddHexBytes(rd, bi, sizeof(*bi) + nc * sizeof(RGBQUAD))) goto done;
859 else
861 sprintf(tmp, "\\wbitmap0\\wbmbitspixel%d\\wbmplanes%d\\picw%d\\pich%d",
862 bi->bmiHeader.biBitCount, bi->bmiHeader.biPlanes,
863 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
864 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
866 if (!HLPFILE_RtfAddHexBytes(rd, pict_beg, bi->bmiHeader.biSizeImage)) goto done;
867 if (!HLPFILE_RtfAddControl(rd, "}")) goto done;
869 ret = TRUE;
870 done:
871 HeapFree(GetProcessHeap(), 0, bi);
872 HeapFree(GetProcessHeap(), 0, alloc);
874 return ret;
877 /******************************************************************
878 * HLPFILE_RtfAddMetaFile
881 static BOOL HLPFILE_RtfAddMetaFile(struct RtfData* rd, const BYTE* beg, BYTE pack)
883 const BYTE* ptr;
884 unsigned long size, csize;
885 unsigned long off, hsoff;
886 const BYTE* bits;
887 BYTE* alloc = NULL;
888 char tmp[256];
889 unsigned mm;
890 BOOL ret;
892 WINE_TRACE("Loading metafile\n");
894 ptr = beg + 2; /* for type and pack */
896 mm = fetch_ushort(&ptr); /* mapping mode */
897 sprintf(tmp, "{\\pict\\wmetafile%d\\picw%d\\pich%d",
898 mm, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2));
899 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
900 ptr += 4;
902 size = fetch_ulong(&ptr); /* decompressed size */
903 csize = fetch_ulong(&ptr); /* compressed size */
904 fetch_ulong(&ptr); /* hotspot size */
905 off = GET_UINT(ptr, 0);
906 hsoff = GET_UINT(ptr, 4);
907 ptr += 8;
909 WINE_TRACE("sz=%lu csz=%lu offs=%lu/%u,%lu\n",
910 size, csize, off, ptr - beg, hsoff);
912 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack, &alloc);
913 if (!bits) return FALSE;
915 ret = HLPFILE_RtfAddHexBytes(rd, bits, size) &&
916 HLPFILE_RtfAddControl(rd, "}");
918 HeapFree(GetProcessHeap(), 0, alloc);
920 return ret;
923 /******************************************************************
924 * HLPFILE_RtfAddGfxByAddr
927 static BOOL HLPFILE_RtfAddGfxByAddr(struct RtfData* rd, HLPFILE *hlpfile,
928 const BYTE* ref, unsigned long size)
930 unsigned i, numpict;
932 numpict = GET_USHORT(ref, 2);
933 WINE_TRACE("Got picture magic=%04x #=%d\n", GET_USHORT(ref, 0), numpict);
935 for (i = 0; i < numpict; i++)
937 const BYTE* beg;
938 const BYTE* ptr;
939 BYTE type, pack;
941 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
942 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
944 type = *ptr++;
945 pack = *ptr++;
947 switch (type)
949 case 5: /* device dependent bmp */
950 case 6: /* device independent bmp */
951 HLPFILE_RtfAddBitmap(rd, beg, type, pack);
952 break;
953 case 8:
954 HLPFILE_RtfAddMetaFile(rd, beg, pack);
955 break;
956 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
959 /* FIXME: hotspots */
961 /* FIXME: implement support for multiple picture format */
962 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
963 break;
965 return TRUE;
968 /******************************************************************
969 * HLPFILE_RtfAddGfxByIndex
973 static BOOL HLPFILE_RtfAddGfxByIndex(struct RtfData* rd, HLPFILE *hlpfile,
974 unsigned index)
976 char tmp[16];
977 BYTE *ref, *end;
979 WINE_TRACE("Loading picture #%d\n", index);
981 sprintf(tmp, "|bm%u", index);
983 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
985 ref += 9;
986 return HLPFILE_RtfAddGfxByAddr(rd, hlpfile, ref, end - ref);
989 /******************************************************************
990 * HLPFILE_AllocLink
994 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
995 const char* str, unsigned len, LONG hash,
996 unsigned clrChange, unsigned wnd)
998 HLPFILE_LINK* link;
999 char* link_str;
1001 /* FIXME: should build a string table for the attributes.link.lpszPath
1002 * they are reallocated for each link
1004 if (len == -1) len = strlen(str);
1005 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + len + 1);
1006 if (!link) return NULL;
1008 link->cookie = cookie;
1009 link->string = link_str = (char*)(link + 1);
1010 memcpy(link_str, str, len);
1011 link_str[len] = '\0';
1012 link->hash = hash;
1013 link->bClrChange = clrChange ? 1 : 0;
1014 link->window = wnd;
1015 link->next = rd->first_link;
1016 rd->first_link = link;
1017 link->cpMin = rd->char_pos;
1018 link->cpMax = 0;
1019 rd->force_color = clrChange;
1020 if (rd->current_link) WINE_FIXME("Pending link\n");
1021 rd->current_link = link;
1023 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
1024 link->cookie, link->string, link->hash, link->window);
1025 return link;
1028 unsigned HLPFILE_HalfPointsToTwips(unsigned pts)
1030 static unsigned logPxY;
1031 if (!logPxY)
1033 HDC hdc = GetDC(NULL);
1034 logPxY = GetDeviceCaps(hdc, LOGPIXELSY);
1035 ReleaseDC(NULL, hdc);
1037 return MulDiv(pts, 72 * 10, logPxY);
1040 /***********************************************************************
1042 * HLPFILE_BrowseParagraph
1044 static BOOL HLPFILE_BrowseParagraph(HLPFILE_PAGE* page, struct RtfData* rd,
1045 BYTE *buf, BYTE* end, unsigned* parlen)
1047 UINT textsize;
1048 const BYTE *format, *format_end;
1049 char *text, *text_base, *text_end;
1050 long size, blocksize, datalen;
1051 unsigned short bits;
1052 unsigned nc, ncol = 1;
1053 short table_width;
1054 BOOL in_table = FALSE;
1055 char tmp[256];
1056 BOOL ret = FALSE;
1058 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
1060 *parlen = 0;
1061 blocksize = GET_UINT(buf, 0);
1062 size = GET_UINT(buf, 0x4);
1063 datalen = GET_UINT(buf, 0x10);
1064 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
1065 if (!text) return FALSE;
1066 if (size > blocksize - datalen)
1068 /* need to decompress */
1069 if (page->file->hasPhrases)
1070 HLPFILE_Uncompress2(page->file, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
1071 else if (page->file->hasPhrases40)
1072 HLPFILE_Uncompress3(page->file, text, text + size, buf + datalen, end);
1073 else
1075 WINE_FIXME("Text size is too long, splitting\n");
1076 size = blocksize - datalen;
1077 memcpy(text, buf + datalen, size);
1080 else
1081 memcpy(text, buf + datalen, size);
1083 text_end = text + size;
1085 format = buf + 0x15;
1086 format_end = buf + GET_UINT(buf, 0x10);
1088 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
1090 fetch_long(&format);
1091 *parlen = fetch_ushort(&format);
1094 if (buf[0x14] == 0x23)
1096 char type;
1098 in_table = TRUE;
1099 ncol = *format++;
1101 if (!HLPFILE_RtfAddControl(rd, "\\trowd")) goto done;
1102 type = *format++;
1103 if (type == 0 || type == 2)
1105 table_width = GET_SHORT(format, 0);
1106 format += 2;
1108 else
1109 table_width = 32767;
1110 WINE_TRACE("New table: cols=%d type=%x width=%d\n",
1111 ncol, type, table_width);
1112 if (ncol > 1)
1114 int pos;
1115 sprintf(tmp, "\\trgaph%d\\trleft%d",
1116 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6), table_width, 32767)),
1117 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)));
1118 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1119 pos = HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6) / 2, table_width, 32767));
1120 for (nc = 0; nc < ncol; nc++)
1122 WINE_TRACE("column(%d/%d) gap=%d width=%d\n",
1123 nc, ncol, GET_SHORT(format, nc*4),
1124 GET_SHORT(format, nc*4+2));
1125 pos += GET_SHORT(format, nc * 4) + GET_SHORT(format, nc * 4 + 2);
1126 sprintf(tmp, "\\cellx%d",
1127 HLPFILE_HalfPointsToTwips(MulDiv(pos, table_width, 32767)));
1128 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1131 else
1133 WINE_TRACE("column(0/%d) gap=%d width=%d\n",
1134 ncol, GET_SHORT(format, 0), GET_SHORT(format, 2));
1135 sprintf(tmp, "\\trleft%d\\cellx%d ",
1136 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)),
1137 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0) + GET_SHORT(format, 2),
1138 table_width, 32767)));
1139 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1141 format += ncol * 4;
1144 for (nc = 0; nc < ncol; /**/)
1146 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
1147 if (!HLPFILE_RtfAddControl(rd, "\\pard")) goto done;
1148 if (in_table)
1150 nc = GET_SHORT(format, 0);
1151 if (nc == -1) break;
1152 format += 5;
1153 if (!HLPFILE_RtfAddControl(rd, "\\intbl")) goto done;
1155 else nc++;
1156 if (buf[0x14] == 0x01)
1157 format += 6;
1158 else
1159 format += 4;
1160 bits = GET_USHORT(format, 0); format += 2;
1161 if (bits & 0x0001) fetch_long(&format);
1162 if (bits & 0x0002)
1164 sprintf(tmp, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1165 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1167 if (bits & 0x0004)
1169 sprintf(tmp, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1170 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1172 if (bits & 0x0008)
1174 sprintf(tmp, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1175 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1177 if (bits & 0x0010)
1179 sprintf(tmp, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1180 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1182 if (bits & 0x0020)
1184 sprintf(tmp, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1185 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1187 if (bits & 0x0040)
1189 sprintf(tmp, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1190 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1192 if (bits & 0x0100)
1194 BYTE brdr = *format++;
1195 short w;
1197 if (brdr & 0x01 && !HLPFILE_RtfAddControl(rd, "\\box")) goto done;
1198 if (brdr & 0x02 && !HLPFILE_RtfAddControl(rd, "\\brdrt")) goto done;
1199 if (brdr & 0x04 && !HLPFILE_RtfAddControl(rd, "\\brdrl")) goto done;
1200 if (brdr & 0x08 && !HLPFILE_RtfAddControl(rd, "\\brdrb")) goto done;
1201 if (brdr & 0x10 && !HLPFILE_RtfAddControl(rd, "\\brdrr")) goto done;
1202 if (brdr & 0x20 && !HLPFILE_RtfAddControl(rd, "\\brdrth")) goto done;
1203 if (!(brdr & 0x20) && !HLPFILE_RtfAddControl(rd, "\\brdrs")) goto done;
1204 if (brdr & 0x40 && !HLPFILE_RtfAddControl(rd, "\\brdrdb")) goto done;
1205 /* 0x80: unknown */
1207 w = GET_SHORT(format, 0); format += 2;
1208 if (w)
1210 sprintf(tmp, "\\brdrw%d", HLPFILE_HalfPointsToTwips(w));
1211 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1214 if (bits & 0x0200)
1216 int i, ntab = fetch_short(&format);
1217 unsigned tab, ts;
1218 const char* kind;
1220 for (i = 0; i < ntab; i++)
1222 tab = fetch_ushort(&format);
1223 ts = (tab & 0x4000) ? fetch_ushort(&format) : 0 /* left */;
1224 switch (ts)
1226 default: WINE_FIXME("Unknown tab style %x\n", ts);
1227 /* fall through */
1228 case 0: kind = ""; break;
1229 case 1: kind = "\\tqr"; break;
1230 case 2: kind = "\\tqc"; break;
1232 /* FIXME: do kind */
1233 sprintf(tmp, "%s\\tx%d",
1234 kind, HLPFILE_HalfPointsToTwips(tab & 0x3FFF));
1235 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1238 switch (bits & 0xc00)
1240 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1241 case 0: if (!HLPFILE_RtfAddControl(rd, "\\ql")) goto done; break;
1242 case 0x400: if (!HLPFILE_RtfAddControl(rd, "\\qr")) goto done; break;
1243 case 0x800: if (!HLPFILE_RtfAddControl(rd, "\\qc")) goto done; break;
1246 /* 0x1000 doesn't need space */
1247 if ((bits & 0x1000) && !HLPFILE_RtfAddControl(rd, "\\keep")) goto done;
1248 if ((bits & 0xE080) != 0)
1249 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
1251 while (text < text_end && format < format_end)
1253 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
1254 textsize = strlen(text);
1255 if (textsize)
1257 if (rd->force_color)
1259 if ((rd->current_link->cookie == hlp_link_popup) ?
1260 !HLPFILE_RtfAddControl(rd, "{\\uld\\cf1") :
1261 !HLPFILE_RtfAddControl(rd, "{\\ul\\cf1")) goto done;
1263 if (!HLPFILE_RtfAddText(rd, text)) goto done;
1264 if (rd->force_color && !HLPFILE_RtfAddControl(rd, "}")) goto done;
1265 rd->char_pos += textsize;
1267 /* else: null text, keep on storing attributes */
1268 text += textsize + 1;
1270 if (*format == 0xff)
1272 format++;
1273 break;
1276 WINE_TRACE("format=%02x\n", *format);
1277 switch (*format)
1279 case 0x20:
1280 WINE_FIXME("NIY20\n");
1281 format += 5;
1282 break;
1284 case 0x21:
1285 WINE_FIXME("NIY21\n");
1286 format += 3;
1287 break;
1289 case 0x80:
1291 unsigned font = GET_USHORT(format, 1);
1292 unsigned fs;
1294 WINE_TRACE("Changing font to %d\n", font);
1295 format += 3;
1296 /* Font size in hlpfile is given in the same units as
1297 rtf control word \fs uses (half-points). */
1298 switch (rd->font_scale)
1300 case 0: fs = page->file->fonts[font].LogFont.lfHeight - 4; break;
1301 default:
1302 case 1: fs = page->file->fonts[font].LogFont.lfHeight; break;
1303 case 2: fs = page->file->fonts[font].LogFont.lfHeight + 4; break;
1305 /* FIXME: missing at least colors, also bold attribute looses information */
1307 sprintf(tmp, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1308 font, font + 2, fs,
1309 page->file->fonts[font].LogFont.lfWeight > 400 ? "\\b" : "\\b0",
1310 page->file->fonts[font].LogFont.lfItalic ? "\\i" : "\\i0",
1311 page->file->fonts[font].LogFont.lfUnderline ? "\\ul" : "\\ul0",
1312 page->file->fonts[font].LogFont.lfStrikeOut ? "\\strike" : "\\strike0");
1313 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1315 break;
1317 case 0x81:
1318 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1319 format += 1;
1320 rd->char_pos++;
1321 break;
1323 case 0x82:
1324 if (in_table)
1326 if (format[1] != 0xFF)
1328 if (!HLPFILE_RtfAddControl(rd, "\\par\\intbl")) goto done;
1330 else
1332 if (!HLPFILE_RtfAddControl(rd, "\\cell\\pard\\intbl")) goto done;
1335 else if (!HLPFILE_RtfAddControl(rd, "\\par")) goto done;
1336 format += 1;
1337 rd->char_pos++;
1338 break;
1340 case 0x83:
1341 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1342 format += 1;
1343 rd->char_pos++;
1344 break;
1346 #if 0
1347 case 0x84:
1348 format += 3;
1349 break;
1350 #endif
1352 case 0x86:
1353 case 0x87:
1354 case 0x88:
1356 BYTE type = format[1];
1357 long size;
1359 /* FIXME: we don't use 'BYTE pos = (*format - 0x86);' for the image position */
1360 format += 2;
1361 size = fetch_long(&format);
1363 switch (type)
1365 case 0x22:
1366 fetch_ushort(&format); /* hot spot */
1367 /* fall thru */
1368 case 0x03:
1369 switch (GET_SHORT(format, 0))
1371 case 0:
1372 HLPFILE_RtfAddGfxByIndex(rd, page->file, GET_SHORT(format, 2));
1373 rd->char_pos++;
1374 break;
1375 case 1:
1376 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1377 GET_SHORT(format, 0),
1378 size, GET_SHORT(format, 2));
1379 HLPFILE_RtfAddGfxByAddr(rd, page->file, format + 2, size - 4);
1380 rd->char_pos++;
1381 break;
1382 default:
1383 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1384 break;
1386 break;
1387 case 0x05:
1388 WINE_FIXME("Got an embedded element %s\n", format + 6);
1389 break;
1390 default:
1391 WINE_FIXME("Got a type %d picture\n", type);
1392 break;
1394 format += size;
1396 break;
1398 case 0x89:
1399 format += 1;
1400 if (!rd->current_link)
1401 WINE_FIXME("No existing link\n");
1402 rd->current_link->cpMax = rd->char_pos;
1403 rd->current_link = NULL;
1404 rd->force_color = FALSE;
1405 break;
1407 case 0x8B:
1408 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1409 format += 1;
1410 rd->char_pos++;
1411 break;
1413 case 0x8C:
1414 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1415 /* FIXME: it could be that hypen is also in input stream !! */
1416 format += 1;
1417 rd->char_pos++;
1418 break;
1420 #if 0
1421 case 0xA9:
1422 format += 2;
1423 break;
1424 #endif
1426 case 0xC8:
1427 case 0xCC:
1428 WINE_TRACE("macro => %s\n", format + 3);
1429 HLPFILE_AllocLink(rd, hlp_link_macro, (const char*)format + 3,
1430 GET_USHORT(format, 1), 0, !(*format & 4), -1);
1431 format += 3 + GET_USHORT(format, 1);
1432 break;
1434 case 0xE0:
1435 case 0xE1:
1436 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1437 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1438 page->file->lpszPath, -1, GET_UINT(format, 1), 1, -1);
1441 format += 5;
1442 break;
1444 case 0xE2:
1445 case 0xE3:
1446 case 0xE6:
1447 case 0xE7:
1448 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1449 page->file->lpszPath, -1, GET_UINT(format, 1),
1450 !(*format & 4), -1);
1451 format += 5;
1452 break;
1454 case 0xEA:
1455 case 0xEB:
1456 case 0xEE:
1457 case 0xEF:
1459 char* ptr = (char*) format + 8;
1460 BYTE type = format[3];
1461 int wnd = -1;
1463 switch (type)
1465 case 1:
1466 wnd = *ptr;
1467 /* fall through */
1468 case 0:
1469 ptr = page->file->lpszPath;
1470 break;
1471 case 6:
1472 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1474 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1476 if (wnd == -1)
1477 WINE_WARN("Couldn't find window info for %s\n", ptr);
1478 ptr += strlen(ptr) + 1;
1479 /* fall through */
1480 case 4:
1481 break;
1482 default:
1483 WINE_WARN("Unknown link type %d\n", type);
1484 break;
1486 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1487 ptr, -1, GET_UINT(format, 4), !(*format & 4), wnd);
1489 format += 3 + GET_USHORT(format, 1);
1490 break;
1492 default:
1493 WINE_WARN("format %02x\n", *format);
1494 format++;
1498 if (in_table)
1500 if (!HLPFILE_RtfAddControl(rd, "\\row\\par\\pard\\plain")) goto done;
1501 rd->char_pos += 2;
1503 ret = TRUE;
1504 done:
1506 HeapFree(GetProcessHeap(), 0, text_base);
1507 return ret;
1510 /******************************************************************
1511 * HLPFILE_BrowsePage
1514 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page, struct RtfData* rd,
1515 unsigned font_scale, unsigned relative)
1517 HLPFILE *hlpfile = page->file;
1518 BYTE *buf, *end;
1519 DWORD ref = page->reference;
1520 unsigned index, old_index = -1, offset, count = 0, offs = 0;
1521 unsigned cpg, parlen;
1522 char tmp[1024];
1523 const char* ck = NULL;
1525 rd->in_text = TRUE;
1526 rd->data = rd->ptr = HeapAlloc(GetProcessHeap(), 0, rd->allocated = 32768);
1527 rd->char_pos = 0;
1528 rd->first_link = rd->current_link = NULL;
1529 rd->force_color = FALSE;
1530 rd->font_scale = font_scale;
1531 rd->relative = relative;
1532 rd->char_pos_rel = 0;
1534 switch (hlpfile->charset)
1536 case DEFAULT_CHARSET:
1537 case ANSI_CHARSET: cpg = 1252; break;
1538 case SHIFTJIS_CHARSET: cpg = 932; break;
1539 case HANGEUL_CHARSET: cpg = 949; break;
1540 case GB2312_CHARSET: cpg = 936; break;
1541 case CHINESEBIG5_CHARSET: cpg = 950; break;
1542 case GREEK_CHARSET: cpg = 1253; break;
1543 case TURKISH_CHARSET: cpg = 1254; break;
1544 case HEBREW_CHARSET: cpg = 1255; break;
1545 case ARABIC_CHARSET: cpg = 1256; break;
1546 case BALTIC_CHARSET: cpg = 1257; break;
1547 case VIETNAMESE_CHARSET: cpg = 1258; break;
1548 case RUSSIAN_CHARSET: cpg = 1251; break;
1549 case EE_CHARSET: cpg = 1250; break;
1550 case THAI_CHARSET: cpg = 874; break;
1551 case JOHAB_CHARSET: cpg = 1361; break;
1552 case MAC_CHARSET: ck = "mac"; break;
1553 default:
1554 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1555 cpg = 1252;
1557 if (ck)
1559 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1560 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1562 else
1564 sprintf(tmp, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg);
1565 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1568 /* generate font table */
1569 if (!HLPFILE_RtfAddControl(rd, "{\\fonttbl")) return FALSE;
1570 for (index = 0; index < hlpfile->numFonts; index++)
1572 const char* family;
1573 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1575 case FF_MODERN: family = "modern"; break;
1576 case FF_ROMAN: family = "roman"; break;
1577 case FF_SWISS: family = "swiss"; break;
1578 case FF_SCRIPT: family = "script"; break;
1579 case FF_DECORATIVE: family = "decor"; break;
1580 default: family = "nil"; break;
1582 sprintf(tmp, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1583 index, family,
1584 hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0x0F,
1585 hlpfile->fonts[index].LogFont.lfCharSet,
1586 hlpfile->fonts[index].LogFont.lfFaceName);
1587 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1589 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1590 /* generate color table */
1591 if (!HLPFILE_RtfAddControl(rd, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE;
1592 for (index = 0; index < hlpfile->numFonts; index++)
1594 const char* family;
1595 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1597 case FF_MODERN: family = "modern"; break;
1598 case FF_ROMAN: family = "roman"; break;
1599 case FF_SWISS: family = "swiss"; break;
1600 case FF_SCRIPT: family = "script"; break;
1601 case FF_DECORATIVE: family = "decor"; break;
1602 default: family = "nil"; break;
1604 sprintf(tmp, "\\red%d\\green%d\\blue%d;",
1605 GetRValue(hlpfile->fonts[index].color),
1606 GetGValue(hlpfile->fonts[index].color),
1607 GetBValue(hlpfile->fonts[index].color));
1608 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1610 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1614 if (hlpfile->version <= 16)
1616 index = (ref - 0x0C) / hlpfile->dsize;
1617 offset = (ref - 0x0C) % hlpfile->dsize;
1619 else
1621 index = (ref - 0x0C) >> 14;
1622 offset = (ref - 0x0C) & 0x3FFF;
1625 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
1627 /* we jumped to the next block, adjust pointers */
1628 ref -= 12;
1629 offset -= 12;
1632 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1633 buf = hlpfile->topic_map[index] + offset;
1634 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1635 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1636 if (index != old_index) {offs = 0; old_index = index;}
1638 switch (buf[0x14])
1640 case 0x02:
1641 if (count++) goto done;
1642 break;
1643 case 0x01:
1644 case 0x20:
1645 case 0x23:
1646 if (!HLPFILE_BrowseParagraph(page, rd, buf, end, &parlen)) return FALSE;
1647 if (relative > index * 0x8000 + offs)
1648 rd->char_pos_rel = rd->char_pos;
1649 offs += parlen;
1650 break;
1651 default:
1652 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1654 if (hlpfile->version <= 16)
1656 ref += GET_UINT(buf, 0xc);
1657 if (GET_UINT(buf, 0xc) == 0)
1658 break;
1660 else
1661 ref = GET_UINT(buf, 0xc);
1662 } while (ref != 0xffffffff);
1663 done:
1664 page->first_link = rd->first_link;
1665 return HLPFILE_RtfAddControl(rd, "}");
1668 /******************************************************************
1669 * HLPFILE_ReadFont
1673 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1675 BYTE *ref, *end;
1676 unsigned i, len, idx;
1677 unsigned face_num, dscr_num, face_offset, dscr_offset;
1678 BYTE flag, family;
1680 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1682 WINE_WARN("no subfile FONT\n");
1683 hlpfile->numFonts = 0;
1684 hlpfile->fonts = NULL;
1685 return FALSE;
1688 ref += 9;
1690 face_num = GET_USHORT(ref, 0);
1691 dscr_num = GET_USHORT(ref, 2);
1692 face_offset = GET_USHORT(ref, 4);
1693 dscr_offset = GET_USHORT(ref, 6);
1695 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1696 face_num, face_offset, dscr_num, dscr_offset);
1698 hlpfile->numFonts = dscr_num;
1699 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1701 len = (dscr_offset - face_offset) / face_num;
1702 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1703 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1704 for (i = 0; i < dscr_num; i++)
1706 flag = ref[dscr_offset + i * 11 + 0];
1707 family = ref[dscr_offset + i * 11 + 2];
1709 hlpfile->fonts[i].LogFont.lfHeight = ref[dscr_offset + i * 11 + 1];
1710 hlpfile->fonts[i].LogFont.lfWidth = 0;
1711 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1712 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1713 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1714 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1715 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1716 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1717 hlpfile->fonts[i].LogFont.lfCharSet = hlpfile->charset;
1718 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1719 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1720 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1721 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1723 switch (family)
1725 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1726 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1727 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1728 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1729 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1730 default: WINE_FIXME("Unknown family %u\n", family);
1732 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1734 if (idx < face_num)
1736 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1737 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1739 else
1741 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1742 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1744 hlpfile->fonts[i].hFont = 0;
1745 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1746 ref[dscr_offset + i * 11 + 6],
1747 ref[dscr_offset + i * 11 + 7]);
1748 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1749 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1750 i, flag,
1751 X(0, "bold"),
1752 X(1, "italic"),
1753 X(2, "underline"),
1754 X(3, "strikeOut"),
1755 X(4, "dblUnderline"),
1756 X(5, "smallCaps"),
1757 ref[dscr_offset + i * 11 + 1],
1758 family,
1759 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1760 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1762 return TRUE;
1765 /***********************************************************************
1767 * HLPFILE_ReadFileToBuffer
1769 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1771 BYTE header[16], dummy[1];
1773 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1775 /* sanity checks */
1776 if (GET_UINT(header, 0) != 0x00035F3F)
1777 {WINE_WARN("wrong header\n"); return FALSE;};
1779 hlpfile->file_buffer_size = GET_UINT(header, 12);
1780 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1781 if (!hlpfile->file_buffer) return FALSE;
1783 memcpy(hlpfile->file_buffer, header, 16);
1784 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1785 {WINE_WARN("filesize1\n"); return FALSE;};
1787 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1789 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1791 return TRUE;
1794 /**************************************************************************
1795 * comp_FindSubFile
1797 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1800 static int comp_FindSubFile(void *p, const void *key,
1801 int leaf, void** next)
1803 *next = (char *)p+strlen(p)+(leaf?5:3);
1804 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1805 return strcmp(p, key);
1808 /***********************************************************************
1810 * HLPFILE_FindSubFile
1812 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
1814 BYTE *ptr;
1816 WINE_TRACE("looking for file '%s'\n", name);
1817 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
1818 name, comp_FindSubFile);
1819 if (!ptr) return FALSE;
1820 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
1821 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
1823 WINE_ERR("internal file %s does not fit\n", name);
1824 return FALSE;
1826 *subend = *subbuf + GET_UINT(*subbuf, 0);
1827 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
1829 WINE_ERR("internal file %s does not fit\n", name);
1830 return FALSE;
1832 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1834 WINE_ERR("invalid size provided for internal file %s\n", name);
1835 return FALSE;
1837 return TRUE;
1840 /***********************************************************************
1842 * HLPFILE_SystemCommands
1844 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1846 BYTE *buf, *ptr, *end;
1847 HLPFILE_MACRO *macro, **m;
1848 LPSTR p;
1849 unsigned short magic, minor, major, flags;
1851 hlpfile->lpszTitle = NULL;
1853 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1855 magic = GET_USHORT(buf + 9, 0);
1856 minor = GET_USHORT(buf + 9, 2);
1857 major = GET_USHORT(buf + 9, 4);
1858 /* gen date on 4 bytes */
1859 flags = GET_USHORT(buf + 9, 10);
1860 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1861 magic, major, minor, flags);
1862 if (magic != 0x036C || major != 1)
1863 {WINE_WARN("Wrong system header\n"); return FALSE;}
1864 if (minor <= 16)
1866 hlpfile->tbsize = 0x800;
1867 hlpfile->compressed = 0;
1869 else if (flags == 0)
1871 hlpfile->tbsize = 0x1000;
1872 hlpfile->compressed = 0;
1874 else if (flags == 4)
1876 hlpfile->tbsize = 0x1000;
1877 hlpfile->compressed = 1;
1879 else
1881 hlpfile->tbsize = 0x800;
1882 hlpfile->compressed = 1;
1885 if (hlpfile->compressed)
1886 hlpfile->dsize = 0x4000;
1887 else
1888 hlpfile->dsize = hlpfile->tbsize - 0x0C;
1890 hlpfile->version = minor;
1891 hlpfile->flags = flags;
1892 hlpfile->charset = DEFAULT_CHARSET;
1894 if (hlpfile->version <= 16)
1896 char *str = (char*)buf + 0x15;
1898 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1899 if (!hlpfile->lpszTitle) return FALSE;
1900 lstrcpy(hlpfile->lpszTitle, str);
1901 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1902 /* Nothing more to parse */
1903 return TRUE;
1905 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1907 char *str = (char*) ptr + 4;
1908 switch (GET_USHORT(ptr, 0))
1910 case 1:
1911 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1912 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1913 if (!hlpfile->lpszTitle) return FALSE;
1914 lstrcpy(hlpfile->lpszTitle, str);
1915 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1916 break;
1918 case 2:
1919 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1920 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1921 if (!hlpfile->lpszCopyright) return FALSE;
1922 lstrcpy(hlpfile->lpszCopyright, str);
1923 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1924 break;
1926 case 3:
1927 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1928 hlpfile->contents_start = GET_UINT(ptr, 4);
1929 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1930 break;
1932 case 4:
1933 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1934 if (!macro) break;
1935 p = (char*)macro + sizeof(HLPFILE_MACRO);
1936 lstrcpy(p, str);
1937 macro->lpszMacro = p;
1938 macro->next = 0;
1939 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1940 *m = macro;
1941 break;
1943 case 5:
1944 if (GET_USHORT(ptr, 4 + 4) != 1)
1945 WINE_FIXME("More than one icon, picking up first\n");
1946 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
1947 hlpfile->hIcon = CreateIconFromResourceEx(ptr + 4 + 0x16,
1948 GET_USHORT(ptr, 2) - 0x16, TRUE,
1949 0x30000, 0, 0, 0);
1950 break;
1952 case 6:
1953 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1955 if (hlpfile->windows)
1956 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1957 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1958 else
1959 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1960 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1962 if (hlpfile->windows)
1964 unsigned flags = GET_USHORT(ptr, 4);
1965 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1967 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1968 else wi->type[0] = '\0';
1969 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1970 else wi->name[0] = '\0';
1971 if (flags & 0x0004) strcpy(wi->caption, &str[21]);
1972 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1973 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1974 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1975 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1976 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1977 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1978 wi->win_style = WS_OVERLAPPEDWINDOW;
1979 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1980 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1981 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",
1982 flags & 0x0001 ? 'T' : 't',
1983 flags & 0x0002 ? 'N' : 'n',
1984 flags & 0x0004 ? 'C' : 'c',
1985 flags & 0x0008 ? 'X' : 'x',
1986 flags & 0x0010 ? 'Y' : 'y',
1987 flags & 0x0020 ? 'W' : 'w',
1988 flags & 0x0040 ? 'H' : 'h',
1989 flags & 0x0080 ? 'S' : 's',
1990 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1991 wi->size.cx, wi->size.cy);
1993 break;
1994 case 8:
1995 WINE_WARN("Citation: '%s'\n", ptr + 4);
1996 break;
1997 case 11:
1998 hlpfile->charset = ptr[4];
1999 WINE_TRACE("Charset: %d\n", hlpfile->charset);
2000 break;
2001 default:
2002 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
2005 if (!hlpfile->lpszTitle)
2006 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
2007 return TRUE;
2010 /***********************************************************************
2012 * HLPFILE_UncompressedLZ77_Size
2014 static INT HLPFILE_UncompressedLZ77_Size(const BYTE *ptr, const BYTE *end)
2016 int i, newsize = 0;
2018 while (ptr < end)
2020 int mask = *ptr++;
2021 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2023 if (mask & 1)
2025 int code = GET_USHORT(ptr, 0);
2026 int len = 3 + (code >> 12);
2027 newsize += len;
2028 ptr += 2;
2030 else newsize++, ptr++;
2034 return newsize;
2037 /***********************************************************************
2039 * HLPFILE_UncompressLZ77
2041 static BYTE *HLPFILE_UncompressLZ77(const BYTE *ptr, const BYTE *end, BYTE *newptr)
2043 int i;
2045 while (ptr < end)
2047 int mask = *ptr++;
2048 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2050 if (mask & 1)
2052 int code = GET_USHORT(ptr, 0);
2053 int len = 3 + (code >> 12);
2054 int offset = code & 0xfff;
2056 * We must copy byte-by-byte here. We cannot use memcpy nor
2057 * memmove here. Just example:
2058 * a[]={1,2,3,4,5,6,7,8,9,10}
2059 * newptr=a+2;
2060 * offset=1;
2061 * We expect:
2062 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
2064 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
2065 ptr += 2;
2067 else *newptr++ = *ptr++;
2071 return newptr;
2074 /***********************************************************************
2076 * HLPFILE_UncompressLZ77_Phrases
2078 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
2080 UINT i, num, dec_size, head_size;
2081 BYTE *buf, *end;
2083 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
2085 if (hlpfile->version <= 16)
2086 head_size = 13;
2087 else
2088 head_size = 17;
2090 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
2091 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
2093 if (hlpfile->version <= 16)
2094 dec_size = end - buf - 15 - 2 * num;
2095 else
2096 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
2098 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2099 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2100 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2102 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2103 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2104 return FALSE;
2107 for (i = 0; i <= num; i++)
2108 hlpfile->phrases_offsets[i] = GET_USHORT(buf, head_size + 2 * i) - 2 * num - 2;
2110 if (hlpfile->version <= 16)
2111 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
2112 else
2113 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
2115 hlpfile->hasPhrases = TRUE;
2116 return TRUE;
2119 /***********************************************************************
2121 * HLPFILE_Uncompress_Phrases40
2123 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
2125 UINT num;
2126 INT dec_size, cpr_size;
2127 BYTE *buf_idx, *end_idx;
2128 BYTE *buf_phs, *end_phs;
2129 long* ptr, mask = 0;
2130 unsigned int i;
2131 unsigned short bc, n;
2133 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
2134 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
2136 ptr = (long*)(buf_idx + 9 + 28);
2137 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
2138 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
2140 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2141 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2142 GET_UINT(buf_idx, 9 + 0),
2143 GET_UINT(buf_idx, 9 + 4),
2144 GET_UINT(buf_idx, 9 + 8),
2145 GET_UINT(buf_idx, 9 + 12),
2146 GET_UINT(buf_idx, 9 + 16),
2147 GET_UINT(buf_idx, 9 + 20),
2148 GET_USHORT(buf_idx, 9 + 24),
2149 GET_USHORT(buf_idx, 9 + 26));
2151 dec_size = GET_UINT(buf_idx, 9 + 12);
2152 cpr_size = GET_UINT(buf_idx, 9 + 16);
2154 if (dec_size != cpr_size &&
2155 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
2157 WINE_WARN("size mismatch %u %u\n",
2158 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2159 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2162 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2163 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2164 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2166 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2167 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2168 return FALSE;
2171 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
2173 hlpfile->phrases_offsets[0] = 0;
2174 for (i = 0; i < num; i++)
2176 for (n = 1; getbit(); n += 1 << bc);
2177 if (getbit()) n++;
2178 if (bc > 1 && getbit()) n += 2;
2179 if (bc > 2 && getbit()) n += 4;
2180 if (bc > 3 && getbit()) n += 8;
2181 if (bc > 4 && getbit()) n += 16;
2182 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
2184 #undef getbit
2186 if (dec_size == cpr_size)
2187 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2188 else
2189 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2191 hlpfile->hasPhrases40 = TRUE;
2192 return TRUE;
2195 /***********************************************************************
2197 * HLPFILE_Uncompress_Topic
2199 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
2201 BYTE *buf, *ptr, *end, *newptr;
2202 unsigned int i, newsize = 0;
2203 unsigned int topic_size;
2205 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
2206 {WINE_WARN("topic0\n"); return FALSE;}
2208 buf += 9; /* Skip file header */
2209 topic_size = end - buf;
2210 if (hlpfile->compressed)
2212 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2214 for (i = 0; i < hlpfile->topic_maplen; i++)
2216 ptr = buf + i * hlpfile->tbsize;
2218 /* I don't know why, it's necessary for printman.hlp */
2219 if (ptr + 0x44 > end) ptr = end - 0x44;
2221 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
2224 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2225 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
2226 if (!hlpfile->topic_map) return FALSE;
2227 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2228 hlpfile->topic_end = newptr + newsize;
2230 for (i = 0; i < hlpfile->topic_maplen; i++)
2232 ptr = buf + i * hlpfile->tbsize;
2233 if (ptr + 0x44 > end) ptr = end - 0x44;
2235 hlpfile->topic_map[i] = newptr;
2236 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
2239 else
2241 /* basically, we need to copy the TopicBlockSize byte pages
2242 * (removing the first 0x0C) in one single area in memory
2244 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2245 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2246 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
2247 if (!hlpfile->topic_map) return FALSE;
2248 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2249 hlpfile->topic_end = newptr + topic_size;
2251 for (i = 0; i < hlpfile->topic_maplen; i++)
2253 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
2254 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
2257 return TRUE;
2260 /***********************************************************************
2262 * HLPFILE_Uncompress2
2265 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
2267 BYTE *phptr, *phend;
2268 UINT code;
2269 UINT index;
2271 while (ptr < end && newptr < newend)
2273 if (!*ptr || *ptr >= 0x10)
2274 *newptr++ = *ptr++;
2275 else
2277 code = 0x100 * ptr[0] + ptr[1];
2278 index = (code - 0x100) / 2;
2280 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
2281 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
2283 if (newptr + (phend - phptr) > newend)
2285 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
2286 newptr, newend, (SIZE_T)(phend - phptr));
2287 return;
2289 memcpy(newptr, phptr, phend - phptr);
2290 newptr += phend - phptr;
2291 if (code & 1) *newptr++ = ' ';
2293 ptr += 2;
2296 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
2299 /******************************************************************
2300 * HLPFILE_Uncompress3
2304 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
2305 const BYTE* src, const BYTE* src_end)
2307 unsigned int idx, len;
2309 for (; src < src_end; src++)
2311 if ((*src & 1) == 0)
2313 idx = *src / 2;
2314 if (idx > hlpfile->num_phrases)
2316 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2317 len = 0;
2319 else
2321 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2322 if (dst + len <= dst_end)
2323 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2326 else if ((*src & 0x03) == 0x01)
2328 idx = (*src + 1) * 64;
2329 idx += *++src;
2330 if (idx > hlpfile->num_phrases)
2332 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2333 len = 0;
2335 else
2337 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2338 if (dst + len <= dst_end)
2339 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2342 else if ((*src & 0x07) == 0x03)
2344 len = (*src / 8) + 1;
2345 if (dst + len <= dst_end)
2346 memcpy(dst, src + 1, len);
2347 src += len;
2349 else
2351 len = (*src / 16) + 1;
2352 if (dst + len <= dst_end)
2353 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
2355 dst += len;
2358 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
2359 return TRUE;
2362 /******************************************************************
2363 * HLPFILE_UncompressRLE
2367 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE* dst, unsigned dstsz)
2369 BYTE ch;
2370 BYTE* sdst = dst + dstsz;
2372 while (src < end)
2374 ch = *src++;
2375 if (ch & 0x80)
2377 ch &= 0x7F;
2378 if (dst + ch <= sdst)
2379 memcpy(dst, src, ch);
2380 src += ch;
2382 else
2384 if (dst + ch <= sdst)
2385 memset(dst, (char)*src, ch);
2386 src++;
2388 dst += ch;
2390 if (dst != sdst)
2391 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
2392 (SIZE_T)(dst - (sdst - dstsz)), dstsz);
2395 /**************************************************************************
2396 * HLPFILE_BPTreeSearch
2398 * Searches for an element in B+ tree
2400 * PARAMS
2401 * buf [I] pointer to the embedded file structured as a B+ tree
2402 * key [I] pointer to data to find
2403 * comp [I] compare function
2405 * RETURNS
2406 * Pointer to block identified by key, or NULL if failure.
2409 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
2410 HLPFILE_BPTreeCompare comp)
2412 unsigned magic;
2413 unsigned page_size;
2414 unsigned cur_page;
2415 unsigned level;
2416 BYTE *pages, *ptr, *newptr;
2417 int i, entries;
2418 int ret;
2420 magic = GET_USHORT(buf, 9);
2421 if (magic != 0x293B)
2423 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2424 return NULL;
2426 page_size = GET_USHORT(buf, 9+4);
2427 cur_page = GET_USHORT(buf, 9+26);
2428 level = GET_USHORT(buf, 9+32);
2429 pages = buf + 9 + 38;
2430 while (--level > 0)
2432 ptr = pages + cur_page*page_size;
2433 entries = GET_SHORT(ptr, 2);
2434 ptr += 6;
2435 for (i = 0; i < entries; i++)
2437 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
2438 ptr = newptr;
2440 cur_page = GET_USHORT(ptr-2, 0);
2442 ptr = pages + cur_page*page_size;
2443 entries = GET_SHORT(ptr, 2);
2444 ptr += 8;
2445 for (i = 0; i < entries; i++)
2447 ret = comp(ptr, key, 1, (void **)&newptr);
2448 if (ret == 0) return ptr;
2449 if (ret > 0) return NULL;
2450 ptr = newptr;
2452 return NULL;
2455 /**************************************************************************
2456 * HLPFILE_BPTreeEnum
2458 * Enumerates elements in B+ tree.
2460 * PARAMS
2461 * buf [I] pointer to the embedded file structured as a B+ tree
2462 * cb [I] compare function
2463 * cookie [IO] cookie for cb function
2465 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
2467 unsigned magic;
2468 unsigned page_size;
2469 unsigned cur_page;
2470 unsigned level;
2471 BYTE *pages, *ptr, *newptr;
2472 int i, entries;
2474 magic = GET_USHORT(buf, 9);
2475 if (magic != 0x293B)
2477 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2478 return;
2480 page_size = GET_USHORT(buf, 9+4);
2481 cur_page = GET_USHORT(buf, 9+26);
2482 level = GET_USHORT(buf, 9+32);
2483 pages = buf + 9 + 38;
2484 while (--level > 0)
2486 ptr = pages + cur_page*page_size;
2487 cur_page = GET_USHORT(ptr, 4);
2489 while (cur_page != 0xFFFF)
2491 ptr = pages + cur_page*page_size;
2492 entries = GET_SHORT(ptr, 2);
2493 ptr += 8;
2494 for (i = 0; i < entries; i++)
2496 cb(ptr, (void **)&newptr, cookie);
2497 ptr = newptr;
2499 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
2504 /***********************************************************************
2506 * HLPFILE_GetContext
2508 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2510 BYTE *cbuf, *cend;
2511 unsigned clen;
2513 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2514 {WINE_WARN("context0\n"); return FALSE;}
2516 clen = cend - cbuf;
2517 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2518 if (!hlpfile->Context) return FALSE;
2519 memcpy(hlpfile->Context, cbuf, clen);
2521 return TRUE;
2524 /***********************************************************************
2526 * HLPFILE_GetKeywords
2528 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2530 BYTE *cbuf, *cend;
2531 unsigned clen;
2533 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2534 clen = cend - cbuf;
2535 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2536 if (!hlpfile->kwbtree) return FALSE;
2537 memcpy(hlpfile->kwbtree, cbuf, clen);
2539 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2541 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2542 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2543 return FALSE;
2545 clen = cend - cbuf;
2546 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2547 if (!hlpfile->kwdata)
2549 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2550 return FALSE;
2552 memcpy(hlpfile->kwdata, cbuf, clen);
2554 return TRUE;
2557 /***********************************************************************
2559 * HLPFILE_GetMap
2561 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2563 BYTE *cbuf, *cend;
2564 unsigned entries, i;
2566 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2567 {WINE_WARN("no map section\n"); return FALSE;}
2569 entries = GET_USHORT(cbuf, 9);
2570 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2571 if (!hlpfile->Map) return FALSE;
2572 hlpfile->wMapLen = entries;
2573 for (i = 0; i < entries; i++)
2575 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2576 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2578 return TRUE;
2581 /***********************************************************************
2583 * HLPFILE_GetTOMap
2585 static BOOL HLPFILE_GetTOMap(HLPFILE *hlpfile)
2587 BYTE *cbuf, *cend;
2588 unsigned clen;
2590 if (!HLPFILE_FindSubFile(hlpfile, "|TOMAP", &cbuf, &cend))
2591 {WINE_WARN("no tomap section\n"); return FALSE;}
2593 clen = cend - cbuf - 9;
2594 hlpfile->TOMap = HeapAlloc(GetProcessHeap(), 0, clen);
2595 if (!hlpfile->TOMap) return FALSE;
2596 memcpy(hlpfile->TOMap, cbuf+9, clen);
2597 hlpfile->wTOMapLen = clen/4;
2598 return TRUE;
2601 /***********************************************************************
2603 * DeleteMacro
2605 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2607 HLPFILE_MACRO* next;
2609 while (macro)
2611 next = macro->next;
2612 HeapFree(GetProcessHeap(), 0, macro);
2613 macro = next;
2617 /***********************************************************************
2619 * DeletePage
2621 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2623 HLPFILE_PAGE* next;
2625 while (page)
2627 next = page->next;
2628 HLPFILE_DeleteMacro(page->first_macro);
2629 HeapFree(GetProcessHeap(), 0, page);
2630 page = next;
2634 /***********************************************************************
2636 * HLPFILE_FreeHlpFile
2638 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2640 unsigned i;
2642 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2644 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2645 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2646 else first_hlpfile = hlpfile->next;
2648 if (hlpfile->numFonts)
2650 for (i = 0; i < hlpfile->numFonts; i++)
2652 DeleteObject(hlpfile->fonts[i].hFont);
2654 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2657 if (hlpfile->numBmps)
2659 for (i = 0; i < hlpfile->numBmps; i++)
2661 DeleteObject(hlpfile->bmps[i]);
2663 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2666 HLPFILE_DeletePage(hlpfile->first_page);
2667 HLPFILE_DeleteMacro(hlpfile->first_macro);
2669 DestroyIcon(hlpfile->hIcon);
2670 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2671 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2672 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2673 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2674 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2675 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2676 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2677 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2678 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2679 HeapFree(GetProcessHeap(), 0, hlpfile->help_on_file);
2680 HeapFree(GetProcessHeap(), 0, hlpfile);