winhelp: Fix the loading of a row in a table (especially, for multiple paragraphs...
[wine/hacks.git] / programs / winhelp / hlpfile.c
blob815e7f10138533d3490a0cea9c53db2923bef247
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 /* FIXME:
100 * this finds the page containing the offset. The offset can either
101 * refer to the top of the page (offset == page->offset), or
102 * to some paragraph inside the page...
103 * As of today, we only return the page... we should also return
104 * a paragraph, and then, while opening a new page, compute the
105 * y-offset of the paragraph to be shown and scroll the window
106 * accordingly
108 /******************************************************************
109 * HLPFILE_PageByOffset
113 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset)
115 HLPFILE_PAGE* page;
116 HLPFILE_PAGE* found;
118 if (!hlpfile) return 0;
120 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, offset);
122 if (offset == 0xFFFFFFFF) return NULL;
123 page = NULL;
125 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
127 if (page->offset <= offset && (!found || found->offset < page->offset))
128 found = page;
130 if (!found)
131 WINE_ERR("Page of offset %u not found in file %s\n",
132 offset, hlpfile->lpszPath);
133 return found;
136 /**************************************************************************
137 * comp_PageByHash
139 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
142 static int comp_PageByHash(void *p, const void *key,
143 int leaf, void** next)
145 LONG lKey = (LONG_PTR)key;
146 LONG lTest = (INT)GET_UINT(p, 0);
148 *next = (char *)p+(leaf?8:6);
149 WINE_TRACE("Comparing '%d' with '%d'\n", lKey, lTest);
150 if (lTest < lKey) return -1;
151 if (lTest > lKey) return 1;
152 return 0;
155 /***********************************************************************
157 * HLPFILE_HlpFilePageByHash
159 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash)
161 BYTE *ptr;
163 if (!hlpfile) return 0;
165 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lHash);
167 /* For win 3.0 files hash values are really page numbers */
168 if (hlpfile->version <= 16)
169 return HLPFILE_PageByNumber(hlpfile, lHash);
171 ptr = HLPFILE_BPTreeSearch(hlpfile->Context, LongToPtr(lHash), comp_PageByHash);
172 if (!ptr)
174 WINE_ERR("Page of hash %x not found in file %s\n", lHash, hlpfile->lpszPath);
175 return NULL;
178 return HLPFILE_PageByOffset(hlpfile, GET_UINT(ptr, 4));
181 /***********************************************************************
183 * HLPFILE_PageByMap
185 HLPFILE_PAGE *HLPFILE_PageByMap(HLPFILE* hlpfile, LONG lMap)
187 unsigned int i;
189 if (!hlpfile) return 0;
191 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lMap);
193 for (i = 0; i < hlpfile->wMapLen; i++)
195 if (hlpfile->Map[i].lMap == lMap)
196 return HLPFILE_PageByOffset(hlpfile, hlpfile->Map[i].offset);
199 WINE_ERR("Page of Map %x not found in file %s\n", lMap, hlpfile->lpszPath);
200 return NULL;
203 /***********************************************************************
205 * HLPFILE_Contents
207 HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile)
209 HLPFILE_PAGE* page = NULL;
211 if (!hlpfile) return NULL;
213 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start);
214 if (!page) page = hlpfile->first_page;
215 return page;
218 /***********************************************************************
220 * HLPFILE_Hash
222 LONG HLPFILE_Hash(LPCSTR lpszContext)
224 LONG lHash = 0;
225 CHAR c;
227 while ((c = *lpszContext++))
229 CHAR x = 0;
230 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
231 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
232 if (c >= '1' && c <= '9') x = c - '0';
233 if (c == '0') x = 10;
234 if (c == '.') x = 12;
235 if (c == '_') x = 13;
236 if (x) lHash = lHash * 43 + x;
238 return lHash;
241 /***********************************************************************
243 * HLPFILE_ReadHlpFile
245 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
247 HLPFILE* hlpfile;
249 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
251 if (!strcmp(lpszPath, hlpfile->lpszPath))
253 hlpfile->wRefCount++;
254 return hlpfile;
258 hlpfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
259 sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
260 if (!hlpfile) return 0;
262 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
263 hlpfile->contents_start = 0xFFFFFFFF;
264 hlpfile->next = first_hlpfile;
265 hlpfile->wRefCount = 1;
267 strcpy(hlpfile->lpszPath, lpszPath);
269 first_hlpfile = hlpfile;
270 if (hlpfile->next) hlpfile->next->prev = hlpfile;
272 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
274 HLPFILE_FreeHlpFile(hlpfile);
275 hlpfile = 0;
278 return hlpfile;
281 /***********************************************************************
283 * HLPFILE_DoReadHlpFile
285 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
287 BOOL ret;
288 HFILE hFile;
289 OFSTRUCT ofs;
290 BYTE* buf;
291 DWORD ref = 0x0C;
292 unsigned index, old_index, offset, len, offs;
294 hFile = OpenFile(lpszPath, &ofs, OF_READ);
295 if (hFile == HFILE_ERROR) return FALSE;
297 ret = HLPFILE_ReadFileToBuffer(hlpfile, hFile);
298 _lclose(hFile);
299 if (!ret) return FALSE;
301 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
303 /* load phrases support */
304 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
305 HLPFILE_Uncompress_Phrases40(hlpfile);
307 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
308 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
310 buf = hlpfile->topic_map[0];
311 old_index = -1;
312 offs = 0;
315 BYTE* end;
317 if (hlpfile->version <= 16)
319 index = (ref - 0x0C) / hlpfile->dsize;
320 offset = (ref - 0x0C) % hlpfile->dsize;
322 else
324 index = (ref - 0x0C) >> 14;
325 offset = (ref - 0x0C) & 0x3FFF;
328 if (hlpfile->version <= 16 && index != old_index && index != 0)
330 /* we jumped to the next block, adjust pointers */
331 ref -= 12;
332 offset -= 12;
335 WINE_TRACE("ref=%08x => [%u/%u]\n", ref, index, offset);
337 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
338 buf = hlpfile->topic_map[index] + offset;
339 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
340 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
341 if (index != old_index) {offs = 0; old_index = index;}
343 switch (buf[0x14])
345 case 0x02:
346 if (!HLPFILE_AddPage(hlpfile, buf, end, ref, index * 0x8000L + offs)) return FALSE;
347 break;
349 case 0x01:
350 case 0x20:
351 case 0x23:
352 if (!HLPFILE_SkipParagraph(hlpfile, buf, end, &len)) return FALSE;
353 offs += len;
354 break;
356 default:
357 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
360 if (hlpfile->version <= 16)
362 ref += GET_UINT(buf, 0xc);
363 if (GET_UINT(buf, 0xc) == 0)
364 break;
366 else
367 ref = GET_UINT(buf, 0xc);
368 } while (ref != 0xffffffff);
370 HLPFILE_GetKeywords(hlpfile);
371 HLPFILE_GetMap(hlpfile);
372 if (hlpfile->version <= 16) return TRUE;
373 return HLPFILE_GetContext(hlpfile);
376 /***********************************************************************
378 * HLPFILE_AddPage
380 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned ref, unsigned offset)
382 HLPFILE_PAGE* page;
383 BYTE* title;
384 UINT titlesize, blocksize, datalen;
385 char* ptr;
386 HLPFILE_MACRO*macro;
388 blocksize = GET_UINT(buf, 0);
389 datalen = GET_UINT(buf, 0x10);
390 title = buf + datalen;
391 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
393 titlesize = GET_UINT(buf, 4);
394 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
395 if (!page) return FALSE;
396 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
398 if (titlesize > blocksize - datalen)
400 /* need to decompress */
401 if (hlpfile->hasPhrases)
402 HLPFILE_Uncompress2(hlpfile, title, end, (BYTE*)page->lpszTitle, (BYTE*)page->lpszTitle + titlesize);
403 else if (hlpfile->hasPhrases40)
404 HLPFILE_Uncompress3(hlpfile, page->lpszTitle, page->lpszTitle + titlesize, title, end);
405 else
407 WINE_FIXME("Text size is too long, splitting\n");
408 titlesize = blocksize - datalen;
409 memcpy(page->lpszTitle, title, titlesize);
412 else
413 memcpy(page->lpszTitle, title, titlesize);
415 page->lpszTitle[titlesize] = '\0';
417 if (hlpfile->first_page)
419 hlpfile->last_page->next = page;
420 page->prev = hlpfile->last_page;
421 hlpfile->last_page = page;
423 else
425 hlpfile->first_page = page;
426 hlpfile->last_page = page;
427 page->prev = NULL;
430 page->file = hlpfile;
431 page->next = NULL;
432 page->first_paragraph = NULL;
433 page->first_macro = NULL;
434 page->wNumber = GET_UINT(buf, 0x21);
435 page->offset = offset;
436 page->reference = ref;
438 page->browse_bwd = GET_UINT(buf, 0x19);
439 page->browse_fwd = GET_UINT(buf, 0x1D);
441 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
442 page->wNumber, page->lpszTitle,
443 page->browse_bwd, page->offset, page->browse_fwd);
445 memset(&attributes, 0, sizeof(attributes));
447 /* now load macros */
448 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
449 while (ptr < page->lpszTitle + titlesize)
451 unsigned len = strlen(ptr);
452 char* macro_str;
454 WINE_TRACE("macro: %s\n", ptr);
455 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
456 macro->lpszMacro = macro_str = (char*)(macro + 1);
457 memcpy(macro_str, ptr, len + 1);
458 /* FIXME: shall we really link macro in reverse order ??
459 * may produce strange results when played at page opening
461 macro->next = page->first_macro;
462 page->first_macro = macro;
463 ptr += len + 1;
466 return TRUE;
469 static long fetch_long(BYTE** ptr)
471 long ret;
473 if (*(*ptr) & 1)
475 ret = (*(unsigned long*)(*ptr) - 0x80000000L) / 2;
476 (*ptr) += 4;
478 else
480 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
481 (*ptr) += 2;
484 return ret;
487 static unsigned long fetch_ulong(BYTE** ptr)
489 unsigned long ret;
491 if (*(*ptr) & 1)
493 ret = *(unsigned long*)(*ptr) / 2;
494 (*ptr) += 4;
496 else
498 ret = *(unsigned short*)(*ptr) / 2;
499 (*ptr) += 2;
501 return ret;
504 static short fetch_short(BYTE** ptr)
506 short ret;
508 if (*(*ptr) & 1)
510 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
511 (*ptr) += 2;
513 else
515 ret = (*(unsigned char*)(*ptr) - 0x80) / 2;
516 (*ptr)++;
518 return ret;
521 static unsigned short fetch_ushort(BYTE** ptr)
523 unsigned short ret;
525 if (*(*ptr) & 1)
527 ret = *(unsigned short*)(*ptr) / 2;
528 (*ptr) += 2;
530 else
532 ret = *(unsigned char*)(*ptr) / 2;
533 (*ptr)++;
535 return ret;
538 /***********************************************************************
540 * HLPFILE_SkipParagraph
542 static BOOL HLPFILE_SkipParagraph(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned* len)
544 BYTE *tmp;
546 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
547 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
549 tmp = buf + 0x15;
550 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
552 fetch_long(&tmp);
553 *len = fetch_ushort(&tmp);
555 else *len = end-buf-15;
557 return TRUE;
560 /******************************************************************
561 * HLPFILE_DecompressGfx
563 * Decompress the data part of a bitmap or a metafile
565 static BYTE* HLPFILE_DecompressGfx(BYTE* src, unsigned csz, unsigned sz, BYTE packing)
567 BYTE* dst;
568 BYTE* tmp;
569 BYTE* tmp2;
570 unsigned sz77;
572 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
574 switch (packing)
576 case 0: /* uncompressed */
577 if (sz != csz)
578 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
579 dst = src;
580 break;
581 case 1: /* RunLen */
582 tmp = dst = HeapAlloc(GetProcessHeap(), 0, sz);
583 if (!dst) return NULL;
584 HLPFILE_UncompressRLE(src, src + csz, &tmp, sz);
585 if (tmp - dst != sz)
586 WINE_WARN("Bogus gfx sizes (RunLen): %lu/%u\n", (SIZE_T)(tmp - dst), sz);
587 break;
588 case 2: /* LZ77 */
589 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
590 dst = HeapAlloc(GetProcessHeap(), 0, sz77);
591 if (!dst) return NULL;
592 HLPFILE_UncompressLZ77(src, src + csz, dst);
593 if (sz77 != sz)
594 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
595 break;
596 case 3: /* LZ77 then RLE */
597 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
598 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
599 if (!tmp) return FALSE;
600 HLPFILE_UncompressLZ77(src, src + csz, tmp);
601 dst = tmp2 = HeapAlloc(GetProcessHeap(), 0, sz);
602 if (!dst)
604 HeapFree(GetProcessHeap(), 0, tmp);
605 return FALSE;
607 HLPFILE_UncompressRLE(tmp, tmp + sz77, &tmp2, sz);
608 if (tmp2 - dst != sz)
609 WINE_WARN("Bogus gfx sizes (LZ77+RunLen): %lu / %u\n", (SIZE_T)(tmp2 - dst), sz);
610 HeapFree(GetProcessHeap(), 0, tmp);
611 break;
612 default:
613 WINE_FIXME("Unsupported packing %u\n", packing);
614 return NULL;
616 return dst;
619 /******************************************************************
620 * HLPFILE_LoadBitmap
624 static BOOL HLPFILE_LoadBitmap(BYTE* beg, BYTE type, BYTE pack,
625 HLPFILE_PARAGRAPH* paragraph)
627 BYTE* ptr;
628 BYTE* pict_beg;
629 BITMAPINFO* bi;
630 unsigned long off, csz;
631 HDC hdc;
633 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
634 if (!bi) return FALSE;
636 ptr = beg + 2; /* for type and pack */
638 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
639 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
640 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
641 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
642 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
643 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
644 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
645 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
646 bi->bmiHeader.biClrImportant = fetch_ulong(&ptr);
647 bi->bmiHeader.biCompression = BI_RGB;
648 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
649 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
650 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
651 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
652 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
653 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
655 csz = fetch_ulong(&ptr);
656 fetch_ulong(&ptr); /* hotspot size */
658 off = GET_UINT(ptr, 0); ptr += 4;
659 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
661 /* now read palette info */
662 if (type == 0x06)
664 unsigned nc = bi->bmiHeader.biClrUsed;
665 unsigned i;
667 /* not quite right, especially for bitfields type of compression */
668 if (!nc && bi->bmiHeader.biBitCount <= 8)
669 nc = 1 << bi->bmiHeader.biBitCount;
671 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
672 if (!bi) return FALSE;
673 for (i = 0; i < nc; i++)
675 bi->bmiColors[i].rgbBlue = ptr[0];
676 bi->bmiColors[i].rgbGreen = ptr[1];
677 bi->bmiColors[i].rgbRed = ptr[2];
678 bi->bmiColors[i].rgbReserved = 0;
679 ptr += 4;
682 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
684 paragraph->u.gfx.u.bmp.hBitmap = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
685 CBM_INIT, pict_beg,
686 bi, DIB_RGB_COLORS);
687 ReleaseDC(0, hdc);
688 if (!paragraph->u.gfx.u.bmp.hBitmap)
689 WINE_ERR("Couldn't create bitmap\n");
691 HeapFree(GetProcessHeap(), 0, bi);
692 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
694 return TRUE;
697 /******************************************************************
698 * HLPFILE_LoadMetaFile
702 static BOOL HLPFILE_LoadMetaFile(BYTE* beg, BYTE pack, HLPFILE_PARAGRAPH* paragraph)
704 BYTE* ptr;
705 unsigned long size, csize;
706 unsigned long off, hsoff;
707 BYTE* bits;
708 LPMETAFILEPICT lpmfp;
710 WINE_TRACE("Loading metafile\n");
712 ptr = beg + 2; /* for type and pack */
714 lpmfp = &paragraph->u.gfx.u.mfp;
715 lpmfp->mm = fetch_ushort(&ptr); /* mapping mode */
717 lpmfp->xExt = GET_USHORT(ptr, 0);
718 lpmfp->yExt = GET_USHORT(ptr, 2);
719 ptr += 4;
721 size = fetch_ulong(&ptr); /* decompressed size */
722 csize = fetch_ulong(&ptr); /* compressed size */
723 fetch_ulong(&ptr); /* hotspot size */
724 off = GET_UINT(ptr, 0);
725 hsoff = GET_UINT(ptr, 4);
726 ptr += 8;
728 WINE_TRACE("sz=%lu csz=%lu (%d,%d) offs=%lu/%lu,%lu\n",
729 size, csize, lpmfp->xExt, lpmfp->yExt, off, (SIZE_T)(ptr - beg), hsoff);
731 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
732 if (!bits) return FALSE;
734 paragraph->cookie = para_metafile;
736 lpmfp->hMF = SetMetaFileBitsEx(size, bits);
738 if (!lpmfp->hMF)
739 WINE_FIXME("Couldn't load metafile\n");
741 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
743 return TRUE;
746 /******************************************************************
747 * HLPFILE_LoadGfxByAddr
751 static BOOL HLPFILE_LoadGfxByAddr(HLPFILE *hlpfile, BYTE* ref,
752 unsigned long size,
753 HLPFILE_PARAGRAPH* paragraph)
755 unsigned i, numpict;
757 numpict = GET_USHORT(ref, 2);
758 WINE_TRACE("Got picture magic=%04x #=%d\n",
759 GET_USHORT(ref, 0), numpict);
761 for (i = 0; i < numpict; i++)
763 BYTE* beg;
764 BYTE* ptr;
765 BYTE type, pack;
767 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
768 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
770 type = *ptr++;
771 pack = *ptr++;
773 switch (type)
775 case 5: /* device dependent bmp */
776 case 6: /* device independent bmp */
777 HLPFILE_LoadBitmap(beg, type, pack, paragraph);
778 break;
779 case 8:
780 HLPFILE_LoadMetaFile(beg, pack, paragraph);
781 break;
782 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
785 /* FIXME: hotspots */
787 /* FIXME: implement support for multiple picture format */
788 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
789 break;
791 return TRUE;
794 /******************************************************************
795 * HLPFILE_LoadGfxByIndex
799 static BOOL HLPFILE_LoadGfxByIndex(HLPFILE *hlpfile, unsigned index,
800 HLPFILE_PARAGRAPH* paragraph)
802 char tmp[16];
803 BYTE *ref, *end;
804 BOOL ret;
806 WINE_TRACE("Loading picture #%d\n", index);
808 if (index < hlpfile->numBmps && hlpfile->bmps[index] != NULL)
810 paragraph->u.gfx.u.bmp.hBitmap = hlpfile->bmps[index];
811 return TRUE;
814 sprintf(tmp, "|bm%u", index);
816 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
818 ref += 9;
820 ret = HLPFILE_LoadGfxByAddr(hlpfile, ref, end - ref, paragraph);
822 /* cache bitmap */
823 if (ret && paragraph->cookie == para_bitmap)
825 if (index >= hlpfile->numBmps)
827 hlpfile->numBmps = index + 1;
828 if (hlpfile->bmps)
829 hlpfile->bmps = HeapReAlloc(GetProcessHeap(), 0, hlpfile->bmps,
830 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
831 else
832 hlpfile->bmps = HeapAlloc(GetProcessHeap(), 0,
833 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
836 hlpfile->bmps[index] = paragraph->u.gfx.u.bmp.hBitmap;
838 return ret;
841 /******************************************************************
842 * HLPFILE_AllocLink
846 static HLPFILE_LINK* HLPFILE_AllocLink(int cookie, const char* str, LONG hash,
847 BOOL clrChange, unsigned wnd)
849 HLPFILE_LINK* link;
850 char* link_str;
852 /* FIXME: should build a string table for the attributes.link.lpszPath
853 * they are reallocated for each link
855 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + strlen(str) + 1);
856 if (!link) return NULL;
858 link->cookie = cookie;
859 link->lpszString = link_str = (char*)link + sizeof(HLPFILE_LINK);
860 strcpy(link_str, str);
861 link->lHash = hash;
862 link->bClrChange = clrChange ? 1 : 0;
863 link->window = wnd;
864 link->wRefCount = 1;
866 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
867 link->cookie, link->lpszString,
868 link->lHash, link->window);
869 return link;
872 /***********************************************************************
874 * HLPFILE_BrowseParagraph
876 static BOOL HLPFILE_BrowseParagraph(HLPFILE_PAGE* page, BYTE *buf, BYTE* end)
878 HLPFILE_PARAGRAPH *paragraph, **paragraphptr;
879 UINT textsize;
880 BYTE *format, *format_end;
881 char *text, *text_base, *text_end;
882 long size, blocksize, datalen;
883 unsigned short bits;
884 unsigned nc, ncol = 1;
885 BOOL in_table = FALSE;
887 for (paragraphptr = &page->first_paragraph; *paragraphptr;
888 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
890 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
892 blocksize = GET_UINT(buf, 0);
893 size = GET_UINT(buf, 0x4);
894 datalen = GET_UINT(buf, 0x10);
895 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
896 if (!text) return FALSE;
897 if (size > blocksize - datalen)
899 /* need to decompress */
900 if (page->file->hasPhrases)
901 HLPFILE_Uncompress2(page->file, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
902 else if (page->file->hasPhrases40)
903 HLPFILE_Uncompress3(page->file, text, text + size, buf + datalen, end);
904 else
906 WINE_FIXME("Text size is too long, splitting\n");
907 size = blocksize - datalen;
908 memcpy(text, buf + datalen, size);
911 else
912 memcpy(text, buf + datalen, size);
914 text_end = text + size;
916 format = buf + 0x15;
917 format_end = buf + GET_UINT(buf, 0x10);
919 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
921 fetch_long(&format);
922 fetch_ushort(&format);
925 if (buf[0x14] == 0x23)
927 char type;
929 in_table = TRUE;
930 ncol = *format++;
932 WINE_TRACE("#cols %u\n", ncol);
933 type = *format++;
934 if (type == 0 || type == 2)
935 format += 2;
936 format += ncol * 4;
939 for (nc = 0; nc < ncol; /**/)
941 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
942 if (in_table)
944 nc = GET_SHORT(format, 0);
945 if (nc == -1) break;
946 format += 5;
948 else nc++;
949 format += 4;
950 bits = GET_USHORT(format, 0); format += 2;
951 if (bits & 0x0001) fetch_long(&format);
952 if (bits & 0x0002) fetch_short(&format);
953 if (bits & 0x0004) fetch_short(&format);
954 if (bits & 0x0008) fetch_short(&format);
955 if (bits & 0x0010) fetch_short(&format);
956 if (bits & 0x0020) fetch_short(&format);
957 if (bits & 0x0040) fetch_short(&format);
958 if (bits & 0x0100) format += 3;
959 if (bits & 0x0200)
961 int ntab = fetch_short(&format);
962 unsigned short ts;
964 while (ntab-- > 0)
966 ts = fetch_ushort(&format);
967 if (ts & 0x4000) fetch_ushort(&format);
970 /* 0x0400, 0x0800 and 0x1000 don't need space */
971 if ((bits & 0xE080) != 0)
972 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
974 while (text < text_end && format < format_end)
976 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
977 textsize = strlen(text) + 1;
978 if (textsize > 1)
980 paragraph = HeapAlloc(GetProcessHeap(), 0,
981 sizeof(HLPFILE_PARAGRAPH) + textsize);
982 if (!paragraph) return FALSE;
983 *paragraphptr = paragraph;
984 paragraphptr = &paragraph->next;
986 paragraph->next = NULL;
987 paragraph->link = attributes.link;
988 if (paragraph->link) paragraph->link->wRefCount++;
989 paragraph->cookie = para_normal_text;
990 paragraph->u.text.wFont = attributes.wFont;
991 paragraph->u.text.wVSpace = attributes.wVSpace;
992 paragraph->u.text.wHSpace = attributes.wHSpace;
993 paragraph->u.text.wIndent = attributes.wIndent;
994 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
995 strcpy(paragraph->u.text.lpszText, text);
997 attributes.wVSpace = 0;
998 attributes.wHSpace = 0;
1000 /* else: null text, keep on storing attributes */
1001 text += textsize;
1003 if (*format == 0xff)
1005 format++;
1006 break;
1009 WINE_TRACE("format=%02x\n", *format);
1010 switch (*format)
1012 case 0x20:
1013 WINE_FIXME("NIY20\n");
1014 format += 5;
1015 break;
1017 case 0x21:
1018 WINE_FIXME("NIY21\n");
1019 format += 3;
1020 break;
1022 case 0x80:
1023 attributes.wFont = GET_USHORT(format, 1);
1024 WINE_TRACE("Changing font to %d\n", attributes.wFont);
1025 format += 3;
1026 break;
1028 case 0x81:
1029 attributes.wVSpace++;
1030 format += 1;
1031 break;
1033 case 0x82:
1034 attributes.wVSpace++;
1035 attributes.wIndent = 0;
1036 format += 1;
1037 break;
1039 case 0x83:
1040 attributes.wIndent++;
1041 format += 1;
1042 break;
1044 #if 0
1045 case 0x84:
1046 format += 3;
1047 break;
1048 #endif
1050 case 0x86:
1051 case 0x87:
1052 case 0x88:
1054 BYTE pos = (*format - 0x86);
1055 BYTE type = format[1];
1056 long size;
1058 format += 2;
1059 size = fetch_long(&format);
1061 paragraph = HeapAlloc(GetProcessHeap(), 0,
1062 sizeof(HLPFILE_PARAGRAPH) + textsize);
1063 if (!paragraph) return FALSE;
1064 *paragraphptr = paragraph;
1065 paragraphptr = &paragraph->next;
1067 paragraph->next = NULL;
1068 paragraph->link = attributes.link;
1069 if (paragraph->link) paragraph->link->wRefCount++;
1070 paragraph->cookie = para_bitmap;
1071 paragraph->u.gfx.pos = pos;
1072 switch (type)
1074 case 0x22:
1075 fetch_ushort(&format); /* hot spot */
1076 /* fall thru */
1077 case 0x03:
1078 switch (GET_SHORT(format, 0))
1080 case 0:
1081 HLPFILE_LoadGfxByIndex(page->file, GET_SHORT(format, 2),
1082 paragraph);
1083 break;
1084 case 1:
1085 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1086 GET_SHORT(format, 0),
1087 size, GET_SHORT(format, 2));
1088 HLPFILE_LoadGfxByAddr(page->file, format + 2, size - 4,
1089 paragraph);
1090 break;
1091 default:
1092 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1093 break;
1095 break;
1096 case 0x05:
1097 WINE_FIXME("Got an embedded element %s\n", format + 6);
1098 break;
1099 default:
1100 WINE_FIXME("Got a type %d picture\n", type);
1101 break;
1103 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1105 format += size;
1107 break;
1109 case 0x89:
1110 HLPFILE_FreeLink(attributes.link);
1111 attributes.link = NULL;
1112 format += 1;
1113 break;
1115 case 0x8B:
1116 case 0x8C:
1117 WINE_FIXME("NIY non-break space/hyphen\n");
1118 format += 1;
1119 break;
1121 #if 0
1122 case 0xA9:
1123 format += 2;
1124 break;
1125 #endif
1127 case 0xC8:
1128 case 0xCC:
1129 WINE_TRACE("macro => %s\n", format + 3);
1130 HLPFILE_FreeLink(attributes.link);
1131 attributes.link = HLPFILE_AllocLink(hlp_link_macro, (const char*)format + 3,
1132 0, !(*format & 4), -1);
1133 format += 3 + GET_USHORT(format, 1);
1134 break;
1136 case 0xE0:
1137 case 0xE1:
1138 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1139 HLPFILE_FreeLink(attributes.link);
1140 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1141 page->file->lpszPath,
1142 GET_UINT(format, 1)-16,
1143 1, -1);
1146 format += 5;
1147 break;
1149 case 0xE2:
1150 case 0xE3:
1151 case 0xE6:
1152 case 0xE7:
1153 HLPFILE_FreeLink(attributes.link);
1154 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1155 page->file->lpszPath,
1156 GET_UINT(format, 1),
1157 !(*format & 4), -1);
1158 format += 5;
1159 break;
1161 case 0xEA:
1162 case 0xEB:
1163 case 0xEE:
1164 case 0xEF:
1166 char* ptr = (char*) format + 8;
1167 BYTE type = format[3];
1168 int wnd = -1;
1170 switch (type)
1172 case 1:
1173 wnd = *ptr;
1174 /* fall through */
1175 case 0:
1176 ptr = page->file->lpszPath;
1177 break;
1178 case 6:
1179 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1181 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1183 if (wnd == -1)
1184 WINE_WARN("Couldn't find window info for %s\n", ptr);
1185 ptr += strlen(ptr) + 1;
1186 /* fall through */
1187 case 4:
1188 break;
1189 default:
1190 WINE_WARN("Unknown link type %d\n", type);
1191 break;
1193 HLPFILE_FreeLink(attributes.link);
1194 attributes.link = HLPFILE_AllocLink((*format & 4) ? hlp_link_link : hlp_link_popup,
1195 ptr, GET_UINT(format, 4),
1196 !(*format & 1), wnd);
1198 format += 3 + GET_USHORT(format, 1);
1199 break;
1201 default:
1202 WINE_WARN("format %02x\n", *format);
1203 format++;
1207 HeapFree(GetProcessHeap(), 0, text_base);
1208 return TRUE;
1211 /******************************************************************
1212 * HLPFILE_BrowsePage
1215 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page)
1217 HLPFILE *hlpfile = page->file;
1218 BYTE *buf, *end;
1219 DWORD ref = page->reference;
1220 unsigned index, old_index = -1, offset, count = 0;
1224 if (hlpfile->version <= 16)
1226 index = (ref - 0x0C) / hlpfile->dsize;
1227 offset = (ref - 0x0C) % hlpfile->dsize;
1229 else
1231 index = (ref - 0x0C) >> 14;
1232 offset = (ref - 0x0C) & 0x3FFF;
1235 if (hlpfile->version <= 16 && index != old_index && index != 0)
1237 /* we jumped to the next block, adjust pointers */
1238 ref -= 12;
1239 offset -= 12;
1242 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1243 buf = hlpfile->topic_map[index] + offset;
1244 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1245 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1246 if (index != old_index) {old_index = index;}
1248 switch (buf[0x14])
1250 case 0x02:
1251 if (count++) goto done;
1252 break;
1253 case 0x01:
1254 case 0x20:
1255 case 0x23:
1256 if (!HLPFILE_BrowseParagraph(page, buf, end)) return FALSE;
1257 break;
1258 default:
1259 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1261 if (hlpfile->version <= 16)
1263 ref += GET_UINT(buf, 0xc);
1264 if (GET_UINT(buf, 0xc) == 0)
1265 break;
1267 else
1268 ref = GET_UINT(buf, 0xc);
1269 } while (ref != 0xffffffff);
1270 done:
1271 return TRUE;
1274 /******************************************************************
1275 * HLPFILE_ReadFont
1279 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1281 BYTE *ref, *end;
1282 unsigned i, len, idx;
1283 unsigned face_num, dscr_num, face_offset, dscr_offset;
1284 BYTE flag, family;
1286 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1288 WINE_WARN("no subfile FONT\n");
1289 hlpfile->numFonts = 0;
1290 hlpfile->fonts = NULL;
1291 return FALSE;
1294 ref += 9;
1296 face_num = GET_USHORT(ref, 0);
1297 dscr_num = GET_USHORT(ref, 2);
1298 face_offset = GET_USHORT(ref, 4);
1299 dscr_offset = GET_USHORT(ref, 6);
1301 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1302 face_num, face_offset, dscr_num, dscr_offset);
1304 hlpfile->numFonts = dscr_num;
1305 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1307 len = (dscr_offset - face_offset) / face_num;
1308 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1309 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1310 for (i = 0; i < dscr_num; i++)
1312 flag = ref[dscr_offset + i * 11 + 0];
1313 family = ref[dscr_offset + i * 11 + 2];
1315 hlpfile->fonts[i].LogFont.lfHeight = -ref[dscr_offset + i * 11 + 1] / 2 - 3;
1316 hlpfile->fonts[i].LogFont.lfWidth = 0;
1317 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1318 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1319 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1320 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1321 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1322 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1323 hlpfile->fonts[i].LogFont.lfCharSet = DEFAULT_CHARSET;
1324 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1325 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1326 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1327 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1329 switch (family)
1331 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1332 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1333 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1334 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1335 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1336 default: WINE_FIXME("Unknown family %u\n", family);
1338 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1340 if (idx < face_num)
1342 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1343 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1345 else
1347 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1348 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1350 hlpfile->fonts[i].hFont = 0;
1351 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1352 ref[dscr_offset + i * 11 + 6],
1353 ref[dscr_offset + i * 11 + 7]);
1354 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1355 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1356 i, flag,
1357 X(0, "bold"),
1358 X(1, "italic"),
1359 X(2, "underline"),
1360 X(3, "strikeOut"),
1361 X(4, "dblUnderline"),
1362 X(5, "smallCaps"),
1363 ref[dscr_offset + i * 11 + 1],
1364 family,
1365 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1366 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1368 return TRUE;
1371 /***********************************************************************
1373 * HLPFILE_ReadFileToBuffer
1375 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1377 BYTE header[16], dummy[1];
1379 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1381 /* sanity checks */
1382 if (GET_UINT(header, 0) != 0x00035F3F)
1383 {WINE_WARN("wrong header\n"); return FALSE;};
1385 hlpfile->file_buffer_size = GET_UINT(header, 12);
1386 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1387 if (!hlpfile->file_buffer) return FALSE;
1389 memcpy(hlpfile->file_buffer, header, 16);
1390 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1391 {WINE_WARN("filesize1\n"); return FALSE;};
1393 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1395 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1397 return TRUE;
1400 /**************************************************************************
1401 * comp_FindSubFile
1403 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1406 static int comp_FindSubFile(void *p, const void *key,
1407 int leaf, void** next)
1409 *next = (char *)p+strlen(p)+(leaf?5:3);
1410 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1411 return strcmp(p, key);
1414 /***********************************************************************
1416 * HLPFILE_FindSubFile
1418 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
1420 BYTE *ptr;
1422 WINE_TRACE("looking for file '%s'\n", name);
1423 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
1424 name, comp_FindSubFile);
1425 if (!ptr) return FALSE;
1426 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
1427 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
1429 WINE_ERR("internal file %s does not fit\n", name);
1430 return FALSE;
1432 *subend = *subbuf + GET_UINT(*subbuf, 0);
1433 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
1435 WINE_ERR("internal file %s does not fit\n", name);
1436 return FALSE;
1438 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1440 WINE_ERR("invalid size provided for internal file %s\n", name);
1441 return FALSE;
1443 return TRUE;
1446 /***********************************************************************
1448 * HLPFILE_SystemCommands
1450 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1452 BYTE *buf, *ptr, *end;
1453 HLPFILE_MACRO *macro, **m;
1454 LPSTR p;
1455 unsigned short magic, minor, major, flags;
1457 hlpfile->lpszTitle = NULL;
1459 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1461 magic = GET_USHORT(buf + 9, 0);
1462 minor = GET_USHORT(buf + 9, 2);
1463 major = GET_USHORT(buf + 9, 4);
1464 /* gen date on 4 bytes */
1465 flags = GET_USHORT(buf + 9, 10);
1466 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1467 magic, major, minor, flags);
1468 if (magic != 0x036C || major != 1)
1469 {WINE_WARN("Wrong system header\n"); return FALSE;}
1470 if (minor <= 16)
1472 hlpfile->tbsize = 0x800;
1473 hlpfile->compressed = 0;
1475 else if (flags == 0)
1477 hlpfile->tbsize = 0x1000;
1478 hlpfile->compressed = 0;
1480 else if (flags == 4)
1482 hlpfile->tbsize = 0x1000;
1483 hlpfile->compressed = 1;
1485 else
1487 hlpfile->tbsize = 0x800;
1488 hlpfile->compressed = 1;
1491 if (hlpfile->compressed)
1492 hlpfile->dsize = 0x4000;
1493 else
1494 hlpfile->dsize = hlpfile->tbsize - 0x0C;
1496 hlpfile->version = minor;
1497 hlpfile->flags = flags;
1499 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1501 char *str = (char*) ptr + 4;
1502 switch (GET_USHORT(ptr, 0))
1504 case 1:
1505 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1506 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1507 if (!hlpfile->lpszTitle) return FALSE;
1508 lstrcpy(hlpfile->lpszTitle, str);
1509 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1510 break;
1512 case 2:
1513 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1514 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1515 if (!hlpfile->lpszCopyright) return FALSE;
1516 lstrcpy(hlpfile->lpszCopyright, str);
1517 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1518 break;
1520 case 3:
1521 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1522 hlpfile->contents_start = GET_UINT(ptr, 4);
1523 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1524 break;
1526 case 4:
1527 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1528 if (!macro) break;
1529 p = (char*)macro + sizeof(HLPFILE_MACRO);
1530 lstrcpy(p, str);
1531 macro->lpszMacro = p;
1532 macro->next = 0;
1533 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1534 *m = macro;
1535 break;
1537 case 6:
1538 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1540 if (hlpfile->windows)
1541 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1542 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1543 else
1544 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1545 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1547 if (hlpfile->windows)
1549 unsigned flags = GET_USHORT(ptr, 4);
1550 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1552 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1553 else wi->type[0] = '\0';
1554 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1555 else wi->name[0] = '\0';
1556 if (flags & 0x0004) strcpy(wi->caption, &str[21]);
1557 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1558 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1559 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1560 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1561 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1562 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1563 wi->win_style = WS_OVERLAPPEDWINDOW;
1564 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1565 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1566 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",
1567 flags & 0x0001 ? 'T' : 't',
1568 flags & 0x0002 ? 'N' : 'n',
1569 flags & 0x0004 ? 'C' : 'c',
1570 flags & 0x0008 ? 'X' : 'x',
1571 flags & 0x0010 ? 'Y' : 'y',
1572 flags & 0x0020 ? 'W' : 'w',
1573 flags & 0x0040 ? 'H' : 'h',
1574 flags & 0x0080 ? 'S' : 's',
1575 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1576 wi->size.cx, wi->size.cy);
1578 break;
1579 case 8:
1580 WINE_WARN("Citation: '%s'\n", ptr + 4);
1581 break;
1582 default:
1583 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1586 if (!hlpfile->lpszTitle)
1587 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
1588 return TRUE;
1591 /***********************************************************************
1593 * HLPFILE_UncompressedLZ77_Size
1595 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
1597 int i, newsize = 0;
1599 while (ptr < end)
1601 int mask = *ptr++;
1602 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1604 if (mask & 1)
1606 int code = GET_USHORT(ptr, 0);
1607 int len = 3 + (code >> 12);
1608 newsize += len;
1609 ptr += 2;
1611 else newsize++, ptr++;
1615 return newsize;
1618 /***********************************************************************
1620 * HLPFILE_UncompressLZ77
1622 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
1624 int i;
1626 while (ptr < end)
1628 int mask = *ptr++;
1629 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1631 if (mask & 1)
1633 int code = GET_USHORT(ptr, 0);
1634 int len = 3 + (code >> 12);
1635 int offset = code & 0xfff;
1637 * We must copy byte-by-byte here. We cannot use memcpy nor
1638 * memmove here. Just example:
1639 * a[]={1,2,3,4,5,6,7,8,9,10}
1640 * newptr=a+2;
1641 * offset=1;
1642 * We expect:
1643 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
1645 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
1646 ptr += 2;
1648 else *newptr++ = *ptr++;
1652 return newptr;
1655 /***********************************************************************
1657 * HLPFILE_UncompressLZ77_Phrases
1659 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
1661 UINT i, num, dec_size, head_size;
1662 BYTE *buf, *end;
1664 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
1666 if (hlpfile->version <= 16)
1667 head_size = 13;
1668 else
1669 head_size = 17;
1671 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
1672 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
1674 if (hlpfile->version <= 16)
1675 dec_size = end - buf - 15 - 2 * num;
1676 else
1677 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
1679 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1680 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1681 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
1683 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
1684 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
1685 return FALSE;
1688 for (i = 0; i <= num; i++)
1689 hlpfile->phrases_offsets[i] = GET_USHORT(buf, 0x11 + 2 * i) - 2 * num - 2;
1691 if (hlpfile->version <= 16)
1692 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
1693 else
1694 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
1696 hlpfile->hasPhrases = TRUE;
1697 return TRUE;
1700 /***********************************************************************
1702 * HLPFILE_Uncompress_Phrases40
1704 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
1706 UINT num;
1707 INT dec_size, cpr_size;
1708 BYTE *buf_idx, *end_idx;
1709 BYTE *buf_phs, *end_phs;
1710 long* ptr, mask = 0;
1711 unsigned int i;
1712 unsigned short bc, n;
1714 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
1715 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
1717 ptr = (long*)(buf_idx + 9 + 28);
1718 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
1719 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
1721 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
1722 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
1723 GET_UINT(buf_idx, 9 + 0),
1724 GET_UINT(buf_idx, 9 + 4),
1725 GET_UINT(buf_idx, 9 + 8),
1726 GET_UINT(buf_idx, 9 + 12),
1727 GET_UINT(buf_idx, 9 + 16),
1728 GET_UINT(buf_idx, 9 + 20),
1729 GET_USHORT(buf_idx, 9 + 24),
1730 GET_USHORT(buf_idx, 9 + 26));
1732 dec_size = GET_UINT(buf_idx, 9 + 12);
1733 cpr_size = GET_UINT(buf_idx, 9 + 16);
1735 if (dec_size != cpr_size &&
1736 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
1738 WINE_WARN("size mismatch %u %u\n",
1739 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1740 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1743 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1744 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1745 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
1747 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
1748 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
1749 return FALSE;
1752 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
1754 hlpfile->phrases_offsets[0] = 0;
1755 for (i = 0; i < num; i++)
1757 for (n = 1; getbit(); n += 1 << bc);
1758 if (getbit()) n++;
1759 if (bc > 1 && getbit()) n += 2;
1760 if (bc > 2 && getbit()) n += 4;
1761 if (bc > 3 && getbit()) n += 8;
1762 if (bc > 4 && getbit()) n += 16;
1763 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
1765 #undef getbit
1767 if (dec_size == cpr_size)
1768 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
1769 else
1770 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
1772 hlpfile->hasPhrases40 = TRUE;
1773 return TRUE;
1776 /***********************************************************************
1778 * HLPFILE_Uncompress_Topic
1780 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
1782 BYTE *buf, *ptr, *end, *newptr;
1783 unsigned int i, newsize = 0;
1784 unsigned int topic_size;
1786 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
1787 {WINE_WARN("topic0\n"); return FALSE;}
1789 buf += 9; /* Skip file header */
1790 topic_size = end - buf;
1791 if (hlpfile->compressed)
1793 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
1795 for (i = 0; i < hlpfile->topic_maplen; i++)
1797 ptr = buf + i * hlpfile->tbsize;
1799 /* I don't know why, it's necessary for printman.hlp */
1800 if (ptr + 0x44 > end) ptr = end - 0x44;
1802 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
1805 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
1806 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
1807 if (!hlpfile->topic_map) return FALSE;
1808 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
1809 hlpfile->topic_end = newptr + newsize;
1811 for (i = 0; i < hlpfile->topic_maplen; i++)
1813 ptr = buf + i * hlpfile->tbsize;
1814 if (ptr + 0x44 > end) ptr = end - 0x44;
1816 hlpfile->topic_map[i] = newptr;
1817 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
1820 else
1822 /* basically, we need to copy the TopicBlockSize byte pages
1823 * (removing the first 0x0C) in one single area in memory
1825 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
1826 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
1827 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
1828 if (!hlpfile->topic_map) return FALSE;
1829 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
1830 hlpfile->topic_end = newptr + topic_size;
1832 for (i = 0; i < hlpfile->topic_maplen; i++)
1834 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
1835 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
1838 return TRUE;
1841 /***********************************************************************
1843 * HLPFILE_Uncompress2
1846 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
1848 BYTE *phptr, *phend;
1849 UINT code;
1850 UINT index;
1852 while (ptr < end && newptr < newend)
1854 if (!*ptr || *ptr >= 0x10)
1855 *newptr++ = *ptr++;
1856 else
1858 code = 0x100 * ptr[0] + ptr[1];
1859 index = (code - 0x100) / 2;
1861 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
1862 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
1864 if (newptr + (phend - phptr) > newend)
1866 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
1867 newptr, newend, (SIZE_T)(phend - phptr));
1868 return;
1870 memcpy(newptr, phptr, phend - phptr);
1871 newptr += phend - phptr;
1872 if (code & 1) *newptr++ = ' ';
1874 ptr += 2;
1877 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
1880 /******************************************************************
1881 * HLPFILE_Uncompress3
1885 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
1886 const BYTE* src, const BYTE* src_end)
1888 unsigned int idx, len;
1890 for (; src < src_end; src++)
1892 if ((*src & 1) == 0)
1894 idx = *src / 2;
1895 if (idx > hlpfile->num_phrases)
1897 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
1898 len = 0;
1900 else
1902 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
1903 if (dst + len <= dst_end)
1904 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
1907 else if ((*src & 0x03) == 0x01)
1909 idx = (*src + 1) * 64;
1910 idx += *++src;
1911 if (idx > hlpfile->num_phrases)
1913 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
1914 len = 0;
1916 else
1918 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
1919 if (dst + len <= dst_end)
1920 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
1923 else if ((*src & 0x07) == 0x03)
1925 len = (*src / 8) + 1;
1926 if (dst + len <= dst_end)
1927 memcpy(dst, src + 1, len);
1928 src += len;
1930 else
1932 len = (*src / 16) + 1;
1933 if (dst + len <= dst_end)
1934 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
1936 dst += len;
1939 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
1940 return TRUE;
1943 /******************************************************************
1944 * HLPFILE_UncompressRLE
1948 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
1950 BYTE ch;
1951 BYTE* sdst = *dst + dstsz;
1953 while (src < end)
1955 ch = *src++;
1956 if (ch & 0x80)
1958 ch &= 0x7F;
1959 if ((*dst) + ch <= sdst)
1960 memcpy(*dst, src, ch);
1961 src += ch;
1963 else
1965 if ((*dst) + ch <= sdst)
1966 memset(*dst, (char)*src, ch);
1967 src++;
1969 *dst += ch;
1971 if (*dst != sdst)
1972 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
1973 (SIZE_T)(*dst - (sdst - dstsz)), dstsz);
1976 /**************************************************************************
1977 * HLPFILE_BPTreeSearch
1979 * Searches for an element in B+ tree
1981 * PARAMS
1982 * buf [I] pointer to the embedded file structured as a B+ tree
1983 * key [I] pointer to data to find
1984 * comp [I] compare function
1986 * RETURNS
1987 * Pointer to block identified by key, or NULL if failure.
1990 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
1991 HLPFILE_BPTreeCompare comp)
1993 unsigned magic;
1994 unsigned page_size;
1995 unsigned cur_page;
1996 unsigned level;
1997 BYTE *pages, *ptr, *newptr;
1998 int i, entries;
1999 int ret;
2001 magic = GET_USHORT(buf, 9);
2002 if (magic != 0x293B)
2004 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2005 return NULL;
2007 page_size = GET_USHORT(buf, 9+4);
2008 cur_page = GET_USHORT(buf, 9+26);
2009 level = GET_USHORT(buf, 9+32);
2010 pages = buf + 9 + 38;
2011 while (--level > 0)
2013 ptr = pages + cur_page*page_size;
2014 entries = GET_SHORT(ptr, 2);
2015 ptr += 6;
2016 for (i = 0; i < entries; i++)
2018 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
2019 ptr = newptr;
2021 cur_page = GET_USHORT(ptr-2, 0);
2023 ptr = pages + cur_page*page_size;
2024 entries = GET_SHORT(ptr, 2);
2025 ptr += 8;
2026 for (i = 0; i < entries; i++)
2028 ret = comp(ptr, key, 1, (void **)&newptr);
2029 if (ret == 0) return ptr;
2030 if (ret > 0) return NULL;
2031 ptr = newptr;
2033 return NULL;
2036 /**************************************************************************
2037 * HLPFILE_BPTreeEnum
2039 * Enumerates elements in B+ tree.
2041 * PARAMS
2042 * buf [I] pointer to the embedded file structured as a B+ tree
2043 * cb [I] compare function
2044 * cookie [IO] cookie for cb function
2046 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
2048 unsigned magic;
2049 unsigned page_size;
2050 unsigned cur_page;
2051 unsigned level;
2052 BYTE *pages, *ptr, *newptr;
2053 int i, entries;
2055 magic = GET_USHORT(buf, 9);
2056 if (magic != 0x293B)
2058 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2059 return;
2061 page_size = GET_USHORT(buf, 9+4);
2062 cur_page = GET_USHORT(buf, 9+26);
2063 level = GET_USHORT(buf, 9+32);
2064 pages = buf + 9 + 38;
2065 while (--level > 0)
2067 ptr = pages + cur_page*page_size;
2068 cur_page = GET_USHORT(ptr, 4);
2070 while (cur_page != 0xFFFF)
2072 ptr = pages + cur_page*page_size;
2073 entries = GET_SHORT(ptr, 2);
2074 ptr += 8;
2075 for (i = 0; i < entries; i++)
2077 cb(ptr, (void **)&newptr, cookie);
2078 ptr = newptr;
2080 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
2085 /***********************************************************************
2087 * HLPFILE_GetContext
2089 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2091 BYTE *cbuf, *cend;
2092 unsigned clen;
2094 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2095 {WINE_WARN("context0\n"); return FALSE;}
2097 clen = cend - cbuf;
2098 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2099 if (!hlpfile->Context) return FALSE;
2100 memcpy(hlpfile->Context, cbuf, clen);
2102 return TRUE;
2105 /***********************************************************************
2107 * HLPFILE_GetKeywords
2109 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2111 BYTE *cbuf, *cend;
2112 unsigned clen;
2114 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2115 clen = cend - cbuf;
2116 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2117 if (!hlpfile->kwbtree) return FALSE;
2118 memcpy(hlpfile->kwbtree, cbuf, clen);
2120 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2122 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2123 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2124 return FALSE;
2126 clen = cend - cbuf;
2127 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2128 if (!hlpfile->kwdata)
2130 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2131 return FALSE;
2133 memcpy(hlpfile->kwdata, cbuf, clen);
2135 return TRUE;
2138 /***********************************************************************
2140 * HLPFILE_GetMap
2142 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2144 BYTE *cbuf, *cend;
2145 unsigned entries, i;
2147 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2148 {WINE_WARN("no map section\n"); return FALSE;}
2150 entries = GET_USHORT(cbuf, 9);
2151 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2152 if (!hlpfile->Map) return FALSE;
2153 hlpfile->wMapLen = entries;
2154 for (i = 0; i < entries; i++)
2156 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2157 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2159 return TRUE;
2162 /******************************************************************
2163 * HLPFILE_DeleteLink
2167 void HLPFILE_FreeLink(HLPFILE_LINK* link)
2169 if (link && !--link->wRefCount)
2170 HeapFree(GetProcessHeap(), 0, link);
2173 /***********************************************************************
2175 * HLPFILE_DeleteParagraph
2177 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
2179 HLPFILE_PARAGRAPH* next;
2181 while (paragraph)
2183 next = paragraph->next;
2185 if (paragraph->cookie == para_metafile)
2186 DeleteMetaFile(paragraph->u.gfx.u.mfp.hMF);
2188 HLPFILE_FreeLink(paragraph->link);
2190 HeapFree(GetProcessHeap(), 0, paragraph);
2191 paragraph = next;
2195 /***********************************************************************
2197 * DeleteMacro
2199 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2201 HLPFILE_MACRO* next;
2203 while (macro)
2205 next = macro->next;
2206 HeapFree(GetProcessHeap(), 0, macro);
2207 macro = next;
2211 /***********************************************************************
2213 * DeletePage
2215 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2217 HLPFILE_PAGE* next;
2219 while (page)
2221 next = page->next;
2222 HLPFILE_DeleteParagraph(page->first_paragraph);
2223 HLPFILE_DeleteMacro(page->first_macro);
2224 HeapFree(GetProcessHeap(), 0, page);
2225 page = next;
2229 /***********************************************************************
2231 * HLPFILE_FreeHlpFile
2233 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2235 unsigned i;
2237 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2239 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2240 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2241 else first_hlpfile = hlpfile->next;
2243 if (hlpfile->numFonts)
2245 for (i = 0; i < hlpfile->numFonts; i++)
2247 DeleteObject(hlpfile->fonts[i].hFont);
2249 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2252 if (hlpfile->numBmps)
2254 for (i = 0; i < hlpfile->numBmps; i++)
2256 DeleteObject(hlpfile->bmps[i]);
2258 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2261 HLPFILE_DeletePage(hlpfile->first_page);
2262 HLPFILE_DeleteMacro(hlpfile->first_macro);
2264 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2265 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2266 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2267 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2268 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2269 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2270 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2271 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2272 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2273 HeapFree(GetProcessHeap(), 0, hlpfile);