Spelling fixes.
[wine/gsoc_dplay.git] / programs / winhelp / hlpfile.c
blob2bbc68a5ae033c9afacd19e5ecfd4df2e7ca9561
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;
886 for (paragraphptr = &page->first_paragraph; *paragraphptr;
887 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
889 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
891 blocksize = GET_UINT(buf, 0);
892 size = GET_UINT(buf, 0x4);
893 datalen = GET_UINT(buf, 0x10);
894 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
895 if (!text) return FALSE;
896 if (size > blocksize - datalen)
898 /* need to decompress */
899 if (page->file->hasPhrases)
900 HLPFILE_Uncompress2(page->file, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
901 else if (page->file->hasPhrases40)
902 HLPFILE_Uncompress3(page->file, text, text + size, buf + datalen, end);
903 else
905 WINE_FIXME("Text size is too long, splitting\n");
906 size = blocksize - datalen;
907 memcpy(text, buf + datalen, size);
910 else
911 memcpy(text, buf + datalen, size);
913 text_end = text + size;
915 format = buf + 0x15;
916 format_end = buf + GET_UINT(buf, 0x10);
918 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
920 fetch_long(&format);
921 fetch_ushort(&format);
924 if (buf[0x14] == 0x23)
926 char type;
928 ncol = *format++;
930 WINE_TRACE("#cols %u\n", ncol);
931 type = *format++;
932 if (type == 0 || type == 2)
933 format += 2;
934 format += ncol * 4;
937 for (nc = 0; nc < ncol; nc++)
939 WINE_TRACE("looking for format at offset %lu for column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
940 if (buf[0x14] == 0x23)
941 format += 5;
942 if (buf[0x14] == 0x01)
943 format += 6;
944 else
945 format += 4;
946 bits = GET_USHORT(format, 0); format += 2;
947 if (bits & 0x0001) fetch_long(&format);
948 if (bits & 0x0002) fetch_short(&format);
949 if (bits & 0x0004) fetch_short(&format);
950 if (bits & 0x0008) fetch_short(&format);
951 if (bits & 0x0010) fetch_short(&format);
952 if (bits & 0x0020) fetch_short(&format);
953 if (bits & 0x0040) fetch_short(&format);
954 if (bits & 0x0100) format += 3;
955 if (bits & 0x0200)
957 int ntab = fetch_short(&format);
958 unsigned short ts;
960 while (ntab-- > 0)
962 ts = fetch_ushort(&format);
963 if (ts & 0x4000) fetch_ushort(&format);
966 /* 0x0400, 0x0800 and 0x1000 don't need space */
967 if ((bits & 0xE080) != 0)
968 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
970 while (text < text_end && format < format_end)
972 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
973 textsize = strlen(text) + 1;
974 if (textsize > 1)
976 paragraph = HeapAlloc(GetProcessHeap(), 0,
977 sizeof(HLPFILE_PARAGRAPH) + textsize);
978 if (!paragraph) return FALSE;
979 *paragraphptr = paragraph;
980 paragraphptr = &paragraph->next;
982 paragraph->next = NULL;
983 paragraph->link = attributes.link;
984 if (paragraph->link) paragraph->link->wRefCount++;
985 paragraph->cookie = para_normal_text;
986 paragraph->u.text.wFont = attributes.wFont;
987 paragraph->u.text.wVSpace = attributes.wVSpace;
988 paragraph->u.text.wHSpace = attributes.wHSpace;
989 paragraph->u.text.wIndent = attributes.wIndent;
990 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
991 strcpy(paragraph->u.text.lpszText, text);
993 attributes.wVSpace = 0;
994 attributes.wHSpace = 0;
996 /* else: null text, keep on storing attributes */
997 text += textsize;
999 if (*format == 0xff)
1001 format++;
1002 break;
1005 WINE_TRACE("format=%02x\n", *format);
1006 switch (*format)
1008 case 0x20:
1009 WINE_FIXME("NIY20\n");
1010 format += 5;
1011 break;
1013 case 0x21:
1014 WINE_FIXME("NIY21\n");
1015 format += 3;
1016 break;
1018 case 0x80:
1019 attributes.wFont = GET_USHORT(format, 1);
1020 WINE_TRACE("Changing font to %d\n", attributes.wFont);
1021 format += 3;
1022 break;
1024 case 0x81:
1025 attributes.wVSpace++;
1026 format += 1;
1027 break;
1029 case 0x82:
1030 attributes.wVSpace++;
1031 attributes.wIndent = 0;
1032 format += 1;
1033 break;
1035 case 0x83:
1036 attributes.wIndent++;
1037 format += 1;
1038 break;
1040 #if 0
1041 case 0x84:
1042 format += 3;
1043 break;
1044 #endif
1046 case 0x86:
1047 case 0x87:
1048 case 0x88:
1050 BYTE pos = (*format - 0x86);
1051 BYTE type = format[1];
1052 long size;
1054 format += 2;
1055 size = fetch_long(&format);
1057 paragraph = HeapAlloc(GetProcessHeap(), 0,
1058 sizeof(HLPFILE_PARAGRAPH) + textsize);
1059 if (!paragraph) return FALSE;
1060 *paragraphptr = paragraph;
1061 paragraphptr = &paragraph->next;
1063 paragraph->next = NULL;
1064 paragraph->link = attributes.link;
1065 if (paragraph->link) paragraph->link->wRefCount++;
1066 paragraph->cookie = para_bitmap;
1067 paragraph->u.gfx.pos = pos;
1068 switch (type)
1070 case 0x22:
1071 fetch_ushort(&format); /* hot spot */
1072 /* fall thru */
1073 case 0x03:
1074 switch (GET_SHORT(format, 0))
1076 case 0:
1077 HLPFILE_LoadGfxByIndex(page->file, GET_SHORT(format, 2),
1078 paragraph);
1079 break;
1080 case 1:
1081 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1082 GET_SHORT(format, 0),
1083 size, GET_SHORT(format, 2));
1084 HLPFILE_LoadGfxByAddr(page->file, format + 2, size - 4,
1085 paragraph);
1086 break;
1087 default:
1088 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1089 break;
1091 break;
1092 case 0x05:
1093 WINE_FIXME("Got an embedded element %s\n", format + 6);
1094 break;
1095 default:
1096 WINE_FIXME("Got a type %d picture\n", type);
1097 break;
1099 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1101 format += size;
1103 break;
1105 case 0x89:
1106 HLPFILE_FreeLink(attributes.link);
1107 attributes.link = NULL;
1108 format += 1;
1109 break;
1111 case 0x8B:
1112 case 0x8C:
1113 WINE_FIXME("NIY non-break space/hyphen\n");
1114 format += 1;
1115 break;
1117 #if 0
1118 case 0xA9:
1119 format += 2;
1120 break;
1121 #endif
1123 case 0xC8:
1124 case 0xCC:
1125 WINE_TRACE("macro => %s\n", format + 3);
1126 HLPFILE_FreeLink(attributes.link);
1127 attributes.link = HLPFILE_AllocLink(hlp_link_macro, (const char*)format + 3,
1128 0, !(*format & 4), -1);
1129 format += 3 + GET_USHORT(format, 1);
1130 break;
1132 case 0xE0:
1133 case 0xE1:
1134 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1135 HLPFILE_FreeLink(attributes.link);
1136 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1137 page->file->lpszPath,
1138 GET_UINT(format, 1)-16,
1139 1, -1);
1142 format += 5;
1143 break;
1145 case 0xE2:
1146 case 0xE3:
1147 case 0xE6:
1148 case 0xE7:
1149 HLPFILE_FreeLink(attributes.link);
1150 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1151 page->file->lpszPath,
1152 GET_UINT(format, 1),
1153 !(*format & 4), -1);
1154 format += 5;
1155 break;
1157 case 0xEA:
1158 case 0xEB:
1159 case 0xEE:
1160 case 0xEF:
1162 char* ptr = (char*) format + 8;
1163 BYTE type = format[3];
1164 int wnd = -1;
1166 switch (type)
1168 case 1:
1169 wnd = *ptr;
1170 /* fall through */
1171 case 0:
1172 ptr = page->file->lpszPath;
1173 break;
1174 case 6:
1175 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1177 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1179 if (wnd == -1)
1180 WINE_WARN("Couldn't find window info for %s\n", ptr);
1181 ptr += strlen(ptr) + 1;
1182 /* fall through */
1183 case 4:
1184 break;
1185 default:
1186 WINE_WARN("Unknown link type %d\n", type);
1187 break;
1189 HLPFILE_FreeLink(attributes.link);
1190 attributes.link = HLPFILE_AllocLink((*format & 4) ? hlp_link_link : hlp_link_popup,
1191 ptr, GET_UINT(format, 4),
1192 !(*format & 1), wnd);
1194 format += 3 + GET_USHORT(format, 1);
1195 break;
1197 default:
1198 WINE_WARN("format %02x\n", *format);
1199 format++;
1203 HeapFree(GetProcessHeap(), 0, text_base);
1204 return TRUE;
1207 /******************************************************************
1208 * HLPFILE_BrowsePage
1211 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page)
1213 HLPFILE *hlpfile = page->file;
1214 BYTE *buf, *end;
1215 DWORD ref = page->reference;
1216 unsigned index, old_index = -1, offset, count = 0;
1220 if (hlpfile->version <= 16)
1222 index = (ref - 0x0C) / hlpfile->dsize;
1223 offset = (ref - 0x0C) % hlpfile->dsize;
1225 else
1227 index = (ref - 0x0C) >> 14;
1228 offset = (ref - 0x0C) & 0x3FFF;
1231 if (hlpfile->version <= 16 && index != old_index && index != 0)
1233 /* we jumped to the next block, adjust pointers */
1234 ref -= 12;
1235 offset -= 12;
1238 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1239 buf = hlpfile->topic_map[index] + offset;
1240 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1241 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1242 if (index != old_index) {old_index = index;}
1244 switch (buf[0x14])
1246 case 0x02:
1247 if (count++) goto done;
1248 break;
1249 case 0x01:
1250 case 0x20:
1251 case 0x23:
1252 if (!HLPFILE_BrowseParagraph(page, buf, end)) return FALSE;
1253 break;
1254 default:
1255 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1257 if (hlpfile->version <= 16)
1259 ref += GET_UINT(buf, 0xc);
1260 if (GET_UINT(buf, 0xc) == 0)
1261 break;
1263 else
1264 ref = GET_UINT(buf, 0xc);
1265 } while (ref != 0xffffffff);
1266 done:
1267 return TRUE;
1270 /******************************************************************
1271 * HLPFILE_ReadFont
1275 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1277 BYTE *ref, *end;
1278 unsigned i, len, idx;
1279 unsigned face_num, dscr_num, face_offset, dscr_offset;
1280 BYTE flag, family;
1282 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1284 WINE_WARN("no subfile FONT\n");
1285 hlpfile->numFonts = 0;
1286 hlpfile->fonts = NULL;
1287 return FALSE;
1290 ref += 9;
1292 face_num = GET_USHORT(ref, 0);
1293 dscr_num = GET_USHORT(ref, 2);
1294 face_offset = GET_USHORT(ref, 4);
1295 dscr_offset = GET_USHORT(ref, 6);
1297 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1298 face_num, face_offset, dscr_num, dscr_offset);
1300 hlpfile->numFonts = dscr_num;
1301 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1303 len = (dscr_offset - face_offset) / face_num;
1304 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1305 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1306 for (i = 0; i < dscr_num; i++)
1308 flag = ref[dscr_offset + i * 11 + 0];
1309 family = ref[dscr_offset + i * 11 + 2];
1311 hlpfile->fonts[i].LogFont.lfHeight = -ref[dscr_offset + i * 11 + 1] / 2 - 3;
1312 hlpfile->fonts[i].LogFont.lfWidth = 0;
1313 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1314 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1315 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1316 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1317 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1318 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1319 hlpfile->fonts[i].LogFont.lfCharSet = DEFAULT_CHARSET;
1320 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1321 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1322 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1323 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1325 switch (family)
1327 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1328 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1329 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1330 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1331 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1332 default: WINE_FIXME("Unknown family %u\n", family);
1334 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1336 if (idx < face_num)
1338 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1339 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1341 else
1343 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1344 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1346 hlpfile->fonts[i].hFont = 0;
1347 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1348 ref[dscr_offset + i * 11 + 6],
1349 ref[dscr_offset + i * 11 + 7]);
1350 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1351 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1352 i, flag,
1353 X(0, "bold"),
1354 X(1, "italic"),
1355 X(2, "underline"),
1356 X(3, "strikeOut"),
1357 X(4, "dblUnderline"),
1358 X(5, "smallCaps"),
1359 ref[dscr_offset + i * 11 + 1],
1360 family,
1361 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1362 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1364 return TRUE;
1367 /***********************************************************************
1369 * HLPFILE_ReadFileToBuffer
1371 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1373 BYTE header[16], dummy[1];
1375 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1377 /* sanity checks */
1378 if (GET_UINT(header, 0) != 0x00035F3F)
1379 {WINE_WARN("wrong header\n"); return FALSE;};
1381 hlpfile->file_buffer_size = GET_UINT(header, 12);
1382 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1383 if (!hlpfile->file_buffer) return FALSE;
1385 memcpy(hlpfile->file_buffer, header, 16);
1386 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1387 {WINE_WARN("filesize1\n"); return FALSE;};
1389 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1391 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1393 return TRUE;
1396 /**************************************************************************
1397 * comp_FindSubFile
1399 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1402 static int comp_FindSubFile(void *p, const void *key,
1403 int leaf, void** next)
1405 *next = (char *)p+strlen(p)+(leaf?5:3);
1406 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
1407 return strcmp(p, key);
1410 /***********************************************************************
1412 * HLPFILE_FindSubFile
1414 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
1416 BYTE *ptr;
1418 WINE_TRACE("looking for file '%s'\n", name);
1419 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
1420 name, comp_FindSubFile);
1421 if (!ptr) return FALSE;
1422 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
1423 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
1425 WINE_ERR("internal file %s does not fit\n", name);
1426 return FALSE;
1428 *subend = *subbuf + GET_UINT(*subbuf, 0);
1429 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
1431 WINE_ERR("internal file %s does not fit\n", name);
1432 return FALSE;
1434 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
1436 WINE_ERR("invalid size provided for internal file %s\n", name);
1437 return FALSE;
1439 return TRUE;
1442 /***********************************************************************
1444 * HLPFILE_SystemCommands
1446 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1448 BYTE *buf, *ptr, *end;
1449 HLPFILE_MACRO *macro, **m;
1450 LPSTR p;
1451 unsigned short magic, minor, major, flags;
1453 hlpfile->lpszTitle = NULL;
1455 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1457 magic = GET_USHORT(buf + 9, 0);
1458 minor = GET_USHORT(buf + 9, 2);
1459 major = GET_USHORT(buf + 9, 4);
1460 /* gen date on 4 bytes */
1461 flags = GET_USHORT(buf + 9, 10);
1462 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1463 magic, major, minor, flags);
1464 if (magic != 0x036C || major != 1)
1465 {WINE_WARN("Wrong system header\n"); return FALSE;}
1466 if (minor <= 16)
1468 hlpfile->tbsize = 0x800;
1469 hlpfile->compressed = 0;
1471 else if (flags == 0)
1473 hlpfile->tbsize = 0x1000;
1474 hlpfile->compressed = 0;
1476 else if (flags == 4)
1478 hlpfile->tbsize = 0x1000;
1479 hlpfile->compressed = 1;
1481 else
1483 hlpfile->tbsize = 0x800;
1484 hlpfile->compressed = 1;
1487 if (hlpfile->compressed)
1488 hlpfile->dsize = 0x4000;
1489 else
1490 hlpfile->dsize = hlpfile->tbsize - 0x0C;
1492 hlpfile->version = minor;
1493 hlpfile->flags = flags;
1495 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1497 char *str = (char*) ptr + 4;
1498 switch (GET_USHORT(ptr, 0))
1500 case 1:
1501 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1502 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1503 if (!hlpfile->lpszTitle) return FALSE;
1504 lstrcpy(hlpfile->lpszTitle, str);
1505 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1506 break;
1508 case 2:
1509 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1510 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1511 if (!hlpfile->lpszCopyright) return FALSE;
1512 lstrcpy(hlpfile->lpszCopyright, str);
1513 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1514 break;
1516 case 3:
1517 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1518 hlpfile->contents_start = GET_UINT(ptr, 4);
1519 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1520 break;
1522 case 4:
1523 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1524 if (!macro) break;
1525 p = (char*)macro + sizeof(HLPFILE_MACRO);
1526 lstrcpy(p, str);
1527 macro->lpszMacro = p;
1528 macro->next = 0;
1529 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1530 *m = macro;
1531 break;
1533 case 6:
1534 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1536 if (hlpfile->windows)
1537 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1538 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1539 else
1540 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1541 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1543 if (hlpfile->windows)
1545 unsigned flags = GET_USHORT(ptr, 4);
1546 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1548 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1549 else wi->type[0] = '\0';
1550 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1551 else wi->name[0] = '\0';
1552 if (flags & 0x0004) strcpy(wi->caption, &str[23]);
1553 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1554 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1555 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1556 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1557 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1558 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1559 wi->win_style = WS_OVERLAPPEDWINDOW;
1560 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1561 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1562 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",
1563 flags & 0x0001 ? 'T' : 't',
1564 flags & 0x0002 ? 'N' : 'n',
1565 flags & 0x0004 ? 'C' : 'c',
1566 flags & 0x0008 ? 'X' : 'x',
1567 flags & 0x0010 ? 'Y' : 'y',
1568 flags & 0x0020 ? 'W' : 'w',
1569 flags & 0x0040 ? 'H' : 'h',
1570 flags & 0x0080 ? 'S' : 's',
1571 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1572 wi->size.cx, wi->size.cy);
1574 break;
1575 case 8:
1576 WINE_WARN("Citation: '%s'\n", ptr + 4);
1577 break;
1578 default:
1579 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1582 if (!hlpfile->lpszTitle)
1583 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
1584 return TRUE;
1587 /***********************************************************************
1589 * HLPFILE_UncompressedLZ77_Size
1591 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
1593 int i, newsize = 0;
1595 while (ptr < end)
1597 int mask = *ptr++;
1598 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1600 if (mask & 1)
1602 int code = GET_USHORT(ptr, 0);
1603 int len = 3 + (code >> 12);
1604 newsize += len;
1605 ptr += 2;
1607 else newsize++, ptr++;
1611 return newsize;
1614 /***********************************************************************
1616 * HLPFILE_UncompressLZ77
1618 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
1620 int i;
1622 while (ptr < end)
1624 int mask = *ptr++;
1625 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1627 if (mask & 1)
1629 int code = GET_USHORT(ptr, 0);
1630 int len = 3 + (code >> 12);
1631 int offset = code & 0xfff;
1633 * We must copy byte-by-byte here. We cannot use memcpy nor
1634 * memmove here. Just example:
1635 * a[]={1,2,3,4,5,6,7,8,9,10}
1636 * newptr=a+2;
1637 * offset=1;
1638 * We expect:
1639 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
1641 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
1642 ptr += 2;
1644 else *newptr++ = *ptr++;
1648 return newptr;
1651 /***********************************************************************
1653 * HLPFILE_UncompressLZ77_Phrases
1655 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
1657 UINT i, num, dec_size, head_size;
1658 BYTE *buf, *end;
1660 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
1662 if (hlpfile->version <= 16)
1663 head_size = 13;
1664 else
1665 head_size = 17;
1667 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
1668 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
1670 if (hlpfile->version <= 16)
1671 dec_size = end - buf - 15 - 2 * num;
1672 else
1673 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
1675 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1676 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1677 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
1679 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
1680 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
1681 return FALSE;
1684 for (i = 0; i <= num; i++)
1685 hlpfile->phrases_offsets[i] = GET_USHORT(buf, 0x11 + 2 * i) - 2 * num - 2;
1687 if (hlpfile->version <= 16)
1688 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
1689 else
1690 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
1692 hlpfile->hasPhrases = TRUE;
1693 return TRUE;
1696 /***********************************************************************
1698 * HLPFILE_Uncompress_Phrases40
1700 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
1702 UINT num;
1703 INT dec_size, cpr_size;
1704 BYTE *buf_idx, *end_idx;
1705 BYTE *buf_phs, *end_phs;
1706 long* ptr, mask = 0;
1707 unsigned int i;
1708 unsigned short bc, n;
1710 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
1711 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
1713 ptr = (long*)(buf_idx + 9 + 28);
1714 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
1715 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
1717 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
1718 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
1719 GET_UINT(buf_idx, 9 + 0),
1720 GET_UINT(buf_idx, 9 + 4),
1721 GET_UINT(buf_idx, 9 + 8),
1722 GET_UINT(buf_idx, 9 + 12),
1723 GET_UINT(buf_idx, 9 + 16),
1724 GET_UINT(buf_idx, 9 + 20),
1725 GET_USHORT(buf_idx, 9 + 24),
1726 GET_USHORT(buf_idx, 9 + 26));
1728 dec_size = GET_UINT(buf_idx, 9 + 12);
1729 cpr_size = GET_UINT(buf_idx, 9 + 16);
1731 if (dec_size != cpr_size &&
1732 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
1734 WINE_WARN("size mismatch %u %u\n",
1735 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1736 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1739 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1740 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1741 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
1743 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
1744 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
1745 return FALSE;
1748 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
1750 hlpfile->phrases_offsets[0] = 0;
1751 for (i = 0; i < num; i++)
1753 for (n = 1; getbit(); n += 1 << bc);
1754 if (getbit()) n++;
1755 if (bc > 1 && getbit()) n += 2;
1756 if (bc > 2 && getbit()) n += 4;
1757 if (bc > 3 && getbit()) n += 8;
1758 if (bc > 4 && getbit()) n += 16;
1759 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
1761 #undef getbit
1763 if (dec_size == cpr_size)
1764 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
1765 else
1766 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
1768 hlpfile->hasPhrases40 = TRUE;
1769 return TRUE;
1772 /***********************************************************************
1774 * HLPFILE_Uncompress_Topic
1776 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
1778 BYTE *buf, *ptr, *end, *newptr;
1779 unsigned int i, newsize = 0;
1780 unsigned int topic_size;
1782 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
1783 {WINE_WARN("topic0\n"); return FALSE;}
1785 buf += 9; /* Skip file header */
1786 topic_size = end - buf;
1787 if (hlpfile->compressed)
1789 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
1791 for (i = 0; i < hlpfile->topic_maplen; i++)
1793 ptr = buf + i * hlpfile->tbsize;
1795 /* I don't know why, it's necessary for printman.hlp */
1796 if (ptr + 0x44 > end) ptr = end - 0x44;
1798 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
1801 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
1802 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
1803 if (!hlpfile->topic_map) return FALSE;
1804 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
1805 hlpfile->topic_end = newptr + newsize;
1807 for (i = 0; i < hlpfile->topic_maplen; i++)
1809 ptr = buf + i * hlpfile->tbsize;
1810 if (ptr + 0x44 > end) ptr = end - 0x44;
1812 hlpfile->topic_map[i] = newptr;
1813 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
1816 else
1818 /* basically, we need to copy the TopicBlockSize byte pages
1819 * (removing the first 0x0C) in one single area in memory
1821 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
1822 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
1823 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
1824 if (!hlpfile->topic_map) return FALSE;
1825 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
1826 hlpfile->topic_end = newptr + topic_size;
1828 for (i = 0; i < hlpfile->topic_maplen; i++)
1830 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
1831 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
1834 return TRUE;
1837 /***********************************************************************
1839 * HLPFILE_Uncompress2
1842 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
1844 BYTE *phptr, *phend;
1845 UINT code;
1846 UINT index;
1848 while (ptr < end && newptr < newend)
1850 if (!*ptr || *ptr >= 0x10)
1851 *newptr++ = *ptr++;
1852 else
1854 code = 0x100 * ptr[0] + ptr[1];
1855 index = (code - 0x100) / 2;
1857 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
1858 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
1860 if (newptr + (phend - phptr) > newend)
1862 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
1863 newptr, newend, (SIZE_T)(phend - phptr));
1864 return;
1866 memcpy(newptr, phptr, phend - phptr);
1867 newptr += phend - phptr;
1868 if (code & 1) *newptr++ = ' ';
1870 ptr += 2;
1873 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
1876 /******************************************************************
1877 * HLPFILE_Uncompress3
1881 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
1882 const BYTE* src, const BYTE* src_end)
1884 unsigned int idx, len;
1886 for (; src < src_end; src++)
1888 if ((*src & 1) == 0)
1890 idx = *src / 2;
1891 if (idx > hlpfile->num_phrases)
1893 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
1894 len = 0;
1896 else
1898 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
1899 if (dst + len <= dst_end)
1900 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
1903 else if ((*src & 0x03) == 0x01)
1905 idx = (*src + 1) * 64;
1906 idx += *++src;
1907 if (idx > hlpfile->num_phrases)
1909 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
1910 len = 0;
1912 else
1914 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
1915 if (dst + len <= dst_end)
1916 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
1919 else if ((*src & 0x07) == 0x03)
1921 len = (*src / 8) + 1;
1922 if (dst + len <= dst_end)
1923 memcpy(dst, src + 1, len);
1924 src += len;
1926 else
1928 len = (*src / 16) + 1;
1929 if (dst + len <= dst_end)
1930 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
1932 dst += len;
1935 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
1936 return TRUE;
1939 /******************************************************************
1940 * HLPFILE_UncompressRLE
1944 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
1946 BYTE ch;
1947 BYTE* sdst = *dst + dstsz;
1949 while (src < end)
1951 ch = *src++;
1952 if (ch & 0x80)
1954 ch &= 0x7F;
1955 if ((*dst) + ch <= sdst)
1956 memcpy(*dst, src, ch);
1957 src += ch;
1959 else
1961 if ((*dst) + ch <= sdst)
1962 memset(*dst, (char)*src, ch);
1963 src++;
1965 *dst += ch;
1967 if (*dst != sdst)
1968 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
1969 (SIZE_T)(*dst - (sdst - dstsz)), dstsz);
1972 /**************************************************************************
1973 * HLPFILE_BPTreeSearch
1975 * Searches for an element in B+ tree
1977 * PARAMS
1978 * buf [I] pointer to the embedded file structured as a B+ tree
1979 * key [I] pointer to data to find
1980 * comp [I] compare function
1982 * RETURNS
1983 * Pointer to block identified by key, or NULL if failure.
1986 void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
1987 HLPFILE_BPTreeCompare comp)
1989 unsigned magic;
1990 unsigned page_size;
1991 unsigned cur_page;
1992 unsigned level;
1993 BYTE *pages, *ptr, *newptr;
1994 int i, entries;
1995 int ret;
1997 magic = GET_USHORT(buf, 9);
1998 if (magic != 0x293B)
2000 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2001 return NULL;
2003 page_size = GET_USHORT(buf, 9+4);
2004 cur_page = GET_USHORT(buf, 9+26);
2005 level = GET_USHORT(buf, 9+32);
2006 pages = buf + 9 + 38;
2007 while (--level > 0)
2009 ptr = pages + cur_page*page_size;
2010 entries = GET_SHORT(ptr, 2);
2011 ptr += 6;
2012 for (i = 0; i < entries; i++)
2014 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
2015 ptr = newptr;
2017 cur_page = GET_USHORT(ptr-2, 0);
2019 ptr = pages + cur_page*page_size;
2020 entries = GET_SHORT(ptr, 2);
2021 ptr += 8;
2022 for (i = 0; i < entries; i++)
2024 ret = comp(ptr, key, 1, (void **)&newptr);
2025 if (ret == 0) return ptr;
2026 if (ret > 0) return NULL;
2027 ptr = newptr;
2029 return NULL;
2032 /**************************************************************************
2033 * HLPFILE_BPTreeEnum
2035 * Enumerates elements in B+ tree.
2037 * PARAMS
2038 * buf [I] pointer to the embedded file structured as a B+ tree
2039 * cb [I] compare function
2040 * cookie [IO] cookie for cb function
2042 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
2044 unsigned magic;
2045 unsigned page_size;
2046 unsigned cur_page;
2047 unsigned level;
2048 BYTE *pages, *ptr, *newptr;
2049 int i, entries;
2051 magic = GET_USHORT(buf, 9);
2052 if (magic != 0x293B)
2054 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
2055 return;
2057 page_size = GET_USHORT(buf, 9+4);
2058 cur_page = GET_USHORT(buf, 9+26);
2059 level = GET_USHORT(buf, 9+32);
2060 pages = buf + 9 + 38;
2061 while (--level > 0)
2063 ptr = pages + cur_page*page_size;
2064 cur_page = GET_USHORT(ptr, 4);
2066 while (cur_page != 0xFFFF)
2068 ptr = pages + cur_page*page_size;
2069 entries = GET_SHORT(ptr, 2);
2070 ptr += 8;
2071 for (i = 0; i < entries; i++)
2073 cb(ptr, (void **)&newptr, cookie);
2074 ptr = newptr;
2076 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
2081 /***********************************************************************
2083 * HLPFILE_GetContext
2085 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2087 BYTE *cbuf, *cend;
2088 unsigned clen;
2090 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2091 {WINE_WARN("context0\n"); return FALSE;}
2093 clen = cend - cbuf;
2094 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2095 if (!hlpfile->Context) return FALSE;
2096 memcpy(hlpfile->Context, cbuf, clen);
2098 return TRUE;
2101 /***********************************************************************
2103 * HLPFILE_GetKeywords
2105 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2107 BYTE *cbuf, *cend;
2108 unsigned clen;
2110 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2111 clen = cend - cbuf;
2112 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2113 if (!hlpfile->kwbtree) return FALSE;
2114 memcpy(hlpfile->kwbtree, cbuf, clen);
2116 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2118 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2119 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2120 return FALSE;
2122 clen = cend - cbuf;
2123 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2124 if (!hlpfile->kwdata)
2126 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2127 return FALSE;
2129 memcpy(hlpfile->kwdata, cbuf, clen);
2131 return TRUE;
2134 /***********************************************************************
2136 * HLPFILE_GetMap
2138 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2140 BYTE *cbuf, *cend;
2141 unsigned entries, i;
2143 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2144 {WINE_WARN("no map section\n"); return FALSE;}
2146 entries = GET_USHORT(cbuf, 9);
2147 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2148 if (!hlpfile->Map) return FALSE;
2149 hlpfile->wMapLen = entries;
2150 for (i = 0; i < entries; i++)
2152 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2153 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2155 return TRUE;
2158 /******************************************************************
2159 * HLPFILE_DeleteLink
2163 void HLPFILE_FreeLink(HLPFILE_LINK* link)
2165 if (link && !--link->wRefCount)
2166 HeapFree(GetProcessHeap(), 0, link);
2169 /***********************************************************************
2171 * HLPFILE_DeleteParagraph
2173 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
2175 HLPFILE_PARAGRAPH* next;
2177 while (paragraph)
2179 next = paragraph->next;
2181 if (paragraph->cookie == para_metafile)
2182 DeleteMetaFile(paragraph->u.gfx.u.mfp.hMF);
2184 HLPFILE_FreeLink(paragraph->link);
2186 HeapFree(GetProcessHeap(), 0, paragraph);
2187 paragraph = next;
2191 /***********************************************************************
2193 * DeleteMacro
2195 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2197 HLPFILE_MACRO* next;
2199 while (macro)
2201 next = macro->next;
2202 HeapFree(GetProcessHeap(), 0, macro);
2203 macro = next;
2207 /***********************************************************************
2209 * DeletePage
2211 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2213 HLPFILE_PAGE* next;
2215 while (page)
2217 next = page->next;
2218 HLPFILE_DeleteParagraph(page->first_paragraph);
2219 HLPFILE_DeleteMacro(page->first_macro);
2220 HeapFree(GetProcessHeap(), 0, page);
2221 page = next;
2225 /***********************************************************************
2227 * HLPFILE_FreeHlpFile
2229 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2231 unsigned i;
2233 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2235 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2236 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2237 else first_hlpfile = hlpfile->next;
2239 if (hlpfile->numFonts)
2241 for (i = 0; i < hlpfile->numFonts; i++)
2243 DeleteObject(hlpfile->fonts[i].hFont);
2245 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2248 if (hlpfile->numBmps)
2250 for (i = 0; i < hlpfile->numBmps; i++)
2252 DeleteObject(hlpfile->bmps[i]);
2254 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2257 HLPFILE_DeletePage(hlpfile->first_page);
2258 HLPFILE_DeleteMacro(hlpfile->first_macro);
2260 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2261 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2262 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2263 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2264 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2265 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2266 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2267 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2268 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2269 HeapFree(GetProcessHeap(), 0, hlpfile);