winhelp: Emit proper RTF tokens for a bitmap.
[wine/wine-jacek.git] / programs / winhelp / hlpfile.c
blob6417c906887f28350acfb5fffa2710c99959f007
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 struct
56 UINT wFont;
57 UINT wIndent;
58 UINT wHSpace;
59 UINT wVSpace;
60 HLPFILE_LINK* link;
61 } attributes;
63 static BOOL HLPFILE_DoReadHlpFile(HLPFILE*, LPCSTR);
64 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE*, HFILE);
65 static BOOL HLPFILE_FindSubFile(HLPFILE*, LPCSTR, BYTE**, BYTE**);
66 static BOOL HLPFILE_SystemCommands(HLPFILE*);
67 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end);
68 static BYTE* HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr);
69 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE*);
70 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE*);
71 static BOOL HLPFILE_Uncompress_Topic(HLPFILE*);
72 static BOOL HLPFILE_GetContext(HLPFILE*);
73 static BOOL HLPFILE_GetKeywords(HLPFILE*);
74 static BOOL HLPFILE_GetMap(HLPFILE*);
75 static BOOL HLPFILE_AddPage(HLPFILE*, BYTE*, BYTE*, unsigned, unsigned);
76 static BOOL HLPFILE_SkipParagraph(HLPFILE*, BYTE *, BYTE*, unsigned*);
77 static void HLPFILE_Uncompress2(HLPFILE*, const BYTE*, const BYTE*, BYTE*, const BYTE*);
78 static BOOL HLPFILE_Uncompress3(HLPFILE*, char*, const char*, const BYTE*, const BYTE*);
79 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz);
80 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile);
82 /***********************************************************************
84 * HLPFILE_PageByNumber
86 static HLPFILE_PAGE *HLPFILE_PageByNumber(HLPFILE* hlpfile, UINT wNum)
88 HLPFILE_PAGE *page;
89 UINT temp = wNum;
91 WINE_TRACE("<%s>[%u]\n", hlpfile->lpszPath, wNum);
93 for (page = hlpfile->first_page; page && temp; page = page->next) temp--;
94 if (!page)
95 WINE_ERR("Page of number %u not found in file %s\n", wNum, hlpfile->lpszPath);
96 return page;
99 /******************************************************************
100 * HLPFILE_PageByOffset
104 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset, ULONG* relative)
106 HLPFILE_PAGE* page;
107 HLPFILE_PAGE* found;
109 if (!hlpfile) return 0;
111 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, offset);
113 if (offset == 0xFFFFFFFF) return NULL;
114 page = NULL;
116 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
118 if (page->offset <= offset && (!found || found->offset < page->offset))
120 *relative = offset - page->offset;
121 found = page;
124 if (!found)
125 WINE_ERR("Page of offset %u not found in file %s\n",
126 offset, hlpfile->lpszPath);
127 return found;
130 /**************************************************************************
131 * comp_PageByHash
133 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
136 static int comp_PageByHash(void *p, const void *key,
137 int leaf, void** next)
139 LONG lKey = (LONG_PTR)key;
140 LONG lTest = (INT)GET_UINT(p, 0);
142 *next = (char *)p+(leaf?8:6);
143 WINE_TRACE("Comparing '%d' with '%d'\n", lKey, lTest);
144 if (lTest < lKey) return -1;
145 if (lTest > lKey) return 1;
146 return 0;
149 /***********************************************************************
151 * HLPFILE_PageByHash
153 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash, ULONG* relative)
155 BYTE *ptr;
157 if (!hlpfile) return NULL;
158 if (!lHash) return HLPFILE_Contents(hlpfile, relative);
160 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lHash);
162 /* For win 3.0 files hash values are really page numbers */
163 if (hlpfile->version <= 16)
165 *relative = 0;
166 return HLPFILE_PageByNumber(hlpfile, lHash);
169 ptr = HLPFILE_BPTreeSearch(hlpfile->Context, LongToPtr(lHash), comp_PageByHash);
170 if (!ptr)
172 WINE_ERR("Page of hash %x not found in file %s\n", lHash, hlpfile->lpszPath);
173 return NULL;
176 return HLPFILE_PageByOffset(hlpfile, GET_UINT(ptr, 4), relative);
179 /***********************************************************************
181 * HLPFILE_PageByMap
183 HLPFILE_PAGE *HLPFILE_PageByMap(HLPFILE* hlpfile, LONG lMap, ULONG* relative)
185 unsigned int i;
187 if (!hlpfile) return 0;
189 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lMap);
191 for (i = 0; i < hlpfile->wMapLen; i++)
193 if (hlpfile->Map[i].lMap == lMap)
194 return HLPFILE_PageByOffset(hlpfile, hlpfile->Map[i].offset, relative);
197 WINE_ERR("Page of Map %x not found in file %s\n", lMap, hlpfile->lpszPath);
198 return NULL;
201 /***********************************************************************
203 * HLPFILE_Contents
205 HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile, ULONG* relative)
207 HLPFILE_PAGE* page = NULL;
209 if (!hlpfile) return NULL;
211 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start, relative);
212 if (!page)
214 page = hlpfile->first_page;
215 *relative = 0;
217 return page;
220 /***********************************************************************
222 * HLPFILE_Hash
224 LONG HLPFILE_Hash(LPCSTR lpszContext)
226 LONG lHash = 0;
227 CHAR c;
229 while ((c = *lpszContext++))
231 CHAR x = 0;
232 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
233 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
234 if (c >= '1' && c <= '9') x = c - '0';
235 if (c == '0') x = 10;
236 if (c == '.') x = 12;
237 if (c == '_') x = 13;
238 if (x) lHash = lHash * 43 + x;
240 return lHash;
243 /***********************************************************************
245 * HLPFILE_ReadHlpFile
247 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
249 HLPFILE* hlpfile;
251 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
253 if (!strcmp(lpszPath, hlpfile->lpszPath))
255 hlpfile->wRefCount++;
256 return hlpfile;
260 hlpfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
261 sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
262 if (!hlpfile) return 0;
264 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
265 hlpfile->contents_start = 0xFFFFFFFF;
266 hlpfile->next = first_hlpfile;
267 hlpfile->wRefCount = 1;
269 strcpy(hlpfile->lpszPath, lpszPath);
271 first_hlpfile = hlpfile;
272 if (hlpfile->next) hlpfile->next->prev = hlpfile;
274 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
276 HLPFILE_FreeHlpFile(hlpfile);
277 hlpfile = 0;
280 return hlpfile;
283 /***********************************************************************
285 * HLPFILE_DoReadHlpFile
287 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
289 BOOL ret;
290 HFILE hFile;
291 OFSTRUCT ofs;
292 BYTE* buf;
293 DWORD ref = 0x0C;
294 unsigned index, old_index, offset, len, offs;
296 hFile = OpenFile(lpszPath, &ofs, OF_READ);
297 if (hFile == HFILE_ERROR) return FALSE;
299 ret = HLPFILE_ReadFileToBuffer(hlpfile, hFile);
300 _lclose(hFile);
301 if (!ret) return FALSE;
303 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
305 /* load phrases support */
306 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
307 HLPFILE_Uncompress_Phrases40(hlpfile);
309 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
310 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
312 buf = hlpfile->topic_map[0];
313 old_index = -1;
314 offs = 0;
317 BYTE* end;
319 if (hlpfile->version <= 16)
321 index = (ref - 0x0C) / hlpfile->dsize;
322 offset = (ref - 0x0C) % hlpfile->dsize;
324 else
326 index = (ref - 0x0C) >> 14;
327 offset = (ref - 0x0C) & 0x3FFF;
330 if (hlpfile->version <= 16 && index != old_index && index != 0)
332 /* we jumped to the next block, adjust pointers */
333 ref -= 12;
334 offset -= 12;
337 WINE_TRACE("ref=%08x => [%u/%u]\n", ref, index, offset);
339 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
340 buf = hlpfile->topic_map[index] + offset;
341 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
342 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
343 if (index != old_index) {offs = 0; old_index = index;}
345 switch (buf[0x14])
347 case 0x02:
348 if (!HLPFILE_AddPage(hlpfile, buf, end, ref, index * 0x8000L + offs)) return FALSE;
349 break;
351 case 0x01:
352 case 0x20:
353 case 0x23:
354 if (!HLPFILE_SkipParagraph(hlpfile, buf, end, &len)) return FALSE;
355 offs += len;
356 break;
358 default:
359 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
362 if (hlpfile->version <= 16)
364 ref += GET_UINT(buf, 0xc);
365 if (GET_UINT(buf, 0xc) == 0)
366 break;
368 else
369 ref = GET_UINT(buf, 0xc);
370 } while (ref != 0xffffffff);
372 HLPFILE_GetKeywords(hlpfile);
373 HLPFILE_GetMap(hlpfile);
374 if (hlpfile->version <= 16) return TRUE;
375 return HLPFILE_GetContext(hlpfile);
378 /***********************************************************************
380 * HLPFILE_AddPage
382 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned ref, unsigned offset)
384 HLPFILE_PAGE* page;
385 BYTE* title;
386 UINT titlesize, blocksize, datalen;
387 char* ptr;
388 HLPFILE_MACRO*macro;
390 blocksize = GET_UINT(buf, 0);
391 datalen = GET_UINT(buf, 0x10);
392 title = buf + datalen;
393 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
395 titlesize = GET_UINT(buf, 4);
396 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
397 if (!page) return FALSE;
398 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
400 if (titlesize > blocksize - datalen)
402 /* need to decompress */
403 if (hlpfile->hasPhrases)
404 HLPFILE_Uncompress2(hlpfile, title, end, (BYTE*)page->lpszTitle, (BYTE*)page->lpszTitle + titlesize);
405 else if (hlpfile->hasPhrases40)
406 HLPFILE_Uncompress3(hlpfile, page->lpszTitle, page->lpszTitle + titlesize, title, end);
407 else
409 WINE_FIXME("Text size is too long, splitting\n");
410 titlesize = blocksize - datalen;
411 memcpy(page->lpszTitle, title, titlesize);
414 else
415 memcpy(page->lpszTitle, title, titlesize);
417 page->lpszTitle[titlesize] = '\0';
419 if (hlpfile->first_page)
421 hlpfile->last_page->next = page;
422 page->prev = hlpfile->last_page;
423 hlpfile->last_page = page;
425 else
427 hlpfile->first_page = page;
428 hlpfile->last_page = page;
429 page->prev = NULL;
432 page->file = hlpfile;
433 page->next = NULL;
434 page->first_paragraph = NULL;
435 page->first_macro = NULL;
436 page->first_link = NULL;
437 page->wNumber = GET_UINT(buf, 0x21);
438 page->offset = offset;
439 page->reference = ref;
441 page->browse_bwd = GET_UINT(buf, 0x19);
442 page->browse_fwd = GET_UINT(buf, 0x1D);
444 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
445 page->wNumber, page->lpszTitle,
446 page->browse_bwd, page->offset, page->browse_fwd);
448 memset(&attributes, 0, sizeof(attributes));
450 /* now load macros */
451 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
452 while (ptr < page->lpszTitle + titlesize)
454 unsigned len = strlen(ptr);
455 char* macro_str;
457 WINE_TRACE("macro: %s\n", ptr);
458 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
459 macro->lpszMacro = macro_str = (char*)(macro + 1);
460 memcpy(macro_str, ptr, len + 1);
461 /* FIXME: shall we really link macro in reverse order ??
462 * may produce strange results when played at page opening
464 macro->next = page->first_macro;
465 page->first_macro = macro;
466 ptr += len + 1;
469 return TRUE;
472 static long fetch_long(BYTE** ptr)
474 long ret;
476 if (*(*ptr) & 1)
478 ret = (*(unsigned long*)(*ptr) - 0x80000000L) / 2;
479 (*ptr) += 4;
481 else
483 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
484 (*ptr) += 2;
487 return ret;
490 static unsigned long fetch_ulong(BYTE** ptr)
492 unsigned long ret;
494 if (*(*ptr) & 1)
496 ret = *(unsigned long*)(*ptr) / 2;
497 (*ptr) += 4;
499 else
501 ret = *(unsigned short*)(*ptr) / 2;
502 (*ptr) += 2;
504 return ret;
507 static short fetch_short(BYTE** ptr)
509 short ret;
511 if (*(*ptr) & 1)
513 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
514 (*ptr) += 2;
516 else
518 ret = (*(unsigned char*)(*ptr) - 0x80) / 2;
519 (*ptr)++;
521 return ret;
524 static unsigned short fetch_ushort(BYTE** ptr)
526 unsigned short ret;
528 if (*(*ptr) & 1)
530 ret = *(unsigned short*)(*ptr) / 2;
531 (*ptr) += 2;
533 else
535 ret = *(unsigned char*)(*ptr) / 2;
536 (*ptr)++;
538 return ret;
541 /***********************************************************************
543 * HLPFILE_SkipParagraph
545 static BOOL HLPFILE_SkipParagraph(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned* len)
547 BYTE *tmp;
549 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
550 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
552 tmp = buf + 0x15;
553 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
555 fetch_long(&tmp);
556 *len = fetch_ushort(&tmp);
558 else *len = end-buf-15;
560 return TRUE;
563 /******************************************************************
564 * HLPFILE_DecompressGfx
566 * Decompress the data part of a bitmap or a metafile
568 static BYTE* HLPFILE_DecompressGfx(BYTE* src, unsigned csz, unsigned sz, BYTE packing)
570 BYTE* dst;
571 BYTE* tmp;
572 BYTE* tmp2;
573 unsigned sz77;
575 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
577 switch (packing)
579 case 0: /* uncompressed */
580 if (sz != csz)
581 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
582 dst = src;
583 break;
584 case 1: /* RunLen */
585 tmp = dst = HeapAlloc(GetProcessHeap(), 0, sz);
586 if (!dst) return NULL;
587 HLPFILE_UncompressRLE(src, src + csz, &tmp, sz);
588 if (tmp - dst != sz)
589 WINE_WARN("Bogus gfx sizes (RunLen): %lu/%u\n", (SIZE_T)(tmp - dst), sz);
590 break;
591 case 2: /* LZ77 */
592 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
593 dst = HeapAlloc(GetProcessHeap(), 0, sz77);
594 if (!dst) return NULL;
595 HLPFILE_UncompressLZ77(src, src + csz, dst);
596 if (sz77 != sz)
597 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
598 break;
599 case 3: /* LZ77 then RLE */
600 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
601 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
602 if (!tmp) return FALSE;
603 HLPFILE_UncompressLZ77(src, src + csz, tmp);
604 dst = tmp2 = HeapAlloc(GetProcessHeap(), 0, sz);
605 if (!dst)
607 HeapFree(GetProcessHeap(), 0, tmp);
608 return FALSE;
610 HLPFILE_UncompressRLE(tmp, tmp + sz77, &tmp2, sz);
611 if (tmp2 - dst != sz)
612 WINE_WARN("Bogus gfx sizes (LZ77+RunLen): %lu / %u\n", (SIZE_T)(tmp2 - dst), sz);
613 HeapFree(GetProcessHeap(), 0, tmp);
614 break;
615 default:
616 WINE_FIXME("Unsupported packing %u\n", packing);
617 return NULL;
619 return dst;
622 static BOOL HLPFILE_RtfAddRawString(struct RtfData* rd, const char* str, size_t sz)
624 if (!rd) return TRUE; /* FIXME: TEMP */
625 if (rd->ptr + sz >= rd->data + rd->allocated)
627 char* new = HeapReAlloc(GetProcessHeap(), 0, rd->data, rd->allocated *= 2);
628 if (!new) return FALSE;
629 rd->ptr = new + (rd->ptr - rd->data);
630 rd->data = new;
632 memcpy(rd->ptr, str, sz);
633 rd->ptr += sz;
635 return TRUE;
638 static BOOL HLPFILE_RtfAddControl(struct RtfData* rd, const char* str)
640 if (!rd) return TRUE; /* FIXME: TEMP */
641 if (*str == '\\' || *str == '{') rd->in_text = FALSE;
642 else if (*str == '}') rd->in_text = TRUE;
643 return HLPFILE_RtfAddRawString(rd, str, strlen(str));
646 static BOOL HLPFILE_RtfAddText(struct RtfData* rd, const char* str)
648 const char* p;
649 const char* last;
650 const char* replace;
651 unsigned rlen;
653 if (!rd) return TRUE; /* FIXME: TEMP */
654 if (!rd->in_text)
656 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
657 rd->in_text = TRUE;
659 for (last = p = str; *p; p++)
661 if (*p < 0) /* escape non ASCII chars */
663 static char xx[8];
664 rlen = sprintf(xx, "\\'%x", *(const BYTE*)p);
665 replace = xx;
667 else switch (*p)
669 case '{': rlen = 2; replace = "\\{"; break;
670 case '}': rlen = 2; replace = "\\}"; break;
671 case '\\': rlen = 2; replace = "\\\\"; break;
672 default: continue;
674 if ((p != last && !HLPFILE_RtfAddRawString(rd, last, p - last)) ||
675 !HLPFILE_RtfAddRawString(rd, replace, rlen)) return FALSE;
676 last = p + 1;
678 return HLPFILE_RtfAddRawString(rd, last, p - last);
681 /******************************************************************
682 * RtfAddHexBytes
685 static BOOL HLPFILE_RtfAddHexBytes(struct RtfData* rd, const void* _ptr, unsigned sz)
687 char tmp[512];
688 unsigned i, step;
689 const BYTE* ptr = _ptr;
690 static const char* _2hex = "0123456789abcdef";
692 if (!rd) return TRUE; /* FIXME: TEMP */
693 if (!rd->in_text)
695 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
696 rd->in_text = TRUE;
698 for (; sz; sz -= step)
700 step = min(256, sz);
701 for (i = 0; i < step; i++)
703 tmp[2 * i + 0] = _2hex[*ptr >> 4];
704 tmp[2 * i + 1] = _2hex[*ptr++ & 0xF];
706 if (!HLPFILE_RtfAddRawString(rd, tmp, 2 * step)) return FALSE;
708 return TRUE;
711 /******************************************************************
712 * HLPFILE_RtfAddBitmap
715 static BOOL HLPFILE_RtfAddBitmap(struct RtfData* rd, BYTE* beg, BYTE type, BYTE pack)
717 BYTE* ptr;
718 BYTE* pict_beg;
719 BITMAPINFO* bi;
720 unsigned long off, csz;
721 unsigned nc = 0;
722 BOOL ret = FALSE;
723 char tmp[256];
725 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
726 if (!bi) return FALSE;
728 ptr = beg + 2; /* for type and pack */
730 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
731 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
732 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
733 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
734 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
735 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
736 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
737 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
738 bi->bmiHeader.biClrImportant = fetch_ulong(&ptr);
739 bi->bmiHeader.biCompression = BI_RGB;
740 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
741 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
742 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
743 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
744 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
745 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
747 csz = fetch_ulong(&ptr);
748 fetch_ulong(&ptr); /* hotspot size */
750 off = GET_UINT(ptr, 0); ptr += 4;
751 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
753 /* now read palette info */
754 if (type == 0x06)
756 unsigned i;
758 nc = bi->bmiHeader.biClrUsed;
759 /* not quite right, especially for bitfields type of compression */
760 if (!nc && bi->bmiHeader.biBitCount <= 8)
761 nc = 1 << bi->bmiHeader.biBitCount;
763 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
764 if (!bi) return FALSE;
765 for (i = 0; i < nc; i++)
767 bi->bmiColors[i].rgbBlue = ptr[0];
768 bi->bmiColors[i].rgbGreen = ptr[1];
769 bi->bmiColors[i].rgbRed = ptr[2];
770 bi->bmiColors[i].rgbReserved = 0;
771 ptr += 4;
774 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
777 if (!HLPFILE_RtfAddControl(rd, "{\\pict")) goto done;
778 if (type == 0x06)
780 sprintf(tmp, "\\dibitmap0\\picw%d\\pich%d",
781 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
782 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
783 if (!HLPFILE_RtfAddHexBytes(rd, bi, sizeof(*bi) + nc * sizeof(RGBQUAD))) goto done;
785 else
787 sprintf(tmp, "\\wbitmap0\\wbmbitspixel%d\\wbmplanes%d\\picw%d\\pich%d",
788 bi->bmiHeader.biBitCount, bi->bmiHeader.biPlanes,
789 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
790 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
792 if (!HLPFILE_RtfAddHexBytes(rd, pict_beg, bi->bmiHeader.biSizeImage)) goto done;
793 if (!HLPFILE_RtfAddControl(rd, "}")) goto done;
795 ret = TRUE;
796 done:
797 HeapFree(GetProcessHeap(), 0, bi);
798 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
800 return ret;
803 /******************************************************************
804 * HLPFILE_RtfAddMetaFile
807 static BOOL HLPFILE_RtfAddMetaFile(struct RtfData* rd, BYTE* beg, BYTE pack)
809 return TRUE;
812 /******************************************************************
813 * HLPFILE_RtfAddGfxByAddr
816 static BOOL HLPFILE_RtfAddGfxByAddr(struct RtfData* rd, HLPFILE *hlpfile,
817 BYTE* ref, unsigned long size)
819 unsigned i, numpict;
821 numpict = GET_USHORT(ref, 2);
822 WINE_TRACE("Got picture magic=%04x #=%d\n", GET_USHORT(ref, 0), numpict);
824 for (i = 0; i < numpict; i++)
826 BYTE* beg;
827 BYTE* ptr;
828 BYTE type, pack;
830 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
831 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
833 type = *ptr++;
834 pack = *ptr++;
836 switch (type)
838 case 5: /* device dependent bmp */
839 case 6: /* device independent bmp */
840 HLPFILE_RtfAddBitmap(rd, beg, type, pack);
841 break;
842 case 8:
843 HLPFILE_RtfAddMetaFile(rd, beg, pack);
844 break;
845 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
848 /* FIXME: hotspots */
850 /* FIXME: implement support for multiple picture format */
851 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
852 break;
854 return TRUE;
857 /******************************************************************
858 * HLPFILE_RtfAddGfxByIndex
862 static BOOL HLPFILE_RtfAddGfxByIndex(struct RtfData* rd, HLPFILE *hlpfile,
863 unsigned index)
865 char tmp[16];
866 BYTE *ref, *end;
868 WINE_TRACE("Loading picture #%d\n", index);
870 sprintf(tmp, "|bm%u", index);
872 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
874 ref += 9;
875 return HLPFILE_RtfAddGfxByAddr(rd, hlpfile, ref, end - ref);
878 /******************************************************************
879 * HLPFILE_LoadBitmap
883 static BOOL HLPFILE_LoadBitmap(BYTE* beg, BYTE type, BYTE pack,
884 HLPFILE_PARAGRAPH* paragraph)
886 BYTE* ptr;
887 BYTE* pict_beg;
888 BITMAPINFO* bi;
889 unsigned long off, csz;
890 HDC hdc;
892 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
893 if (!bi) return FALSE;
895 ptr = beg + 2; /* for type and pack */
897 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
898 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
899 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
900 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
901 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
902 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
903 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
904 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
905 bi->bmiHeader.biClrImportant = fetch_ulong(&ptr);
906 bi->bmiHeader.biCompression = BI_RGB;
907 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
908 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
909 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
910 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
911 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
912 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
914 csz = fetch_ulong(&ptr);
915 fetch_ulong(&ptr); /* hotspot size */
917 off = GET_UINT(ptr, 0); ptr += 4;
918 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
920 /* now read palette info */
921 if (type == 0x06)
923 unsigned nc = bi->bmiHeader.biClrUsed;
924 unsigned i;
926 /* not quite right, especially for bitfields type of compression */
927 if (!nc && bi->bmiHeader.biBitCount <= 8)
928 nc = 1 << bi->bmiHeader.biBitCount;
930 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
931 if (!bi) return FALSE;
932 for (i = 0; i < nc; i++)
934 bi->bmiColors[i].rgbBlue = ptr[0];
935 bi->bmiColors[i].rgbGreen = ptr[1];
936 bi->bmiColors[i].rgbRed = ptr[2];
937 bi->bmiColors[i].rgbReserved = 0;
938 ptr += 4;
941 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
943 paragraph->u.gfx.u.bmp.hBitmap = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
944 CBM_INIT, pict_beg,
945 bi, DIB_RGB_COLORS);
946 ReleaseDC(0, hdc);
947 if (!paragraph->u.gfx.u.bmp.hBitmap)
948 WINE_ERR("Couldn't create bitmap\n");
950 HeapFree(GetProcessHeap(), 0, bi);
951 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
953 return TRUE;
956 /******************************************************************
957 * HLPFILE_LoadMetaFile
961 static BOOL HLPFILE_LoadMetaFile(BYTE* beg, BYTE pack, HLPFILE_PARAGRAPH* paragraph)
963 BYTE* ptr;
964 unsigned long size, csize;
965 unsigned long off, hsoff;
966 BYTE* bits;
967 LPMETAFILEPICT lpmfp;
969 WINE_TRACE("Loading metafile\n");
971 ptr = beg + 2; /* for type and pack */
973 lpmfp = &paragraph->u.gfx.u.mfp;
974 lpmfp->mm = fetch_ushort(&ptr); /* mapping mode */
976 lpmfp->xExt = GET_USHORT(ptr, 0);
977 lpmfp->yExt = GET_USHORT(ptr, 2);
978 ptr += 4;
980 size = fetch_ulong(&ptr); /* decompressed size */
981 csize = fetch_ulong(&ptr); /* compressed size */
982 fetch_ulong(&ptr); /* hotspot size */
983 off = GET_UINT(ptr, 0);
984 hsoff = GET_UINT(ptr, 4);
985 ptr += 8;
987 WINE_TRACE("sz=%lu csz=%lu (%d,%d) offs=%lu/%lu,%lu\n",
988 size, csize, lpmfp->xExt, lpmfp->yExt, off, (SIZE_T)(ptr - beg), hsoff);
990 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
991 if (!bits) return FALSE;
993 paragraph->cookie = para_metafile;
995 lpmfp->hMF = SetMetaFileBitsEx(size, bits);
997 if (!lpmfp->hMF)
998 WINE_FIXME("Couldn't load metafile\n");
1000 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
1002 return TRUE;
1005 /******************************************************************
1006 * HLPFILE_LoadGfxByAddr
1010 static BOOL HLPFILE_LoadGfxByAddr(HLPFILE *hlpfile, BYTE* ref,
1011 unsigned long size,
1012 HLPFILE_PARAGRAPH* paragraph)
1014 unsigned i, numpict;
1016 numpict = GET_USHORT(ref, 2);
1017 WINE_TRACE("Got picture magic=%04x #=%d\n",
1018 GET_USHORT(ref, 0), numpict);
1020 for (i = 0; i < numpict; i++)
1022 BYTE* beg;
1023 BYTE* ptr;
1024 BYTE type, pack;
1026 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
1027 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
1029 type = *ptr++;
1030 pack = *ptr++;
1032 switch (type)
1034 case 5: /* device dependent bmp */
1035 case 6: /* device independent bmp */
1036 HLPFILE_LoadBitmap(beg, type, pack, paragraph);
1037 break;
1038 case 8:
1039 HLPFILE_LoadMetaFile(beg, pack, paragraph);
1040 break;
1041 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
1044 /* FIXME: hotspots */
1046 /* FIXME: implement support for multiple picture format */
1047 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
1048 break;
1050 return TRUE;
1053 /******************************************************************
1054 * HLPFILE_LoadGfxByIndex
1058 static BOOL HLPFILE_LoadGfxByIndex(HLPFILE *hlpfile, unsigned index,
1059 HLPFILE_PARAGRAPH* paragraph)
1061 char tmp[16];
1062 BYTE *ref, *end;
1063 BOOL ret;
1065 WINE_TRACE("Loading picture #%d\n", index);
1067 if (index < hlpfile->numBmps && hlpfile->bmps[index] != NULL)
1069 paragraph->u.gfx.u.bmp.hBitmap = hlpfile->bmps[index];
1070 return TRUE;
1073 sprintf(tmp, "|bm%u", index);
1075 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
1077 ref += 9;
1079 ret = HLPFILE_LoadGfxByAddr(hlpfile, ref, end - ref, paragraph);
1081 /* cache bitmap */
1082 if (ret && paragraph->cookie == para_bitmap)
1084 if (index >= hlpfile->numBmps)
1086 hlpfile->numBmps = index + 1;
1087 if (hlpfile->bmps)
1088 hlpfile->bmps = HeapReAlloc(GetProcessHeap(), 0, hlpfile->bmps,
1089 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
1090 else
1091 hlpfile->bmps = HeapAlloc(GetProcessHeap(), 0,
1092 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
1095 hlpfile->bmps[index] = paragraph->u.gfx.u.bmp.hBitmap;
1097 return ret;
1100 /******************************************************************
1101 * HLPFILE_AllocLink
1105 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
1106 const char* str, unsigned len, LONG hash,
1107 unsigned clrChange, unsigned wnd)
1109 HLPFILE_LINK* link;
1110 char* link_str;
1112 /* FIXME: should build a string table for the attributes.link.lpszPath
1113 * they are reallocated for each link
1115 if (len == -1) len = strlen(str);
1116 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + len + 1);
1117 if (!link) return NULL;
1119 link->cookie = cookie;
1120 link->string = link_str = (char*)(link + 1);
1121 memcpy(link_str, str, len);
1122 link_str[len] = '\0';
1123 link->hash = hash;
1124 link->bClrChange = clrChange ? 1 : 0;
1125 link->window = wnd;
1126 link->wRefCount = 1;
1127 if (rd) {
1128 link->next = rd->first_link;
1129 rd->first_link = link;
1130 link->cpMin = rd->char_pos;
1131 link->cpMax = 0;
1132 rd->force_color = clrChange;
1133 link->wRefCount++;
1134 if (rd->current_link) WINE_FIXME("Pending link\n");
1135 rd->current_link = link;
1138 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
1139 link->cookie, link->string, link->hash, link->window);
1140 return link;
1143 unsigned HLPFILE_HalfPointsToTwips(unsigned pts)
1145 static unsigned logPxY;
1146 if (!logPxY)
1148 HDC hdc = GetDC(NULL);
1149 logPxY = GetDeviceCaps(hdc, LOGPIXELSY);
1150 ReleaseDC(NULL, hdc);
1152 return MulDiv(pts, 72 * 10, logPxY);
1155 /***********************************************************************
1157 * HLPFILE_BrowseParagraph
1159 static BOOL HLPFILE_BrowseParagraph(HLPFILE_PAGE* page, struct RtfData* rd, BYTE *buf, BYTE* end)
1161 HLPFILE_PARAGRAPH *paragraph, **paragraphptr;
1162 UINT textsize;
1163 BYTE *format, *format_end;
1164 char *text, *text_base, *text_end;
1165 long size, blocksize, datalen;
1166 unsigned short bits;
1167 unsigned nc, ncol = 1;
1168 BOOL in_table = FALSE;
1169 char tmp[256];
1170 BOOL ret = FALSE;
1172 for (paragraphptr = &page->first_paragraph; *paragraphptr;
1173 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
1175 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
1177 blocksize = GET_UINT(buf, 0);
1178 size = GET_UINT(buf, 0x4);
1179 datalen = GET_UINT(buf, 0x10);
1180 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
1181 if (!text) return FALSE;
1182 if (size > blocksize - datalen)
1184 /* need to decompress */
1185 if (page->file->hasPhrases)
1186 HLPFILE_Uncompress2(page->file, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
1187 else if (page->file->hasPhrases40)
1188 HLPFILE_Uncompress3(page->file, text, text + size, buf + datalen, end);
1189 else
1191 WINE_FIXME("Text size is too long, splitting\n");
1192 size = blocksize - datalen;
1193 memcpy(text, buf + datalen, size);
1196 else
1197 memcpy(text, buf + datalen, size);
1199 text_end = text + size;
1201 format = buf + 0x15;
1202 format_end = buf + GET_UINT(buf, 0x10);
1204 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
1206 fetch_long(&format);
1207 fetch_ushort(&format);
1210 if (buf[0x14] == 0x23)
1212 char type;
1214 in_table = TRUE;
1215 ncol = *format++;
1217 WINE_TRACE("#cols %u\n", ncol);
1218 type = *format++;
1219 if (type == 0 || type == 2)
1220 format += 2;
1221 format += ncol * 4;
1224 for (nc = 0; nc < ncol; /**/)
1226 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
1227 if (in_table)
1229 nc = GET_SHORT(format, 0);
1230 if (nc == -1) break;
1231 format += 5;
1233 else nc++;
1234 format += 4;
1235 bits = GET_USHORT(format, 0); format += 2;
1236 if (bits & 0x0001) fetch_long(&format);
1237 if (bits & 0x0002)
1239 sprintf(tmp, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1240 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1242 if (bits & 0x0004)
1244 sprintf(tmp, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1245 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1247 if (bits & 0x0008)
1249 sprintf(tmp, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1250 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1252 if (bits & 0x0010)
1254 sprintf(tmp, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1255 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1257 if (bits & 0x0020)
1259 sprintf(tmp, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1260 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1262 if (bits & 0x0040)
1264 sprintf(tmp, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1265 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1267 if (bits & 0x0100)
1269 BYTE brdr = *format++;
1270 short w;
1272 if (brdr & 0x01 && !HLPFILE_RtfAddControl(rd, "\\box")) goto done;
1273 if (brdr & 0x02 && !HLPFILE_RtfAddControl(rd, "\\brdrt")) goto done;
1274 if (brdr & 0x04 && !HLPFILE_RtfAddControl(rd, "\\brdrl")) goto done;
1275 if (brdr & 0x08 && !HLPFILE_RtfAddControl(rd, "\\brdrb")) goto done;
1276 if (brdr & 0x10 && !HLPFILE_RtfAddControl(rd, "\\brdrr")) goto done;
1277 if (brdr & 0x20 && !HLPFILE_RtfAddControl(rd, "\\brdrth")) goto done;
1278 if (!(brdr & 0x20) && !HLPFILE_RtfAddControl(rd, "\\brdrs")) goto done;
1279 if (brdr & 0x40 && !HLPFILE_RtfAddControl(rd, "\\brdrdb")) goto done;
1280 /* 0x80: unknown */
1282 w = GET_SHORT(format, 0); format += 2;
1283 if (w)
1285 sprintf(tmp, "\\brdrw%d", HLPFILE_HalfPointsToTwips(w));
1286 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1289 if (bits & 0x0200)
1291 int i, ntab = fetch_short(&format);
1292 unsigned tab, ts;
1293 const char* kind;
1295 for (i = 0; i < ntab; i++)
1297 tab = fetch_ushort(&format);
1298 ts = (tab & 0x4000) ? fetch_ushort(&format) : 0 /* left */;
1299 switch (ts)
1301 default: WINE_FIXME("Unknown tab style %x\n", ts);
1302 /* fall through */
1303 case 0: kind = ""; break;
1304 case 1: kind = "\\tqr"; break;
1305 case 2: kind = "\\tqc"; break;
1307 /* FIXME: do kind */
1308 sprintf(tmp, "%s\\tx%d",
1309 kind, HLPFILE_HalfPointsToTwips(tab & 0x3FFF));
1310 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1313 switch (bits & 0xc00)
1315 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1316 case 0: if (!HLPFILE_RtfAddControl(rd, "\\ql")) goto done; break;
1317 case 0x400: if (!HLPFILE_RtfAddControl(rd, "\\qr")) goto done; break;
1318 case 0x800: if (!HLPFILE_RtfAddControl(rd, "\\qc")) goto done; break;
1321 /* 0x1000 doesn't need space */
1322 if ((bits & 0x1000) && !HLPFILE_RtfAddControl(rd, "\\keep")) goto done;
1323 if ((bits & 0xE080) != 0)
1324 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
1326 while (text < text_end && format < format_end)
1328 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
1329 textsize = strlen(text);
1330 if (textsize)
1332 paragraph = HeapAlloc(GetProcessHeap(), 0,
1333 sizeof(HLPFILE_PARAGRAPH) + textsize + 1);
1334 if (!paragraph) return FALSE;
1335 *paragraphptr = paragraph;
1336 paragraphptr = &paragraph->next;
1338 paragraph->next = NULL;
1339 if (!rd)
1341 paragraph->link = attributes.link;
1342 if (paragraph->link) paragraph->link->wRefCount++;
1344 else paragraph->link = NULL;
1345 paragraph->cookie = para_normal_text;
1346 paragraph->u.text.wFont = attributes.wFont;
1347 paragraph->u.text.wVSpace = attributes.wVSpace;
1348 paragraph->u.text.wHSpace = attributes.wHSpace;
1349 paragraph->u.text.wIndent = attributes.wIndent;
1350 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
1351 strcpy(paragraph->u.text.lpszText, text);
1353 attributes.wVSpace = 0;
1354 attributes.wHSpace = 0;
1355 if (rd) /* FIXME: TEMP */ {
1356 if (rd->force_color)
1358 if ((rd->current_link->cookie == hlp_link_popup) ?
1359 !HLPFILE_RtfAddControl(rd, "{\\uld\\cf1") :
1360 !HLPFILE_RtfAddControl(rd, "{\\ul\\cf1")) goto done;
1362 if (!HLPFILE_RtfAddText(rd, text)) goto done;
1363 if (rd->force_color && !HLPFILE_RtfAddControl(rd, "}")) goto done;
1364 rd->char_pos += textsize;
1367 /* else: null text, keep on storing attributes */
1368 text += textsize + 1;
1370 if (*format == 0xff)
1372 format++;
1373 break;
1376 WINE_TRACE("format=%02x\n", *format);
1377 switch (*format)
1379 case 0x20:
1380 WINE_FIXME("NIY20\n");
1381 format += 5;
1382 break;
1384 case 0x21:
1385 WINE_FIXME("NIY21\n");
1386 format += 3;
1387 break;
1389 case 0x80:
1391 unsigned font = GET_USHORT(format, 1);
1392 unsigned fs;
1393 attributes.wFont = font;
1394 WINE_TRACE("Changing font to %d\n", attributes.wFont);
1395 format += 3;
1396 fs = (4 * page->file->fonts[font].LogFont.lfHeight - 3) / 5;
1397 /* FIXME: missing at least colors, also bold attribute looses information */
1399 sprintf(tmp, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1400 font, font + 2, fs,
1401 page->file->fonts[font].LogFont.lfWeight > 400 ? "\\b" : "\\b0",
1402 page->file->fonts[font].LogFont.lfItalic ? "\\i" : "\\i0",
1403 page->file->fonts[font].LogFont.lfUnderline ? "\\ul" : "\\ul0",
1404 page->file->fonts[font].LogFont.lfStrikeOut ? "\\strike" : "\\strike0");
1405 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1407 break;
1409 case 0x81:
1410 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1411 attributes.wVSpace++;
1412 format += 1;
1413 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1414 break;
1416 case 0x82:
1417 if (!HLPFILE_RtfAddControl(rd, "\\par\\pard")) goto done;
1418 attributes.wVSpace++;
1419 attributes.wIndent = 0;
1420 format += 1;
1421 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1422 break;
1424 case 0x83:
1425 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1426 attributes.wIndent++;
1427 format += 1;
1428 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1429 break;
1431 #if 0
1432 case 0x84:
1433 format += 3;
1434 break;
1435 #endif
1437 case 0x86:
1438 case 0x87:
1439 case 0x88:
1441 BYTE pos = (*format - 0x86);
1442 BYTE type = format[1];
1443 long size;
1445 format += 2;
1446 size = fetch_long(&format);
1448 paragraph = HeapAlloc(GetProcessHeap(), 0,
1449 sizeof(HLPFILE_PARAGRAPH) + textsize);
1450 if (!paragraph) return FALSE;
1451 *paragraphptr = paragraph;
1452 paragraphptr = &paragraph->next;
1454 paragraph->next = NULL;
1455 if (!rd){
1456 paragraph->link = attributes.link;
1457 if (paragraph->link) paragraph->link->wRefCount++;
1459 else paragraph->link = NULL;
1460 paragraph->cookie = para_bitmap;
1461 paragraph->u.gfx.pos = pos;
1462 switch (type)
1464 case 0x22:
1465 fetch_ushort(&format); /* hot spot */
1466 /* fall thru */
1467 case 0x03:
1468 switch (GET_SHORT(format, 0))
1470 case 0:
1471 HLPFILE_LoadGfxByIndex(page->file, GET_SHORT(format, 2),
1472 paragraph);
1473 if (rd) { /* FIXME: TEMP */
1474 HLPFILE_RtfAddGfxByIndex(rd, page->file, GET_SHORT(format, 2));
1475 rd->char_pos++;
1477 break;
1478 case 1:
1479 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1480 GET_SHORT(format, 0),
1481 size, GET_SHORT(format, 2));
1482 HLPFILE_LoadGfxByAddr(page->file, format + 2, size - 4,
1483 paragraph);
1484 if (rd) { /* FIXME: TEMP */
1485 HLPFILE_RtfAddGfxByAddr(rd, page->file, format + 2, size - 4);
1486 rd->char_pos++;
1488 break;
1489 default:
1490 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1491 break;
1493 break;
1494 case 0x05:
1495 WINE_FIXME("Got an embedded element %s\n", format + 6);
1496 break;
1497 default:
1498 WINE_FIXME("Got a type %d picture\n", type);
1499 break;
1501 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1503 format += size;
1505 break;
1507 case 0x89:
1508 HLPFILE_FreeLink(attributes.link);
1509 attributes.link = NULL;
1510 format += 1;
1511 if (rd) {
1512 if (!rd->current_link)
1513 WINE_FIXME("No existing link\n");
1514 else
1515 rd->current_link->cpMax = rd->char_pos;
1516 rd->current_link = NULL;
1517 rd->force_color = FALSE;
1519 break;
1521 case 0x8B:
1522 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1523 format += 1;
1524 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1525 break;
1527 case 0x8C:
1528 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1529 /* FIXME: it could be that hypen is also in input stream !! */
1530 format += 1;
1531 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1532 break;
1534 #if 0
1535 case 0xA9:
1536 format += 2;
1537 break;
1538 #endif
1540 case 0xC8:
1541 case 0xCC:
1542 WINE_TRACE("macro => %s\n", format + 3);
1543 HLPFILE_FreeLink(attributes.link);
1544 attributes.link = HLPFILE_AllocLink(rd, hlp_link_macro, (const char*)format + 3,
1545 GET_USHORT(format, 1), 0, !(*format & 4), -1);
1546 format += 3 + GET_USHORT(format, 1);
1547 break;
1549 case 0xE0:
1550 case 0xE1:
1551 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1552 HLPFILE_FreeLink(attributes.link);
1553 attributes.link = HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1554 page->file->lpszPath, -1,
1555 GET_UINT(format, 1)-16,
1556 1, -1);
1559 format += 5;
1560 break;
1562 case 0xE2:
1563 case 0xE3:
1564 case 0xE6:
1565 case 0xE7:
1566 attributes.link = HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1567 page->file->lpszPath, -1,
1568 GET_UINT(format, 1),
1569 !(*format & 4), -1);
1570 format += 5;
1571 break;
1573 case 0xEA:
1574 case 0xEB:
1575 case 0xEE:
1576 case 0xEF:
1578 char* ptr = (char*) format + 8;
1579 BYTE type = format[3];
1580 int wnd = -1;
1582 switch (type)
1584 case 1:
1585 wnd = *ptr;
1586 /* fall through */
1587 case 0:
1588 ptr = page->file->lpszPath;
1589 break;
1590 case 6:
1591 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1593 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1595 if (wnd == -1)
1596 WINE_WARN("Couldn't find window info for %s\n", ptr);
1597 ptr += strlen(ptr) + 1;
1598 /* fall through */
1599 case 4:
1600 break;
1601 default:
1602 WINE_WARN("Unknown link type %d\n", type);
1603 break;
1605 HLPFILE_FreeLink(attributes.link);
1606 attributes.link = HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1607 ptr, -1, GET_UINT(format, 4),
1608 !(*format & 4), wnd);
1610 format += 3 + GET_USHORT(format, 1);
1611 break;
1613 default:
1614 WINE_WARN("format %02x\n", *format);
1615 format++;
1619 ret = TRUE;
1620 done:
1621 HeapFree(GetProcessHeap(), 0, text_base);
1622 return ret;
1625 /******************************************************************
1626 * HLPFILE_BrowsePage
1629 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page, struct RtfData* rd)
1631 HLPFILE *hlpfile = page->file;
1632 BYTE *buf, *end;
1633 DWORD ref = page->reference;
1634 unsigned index, old_index = -1, offset, count = 0, cpg;
1635 char tmp[1024];
1636 const char* ck = NULL;
1638 if (rd) { /* FIXME: TEMP */
1639 rd->in_text = TRUE;
1640 rd->data = rd->ptr = HeapAlloc(GetProcessHeap(), 0, rd->allocated = 32768);
1641 rd->char_pos = 0;
1642 rd->first_link = rd->current_link = NULL;
1643 rd->force_color = FALSE;
1646 switch (hlpfile->charset)
1648 case DEFAULT_CHARSET:
1649 case ANSI_CHARSET: cpg = 1252; break;
1650 case SHIFTJIS_CHARSET: cpg = 932; break;
1651 case HANGEUL_CHARSET: cpg = 949; break;
1652 case GB2312_CHARSET: cpg = 936; break;
1653 case CHINESEBIG5_CHARSET: cpg = 950; break;
1654 case GREEK_CHARSET: cpg = 1253; break;
1655 case TURKISH_CHARSET: cpg = 1254; break;
1656 case HEBREW_CHARSET: cpg = 1255; break;
1657 case ARABIC_CHARSET: cpg = 1256; break;
1658 case BALTIC_CHARSET: cpg = 1257; break;
1659 case VIETNAMESE_CHARSET: cpg = 1258; break;
1660 case RUSSIAN_CHARSET: cpg = 1251; break;
1661 case EE_CHARSET: cpg = 1250; break;
1662 case THAI_CHARSET: cpg = 874; break;
1663 case JOHAB_CHARSET: cpg = 1361; break;
1664 case MAC_CHARSET: ck = "mac"; break;
1665 default:
1666 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1667 cpg = 1252;
1669 if (ck)
1671 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1672 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1674 else
1676 sprintf(tmp, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg);
1677 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1680 /* generate font table */
1681 if (!HLPFILE_RtfAddControl(rd, "{\\fonttbl")) return FALSE;
1682 for (index = 0; index < hlpfile->numFonts; index++)
1684 const char* family;
1685 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1687 case FF_MODERN: family = "modern"; break;
1688 case FF_ROMAN: family = "roman"; break;
1689 case FF_SWISS: family = "swiss"; break;
1690 case FF_SCRIPT: family = "script"; break;
1691 case FF_DECORATIVE: family = "decor"; break;
1692 default: family = "nil"; break;
1694 sprintf(tmp, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1695 index, family,
1696 hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0x0F,
1697 hlpfile->fonts[index].LogFont.lfCharSet,
1698 hlpfile->fonts[index].LogFont.lfFaceName);
1699 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1701 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1702 /* generate color table */
1703 if (!HLPFILE_RtfAddControl(rd, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE;
1704 for (index = 0; index < hlpfile->numFonts; index++)
1706 const char* family;
1707 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1709 case FF_MODERN: family = "modern"; break;
1710 case FF_ROMAN: family = "roman"; break;
1711 case FF_SWISS: family = "swiss"; break;
1712 case FF_SCRIPT: family = "script"; break;
1713 case FF_DECORATIVE: family = "decor"; break;
1714 default: family = "nil"; break;
1716 sprintf(tmp, "\\red%d\\green%d\\blue%d;",
1717 GetRValue(hlpfile->fonts[index].color),
1718 GetGValue(hlpfile->fonts[index].color),
1719 GetBValue(hlpfile->fonts[index].color));
1720 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1722 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1726 if (hlpfile->version <= 16)
1728 index = (ref - 0x0C) / hlpfile->dsize;
1729 offset = (ref - 0x0C) % hlpfile->dsize;
1731 else
1733 index = (ref - 0x0C) >> 14;
1734 offset = (ref - 0x0C) & 0x3FFF;
1737 if (hlpfile->version <= 16 && index != old_index && index != 0)
1739 /* we jumped to the next block, adjust pointers */
1740 ref -= 12;
1741 offset -= 12;
1744 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1745 buf = hlpfile->topic_map[index] + offset;
1746 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1747 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1748 if (index != old_index) {old_index = index;}
1750 switch (buf[0x14])
1752 case 0x02:
1753 if (count++) goto done;
1754 break;
1755 case 0x01:
1756 case 0x20:
1757 case 0x23:
1758 if (!HLPFILE_BrowseParagraph(page, rd, buf, end)) return FALSE;
1759 break;
1760 default:
1761 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1763 if (hlpfile->version <= 16)
1765 ref += GET_UINT(buf, 0xc);
1766 if (GET_UINT(buf, 0xc) == 0)
1767 break;
1769 else
1770 ref = GET_UINT(buf, 0xc);
1771 } while (ref != 0xffffffff);
1772 done:
1773 if (rd)
1774 page->first_link = rd->first_link;
1775 return HLPFILE_RtfAddControl(rd, "}");
1778 /******************************************************************
1779 * HLPFILE_ReadFont
1783 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1785 BYTE *ref, *end;
1786 unsigned i, len, idx;
1787 unsigned face_num, dscr_num, face_offset, dscr_offset;
1788 BYTE flag, family;
1790 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1792 WINE_WARN("no subfile FONT\n");
1793 hlpfile->numFonts = 0;
1794 hlpfile->fonts = NULL;
1795 return FALSE;
1798 ref += 9;
1800 face_num = GET_USHORT(ref, 0);
1801 dscr_num = GET_USHORT(ref, 2);
1802 face_offset = GET_USHORT(ref, 4);
1803 dscr_offset = GET_USHORT(ref, 6);
1805 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1806 face_num, face_offset, dscr_num, dscr_offset);
1808 hlpfile->numFonts = dscr_num;
1809 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1811 len = (dscr_offset - face_offset) / face_num;
1812 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1813 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1814 for (i = 0; i < dscr_num; i++)
1816 flag = ref[dscr_offset + i * 11 + 0];
1817 family = ref[dscr_offset + i * 11 + 2];
1819 hlpfile->fonts[i].LogFont.lfHeight = ref[dscr_offset + i * 11 + 1];
1820 hlpfile->fonts[i].LogFont.lfWidth = 0;
1821 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1822 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1823 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1824 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1825 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1826 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1827 hlpfile->fonts[i].LogFont.lfCharSet = hlpfile->charset;
1828 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1829 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1830 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1831 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1833 switch (family)
1835 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1836 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1837 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1838 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1839 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1840 default: WINE_FIXME("Unknown family %u\n", family);
1842 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1844 if (idx < face_num)
1846 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1847 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1849 else
1851 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1852 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1854 hlpfile->fonts[i].hFont = 0;
1855 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1856 ref[dscr_offset + i * 11 + 6],
1857 ref[dscr_offset + i * 11 + 7]);
1858 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1859 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1860 i, flag,
1861 X(0, "bold"),
1862 X(1, "italic"),
1863 X(2, "underline"),
1864 X(3, "strikeOut"),
1865 X(4, "dblUnderline"),
1866 X(5, "smallCaps"),
1867 ref[dscr_offset + i * 11 + 1],
1868 family,
1869 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1870 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1872 return TRUE;
1875 /***********************************************************************
1877 * HLPFILE_ReadFileToBuffer
1879 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1881 BYTE header[16], dummy[1];
1883 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1885 /* sanity checks */
1886 if (GET_UINT(header, 0) != 0x00035F3F)
1887 {WINE_WARN("wrong header\n"); return FALSE;};
1889 hlpfile->file_buffer_size = GET_UINT(header, 12);
1890 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1891 if (!hlpfile->file_buffer) return FALSE;
1893 memcpy(hlpfile->file_buffer, header, 16);
1894 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1895 {WINE_WARN("filesize1\n"); return FALSE;};
1897 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1899 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1901 return TRUE;
1904 /**************************************************************************
1905 * comp_FindSubFile
1907 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1910 static int comp_FindSubFile(void *p, const void *key,
1911 int leaf, void** next)
1913 *next = (char *)p+strlen(p)+(leaf?5:3);
1914 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1915 return strcmp(p, key);
1918 /***********************************************************************
1920 * HLPFILE_FindSubFile
1922 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
1924 BYTE *ptr;
1926 WINE_TRACE("looking for file '%s'\n", name);
1927 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
1928 name, comp_FindSubFile);
1929 if (!ptr) return FALSE;
1930 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
1931 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
1933 WINE_ERR("internal file %s does not fit\n", name);
1934 return FALSE;
1936 *subend = *subbuf + GET_UINT(*subbuf, 0);
1937 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
1939 WINE_ERR("internal file %s does not fit\n", name);
1940 return FALSE;
1942 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1944 WINE_ERR("invalid size provided for internal file %s\n", name);
1945 return FALSE;
1947 return TRUE;
1950 /***********************************************************************
1952 * HLPFILE_SystemCommands
1954 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1956 BYTE *buf, *ptr, *end;
1957 HLPFILE_MACRO *macro, **m;
1958 LPSTR p;
1959 unsigned short magic, minor, major, flags;
1961 hlpfile->lpszTitle = NULL;
1963 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1965 magic = GET_USHORT(buf + 9, 0);
1966 minor = GET_USHORT(buf + 9, 2);
1967 major = GET_USHORT(buf + 9, 4);
1968 /* gen date on 4 bytes */
1969 flags = GET_USHORT(buf + 9, 10);
1970 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1971 magic, major, minor, flags);
1972 if (magic != 0x036C || major != 1)
1973 {WINE_WARN("Wrong system header\n"); return FALSE;}
1974 if (minor <= 16)
1976 hlpfile->tbsize = 0x800;
1977 hlpfile->compressed = 0;
1979 else if (flags == 0)
1981 hlpfile->tbsize = 0x1000;
1982 hlpfile->compressed = 0;
1984 else if (flags == 4)
1986 hlpfile->tbsize = 0x1000;
1987 hlpfile->compressed = 1;
1989 else
1991 hlpfile->tbsize = 0x800;
1992 hlpfile->compressed = 1;
1995 if (hlpfile->compressed)
1996 hlpfile->dsize = 0x4000;
1997 else
1998 hlpfile->dsize = hlpfile->tbsize - 0x0C;
2000 hlpfile->version = minor;
2001 hlpfile->flags = flags;
2002 hlpfile->charset = DEFAULT_CHARSET;
2004 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
2006 char *str = (char*) ptr + 4;
2007 switch (GET_USHORT(ptr, 0))
2009 case 1:
2010 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
2011 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
2012 if (!hlpfile->lpszTitle) return FALSE;
2013 lstrcpy(hlpfile->lpszTitle, str);
2014 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
2015 break;
2017 case 2:
2018 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
2019 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
2020 if (!hlpfile->lpszCopyright) return FALSE;
2021 lstrcpy(hlpfile->lpszCopyright, str);
2022 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
2023 break;
2025 case 3:
2026 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
2027 hlpfile->contents_start = GET_UINT(ptr, 4);
2028 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
2029 break;
2031 case 4:
2032 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
2033 if (!macro) break;
2034 p = (char*)macro + sizeof(HLPFILE_MACRO);
2035 lstrcpy(p, str);
2036 macro->lpszMacro = p;
2037 macro->next = 0;
2038 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
2039 *m = macro;
2040 break;
2042 case 5:
2043 if (GET_USHORT(ptr, 4 + 4) != 1)
2044 WINE_FIXME("More than one icon, picking up first\n");
2045 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
2046 hlpfile->hIcon = CreateIconFromResourceEx(ptr + 4 + 0x16,
2047 GET_USHORT(ptr, 2) - 0x16, TRUE,
2048 0x30000, 0, 0, 0);
2049 break;
2051 case 6:
2052 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
2054 if (hlpfile->windows)
2055 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
2056 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
2057 else
2058 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
2059 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
2061 if (hlpfile->windows)
2063 unsigned flags = GET_USHORT(ptr, 4);
2064 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
2066 if (flags & 0x0001) strcpy(wi->type, &str[2]);
2067 else wi->type[0] = '\0';
2068 if (flags & 0x0002) strcpy(wi->name, &str[12]);
2069 else wi->name[0] = '\0';
2070 if (flags & 0x0004) strcpy(wi->caption, &str[21]);
2071 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
2072 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
2073 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
2074 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
2075 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
2076 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
2077 wi->win_style = WS_OVERLAPPEDWINDOW;
2078 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
2079 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
2080 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",
2081 flags & 0x0001 ? 'T' : 't',
2082 flags & 0x0002 ? 'N' : 'n',
2083 flags & 0x0004 ? 'C' : 'c',
2084 flags & 0x0008 ? 'X' : 'x',
2085 flags & 0x0010 ? 'Y' : 'y',
2086 flags & 0x0020 ? 'W' : 'w',
2087 flags & 0x0040 ? 'H' : 'h',
2088 flags & 0x0080 ? 'S' : 's',
2089 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
2090 wi->size.cx, wi->size.cy);
2092 break;
2093 case 8:
2094 WINE_WARN("Citation: '%s'\n", ptr + 4);
2095 break;
2096 case 11:
2097 hlpfile->charset = ptr[4];
2098 WINE_TRACE("Charset: %d\n", hlpfile->charset);
2099 break;
2100 default:
2101 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
2104 if (!hlpfile->lpszTitle)
2105 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
2106 return TRUE;
2109 /***********************************************************************
2111 * HLPFILE_UncompressedLZ77_Size
2113 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
2115 int i, newsize = 0;
2117 while (ptr < end)
2119 int mask = *ptr++;
2120 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2122 if (mask & 1)
2124 int code = GET_USHORT(ptr, 0);
2125 int len = 3 + (code >> 12);
2126 newsize += len;
2127 ptr += 2;
2129 else newsize++, ptr++;
2133 return newsize;
2136 /***********************************************************************
2138 * HLPFILE_UncompressLZ77
2140 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
2142 int i;
2144 while (ptr < end)
2146 int mask = *ptr++;
2147 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
2149 if (mask & 1)
2151 int code = GET_USHORT(ptr, 0);
2152 int len = 3 + (code >> 12);
2153 int offset = code & 0xfff;
2155 * We must copy byte-by-byte here. We cannot use memcpy nor
2156 * memmove here. Just example:
2157 * a[]={1,2,3,4,5,6,7,8,9,10}
2158 * newptr=a+2;
2159 * offset=1;
2160 * We expect:
2161 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
2163 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
2164 ptr += 2;
2166 else *newptr++ = *ptr++;
2170 return newptr;
2173 /***********************************************************************
2175 * HLPFILE_UncompressLZ77_Phrases
2177 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
2179 UINT i, num, dec_size, head_size;
2180 BYTE *buf, *end;
2182 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
2184 if (hlpfile->version <= 16)
2185 head_size = 13;
2186 else
2187 head_size = 17;
2189 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
2190 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
2192 if (hlpfile->version <= 16)
2193 dec_size = end - buf - 15 - 2 * num;
2194 else
2195 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
2197 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2198 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2199 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2201 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2202 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2203 return FALSE;
2206 for (i = 0; i <= num; i++)
2207 hlpfile->phrases_offsets[i] = GET_USHORT(buf, 0x11 + 2 * i) - 2 * num - 2;
2209 if (hlpfile->version <= 16)
2210 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
2211 else
2212 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
2214 hlpfile->hasPhrases = TRUE;
2215 return TRUE;
2218 /***********************************************************************
2220 * HLPFILE_Uncompress_Phrases40
2222 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
2224 UINT num;
2225 INT dec_size, cpr_size;
2226 BYTE *buf_idx, *end_idx;
2227 BYTE *buf_phs, *end_phs;
2228 long* ptr, mask = 0;
2229 unsigned int i;
2230 unsigned short bc, n;
2232 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
2233 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
2235 ptr = (long*)(buf_idx + 9 + 28);
2236 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
2237 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
2239 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2240 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2241 GET_UINT(buf_idx, 9 + 0),
2242 GET_UINT(buf_idx, 9 + 4),
2243 GET_UINT(buf_idx, 9 + 8),
2244 GET_UINT(buf_idx, 9 + 12),
2245 GET_UINT(buf_idx, 9 + 16),
2246 GET_UINT(buf_idx, 9 + 20),
2247 GET_USHORT(buf_idx, 9 + 24),
2248 GET_USHORT(buf_idx, 9 + 26));
2250 dec_size = GET_UINT(buf_idx, 9 + 12);
2251 cpr_size = GET_UINT(buf_idx, 9 + 16);
2253 if (dec_size != cpr_size &&
2254 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
2256 WINE_WARN("size mismatch %u %u\n",
2257 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2258 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2261 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2262 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2263 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2265 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2266 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2267 return FALSE;
2270 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
2272 hlpfile->phrases_offsets[0] = 0;
2273 for (i = 0; i < num; i++)
2275 for (n = 1; getbit(); n += 1 << bc);
2276 if (getbit()) n++;
2277 if (bc > 1 && getbit()) n += 2;
2278 if (bc > 2 && getbit()) n += 4;
2279 if (bc > 3 && getbit()) n += 8;
2280 if (bc > 4 && getbit()) n += 16;
2281 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
2283 #undef getbit
2285 if (dec_size == cpr_size)
2286 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2287 else
2288 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2290 hlpfile->hasPhrases40 = TRUE;
2291 return TRUE;
2294 /***********************************************************************
2296 * HLPFILE_Uncompress_Topic
2298 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
2300 BYTE *buf, *ptr, *end, *newptr;
2301 unsigned int i, newsize = 0;
2302 unsigned int topic_size;
2304 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
2305 {WINE_WARN("topic0\n"); return FALSE;}
2307 buf += 9; /* Skip file header */
2308 topic_size = end - buf;
2309 if (hlpfile->compressed)
2311 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2313 for (i = 0; i < hlpfile->topic_maplen; i++)
2315 ptr = buf + i * hlpfile->tbsize;
2317 /* I don't know why, it's necessary for printman.hlp */
2318 if (ptr + 0x44 > end) ptr = end - 0x44;
2320 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
2323 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2324 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
2325 if (!hlpfile->topic_map) return FALSE;
2326 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2327 hlpfile->topic_end = newptr + newsize;
2329 for (i = 0; i < hlpfile->topic_maplen; i++)
2331 ptr = buf + i * hlpfile->tbsize;
2332 if (ptr + 0x44 > end) ptr = end - 0x44;
2334 hlpfile->topic_map[i] = newptr;
2335 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
2338 else
2340 /* basically, we need to copy the TopicBlockSize byte pages
2341 * (removing the first 0x0C) in one single area in memory
2343 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2344 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2345 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
2346 if (!hlpfile->topic_map) return FALSE;
2347 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2348 hlpfile->topic_end = newptr + topic_size;
2350 for (i = 0; i < hlpfile->topic_maplen; i++)
2352 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
2353 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
2356 return TRUE;
2359 /***********************************************************************
2361 * HLPFILE_Uncompress2
2364 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
2366 BYTE *phptr, *phend;
2367 UINT code;
2368 UINT index;
2370 while (ptr < end && newptr < newend)
2372 if (!*ptr || *ptr >= 0x10)
2373 *newptr++ = *ptr++;
2374 else
2376 code = 0x100 * ptr[0] + ptr[1];
2377 index = (code - 0x100) / 2;
2379 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
2380 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
2382 if (newptr + (phend - phptr) > newend)
2384 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
2385 newptr, newend, (SIZE_T)(phend - phptr));
2386 return;
2388 memcpy(newptr, phptr, phend - phptr);
2389 newptr += phend - phptr;
2390 if (code & 1) *newptr++ = ' ';
2392 ptr += 2;
2395 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
2398 /******************************************************************
2399 * HLPFILE_Uncompress3
2403 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
2404 const BYTE* src, const BYTE* src_end)
2406 unsigned int idx, len;
2408 for (; src < src_end; src++)
2410 if ((*src & 1) == 0)
2412 idx = *src / 2;
2413 if (idx > hlpfile->num_phrases)
2415 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2416 len = 0;
2418 else
2420 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2421 if (dst + len <= dst_end)
2422 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2425 else if ((*src & 0x03) == 0x01)
2427 idx = (*src + 1) * 64;
2428 idx += *++src;
2429 if (idx > hlpfile->num_phrases)
2431 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2432 len = 0;
2434 else
2436 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2437 if (dst + len <= dst_end)
2438 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2441 else if ((*src & 0x07) == 0x03)
2443 len = (*src / 8) + 1;
2444 if (dst + len <= dst_end)
2445 memcpy(dst, src + 1, len);
2446 src += len;
2448 else
2450 len = (*src / 16) + 1;
2451 if (dst + len <= dst_end)
2452 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
2454 dst += len;
2457 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
2458 return TRUE;
2461 /******************************************************************
2462 * HLPFILE_UncompressRLE
2466 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
2468 BYTE ch;
2469 BYTE* sdst = *dst + dstsz;
2471 while (src < end)
2473 ch = *src++;
2474 if (ch & 0x80)
2476 ch &= 0x7F;
2477 if ((*dst) + ch <= sdst)
2478 memcpy(*dst, src, ch);
2479 src += ch;
2481 else
2483 if ((*dst) + ch <= sdst)
2484 memset(*dst, (char)*src, ch);
2485 src++;
2487 *dst += ch;
2489 if (*dst != sdst)
2490 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
2491 (SIZE_T)(*dst - (sdst - dstsz)), dstsz);
2494 /**************************************************************************
2495 * HLPFILE_BPTreeSearch
2497 * Searches for an element in B+ tree
2499 * PARAMS
2500 * buf [I] pointer to the embedded file structured as a B+ tree
2501 * key [I] pointer to data to find
2502 * comp [I] compare function
2504 * RETURNS
2505 * Pointer to block identified by key, or NULL if failure.
2508 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
2509 HLPFILE_BPTreeCompare comp)
2511 unsigned magic;
2512 unsigned page_size;
2513 unsigned cur_page;
2514 unsigned level;
2515 BYTE *pages, *ptr, *newptr;
2516 int i, entries;
2517 int ret;
2519 magic = GET_USHORT(buf, 9);
2520 if (magic != 0x293B)
2522 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2523 return NULL;
2525 page_size = GET_USHORT(buf, 9+4);
2526 cur_page = GET_USHORT(buf, 9+26);
2527 level = GET_USHORT(buf, 9+32);
2528 pages = buf + 9 + 38;
2529 while (--level > 0)
2531 ptr = pages + cur_page*page_size;
2532 entries = GET_SHORT(ptr, 2);
2533 ptr += 6;
2534 for (i = 0; i < entries; i++)
2536 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
2537 ptr = newptr;
2539 cur_page = GET_USHORT(ptr-2, 0);
2541 ptr = pages + cur_page*page_size;
2542 entries = GET_SHORT(ptr, 2);
2543 ptr += 8;
2544 for (i = 0; i < entries; i++)
2546 ret = comp(ptr, key, 1, (void **)&newptr);
2547 if (ret == 0) return ptr;
2548 if (ret > 0) return NULL;
2549 ptr = newptr;
2551 return NULL;
2554 /**************************************************************************
2555 * HLPFILE_BPTreeEnum
2557 * Enumerates elements in B+ tree.
2559 * PARAMS
2560 * buf [I] pointer to the embedded file structured as a B+ tree
2561 * cb [I] compare function
2562 * cookie [IO] cookie for cb function
2564 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
2566 unsigned magic;
2567 unsigned page_size;
2568 unsigned cur_page;
2569 unsigned level;
2570 BYTE *pages, *ptr, *newptr;
2571 int i, entries;
2573 magic = GET_USHORT(buf, 9);
2574 if (magic != 0x293B)
2576 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2577 return;
2579 page_size = GET_USHORT(buf, 9+4);
2580 cur_page = GET_USHORT(buf, 9+26);
2581 level = GET_USHORT(buf, 9+32);
2582 pages = buf + 9 + 38;
2583 while (--level > 0)
2585 ptr = pages + cur_page*page_size;
2586 cur_page = GET_USHORT(ptr, 4);
2588 while (cur_page != 0xFFFF)
2590 ptr = pages + cur_page*page_size;
2591 entries = GET_SHORT(ptr, 2);
2592 ptr += 8;
2593 for (i = 0; i < entries; i++)
2595 cb(ptr, (void **)&newptr, cookie);
2596 ptr = newptr;
2598 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
2603 /***********************************************************************
2605 * HLPFILE_GetContext
2607 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2609 BYTE *cbuf, *cend;
2610 unsigned clen;
2612 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2613 {WINE_WARN("context0\n"); return FALSE;}
2615 clen = cend - cbuf;
2616 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2617 if (!hlpfile->Context) return FALSE;
2618 memcpy(hlpfile->Context, cbuf, clen);
2620 return TRUE;
2623 /***********************************************************************
2625 * HLPFILE_GetKeywords
2627 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2629 BYTE *cbuf, *cend;
2630 unsigned clen;
2632 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2633 clen = cend - cbuf;
2634 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2635 if (!hlpfile->kwbtree) return FALSE;
2636 memcpy(hlpfile->kwbtree, cbuf, clen);
2638 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2640 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2641 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2642 return FALSE;
2644 clen = cend - cbuf;
2645 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2646 if (!hlpfile->kwdata)
2648 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2649 return FALSE;
2651 memcpy(hlpfile->kwdata, cbuf, clen);
2653 return TRUE;
2656 /***********************************************************************
2658 * HLPFILE_GetMap
2660 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2662 BYTE *cbuf, *cend;
2663 unsigned entries, i;
2665 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2666 {WINE_WARN("no map section\n"); return FALSE;}
2668 entries = GET_USHORT(cbuf, 9);
2669 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2670 if (!hlpfile->Map) return FALSE;
2671 hlpfile->wMapLen = entries;
2672 for (i = 0; i < entries; i++)
2674 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2675 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2677 return TRUE;
2680 /******************************************************************
2681 * HLPFILE_DeleteLink
2685 void HLPFILE_FreeLink(HLPFILE_LINK* link)
2687 if (link && !--link->wRefCount)
2689 HeapFree(GetProcessHeap(), 0, link);
2693 /***********************************************************************
2695 * HLPFILE_DeleteParagraph
2697 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
2699 HLPFILE_PARAGRAPH* next;
2701 while (paragraph)
2703 next = paragraph->next;
2705 if (paragraph->cookie == para_metafile)
2706 DeleteMetaFile(paragraph->u.gfx.u.mfp.hMF);
2708 HLPFILE_FreeLink(paragraph->link);
2710 HeapFree(GetProcessHeap(), 0, paragraph);
2711 paragraph = next;
2715 /***********************************************************************
2717 * DeleteMacro
2719 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2721 HLPFILE_MACRO* next;
2723 while (macro)
2725 next = macro->next;
2726 HeapFree(GetProcessHeap(), 0, macro);
2727 macro = next;
2731 /***********************************************************************
2733 * DeletePage
2735 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2737 HLPFILE_PAGE* next;
2739 while (page)
2741 next = page->next;
2742 HLPFILE_DeleteParagraph(page->first_paragraph);
2743 HLPFILE_DeleteMacro(page->first_macro);
2744 HeapFree(GetProcessHeap(), 0, page);
2745 page = next;
2749 /***********************************************************************
2751 * HLPFILE_FreeHlpFile
2753 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2755 unsigned i;
2757 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2759 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2760 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2761 else first_hlpfile = hlpfile->next;
2763 if (hlpfile->numFonts)
2765 for (i = 0; i < hlpfile->numFonts; i++)
2767 DeleteObject(hlpfile->fonts[i].hFont);
2769 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2772 if (hlpfile->numBmps)
2774 for (i = 0; i < hlpfile->numBmps; i++)
2776 DeleteObject(hlpfile->bmps[i]);
2778 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2781 HLPFILE_DeletePage(hlpfile->first_page);
2782 HLPFILE_DeleteMacro(hlpfile->first_macro);
2784 DestroyIcon(hlpfile->hIcon);
2785 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2786 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2787 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2788 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2789 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2790 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2791 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2792 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2793 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2794 HeapFree(GetProcessHeap(), 0, hlpfile);