winhelp: Set the alignment of paragraph in richedit.
[wine.git] / programs / winhelp / hlpfile.c
blobcbac2c37a4e2defe8436ec2026fc0d661937ad87
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 * HLPFILE_LoadBitmap
686 static BOOL HLPFILE_LoadBitmap(BYTE* beg, BYTE type, BYTE pack,
687 HLPFILE_PARAGRAPH* paragraph)
689 BYTE* ptr;
690 BYTE* pict_beg;
691 BITMAPINFO* bi;
692 unsigned long off, csz;
693 HDC hdc;
695 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
696 if (!bi) return FALSE;
698 ptr = beg + 2; /* for type and pack */
700 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
701 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
702 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
703 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
704 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
705 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
706 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
707 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
708 bi->bmiHeader.biClrImportant = fetch_ulong(&ptr);
709 bi->bmiHeader.biCompression = BI_RGB;
710 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
711 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
712 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
713 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
714 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
715 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
717 csz = fetch_ulong(&ptr);
718 fetch_ulong(&ptr); /* hotspot size */
720 off = GET_UINT(ptr, 0); ptr += 4;
721 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
723 /* now read palette info */
724 if (type == 0x06)
726 unsigned nc = bi->bmiHeader.biClrUsed;
727 unsigned i;
729 /* not quite right, especially for bitfields type of compression */
730 if (!nc && bi->bmiHeader.biBitCount <= 8)
731 nc = 1 << bi->bmiHeader.biBitCount;
733 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
734 if (!bi) return FALSE;
735 for (i = 0; i < nc; i++)
737 bi->bmiColors[i].rgbBlue = ptr[0];
738 bi->bmiColors[i].rgbGreen = ptr[1];
739 bi->bmiColors[i].rgbRed = ptr[2];
740 bi->bmiColors[i].rgbReserved = 0;
741 ptr += 4;
744 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
746 paragraph->u.gfx.u.bmp.hBitmap = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
747 CBM_INIT, pict_beg,
748 bi, DIB_RGB_COLORS);
749 ReleaseDC(0, hdc);
750 if (!paragraph->u.gfx.u.bmp.hBitmap)
751 WINE_ERR("Couldn't create bitmap\n");
753 HeapFree(GetProcessHeap(), 0, bi);
754 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
756 return TRUE;
759 /******************************************************************
760 * HLPFILE_LoadMetaFile
764 static BOOL HLPFILE_LoadMetaFile(BYTE* beg, BYTE pack, HLPFILE_PARAGRAPH* paragraph)
766 BYTE* ptr;
767 unsigned long size, csize;
768 unsigned long off, hsoff;
769 BYTE* bits;
770 LPMETAFILEPICT lpmfp;
772 WINE_TRACE("Loading metafile\n");
774 ptr = beg + 2; /* for type and pack */
776 lpmfp = &paragraph->u.gfx.u.mfp;
777 lpmfp->mm = fetch_ushort(&ptr); /* mapping mode */
779 lpmfp->xExt = GET_USHORT(ptr, 0);
780 lpmfp->yExt = GET_USHORT(ptr, 2);
781 ptr += 4;
783 size = fetch_ulong(&ptr); /* decompressed size */
784 csize = fetch_ulong(&ptr); /* compressed size */
785 fetch_ulong(&ptr); /* hotspot size */
786 off = GET_UINT(ptr, 0);
787 hsoff = GET_UINT(ptr, 4);
788 ptr += 8;
790 WINE_TRACE("sz=%lu csz=%lu (%d,%d) offs=%lu/%lu,%lu\n",
791 size, csize, lpmfp->xExt, lpmfp->yExt, off, (SIZE_T)(ptr - beg), hsoff);
793 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
794 if (!bits) return FALSE;
796 paragraph->cookie = para_metafile;
798 lpmfp->hMF = SetMetaFileBitsEx(size, bits);
800 if (!lpmfp->hMF)
801 WINE_FIXME("Couldn't load metafile\n");
803 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
805 return TRUE;
808 /******************************************************************
809 * HLPFILE_LoadGfxByAddr
813 static BOOL HLPFILE_LoadGfxByAddr(HLPFILE *hlpfile, BYTE* ref,
814 unsigned long size,
815 HLPFILE_PARAGRAPH* paragraph)
817 unsigned i, numpict;
819 numpict = GET_USHORT(ref, 2);
820 WINE_TRACE("Got picture magic=%04x #=%d\n",
821 GET_USHORT(ref, 0), numpict);
823 for (i = 0; i < numpict; i++)
825 BYTE* beg;
826 BYTE* ptr;
827 BYTE type, pack;
829 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
830 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
832 type = *ptr++;
833 pack = *ptr++;
835 switch (type)
837 case 5: /* device dependent bmp */
838 case 6: /* device independent bmp */
839 HLPFILE_LoadBitmap(beg, type, pack, paragraph);
840 break;
841 case 8:
842 HLPFILE_LoadMetaFile(beg, pack, paragraph);
843 break;
844 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
847 /* FIXME: hotspots */
849 /* FIXME: implement support for multiple picture format */
850 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
851 break;
853 return TRUE;
856 /******************************************************************
857 * HLPFILE_LoadGfxByIndex
861 static BOOL HLPFILE_LoadGfxByIndex(HLPFILE *hlpfile, unsigned index,
862 HLPFILE_PARAGRAPH* paragraph)
864 char tmp[16];
865 BYTE *ref, *end;
866 BOOL ret;
868 WINE_TRACE("Loading picture #%d\n", index);
870 if (index < hlpfile->numBmps && hlpfile->bmps[index] != NULL)
872 paragraph->u.gfx.u.bmp.hBitmap = hlpfile->bmps[index];
873 return TRUE;
876 sprintf(tmp, "|bm%u", index);
878 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
880 ref += 9;
882 ret = HLPFILE_LoadGfxByAddr(hlpfile, ref, end - ref, paragraph);
884 /* cache bitmap */
885 if (ret && paragraph->cookie == para_bitmap)
887 if (index >= hlpfile->numBmps)
889 hlpfile->numBmps = index + 1;
890 if (hlpfile->bmps)
891 hlpfile->bmps = HeapReAlloc(GetProcessHeap(), 0, hlpfile->bmps,
892 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
893 else
894 hlpfile->bmps = HeapAlloc(GetProcessHeap(), 0,
895 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
898 hlpfile->bmps[index] = paragraph->u.gfx.u.bmp.hBitmap;
900 return ret;
903 /******************************************************************
904 * HLPFILE_AllocLink
908 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
909 const char* str, unsigned len, LONG hash,
910 unsigned clrChange, unsigned wnd)
912 HLPFILE_LINK* link;
913 char* link_str;
915 /* FIXME: should build a string table for the attributes.link.lpszPath
916 * they are reallocated for each link
918 if (len == -1) len = strlen(str);
919 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + len + 1);
920 if (!link) return NULL;
922 link->cookie = cookie;
923 link->string = link_str = (char*)(link + 1);
924 memcpy(link_str, str, len);
925 link_str[len] = '\0';
926 link->hash = hash;
927 link->bClrChange = clrChange ? 1 : 0;
928 link->window = wnd;
929 link->wRefCount = 1;
930 if (rd) {
931 link->next = rd->first_link;
932 rd->first_link = link;
933 link->cpMin = rd->char_pos;
934 link->cpMax = 0;
935 rd->force_color = clrChange;
936 link->wRefCount++;
937 if (rd->current_link) WINE_FIXME("Pending link\n");
938 rd->current_link = link;
941 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
942 link->cookie, link->string, link->hash, link->window);
943 return link;
946 unsigned HLPFILE_HalfPointsToTwips(unsigned pts)
948 static unsigned logPxY;
949 if (!logPxY)
951 HDC hdc = GetDC(NULL);
952 logPxY = GetDeviceCaps(hdc, LOGPIXELSY);
953 ReleaseDC(NULL, hdc);
955 return MulDiv(pts, 72 * 10, logPxY);
958 /***********************************************************************
960 * HLPFILE_BrowseParagraph
962 static BOOL HLPFILE_BrowseParagraph(HLPFILE_PAGE* page, struct RtfData* rd, BYTE *buf, BYTE* end)
964 HLPFILE_PARAGRAPH *paragraph, **paragraphptr;
965 UINT textsize;
966 BYTE *format, *format_end;
967 char *text, *text_base, *text_end;
968 long size, blocksize, datalen;
969 unsigned short bits;
970 unsigned nc, ncol = 1;
971 BOOL in_table = FALSE;
972 char tmp[256];
973 BOOL ret = FALSE;
975 for (paragraphptr = &page->first_paragraph; *paragraphptr;
976 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
978 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
980 blocksize = GET_UINT(buf, 0);
981 size = GET_UINT(buf, 0x4);
982 datalen = GET_UINT(buf, 0x10);
983 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
984 if (!text) return FALSE;
985 if (size > blocksize - datalen)
987 /* need to decompress */
988 if (page->file->hasPhrases)
989 HLPFILE_Uncompress2(page->file, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
990 else if (page->file->hasPhrases40)
991 HLPFILE_Uncompress3(page->file, text, text + size, buf + datalen, end);
992 else
994 WINE_FIXME("Text size is too long, splitting\n");
995 size = blocksize - datalen;
996 memcpy(text, buf + datalen, size);
999 else
1000 memcpy(text, buf + datalen, size);
1002 text_end = text + size;
1004 format = buf + 0x15;
1005 format_end = buf + GET_UINT(buf, 0x10);
1007 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
1009 fetch_long(&format);
1010 fetch_ushort(&format);
1013 if (buf[0x14] == 0x23)
1015 char type;
1017 in_table = TRUE;
1018 ncol = *format++;
1020 WINE_TRACE("#cols %u\n", ncol);
1021 type = *format++;
1022 if (type == 0 || type == 2)
1023 format += 2;
1024 format += ncol * 4;
1027 for (nc = 0; nc < ncol; /**/)
1029 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
1030 if (in_table)
1032 nc = GET_SHORT(format, 0);
1033 if (nc == -1) break;
1034 format += 5;
1036 else nc++;
1037 format += 4;
1038 bits = GET_USHORT(format, 0); format += 2;
1039 if (bits & 0x0001) fetch_long(&format);
1040 if (bits & 0x0002)
1042 sprintf(tmp, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1043 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1045 if (bits & 0x0004)
1047 sprintf(tmp, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1048 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1050 if (bits & 0x0008)
1052 sprintf(tmp, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1053 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1055 if (bits & 0x0010)
1057 sprintf(tmp, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1058 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1060 if (bits & 0x0020)
1062 sprintf(tmp, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1063 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1065 if (bits & 0x0040)
1067 sprintf(tmp, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1068 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1070 if (bits & 0x0100) format += 3;
1071 if (bits & 0x0200)
1073 int i, ntab = fetch_short(&format);
1074 unsigned tab, ts;
1075 const char* kind;
1077 for (i = 0; i < ntab; i++)
1079 tab = fetch_ushort(&format);
1080 ts = (tab & 0x4000) ? fetch_ushort(&format) : 0 /* left */;
1081 switch (ts)
1083 default: WINE_FIXME("Unknown tab style %x\n", ts);
1084 /* fall through */
1085 case 0: kind = ""; break;
1086 case 1: kind = "\\tqr"; break;
1087 case 2: kind = "\\tqc"; break;
1089 /* FIXME: do kind */
1090 sprintf(tmp, "%s\\tx%d",
1091 kind, HLPFILE_HalfPointsToTwips(tab & 0x3FFF));
1092 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1095 switch (bits & 0xc00)
1097 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1098 case 0: if (!HLPFILE_RtfAddControl(rd, "\\ql")) goto done; break;
1099 case 0x400: if (!HLPFILE_RtfAddControl(rd, "\\qr")) goto done; break;
1100 case 0x800: if (!HLPFILE_RtfAddControl(rd, "\\qc")) goto done; break;
1103 /* 0x1000 doesn't need space */
1104 if ((bits & 0xE080) != 0)
1105 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
1107 while (text < text_end && format < format_end)
1109 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
1110 textsize = strlen(text);
1111 if (textsize)
1113 paragraph = HeapAlloc(GetProcessHeap(), 0,
1114 sizeof(HLPFILE_PARAGRAPH) + textsize + 1);
1115 if (!paragraph) return FALSE;
1116 *paragraphptr = paragraph;
1117 paragraphptr = &paragraph->next;
1119 paragraph->next = NULL;
1120 if (!rd)
1122 paragraph->link = attributes.link;
1123 if (paragraph->link) paragraph->link->wRefCount++;
1125 else paragraph->link = NULL;
1126 paragraph->cookie = para_normal_text;
1127 paragraph->u.text.wFont = attributes.wFont;
1128 paragraph->u.text.wVSpace = attributes.wVSpace;
1129 paragraph->u.text.wHSpace = attributes.wHSpace;
1130 paragraph->u.text.wIndent = attributes.wIndent;
1131 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
1132 strcpy(paragraph->u.text.lpszText, text);
1134 attributes.wVSpace = 0;
1135 attributes.wHSpace = 0;
1136 if (rd) /* FIXME: TEMP */ {
1137 if (rd->force_color)
1139 if ((rd->current_link->cookie == hlp_link_popup) ?
1140 !HLPFILE_RtfAddControl(rd, "{\\uld\\cf1") :
1141 !HLPFILE_RtfAddControl(rd, "{\\ul\\cf1")) goto done;
1143 if (!HLPFILE_RtfAddText(rd, text)) goto done;
1144 if (rd->force_color && !HLPFILE_RtfAddControl(rd, "}")) goto done;
1145 rd->char_pos += textsize;
1148 /* else: null text, keep on storing attributes */
1149 text += textsize + 1;
1151 if (*format == 0xff)
1153 format++;
1154 break;
1157 WINE_TRACE("format=%02x\n", *format);
1158 switch (*format)
1160 case 0x20:
1161 WINE_FIXME("NIY20\n");
1162 format += 5;
1163 break;
1165 case 0x21:
1166 WINE_FIXME("NIY21\n");
1167 format += 3;
1168 break;
1170 case 0x80:
1172 unsigned font = GET_USHORT(format, 1);
1173 unsigned fs;
1174 attributes.wFont = font;
1175 WINE_TRACE("Changing font to %d\n", attributes.wFont);
1176 format += 3;
1177 fs = (4 * page->file->fonts[font].LogFont.lfHeight - 3) / 5;
1178 /* FIXME: missing at least colors, also bold attribute looses information */
1180 sprintf(tmp, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1181 font, font + 2, fs,
1182 page->file->fonts[font].LogFont.lfWeight > 400 ? "\\b" : "\\b0",
1183 page->file->fonts[font].LogFont.lfItalic ? "\\i" : "\\i0",
1184 page->file->fonts[font].LogFont.lfUnderline ? "\\ul" : "\\ul0",
1185 page->file->fonts[font].LogFont.lfStrikeOut ? "\\strike" : "\\strike0");
1186 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1188 break;
1190 case 0x81:
1191 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1192 attributes.wVSpace++;
1193 format += 1;
1194 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1195 break;
1197 case 0x82:
1198 if (!HLPFILE_RtfAddControl(rd, "\\par\\pard")) goto done;
1199 attributes.wVSpace++;
1200 attributes.wIndent = 0;
1201 format += 1;
1202 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1203 break;
1205 case 0x83:
1206 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1207 attributes.wIndent++;
1208 format += 1;
1209 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1210 break;
1212 #if 0
1213 case 0x84:
1214 format += 3;
1215 break;
1216 #endif
1218 case 0x86:
1219 case 0x87:
1220 case 0x88:
1222 BYTE pos = (*format - 0x86);
1223 BYTE type = format[1];
1224 long size;
1226 format += 2;
1227 size = fetch_long(&format);
1229 paragraph = HeapAlloc(GetProcessHeap(), 0,
1230 sizeof(HLPFILE_PARAGRAPH) + textsize);
1231 if (!paragraph) return FALSE;
1232 *paragraphptr = paragraph;
1233 paragraphptr = &paragraph->next;
1235 paragraph->next = NULL;
1236 if (!rd){
1237 paragraph->link = attributes.link;
1238 if (paragraph->link) paragraph->link->wRefCount++;
1240 else paragraph->link = NULL;
1241 paragraph->cookie = para_bitmap;
1242 paragraph->u.gfx.pos = pos;
1243 switch (type)
1245 case 0x22:
1246 fetch_ushort(&format); /* hot spot */
1247 /* fall thru */
1248 case 0x03:
1249 switch (GET_SHORT(format, 0))
1251 case 0:
1252 HLPFILE_LoadGfxByIndex(page->file, GET_SHORT(format, 2),
1253 paragraph);
1254 break;
1255 case 1:
1256 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1257 GET_SHORT(format, 0),
1258 size, GET_SHORT(format, 2));
1259 HLPFILE_LoadGfxByAddr(page->file, format + 2, size - 4,
1260 paragraph);
1261 break;
1262 default:
1263 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1264 break;
1266 break;
1267 case 0x05:
1268 WINE_FIXME("Got an embedded element %s\n", format + 6);
1269 break;
1270 default:
1271 WINE_FIXME("Got a type %d picture\n", type);
1272 break;
1274 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1276 format += size;
1278 break;
1280 case 0x89:
1281 HLPFILE_FreeLink(attributes.link);
1282 attributes.link = NULL;
1283 format += 1;
1284 if (rd) {
1285 if (!rd->current_link)
1286 WINE_FIXME("No existing link\n");
1287 else
1288 rd->current_link->cpMax = rd->char_pos;
1289 rd->current_link = NULL;
1290 rd->force_color = FALSE;
1292 break;
1294 case 0x8B:
1295 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1296 format += 1;
1297 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1298 break;
1300 case 0x8C:
1301 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1302 /* FIXME: it could be that hypen is also in input stream !! */
1303 format += 1;
1304 if (rd) /* FIXME: TEMP */ rd->char_pos++;
1305 break;
1307 #if 0
1308 case 0xA9:
1309 format += 2;
1310 break;
1311 #endif
1313 case 0xC8:
1314 case 0xCC:
1315 WINE_TRACE("macro => %s\n", format + 3);
1316 HLPFILE_FreeLink(attributes.link);
1317 attributes.link = HLPFILE_AllocLink(rd, hlp_link_macro, (const char*)format + 3,
1318 GET_USHORT(format, 1), 0, !(*format & 4), -1);
1319 format += 3 + GET_USHORT(format, 1);
1320 break;
1322 case 0xE0:
1323 case 0xE1:
1324 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1325 HLPFILE_FreeLink(attributes.link);
1326 attributes.link = HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1327 page->file->lpszPath, -1,
1328 GET_UINT(format, 1)-16,
1329 1, -1);
1332 format += 5;
1333 break;
1335 case 0xE2:
1336 case 0xE3:
1337 case 0xE6:
1338 case 0xE7:
1339 attributes.link = HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1340 page->file->lpszPath, -1,
1341 GET_UINT(format, 1),
1342 !(*format & 4), -1);
1343 format += 5;
1344 break;
1346 case 0xEA:
1347 case 0xEB:
1348 case 0xEE:
1349 case 0xEF:
1351 char* ptr = (char*) format + 8;
1352 BYTE type = format[3];
1353 int wnd = -1;
1355 switch (type)
1357 case 1:
1358 wnd = *ptr;
1359 /* fall through */
1360 case 0:
1361 ptr = page->file->lpszPath;
1362 break;
1363 case 6:
1364 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1366 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1368 if (wnd == -1)
1369 WINE_WARN("Couldn't find window info for %s\n", ptr);
1370 ptr += strlen(ptr) + 1;
1371 /* fall through */
1372 case 4:
1373 break;
1374 default:
1375 WINE_WARN("Unknown link type %d\n", type);
1376 break;
1378 HLPFILE_FreeLink(attributes.link);
1379 attributes.link = HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1380 ptr, -1, GET_UINT(format, 4),
1381 !(*format & 4), wnd);
1383 format += 3 + GET_USHORT(format, 1);
1384 break;
1386 default:
1387 WINE_WARN("format %02x\n", *format);
1388 format++;
1392 ret = TRUE;
1393 done:
1394 HeapFree(GetProcessHeap(), 0, text_base);
1395 return ret;
1398 /******************************************************************
1399 * HLPFILE_BrowsePage
1402 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page, struct RtfData* rd)
1404 HLPFILE *hlpfile = page->file;
1405 BYTE *buf, *end;
1406 DWORD ref = page->reference;
1407 unsigned index, old_index = -1, offset, count = 0, cpg;
1408 char tmp[1024];
1409 const char* ck = NULL;
1411 if (rd) { /* FIXME: TEMP */
1412 rd->in_text = TRUE;
1413 rd->data = rd->ptr = HeapAlloc(GetProcessHeap(), 0, rd->allocated = 32768);
1414 rd->char_pos = 0;
1415 rd->first_link = rd->current_link = NULL;
1416 rd->force_color = FALSE;
1419 switch (hlpfile->charset)
1421 case DEFAULT_CHARSET:
1422 case ANSI_CHARSET: cpg = 1252; break;
1423 case SHIFTJIS_CHARSET: cpg = 932; break;
1424 case HANGEUL_CHARSET: cpg = 949; break;
1425 case GB2312_CHARSET: cpg = 936; break;
1426 case CHINESEBIG5_CHARSET: cpg = 950; break;
1427 case GREEK_CHARSET: cpg = 1253; break;
1428 case TURKISH_CHARSET: cpg = 1254; break;
1429 case HEBREW_CHARSET: cpg = 1255; break;
1430 case ARABIC_CHARSET: cpg = 1256; break;
1431 case BALTIC_CHARSET: cpg = 1257; break;
1432 case VIETNAMESE_CHARSET: cpg = 1258; break;
1433 case RUSSIAN_CHARSET: cpg = 1251; break;
1434 case EE_CHARSET: cpg = 1250; break;
1435 case THAI_CHARSET: cpg = 874; break;
1436 case JOHAB_CHARSET: cpg = 1361; break;
1437 case MAC_CHARSET: ck = "mac"; break;
1438 default:
1439 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1440 cpg = 1252;
1442 if (ck)
1444 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1445 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1447 else
1449 sprintf(tmp, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg);
1450 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1453 /* generate font table */
1454 if (!HLPFILE_RtfAddControl(rd, "{\\fonttbl")) return FALSE;
1455 for (index = 0; index < hlpfile->numFonts; index++)
1457 const char* family;
1458 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1460 case FF_MODERN: family = "modern"; break;
1461 case FF_ROMAN: family = "roman"; break;
1462 case FF_SWISS: family = "swiss"; break;
1463 case FF_SCRIPT: family = "script"; break;
1464 case FF_DECORATIVE: family = "decor"; break;
1465 default: family = "nil"; break;
1467 sprintf(tmp, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1468 index, family,
1469 hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0x0F,
1470 hlpfile->fonts[index].LogFont.lfCharSet,
1471 hlpfile->fonts[index].LogFont.lfFaceName);
1472 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1474 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1475 /* generate color table */
1476 if (!HLPFILE_RtfAddControl(rd, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE;
1477 for (index = 0; index < hlpfile->numFonts; index++)
1479 const char* family;
1480 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1482 case FF_MODERN: family = "modern"; break;
1483 case FF_ROMAN: family = "roman"; break;
1484 case FF_SWISS: family = "swiss"; break;
1485 case FF_SCRIPT: family = "script"; break;
1486 case FF_DECORATIVE: family = "decor"; break;
1487 default: family = "nil"; break;
1489 sprintf(tmp, "\\red%d\\green%d\\blue%d;",
1490 GetRValue(hlpfile->fonts[index].color),
1491 GetGValue(hlpfile->fonts[index].color),
1492 GetBValue(hlpfile->fonts[index].color));
1493 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1495 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1499 if (hlpfile->version <= 16)
1501 index = (ref - 0x0C) / hlpfile->dsize;
1502 offset = (ref - 0x0C) % hlpfile->dsize;
1504 else
1506 index = (ref - 0x0C) >> 14;
1507 offset = (ref - 0x0C) & 0x3FFF;
1510 if (hlpfile->version <= 16 && index != old_index && index != 0)
1512 /* we jumped to the next block, adjust pointers */
1513 ref -= 12;
1514 offset -= 12;
1517 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1518 buf = hlpfile->topic_map[index] + offset;
1519 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1520 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1521 if (index != old_index) {old_index = index;}
1523 switch (buf[0x14])
1525 case 0x02:
1526 if (count++) goto done;
1527 break;
1528 case 0x01:
1529 case 0x20:
1530 case 0x23:
1531 if (!HLPFILE_BrowseParagraph(page, rd, buf, end)) return FALSE;
1532 break;
1533 default:
1534 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1536 if (hlpfile->version <= 16)
1538 ref += GET_UINT(buf, 0xc);
1539 if (GET_UINT(buf, 0xc) == 0)
1540 break;
1542 else
1543 ref = GET_UINT(buf, 0xc);
1544 } while (ref != 0xffffffff);
1545 done:
1546 if (rd)
1547 page->first_link = rd->first_link;
1548 return HLPFILE_RtfAddControl(rd, "}");
1551 /******************************************************************
1552 * HLPFILE_ReadFont
1556 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1558 BYTE *ref, *end;
1559 unsigned i, len, idx;
1560 unsigned face_num, dscr_num, face_offset, dscr_offset;
1561 BYTE flag, family;
1563 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1565 WINE_WARN("no subfile FONT\n");
1566 hlpfile->numFonts = 0;
1567 hlpfile->fonts = NULL;
1568 return FALSE;
1571 ref += 9;
1573 face_num = GET_USHORT(ref, 0);
1574 dscr_num = GET_USHORT(ref, 2);
1575 face_offset = GET_USHORT(ref, 4);
1576 dscr_offset = GET_USHORT(ref, 6);
1578 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1579 face_num, face_offset, dscr_num, dscr_offset);
1581 hlpfile->numFonts = dscr_num;
1582 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1584 len = (dscr_offset - face_offset) / face_num;
1585 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1586 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1587 for (i = 0; i < dscr_num; i++)
1589 flag = ref[dscr_offset + i * 11 + 0];
1590 family = ref[dscr_offset + i * 11 + 2];
1592 hlpfile->fonts[i].LogFont.lfHeight = ref[dscr_offset + i * 11 + 1];
1593 hlpfile->fonts[i].LogFont.lfWidth = 0;
1594 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1595 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1596 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1597 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1598 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1599 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1600 hlpfile->fonts[i].LogFont.lfCharSet = hlpfile->charset;
1601 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1602 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1603 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1604 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1606 switch (family)
1608 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1609 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1610 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1611 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1612 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1613 default: WINE_FIXME("Unknown family %u\n", family);
1615 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1617 if (idx < face_num)
1619 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1620 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1622 else
1624 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1625 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1627 hlpfile->fonts[i].hFont = 0;
1628 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1629 ref[dscr_offset + i * 11 + 6],
1630 ref[dscr_offset + i * 11 + 7]);
1631 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1632 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1633 i, flag,
1634 X(0, "bold"),
1635 X(1, "italic"),
1636 X(2, "underline"),
1637 X(3, "strikeOut"),
1638 X(4, "dblUnderline"),
1639 X(5, "smallCaps"),
1640 ref[dscr_offset + i * 11 + 1],
1641 family,
1642 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1643 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1645 return TRUE;
1648 /***********************************************************************
1650 * HLPFILE_ReadFileToBuffer
1652 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1654 BYTE header[16], dummy[1];
1656 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1658 /* sanity checks */
1659 if (GET_UINT(header, 0) != 0x00035F3F)
1660 {WINE_WARN("wrong header\n"); return FALSE;};
1662 hlpfile->file_buffer_size = GET_UINT(header, 12);
1663 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1664 if (!hlpfile->file_buffer) return FALSE;
1666 memcpy(hlpfile->file_buffer, header, 16);
1667 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1668 {WINE_WARN("filesize1\n"); return FALSE;};
1670 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1672 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1674 return TRUE;
1677 /**************************************************************************
1678 * comp_FindSubFile
1680 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1683 static int comp_FindSubFile(void *p, const void *key,
1684 int leaf, void** next)
1686 *next = (char *)p+strlen(p)+(leaf?5:3);
1687 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1688 return strcmp(p, key);
1691 /***********************************************************************
1693 * HLPFILE_FindSubFile
1695 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
1697 BYTE *ptr;
1699 WINE_TRACE("looking for file '%s'\n", name);
1700 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
1701 name, comp_FindSubFile);
1702 if (!ptr) return FALSE;
1703 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
1704 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
1706 WINE_ERR("internal file %s does not fit\n", name);
1707 return FALSE;
1709 *subend = *subbuf + GET_UINT(*subbuf, 0);
1710 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
1712 WINE_ERR("internal file %s does not fit\n", name);
1713 return FALSE;
1715 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1717 WINE_ERR("invalid size provided for internal file %s\n", name);
1718 return FALSE;
1720 return TRUE;
1723 /***********************************************************************
1725 * HLPFILE_SystemCommands
1727 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1729 BYTE *buf, *ptr, *end;
1730 HLPFILE_MACRO *macro, **m;
1731 LPSTR p;
1732 unsigned short magic, minor, major, flags;
1734 hlpfile->lpszTitle = NULL;
1736 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1738 magic = GET_USHORT(buf + 9, 0);
1739 minor = GET_USHORT(buf + 9, 2);
1740 major = GET_USHORT(buf + 9, 4);
1741 /* gen date on 4 bytes */
1742 flags = GET_USHORT(buf + 9, 10);
1743 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1744 magic, major, minor, flags);
1745 if (magic != 0x036C || major != 1)
1746 {WINE_WARN("Wrong system header\n"); return FALSE;}
1747 if (minor <= 16)
1749 hlpfile->tbsize = 0x800;
1750 hlpfile->compressed = 0;
1752 else if (flags == 0)
1754 hlpfile->tbsize = 0x1000;
1755 hlpfile->compressed = 0;
1757 else if (flags == 4)
1759 hlpfile->tbsize = 0x1000;
1760 hlpfile->compressed = 1;
1762 else
1764 hlpfile->tbsize = 0x800;
1765 hlpfile->compressed = 1;
1768 if (hlpfile->compressed)
1769 hlpfile->dsize = 0x4000;
1770 else
1771 hlpfile->dsize = hlpfile->tbsize - 0x0C;
1773 hlpfile->version = minor;
1774 hlpfile->flags = flags;
1775 hlpfile->charset = DEFAULT_CHARSET;
1777 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1779 char *str = (char*) ptr + 4;
1780 switch (GET_USHORT(ptr, 0))
1782 case 1:
1783 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1784 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1785 if (!hlpfile->lpszTitle) return FALSE;
1786 lstrcpy(hlpfile->lpszTitle, str);
1787 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1788 break;
1790 case 2:
1791 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1792 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1793 if (!hlpfile->lpszCopyright) return FALSE;
1794 lstrcpy(hlpfile->lpszCopyright, str);
1795 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1796 break;
1798 case 3:
1799 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1800 hlpfile->contents_start = GET_UINT(ptr, 4);
1801 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1802 break;
1804 case 4:
1805 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1806 if (!macro) break;
1807 p = (char*)macro + sizeof(HLPFILE_MACRO);
1808 lstrcpy(p, str);
1809 macro->lpszMacro = p;
1810 macro->next = 0;
1811 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1812 *m = macro;
1813 break;
1815 case 5:
1816 if (GET_USHORT(ptr, 4 + 4) != 1)
1817 WINE_FIXME("More than one icon, picking up first\n");
1818 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
1819 hlpfile->hIcon = CreateIconFromResourceEx(ptr + 4 + 0x16,
1820 GET_USHORT(ptr, 2) - 0x16, TRUE,
1821 0x30000, 0, 0, 0);
1822 break;
1824 case 6:
1825 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1827 if (hlpfile->windows)
1828 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1829 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1830 else
1831 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1832 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1834 if (hlpfile->windows)
1836 unsigned flags = GET_USHORT(ptr, 4);
1837 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1839 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1840 else wi->type[0] = '\0';
1841 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1842 else wi->name[0] = '\0';
1843 if (flags & 0x0004) strcpy(wi->caption, &str[21]);
1844 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1845 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1846 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1847 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1848 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1849 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1850 wi->win_style = WS_OVERLAPPEDWINDOW;
1851 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1852 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1853 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",
1854 flags & 0x0001 ? 'T' : 't',
1855 flags & 0x0002 ? 'N' : 'n',
1856 flags & 0x0004 ? 'C' : 'c',
1857 flags & 0x0008 ? 'X' : 'x',
1858 flags & 0x0010 ? 'Y' : 'y',
1859 flags & 0x0020 ? 'W' : 'w',
1860 flags & 0x0040 ? 'H' : 'h',
1861 flags & 0x0080 ? 'S' : 's',
1862 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1863 wi->size.cx, wi->size.cy);
1865 break;
1866 case 8:
1867 WINE_WARN("Citation: '%s'\n", ptr + 4);
1868 break;
1869 case 11:
1870 hlpfile->charset = ptr[4];
1871 WINE_TRACE("Charset: %d\n", hlpfile->charset);
1872 break;
1873 default:
1874 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1877 if (!hlpfile->lpszTitle)
1878 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
1879 return TRUE;
1882 /***********************************************************************
1884 * HLPFILE_UncompressedLZ77_Size
1886 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
1888 int i, newsize = 0;
1890 while (ptr < end)
1892 int mask = *ptr++;
1893 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1895 if (mask & 1)
1897 int code = GET_USHORT(ptr, 0);
1898 int len = 3 + (code >> 12);
1899 newsize += len;
1900 ptr += 2;
1902 else newsize++, ptr++;
1906 return newsize;
1909 /***********************************************************************
1911 * HLPFILE_UncompressLZ77
1913 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
1915 int i;
1917 while (ptr < end)
1919 int mask = *ptr++;
1920 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1922 if (mask & 1)
1924 int code = GET_USHORT(ptr, 0);
1925 int len = 3 + (code >> 12);
1926 int offset = code & 0xfff;
1928 * We must copy byte-by-byte here. We cannot use memcpy nor
1929 * memmove here. Just example:
1930 * a[]={1,2,3,4,5,6,7,8,9,10}
1931 * newptr=a+2;
1932 * offset=1;
1933 * We expect:
1934 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
1936 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
1937 ptr += 2;
1939 else *newptr++ = *ptr++;
1943 return newptr;
1946 /***********************************************************************
1948 * HLPFILE_UncompressLZ77_Phrases
1950 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
1952 UINT i, num, dec_size, head_size;
1953 BYTE *buf, *end;
1955 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
1957 if (hlpfile->version <= 16)
1958 head_size = 13;
1959 else
1960 head_size = 17;
1962 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
1963 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
1965 if (hlpfile->version <= 16)
1966 dec_size = end - buf - 15 - 2 * num;
1967 else
1968 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
1970 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1971 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1972 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
1974 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
1975 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
1976 return FALSE;
1979 for (i = 0; i <= num; i++)
1980 hlpfile->phrases_offsets[i] = GET_USHORT(buf, 0x11 + 2 * i) - 2 * num - 2;
1982 if (hlpfile->version <= 16)
1983 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
1984 else
1985 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
1987 hlpfile->hasPhrases = TRUE;
1988 return TRUE;
1991 /***********************************************************************
1993 * HLPFILE_Uncompress_Phrases40
1995 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
1997 UINT num;
1998 INT dec_size, cpr_size;
1999 BYTE *buf_idx, *end_idx;
2000 BYTE *buf_phs, *end_phs;
2001 long* ptr, mask = 0;
2002 unsigned int i;
2003 unsigned short bc, n;
2005 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
2006 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
2008 ptr = (long*)(buf_idx + 9 + 28);
2009 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
2010 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
2012 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2013 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2014 GET_UINT(buf_idx, 9 + 0),
2015 GET_UINT(buf_idx, 9 + 4),
2016 GET_UINT(buf_idx, 9 + 8),
2017 GET_UINT(buf_idx, 9 + 12),
2018 GET_UINT(buf_idx, 9 + 16),
2019 GET_UINT(buf_idx, 9 + 20),
2020 GET_USHORT(buf_idx, 9 + 24),
2021 GET_USHORT(buf_idx, 9 + 26));
2023 dec_size = GET_UINT(buf_idx, 9 + 12);
2024 cpr_size = GET_UINT(buf_idx, 9 + 16);
2026 if (dec_size != cpr_size &&
2027 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
2029 WINE_WARN("size mismatch %u %u\n",
2030 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2031 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2034 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2035 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2036 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2038 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2039 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2040 return FALSE;
2043 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
2045 hlpfile->phrases_offsets[0] = 0;
2046 for (i = 0; i < num; i++)
2048 for (n = 1; getbit(); n += 1 << bc);
2049 if (getbit()) n++;
2050 if (bc > 1 && getbit()) n += 2;
2051 if (bc > 2 && getbit()) n += 4;
2052 if (bc > 3 && getbit()) n += 8;
2053 if (bc > 4 && getbit()) n += 16;
2054 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
2056 #undef getbit
2058 if (dec_size == cpr_size)
2059 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2060 else
2061 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2063 hlpfile->hasPhrases40 = TRUE;
2064 return TRUE;
2067 /***********************************************************************
2069 * HLPFILE_Uncompress_Topic
2071 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
2073 BYTE *buf, *ptr, *end, *newptr;
2074 unsigned int i, newsize = 0;
2075 unsigned int topic_size;
2077 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
2078 {WINE_WARN("topic0\n"); return FALSE;}
2080 buf += 9; /* Skip file header */
2081 topic_size = end - buf;
2082 if (hlpfile->compressed)
2084 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2086 for (i = 0; i < hlpfile->topic_maplen; i++)
2088 ptr = buf + i * hlpfile->tbsize;
2090 /* I don't know why, it's necessary for printman.hlp */
2091 if (ptr + 0x44 > end) ptr = end - 0x44;
2093 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
2096 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2097 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
2098 if (!hlpfile->topic_map) return FALSE;
2099 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2100 hlpfile->topic_end = newptr + newsize;
2102 for (i = 0; i < hlpfile->topic_maplen; i++)
2104 ptr = buf + i * hlpfile->tbsize;
2105 if (ptr + 0x44 > end) ptr = end - 0x44;
2107 hlpfile->topic_map[i] = newptr;
2108 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
2111 else
2113 /* basically, we need to copy the TopicBlockSize byte pages
2114 * (removing the first 0x0C) in one single area in memory
2116 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2117 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2118 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
2119 if (!hlpfile->topic_map) return FALSE;
2120 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2121 hlpfile->topic_end = newptr + topic_size;
2123 for (i = 0; i < hlpfile->topic_maplen; i++)
2125 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
2126 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
2129 return TRUE;
2132 /***********************************************************************
2134 * HLPFILE_Uncompress2
2137 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
2139 BYTE *phptr, *phend;
2140 UINT code;
2141 UINT index;
2143 while (ptr < end && newptr < newend)
2145 if (!*ptr || *ptr >= 0x10)
2146 *newptr++ = *ptr++;
2147 else
2149 code = 0x100 * ptr[0] + ptr[1];
2150 index = (code - 0x100) / 2;
2152 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
2153 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
2155 if (newptr + (phend - phptr) > newend)
2157 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
2158 newptr, newend, (SIZE_T)(phend - phptr));
2159 return;
2161 memcpy(newptr, phptr, phend - phptr);
2162 newptr += phend - phptr;
2163 if (code & 1) *newptr++ = ' ';
2165 ptr += 2;
2168 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
2171 /******************************************************************
2172 * HLPFILE_Uncompress3
2176 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
2177 const BYTE* src, const BYTE* src_end)
2179 unsigned int idx, len;
2181 for (; src < src_end; src++)
2183 if ((*src & 1) == 0)
2185 idx = *src / 2;
2186 if (idx > hlpfile->num_phrases)
2188 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2189 len = 0;
2191 else
2193 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2194 if (dst + len <= dst_end)
2195 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2198 else if ((*src & 0x03) == 0x01)
2200 idx = (*src + 1) * 64;
2201 idx += *++src;
2202 if (idx > hlpfile->num_phrases)
2204 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
2205 len = 0;
2207 else
2209 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
2210 if (dst + len <= dst_end)
2211 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
2214 else if ((*src & 0x07) == 0x03)
2216 len = (*src / 8) + 1;
2217 if (dst + len <= dst_end)
2218 memcpy(dst, src + 1, len);
2219 src += len;
2221 else
2223 len = (*src / 16) + 1;
2224 if (dst + len <= dst_end)
2225 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
2227 dst += len;
2230 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
2231 return TRUE;
2234 /******************************************************************
2235 * HLPFILE_UncompressRLE
2239 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
2241 BYTE ch;
2242 BYTE* sdst = *dst + dstsz;
2244 while (src < end)
2246 ch = *src++;
2247 if (ch & 0x80)
2249 ch &= 0x7F;
2250 if ((*dst) + ch <= sdst)
2251 memcpy(*dst, src, ch);
2252 src += ch;
2254 else
2256 if ((*dst) + ch <= sdst)
2257 memset(*dst, (char)*src, ch);
2258 src++;
2260 *dst += ch;
2262 if (*dst != sdst)
2263 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
2264 (SIZE_T)(*dst - (sdst - dstsz)), dstsz);
2267 /**************************************************************************
2268 * HLPFILE_BPTreeSearch
2270 * Searches for an element in B+ tree
2272 * PARAMS
2273 * buf [I] pointer to the embedded file structured as a B+ tree
2274 * key [I] pointer to data to find
2275 * comp [I] compare function
2277 * RETURNS
2278 * Pointer to block identified by key, or NULL if failure.
2281 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
2282 HLPFILE_BPTreeCompare comp)
2284 unsigned magic;
2285 unsigned page_size;
2286 unsigned cur_page;
2287 unsigned level;
2288 BYTE *pages, *ptr, *newptr;
2289 int i, entries;
2290 int ret;
2292 magic = GET_USHORT(buf, 9);
2293 if (magic != 0x293B)
2295 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2296 return NULL;
2298 page_size = GET_USHORT(buf, 9+4);
2299 cur_page = GET_USHORT(buf, 9+26);
2300 level = GET_USHORT(buf, 9+32);
2301 pages = buf + 9 + 38;
2302 while (--level > 0)
2304 ptr = pages + cur_page*page_size;
2305 entries = GET_SHORT(ptr, 2);
2306 ptr += 6;
2307 for (i = 0; i < entries; i++)
2309 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
2310 ptr = newptr;
2312 cur_page = GET_USHORT(ptr-2, 0);
2314 ptr = pages + cur_page*page_size;
2315 entries = GET_SHORT(ptr, 2);
2316 ptr += 8;
2317 for (i = 0; i < entries; i++)
2319 ret = comp(ptr, key, 1, (void **)&newptr);
2320 if (ret == 0) return ptr;
2321 if (ret > 0) return NULL;
2322 ptr = newptr;
2324 return NULL;
2327 /**************************************************************************
2328 * HLPFILE_BPTreeEnum
2330 * Enumerates elements in B+ tree.
2332 * PARAMS
2333 * buf [I] pointer to the embedded file structured as a B+ tree
2334 * cb [I] compare function
2335 * cookie [IO] cookie for cb function
2337 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
2339 unsigned magic;
2340 unsigned page_size;
2341 unsigned cur_page;
2342 unsigned level;
2343 BYTE *pages, *ptr, *newptr;
2344 int i, entries;
2346 magic = GET_USHORT(buf, 9);
2347 if (magic != 0x293B)
2349 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2350 return;
2352 page_size = GET_USHORT(buf, 9+4);
2353 cur_page = GET_USHORT(buf, 9+26);
2354 level = GET_USHORT(buf, 9+32);
2355 pages = buf + 9 + 38;
2356 while (--level > 0)
2358 ptr = pages + cur_page*page_size;
2359 cur_page = GET_USHORT(ptr, 4);
2361 while (cur_page != 0xFFFF)
2363 ptr = pages + cur_page*page_size;
2364 entries = GET_SHORT(ptr, 2);
2365 ptr += 8;
2366 for (i = 0; i < entries; i++)
2368 cb(ptr, (void **)&newptr, cookie);
2369 ptr = newptr;
2371 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
2376 /***********************************************************************
2378 * HLPFILE_GetContext
2380 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2382 BYTE *cbuf, *cend;
2383 unsigned clen;
2385 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2386 {WINE_WARN("context0\n"); return FALSE;}
2388 clen = cend - cbuf;
2389 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2390 if (!hlpfile->Context) return FALSE;
2391 memcpy(hlpfile->Context, cbuf, clen);
2393 return TRUE;
2396 /***********************************************************************
2398 * HLPFILE_GetKeywords
2400 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2402 BYTE *cbuf, *cend;
2403 unsigned clen;
2405 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2406 clen = cend - cbuf;
2407 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2408 if (!hlpfile->kwbtree) return FALSE;
2409 memcpy(hlpfile->kwbtree, cbuf, clen);
2411 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2413 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2414 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2415 return FALSE;
2417 clen = cend - cbuf;
2418 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2419 if (!hlpfile->kwdata)
2421 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2422 return FALSE;
2424 memcpy(hlpfile->kwdata, cbuf, clen);
2426 return TRUE;
2429 /***********************************************************************
2431 * HLPFILE_GetMap
2433 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2435 BYTE *cbuf, *cend;
2436 unsigned entries, i;
2438 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2439 {WINE_WARN("no map section\n"); return FALSE;}
2441 entries = GET_USHORT(cbuf, 9);
2442 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2443 if (!hlpfile->Map) return FALSE;
2444 hlpfile->wMapLen = entries;
2445 for (i = 0; i < entries; i++)
2447 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2448 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2450 return TRUE;
2453 /******************************************************************
2454 * HLPFILE_DeleteLink
2458 void HLPFILE_FreeLink(HLPFILE_LINK* link)
2460 if (link && !--link->wRefCount)
2462 HeapFree(GetProcessHeap(), 0, link);
2466 /***********************************************************************
2468 * HLPFILE_DeleteParagraph
2470 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
2472 HLPFILE_PARAGRAPH* next;
2474 while (paragraph)
2476 next = paragraph->next;
2478 if (paragraph->cookie == para_metafile)
2479 DeleteMetaFile(paragraph->u.gfx.u.mfp.hMF);
2481 HLPFILE_FreeLink(paragraph->link);
2483 HeapFree(GetProcessHeap(), 0, paragraph);
2484 paragraph = next;
2488 /***********************************************************************
2490 * DeleteMacro
2492 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2494 HLPFILE_MACRO* next;
2496 while (macro)
2498 next = macro->next;
2499 HeapFree(GetProcessHeap(), 0, macro);
2500 macro = next;
2504 /***********************************************************************
2506 * DeletePage
2508 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2510 HLPFILE_PAGE* next;
2512 while (page)
2514 next = page->next;
2515 HLPFILE_DeleteParagraph(page->first_paragraph);
2516 HLPFILE_DeleteMacro(page->first_macro);
2517 HeapFree(GetProcessHeap(), 0, page);
2518 page = next;
2522 /***********************************************************************
2524 * HLPFILE_FreeHlpFile
2526 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2528 unsigned i;
2530 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2532 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2533 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2534 else first_hlpfile = hlpfile->next;
2536 if (hlpfile->numFonts)
2538 for (i = 0; i < hlpfile->numFonts; i++)
2540 DeleteObject(hlpfile->fonts[i].hFont);
2542 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2545 if (hlpfile->numBmps)
2547 for (i = 0; i < hlpfile->numBmps; i++)
2549 DeleteObject(hlpfile->bmps[i]);
2551 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2554 HLPFILE_DeletePage(hlpfile->first_page);
2555 HLPFILE_DeleteMacro(hlpfile->first_macro);
2557 DestroyIcon(hlpfile->hIcon);
2558 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2559 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2560 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2561 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2562 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2563 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2564 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2565 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2566 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2567 HeapFree(GetProcessHeap(), 0, hlpfile);