Warning fix.
[wine/multimedia.git] / programs / winhelp / hlpfile.c
blob6c6c393e093583c27261f66981b02330e42a7a4d
1 /*
2 * Help Viewer
4 * Copyright 1996 Ulrich Schmid
5 * 2002 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdio.h>
23 #include <string.h>
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winhelp.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
33 static inline unsigned short GET_USHORT(const BYTE* buffer, unsigned i)
35 return (BYTE)buffer[i] + 0x100 * (BYTE)buffer[i + 1];
38 static inline short GET_SHORT(const BYTE* buffer, unsigned i)
40 return (BYTE)buffer[i] + 0x100 * (signed char)buffer[i+1];
43 static inline unsigned GET_UINT(const BYTE* buffer, unsigned i)
45 return GET_USHORT(buffer, i) + 0x10000 * GET_USHORT(buffer, i + 2);
48 static HLPFILE *first_hlpfile = 0;
49 static BYTE *file_buffer;
51 static struct
53 UINT num;
54 unsigned* offsets;
55 char* buffer;
56 } phrases;
58 static struct
60 BYTE** map;
61 BYTE* end;
62 UINT wMapLen;
63 } topic;
65 static struct
67 UINT wFont;
68 UINT wIndent;
69 UINT wHSpace;
70 UINT wVSpace;
71 HLPFILE_LINK* link;
72 } attributes;
74 static BOOL HLPFILE_DoReadHlpFile(HLPFILE*, LPCSTR);
75 static BOOL HLPFILE_ReadFileToBuffer(HFILE);
76 static BOOL HLPFILE_FindSubFile(LPCSTR name, BYTE**, BYTE**);
77 static BOOL HLPFILE_SystemCommands(HLPFILE*);
78 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end);
79 static BYTE* HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr);
80 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE*);
81 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE*);
82 static BOOL HLPFILE_Uncompress_Topic(HLPFILE*);
83 static BOOL HLPFILE_GetContext(HLPFILE*);
84 static BOOL HLPFILE_AddPage(HLPFILE*, BYTE*, BYTE*, unsigned);
85 static BOOL HLPFILE_AddParagraph(HLPFILE*, BYTE *, BYTE*, unsigned*);
86 static void HLPFILE_Uncompress2(const BYTE*, const BYTE*, BYTE*, const BYTE*);
87 static BOOL HLPFILE_Uncompress3(char*, const char*, const BYTE*, const BYTE*);
88 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz);
89 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile);
91 /***********************************************************************
93 * HLPFILE_PageByNumber
95 HLPFILE_PAGE *HLPFILE_PageByNumber(LPCSTR lpszPath, UINT wNum)
97 HLPFILE_PAGE *page;
98 HLPFILE *hlpfile = HLPFILE_ReadHlpFile(lpszPath);
100 if (!hlpfile) return 0;
102 WINE_TRACE("[%s/%u]\n", lpszPath, wNum);
104 for (page = hlpfile->first_page; page && wNum; page = page->next) wNum--;
106 /* HLPFILE_FreeHlpFile(lpszPath); */
108 return page;
111 /* FIXME:
112 * this finds the page containing the offset. The offset can either
113 * refer to the top of the page (offset == page->offset), or
114 * to some paragraph inside the page...
115 * As of today, we only return the page... we should also return
116 * a paragraph, and then, while opening a new page, compute the
117 * y-offset of the paragraph to be shown and scroll the window
118 * accordinly
120 /******************************************************************
121 * HLPFILE_PageByOffset
125 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset)
127 HLPFILE_PAGE* page;
128 HLPFILE_PAGE* found;
130 if (!hlpfile) return 0;
132 WINE_TRACE("<%s>[%lx]\n", hlpfile->lpszPath, offset);
134 if (offset == 0xFFFFFFFF) return NULL;
135 page = NULL;
137 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
139 if (page->offset <= offset && (!found || found->offset < page->offset))
140 found = page;
142 if (!found)
143 WINE_ERR("Page of offset %lu not found in file %s\n",
144 offset, hlpfile->lpszPath);
145 return found;
148 /***********************************************************************
150 * HLPFILE_HlpFilePageByHash
152 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash)
154 int i;
156 if (!hlpfile) return 0;
158 WINE_TRACE("<%s>[%lx]\n", hlpfile->lpszPath, lHash);
160 for (i = 0; i < hlpfile->wContextLen; i++)
162 if (hlpfile->Context[i].lHash == lHash)
163 return HLPFILE_PageByOffset(hlpfile, hlpfile->Context[i].offset);
166 WINE_ERR("Page of hash %lx not found in file %s\n", lHash, hlpfile->lpszPath);
167 return NULL;
170 /***********************************************************************
172 * HLPFILE_Contents
174 HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile)
176 HLPFILE_PAGE* page = NULL;
178 if (!hlpfile) return NULL;
180 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start);
181 if (!page) page = hlpfile->first_page;
182 return page;
185 /***********************************************************************
187 * HLPFILE_Hash
189 LONG HLPFILE_Hash(LPCSTR lpszContext)
191 LONG lHash = 0;
192 CHAR c;
194 while ((c = *lpszContext++))
196 CHAR x = 0;
197 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
198 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
199 if (c >= '1' && c <= '9') x = c - '0';
200 if (c == '0') x = 10;
201 if (c == '.') x = 12;
202 if (c == '_') x = 13;
203 if (x) lHash = lHash * 43 + x;
205 return lHash;
208 /***********************************************************************
210 * HLPFILE_ReadHlpFile
212 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
214 HLPFILE* hlpfile;
216 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
218 if (!strcmp(lpszPath, hlpfile->lpszPath))
220 hlpfile->wRefCount++;
221 return hlpfile;
225 hlpfile = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
226 if (!hlpfile) return 0;
228 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
229 hlpfile->lpszTitle = NULL;
230 hlpfile->lpszCopyright = NULL;
231 hlpfile->first_page = NULL;
232 hlpfile->first_macro = NULL;
233 hlpfile->wContextLen = 0;
234 hlpfile->Context = NULL;
235 hlpfile->contents_start = 0xFFFFFFFF;
236 hlpfile->prev = NULL;
237 hlpfile->next = first_hlpfile;
238 hlpfile->wRefCount = 1;
240 hlpfile->numBmps = 0;
241 hlpfile->bmps = NULL;
243 hlpfile->numFonts = 0;
244 hlpfile->fonts = NULL;
246 hlpfile->numWindows = 0;
247 hlpfile->windows = NULL;
249 strcpy(hlpfile->lpszPath, lpszPath);
251 first_hlpfile = hlpfile;
252 if (hlpfile->next) hlpfile->next->prev = hlpfile;
254 phrases.offsets = NULL;
255 phrases.buffer = NULL;
256 topic.map = NULL;
257 topic.end = NULL;
258 file_buffer = NULL;
260 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
262 HLPFILE_FreeHlpFile(hlpfile);
263 hlpfile = 0;
266 if (phrases.offsets) HeapFree(GetProcessHeap(), 0, phrases.offsets);
267 if (phrases.buffer) HeapFree(GetProcessHeap(), 0, phrases.buffer);
268 if (topic.map) HeapFree(GetProcessHeap(), 0, topic.map);
269 if (file_buffer) HeapFree(GetProcessHeap(), 0, file_buffer);
271 return hlpfile;
274 /***********************************************************************
276 * HLPFILE_DoReadHlpFile
278 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
280 BOOL ret;
281 HFILE hFile;
282 OFSTRUCT ofs;
283 BYTE* buf;
284 DWORD ref = 0x0C;
285 unsigned index, old_index, offset, len, offs;
287 hFile = OpenFile(lpszPath, &ofs, OF_READ | OF_SEARCH);
288 if (hFile == HFILE_ERROR) return FALSE;
290 ret = HLPFILE_ReadFileToBuffer(hFile);
291 _lclose(hFile);
292 if (!ret) return FALSE;
294 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
296 /* load phrases support */
297 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
298 HLPFILE_Uncompress_Phrases40(hlpfile);
300 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
301 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
303 buf = topic.map[0];
304 old_index = -1;
305 offs = 0;
308 BYTE* end;
310 /* FIXME this depends on the blocksize, can be 2k in some cases */
311 index = (ref - 0x0C) >> 14;
312 offset = (ref - 0x0C) & 0x3fff;
314 WINE_TRACE("ref=%08lx => [%u/%u]\n", ref, index, offset);
316 if (index >= topic.wMapLen) {WINE_WARN("maplen\n"); break;}
317 buf = topic.map[index] + offset;
318 if (buf + 0x15 >= topic.end) {WINE_WARN("extra\n"); break;}
319 end = min(buf + GET_UINT(buf, 0), topic.end);
320 if (index != old_index) {offs = 0; old_index = index;}
322 switch (buf[0x14])
324 case 0x02:
325 if (!HLPFILE_AddPage(hlpfile, buf, end, index * 0x8000L + offs)) return FALSE;
326 break;
328 case 0x20:
329 if (!HLPFILE_AddParagraph(hlpfile, buf, end, &len)) return FALSE;
330 offs += len;
331 break;
333 case 0x23:
334 if (!HLPFILE_AddParagraph(hlpfile, buf, end, &len)) return FALSE;
335 offs += len;
336 break;
338 default:
339 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
342 ref = GET_UINT(buf, 0xc);
343 } while (ref != 0xffffffff);
345 return HLPFILE_GetContext(hlpfile);
348 /***********************************************************************
350 * HLPFILE_AddPage
352 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned offset)
354 HLPFILE_PAGE* page;
355 BYTE* title;
356 UINT titlesize;
357 char* ptr;
358 HLPFILE_MACRO*macro;
360 if (buf + 0x31 > end) {WINE_WARN("page1\n"); return FALSE;};
361 title = buf + GET_UINT(buf, 0x10);
362 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
364 titlesize = GET_UINT(buf, 4);
365 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
366 if (!page) return FALSE;
367 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
369 if (hlpfile->hasPhrases)
371 HLPFILE_Uncompress2(title, end, page->lpszTitle, page->lpszTitle + titlesize);
373 else
375 if (GET_UINT(buf, 0x4) > GET_UINT(buf, 0) - GET_UINT(buf, 0x10))
377 /* need to decompress */
378 HLPFILE_Uncompress3(page->lpszTitle, page->lpszTitle + titlesize,
379 title, end);
381 else
383 memcpy(page->lpszTitle, title, titlesize);
387 page->lpszTitle[titlesize] = '\0';
389 if (hlpfile->first_page)
391 HLPFILE_PAGE *p;
393 for (p = hlpfile->first_page; p->next; p = p->next);
394 page->prev = p;
395 p->next = page;
397 else
399 hlpfile->first_page = page;
400 page->prev = NULL;
403 page->file = hlpfile;
404 page->next = NULL;
405 page->first_paragraph = NULL;
406 page->first_macro = NULL;
407 page->wNumber = GET_UINT(buf, 0x21);
408 page->offset = offset;
410 page->browse_bwd = GET_UINT(buf, 0x19);
411 page->browse_fwd = GET_UINT(buf, 0x1D);
413 WINE_TRACE("Added page[%d]: title='%s' %08lx << %08x >> %08lx\n",
414 page->wNumber, page->lpszTitle,
415 page->browse_bwd, page->offset, page->browse_fwd);
417 memset(&attributes, 0, sizeof(attributes));
419 /* now load macros */
420 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
421 while (ptr < page->lpszTitle + titlesize)
423 unsigned len = strlen(ptr);
424 WINE_TRACE("macro: %s\n", ptr);
425 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
426 macro->lpszMacro = (char*)(macro + 1);
427 memcpy((char*)macro->lpszMacro, ptr, len + 1);
428 /* FIXME: shall we really link macro in reverse order ??
429 * may produce strange results when played at page opening
431 macro->next = page->first_macro;
432 page->first_macro = macro;
433 ptr += len + 1;
436 return TRUE;
439 static long fetch_long(BYTE** ptr)
441 long ret;
443 if (*(*ptr) & 1)
445 ret = (*(unsigned long*)(*ptr) - 0x80000000L) / 2;
446 (*ptr) += 4;
448 else
450 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
451 (*ptr) += 2;
454 return ret;
457 static unsigned long fetch_ulong(BYTE** ptr)
459 unsigned long ret;
461 if (*(*ptr) & 1)
463 ret = *(unsigned long*)(*ptr) / 2;
464 (*ptr) += 4;
466 else
468 ret = *(unsigned short*)(*ptr) / 2;
469 (*ptr) += 2;
471 return ret;
474 static short fetch_short(BYTE** ptr)
476 short ret;
478 if (*(*ptr) & 1)
480 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
481 (*ptr) += 2;
483 else
485 ret = (*(unsigned char*)(*ptr) - 0x80) / 2;
486 (*ptr)++;
488 return ret;
491 static unsigned short fetch_ushort(BYTE** ptr)
493 unsigned short ret;
495 if (*(*ptr) & 1)
497 ret = *(unsigned short*)(*ptr) / 2;
498 (*ptr) += 2;
500 else
502 ret = *(unsigned char*)(*ptr) / 2;
503 (*ptr)++;
505 return ret;
508 /******************************************************************
509 * HLPFILE_DecompressGfx
511 * Decompress the data part of a bitmap or a metafile
513 static BYTE* HLPFILE_DecompressGfx(BYTE* src, unsigned csz, unsigned sz, BYTE packing)
515 BYTE* dst;
516 BYTE* tmp;
517 BYTE* tmp2;
518 unsigned sz77;
520 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
522 switch (packing)
524 case 0: /* uncompressed */
525 if (sz != csz)
526 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
527 dst = src;
528 break;
529 case 1: /* RunLen */
530 tmp = dst = HeapAlloc(GetProcessHeap(), 0, sz);
531 if (!dst) return NULL;
532 HLPFILE_UncompressRLE(src, src + csz, &tmp, sz);
533 if (tmp - dst != sz)
534 WINE_FIXME("Bogus gfx sizes (RunLen): %u/%u\n", tmp - dst, sz);
535 break;
536 case 2: /* LZ77 */
537 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
538 dst = HeapAlloc(GetProcessHeap(), 0, sz77);
539 if (!dst) return NULL;
540 HLPFILE_UncompressLZ77(src, src + csz, dst);
541 if (sz77 != sz)
542 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
543 break;
544 case 3: /* LZ77 then RLE */
545 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
546 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
547 if (!tmp) return FALSE;
548 HLPFILE_UncompressLZ77(src, src + csz, tmp);
549 dst = tmp2 = HeapAlloc(GetProcessHeap(), 0, sz);
550 if (!dst) return FALSE;
551 HLPFILE_UncompressRLE(tmp, tmp + sz77, &tmp2, sz);
552 if (tmp2 - dst != sz)
553 WINE_WARN("Bogus gfx sizes (LZ77+RunLen): %u / %u\n", tmp2 - dst, sz);
554 HeapFree(GetProcessHeap(), 0, tmp);
555 break;
556 default:
557 WINE_FIXME("Unsupported packing %u\n", packing);
558 return NULL;
560 return dst;
563 /******************************************************************
564 * HLPFILE_LoadBitmap
568 static BOOL HLPFILE_LoadBitmap(BYTE* beg, BYTE type, BYTE pack,
569 HLPFILE_PARAGRAPH* paragraph)
571 BYTE* ptr;
572 BYTE* pict_beg;
573 BITMAPINFO* bi;
574 unsigned long off, csz;
575 HDC hdc;
577 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
578 if (!bi) return FALSE;
580 ptr = beg + 2; /* for type and pack */
582 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
583 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
584 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
585 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
586 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
587 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
588 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
589 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
590 bi->bmiHeader.biClrImportant = fetch_ulong(&ptr);
591 bi->bmiHeader.biCompression = BI_RGB;
592 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
593 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
594 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
595 WINE_TRACE("planes=%d bc=%d size=(%ld,%ld)\n",
596 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
597 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
599 csz = fetch_ulong(&ptr);
600 fetch_ulong(&ptr); /* hotspot size */
602 off = GET_UINT(ptr, 0); ptr += 4;
603 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
605 /* now read palette info */
606 if (type == 0x06)
608 unsigned nc = bi->bmiHeader.biClrUsed;
609 unsigned i;
611 /* not quite right, especially for bitfields type of compression */
612 if (!nc && bi->bmiHeader.biBitCount <= 8)
613 nc = 1 << bi->bmiHeader.biBitCount;
615 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
616 if (!bi) return FALSE;
617 for (i = 0; i < nc; i++)
619 bi->bmiColors[i].rgbBlue = ptr[0];
620 bi->bmiColors[i].rgbGreen = ptr[1];
621 bi->bmiColors[i].rgbRed = ptr[2];
622 bi->bmiColors[i].rgbReserved = 0;
623 ptr += 4;
626 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
628 paragraph->u.gfx.u.bmp.hBitmap = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
629 CBM_INIT, pict_beg,
630 bi, DIB_RGB_COLORS);
631 ReleaseDC(0, hdc);
632 if (!paragraph->u.gfx.u.bmp.hBitmap)
633 WINE_ERR("Couldn't create bitmap\n");
635 HeapFree(GetProcessHeap(), 0, bi);
636 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
638 return TRUE;
641 /******************************************************************
642 * HLPFILE_LoadMetaFile
646 static BOOL HLPFILE_LoadMetaFile(BYTE* beg, BYTE pack, HLPFILE_PARAGRAPH* paragraph)
648 BYTE* ptr;
649 unsigned long size, csize;
650 unsigned long off, hsoff;
651 BYTE* bits;
652 METAFILEPICT mfp;
654 WINE_TRACE("Loading metafile\n");
656 ptr = beg + 2; /* for type and pack */
658 mfp.mm = fetch_ushort(&ptr); /* mapping mode */
660 mfp.xExt = GET_USHORT(ptr, 0);
661 mfp.yExt = GET_USHORT(ptr, 2);
662 ptr += 4;
664 size = fetch_ulong(&ptr); /* decompressed size */
665 csize = fetch_ulong(&ptr); /* compressed size */
666 fetch_ulong(&ptr); /* hotspot size */
667 off = GET_UINT(ptr, 0);
668 hsoff = GET_UINT(ptr, 4);
669 ptr += 8;
671 WINE_FIXME("sz=%lu csz=%lu (%ld,%ld) offs=%lu/%u,%lu\n",
672 size, csize, mfp.xExt, mfp.yExt, off, ptr - beg, hsoff);
674 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
675 if (!bits) return FALSE;
677 paragraph->cookie = para_metafile;
679 mfp.hMF = NULL;
681 paragraph->u.gfx.u.mf.hMetaFile = SetMetaFileBitsEx(size, bits);
683 if (!paragraph->u.gfx.u.mf.hMetaFile)
684 WINE_FIXME("Couldn't load metafile\n");
686 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
688 paragraph->u.gfx.u.mf.mfSize.cx = mfp.xExt;
689 paragraph->u.gfx.u.mf.mfSize.cy = mfp.yExt;
691 return TRUE;
694 /******************************************************************
695 * HLPFILE_LoadGfxByAddr
699 static BOOL HLPFILE_LoadGfxByAddr(HLPFILE *hlpfile, BYTE* ref,
700 unsigned long size,
701 HLPFILE_PARAGRAPH* paragraph)
703 unsigned i, numpict;
705 numpict = GET_USHORT(ref, 2);
706 WINE_TRACE("Got picture magic=%04x #=%d\n",
707 GET_USHORT(ref, 0), numpict);
709 for (i = 0; i < numpict; i++)
711 BYTE* beg;
712 BYTE* ptr;
713 BYTE type, pack;
715 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
716 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
718 type = *ptr++;
719 pack = *ptr++;
721 switch (type)
723 case 5: /* device dependent bmp */
724 case 6: /* device independent bmp */
725 HLPFILE_LoadBitmap(beg, type, pack, paragraph);
726 break;
727 case 8:
728 HLPFILE_LoadMetaFile(beg, pack, paragraph);
729 break;
730 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
733 /* FIXME: hotspots */
735 /* FIXME: implement support for multiple picture format */
736 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
737 break;
739 return TRUE;
742 /******************************************************************
743 * HLPFILE_LoadGfxByIndex
747 static BOOL HLPFILE_LoadGfxByIndex(HLPFILE *hlpfile, unsigned index,
748 HLPFILE_PARAGRAPH* paragraph)
750 char tmp[16];
751 BYTE *ref, *end;
752 BOOL ret;
754 WINE_TRACE("Loading picture #%d\n", index);
756 if (index < hlpfile->numBmps && hlpfile->bmps[index] != NULL)
758 paragraph->u.gfx.u.bmp.hBitmap = hlpfile->bmps[index];
759 return TRUE;
762 sprintf(tmp, "|bm%u", index);
764 if (!HLPFILE_FindSubFile(tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
766 ref += 9;
768 ret = HLPFILE_LoadGfxByAddr(hlpfile, ref, end - ref, paragraph);
770 /* cache bitmap */
771 if (ret && paragraph->cookie == para_bitmap)
773 if (index >= hlpfile->numBmps)
775 hlpfile->numBmps = index + 1;
776 hlpfile->bmps = HeapReAlloc(GetProcessHeap(), 0, hlpfile->bmps,
777 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
779 hlpfile->bmps[index] = paragraph->u.gfx.u.bmp.hBitmap;
781 return ret;
784 /******************************************************************
785 * HLPFILE_AllocLink
789 static HLPFILE_LINK* HLPFILE_AllocLink(int cookie, const char* str, LONG hash,
790 BOOL clrChange, unsigned wnd)
792 HLPFILE_LINK* link;
794 /* FIXME: should build a string table for the attributes.link.lpszPath
795 * they are reallocated for each link
797 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + strlen(str) + 1);
798 if (!link) return NULL;
800 link->cookie = cookie;
801 link->lpszString = (char*)link + sizeof(HLPFILE_LINK);
802 strcpy((char*)link->lpszString, str);
803 link->lHash = hash;
804 link->bClrChange = clrChange ? 1 : 0;
805 link->window = wnd;
806 link->wRefCount = 1;
808 WINE_TRACE("Link[%d] to %s@%08lx:%d\n",
809 link->cookie, link->lpszString,
810 link->lHash, link->window);
811 return link;
814 /***********************************************************************
816 * HLPFILE_AddParagraph
818 static BOOL HLPFILE_AddParagraph(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned* len)
820 HLPFILE_PAGE *page;
821 HLPFILE_PARAGRAPH *paragraph, **paragraphptr;
822 UINT textsize;
823 BYTE *format, *format_end, *text, *text_end;
824 long size;
825 unsigned short bits;
826 unsigned nc, ncol = 1;
828 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
830 for (page = hlpfile->first_page; page->next; page = page->next) /* Nothing */;
831 for (paragraphptr = &page->first_paragraph; *paragraphptr;
832 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
834 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
836 size = GET_UINT(buf, 0x4);
837 text = HeapAlloc(GetProcessHeap(), 0, size);
838 if (!text) return FALSE;
839 if (hlpfile->hasPhrases)
841 HLPFILE_Uncompress2(buf + GET_UINT(buf, 0x10), end, text, text + size);
843 else
845 if (GET_UINT(buf, 0x4) > GET_UINT(buf, 0) - GET_UINT(buf, 0x10))
847 /* block is compressed */
848 HLPFILE_Uncompress3(text, text + size, buf + GET_UINT(buf, 0x10), end);
850 else
852 text = buf + GET_UINT(buf, 0x10);
855 text_end = text + size;
857 format = buf + 0x15;
858 format_end = buf + GET_UINT(buf, 0x10);
860 fetch_long(&format);
861 *len = fetch_ushort(&format);
863 if (buf[0x14] == 0x23)
865 char type;
867 ncol = *format++;
869 WINE_TRACE("#cols %u\n", ncol);
870 type = *format++;
871 if (type == 0 || type == 2)
872 format += 2;
873 format += ncol * 4;
876 for (nc = 0; nc < ncol; nc++)
878 WINE_TRACE("looking for format at offset %u for column %d\n", format - (buf + 0x15), nc);
879 if (buf[0x14] == 0x23)
880 format += 5;
881 format += 4;
882 bits = GET_USHORT(format, 0); format += 2;
883 if (bits & 0x0001) fetch_long(&format);
884 if (bits & 0x0002) fetch_short(&format);
885 if (bits & 0x0004) fetch_short(&format);
886 if (bits & 0x0008) fetch_short(&format);
887 if (bits & 0x0010) fetch_short(&format);
888 if (bits & 0x0020) fetch_short(&format);
889 if (bits & 0x0040) fetch_short(&format);
890 if (bits & 0x0100) format += 3;
891 if (bits & 0x0200)
893 int ntab = fetch_short(&format);
894 unsigned short ts;
896 while (ntab-- > 0)
898 ts = fetch_ushort(&format);
899 if (ts & 0x4000) fetch_ushort(&format);
902 /* 0x0400, 0x0800 and 0x1000 don't need space */
903 if ((bits & 0xE080) != 0)
904 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
906 while (text < text_end && format < format_end)
908 WINE_TRACE("Got text: '%s' (%p/%p - %p/%p)\n", text, text, text_end, format, format_end);
909 textsize = strlen(text) + 1;
910 if (textsize > 1)
912 paragraph = HeapAlloc(GetProcessHeap(), 0,
913 sizeof(HLPFILE_PARAGRAPH) + textsize);
914 if (!paragraph) return FALSE;
915 *paragraphptr = paragraph;
916 paragraphptr = &paragraph->next;
918 paragraph->next = NULL;
919 paragraph->link = attributes.link;
920 if (paragraph->link) paragraph->link->wRefCount++;
921 paragraph->cookie = para_normal_text;
922 paragraph->u.text.wFont = attributes.wFont;
923 paragraph->u.text.wVSpace = attributes.wVSpace;
924 paragraph->u.text.wHSpace = attributes.wHSpace;
925 paragraph->u.text.wIndent = attributes.wIndent;
926 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
927 strcpy(paragraph->u.text.lpszText, text);
929 attributes.wVSpace = 0;
930 attributes.wHSpace = 0;
932 /* else: null text, keep on storing attributes */
933 text += textsize;
935 if (*format == 0xff)
937 format++;
938 break;
941 WINE_TRACE("format=%02x\n", *format);
942 switch (*format)
944 case 0x20:
945 WINE_FIXME("NIY20\n");
946 format += 5;
947 break;
949 case 0x21:
950 WINE_FIXME("NIY21\n");
951 format += 3;
952 break;
954 case 0x80:
955 attributes.wFont = GET_USHORT(format, 1);
956 WINE_TRACE("Changing font to %d\n", attributes.wFont);
957 format += 3;
958 break;
960 case 0x81:
961 attributes.wVSpace++;
962 format += 1;
963 break;
965 case 0x82:
966 attributes.wVSpace++;
967 attributes.wIndent = 0;
968 format += 1;
969 break;
971 case 0x83:
972 attributes.wIndent++;
973 format += 1;
974 break;
976 #if 0
977 case 0x84:
978 format += 3;
979 break;
980 #endif
982 case 0x86:
983 case 0x87:
984 case 0x88:
986 BYTE pos = (*format - 0x86);
987 BYTE type = format[1];
988 long size;
990 format += 2;
991 size = fetch_long(&format);
993 paragraph = HeapAlloc(GetProcessHeap(), 0,
994 sizeof(HLPFILE_PARAGRAPH) + textsize);
995 if (!paragraph) return FALSE;
996 *paragraphptr = paragraph;
997 paragraphptr = &paragraph->next;
999 paragraph->next = NULL;
1000 paragraph->link = attributes.link;
1001 if (paragraph->link) paragraph->link->wRefCount++;
1002 paragraph->cookie = para_bitmap;
1003 paragraph->u.gfx.pos = pos;
1004 switch (type)
1006 case 0x22:
1007 fetch_ushort(&format); /* hot spot */
1008 /* fall thru */
1009 case 0x03:
1010 switch (GET_SHORT(format, 0))
1012 case 0:
1013 HLPFILE_LoadGfxByIndex(hlpfile, GET_SHORT(format, 2),
1014 paragraph);
1015 break;
1016 case 1:
1017 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1018 GET_SHORT(format, 0),
1019 size, GET_SHORT(format, 2));
1020 HLPFILE_LoadGfxByAddr(hlpfile, format + 2, size - 4,
1021 paragraph);
1022 break;
1023 default:
1024 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1025 break;
1027 break;
1028 case 0x05:
1029 WINE_FIXME("Got an embedded element %s\n", format + 6);
1030 break;
1031 default:
1032 WINE_FIXME("Got a type %d picture\n", type);
1033 break;
1035 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1037 format += size;
1039 break;
1041 case 0x89:
1042 HLPFILE_FreeLink(attributes.link);
1043 attributes.link = NULL;
1044 format += 1;
1045 break;
1047 case 0x8B:
1048 case 0x8C:
1049 WINE_FIXME("NIY non-break space/hyphen\n");
1050 format += 1;
1051 break;
1053 #if 0
1054 case 0xA9:
1055 format += 2;
1056 break;
1057 #endif
1059 case 0xC8:
1060 case 0xCC:
1061 WINE_TRACE("macro => %s\n", format + 3);
1062 HLPFILE_FreeLink(attributes.link);
1063 attributes.link = HLPFILE_AllocLink(hlp_link_macro, format + 3,
1064 0, !(*format & 4), -1);
1065 format += 3 + GET_USHORT(format, 1);
1066 break;
1068 case 0xE0:
1069 case 0xE1:
1070 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1071 format += 5;
1072 break;
1074 case 0xE2:
1075 case 0xE3:
1076 case 0xE6:
1077 case 0xE7:
1078 HLPFILE_FreeLink(attributes.link);
1079 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1080 hlpfile->lpszPath,
1081 GET_UINT(format, 1),
1082 !(*format & 4), -1);
1083 format += 5;
1084 break;
1086 case 0xEA:
1087 case 0xEB:
1088 case 0xEE:
1089 case 0xEF:
1091 char* ptr = format + 8;
1092 BYTE type = format[3];
1093 int wnd = -1;
1094 char* str;
1096 if (type == 1) wnd = *ptr++;
1097 if (type == 4 || type == 6)
1099 str = ptr;
1100 ptr += strlen(ptr) + 1;
1102 else
1103 str = hlpfile->lpszPath;
1104 if (type == 6)
1106 for (wnd = hlpfile->numWindows - 1; wnd >= 0; wnd--)
1108 if (!strcmp(ptr, hlpfile->windows[wnd].name)) break;
1110 if (wnd == -1)
1111 WINE_WARN("Couldn't find window info for %s\n", ptr);
1113 HLPFILE_FreeLink(attributes.link);
1114 attributes.link = HLPFILE_AllocLink((*format & 4) ? hlp_link_link : hlp_link_popup,
1115 str, GET_UINT(format, 4),
1116 !(*format & 1), wnd);
1118 format += 3 + GET_USHORT(format, 1);
1119 break;
1121 default:
1122 WINE_WARN("format %02x\n", *format);
1123 format++;
1127 if (text_end != buf + GET_UINT(buf, 0x10) + size)
1128 HeapFree(GetProcessHeap(), 0, text_end - size);
1129 return TRUE;
1132 /******************************************************************
1133 * HLPFILE_ReadFont
1137 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1139 BYTE *ref, *end;
1140 unsigned i, len, idx;
1141 unsigned face_num, dscr_num, face_offset, dscr_offset;
1142 BYTE flag, family;
1144 if (!HLPFILE_FindSubFile("|FONT", &ref, &end))
1146 WINE_WARN("no subfile FONT\n");
1147 hlpfile->numFonts = 0;
1148 hlpfile->fonts = NULL;
1149 return FALSE;
1152 ref += 9;
1154 face_num = GET_USHORT(ref, 0);
1155 dscr_num = GET_USHORT(ref, 2);
1156 face_offset = GET_USHORT(ref, 4);
1157 dscr_offset = GET_USHORT(ref, 6);
1159 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1160 face_num, face_offset, dscr_num, dscr_offset);
1162 hlpfile->numFonts = dscr_num;
1163 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1165 len = (dscr_offset - face_offset) / face_num;
1166 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1167 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1168 for (i = 0; i < dscr_num; i++)
1170 flag = ref[dscr_offset + i * 11 + 0];
1171 family = ref[dscr_offset + i * 11 + 2];
1173 hlpfile->fonts[i].LogFont.lfHeight = -ref[dscr_offset + i * 11 + 1] / 2;
1174 hlpfile->fonts[i].LogFont.lfWidth = 0;
1175 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1176 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1177 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1178 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1179 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1180 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1181 hlpfile->fonts[i].LogFont.lfCharSet = ANSI_CHARSET;
1182 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1183 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1184 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1185 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1187 switch (family)
1189 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1190 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1191 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1192 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1193 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1194 default: WINE_FIXME("Unknown family %u\n", family);
1196 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1198 if (idx < face_num)
1200 strncpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1201 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1) + 1] = '\0';
1203 else
1205 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1206 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1208 hlpfile->fonts[i].hFont = 0;
1209 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1210 ref[dscr_offset + i * 11 + 6],
1211 ref[dscr_offset + i * 11 + 7]);
1212 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1213 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1214 i, flag,
1215 X(0, "bold"),
1216 X(1, "italic"),
1217 X(2, "underline"),
1218 X(3, "strikeOut"),
1219 X(4, "dblUnderline"),
1220 X(5, "smallCaps"),
1221 ref[dscr_offset + i * 11 + 1],
1222 family,
1223 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1224 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1226 return TRUE;
1229 /***********************************************************************
1231 * HLPFILE_ReadFileToBuffer
1233 static BOOL HLPFILE_ReadFileToBuffer(HFILE hFile)
1235 BYTE header[16], dummy[1];
1236 UINT size;
1238 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1240 /* sanity checks */
1241 if (GET_UINT(header, 0) != 0x00035F3F)
1242 {WINE_WARN("wrong header\n"); return FALSE;};
1244 size = GET_UINT(header, 12);
1245 file_buffer = HeapAlloc(GetProcessHeap(), 0, size + 1);
1246 if (!file_buffer) return FALSE;
1248 memcpy(file_buffer, header, 16);
1249 if (_hread(hFile, file_buffer + 16, size - 16) != size - 16)
1250 {WINE_WARN("filesize1\n"); return FALSE;};
1252 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1254 file_buffer[size] = '\0'; /* FIXME: was '0', sounds ackward to me */
1256 return TRUE;
1259 /***********************************************************************
1261 * HLPFILE_FindSubFile
1263 static BOOL HLPFILE_FindSubFile(LPCSTR name, BYTE **subbuf, BYTE **subend)
1265 BYTE *root = file_buffer + GET_UINT(file_buffer, 4);
1266 BYTE *end = file_buffer + GET_UINT(file_buffer, 12);
1267 BYTE *ptr;
1268 BYTE *bth;
1270 unsigned pgsize;
1271 unsigned pglast;
1272 unsigned nentries;
1273 unsigned i, n;
1275 bth = root + 9;
1277 /* FIXME: this should be using the EnumBTree functions from this file */
1278 pgsize = GET_USHORT(bth, 4);
1279 WINE_TRACE("%s => pgsize=%u #pg=%u rootpg=%u #lvl=%u\n",
1280 name, pgsize, GET_USHORT(bth, 30), GET_USHORT(bth, 26), GET_USHORT(bth, 32));
1282 ptr = bth + 38 + GET_USHORT(bth, 26) * pgsize;
1284 for (n = 1; n < GET_USHORT(bth, 32); n++)
1286 nentries = GET_USHORT(ptr, 2);
1287 pglast = GET_USHORT(ptr, 4);
1288 WINE_TRACE("[%u]: #entries=%u next=%u\n", n, nentries, pglast);
1290 ptr += 6;
1291 for (i = 0; i < nentries; i++)
1293 WINE_TRACE("<= %s\n", ptr);
1294 if (strcmp(name, ptr) < 0) break;
1295 ptr += strlen(ptr) + 1;
1296 pglast = GET_USHORT(ptr, 0);
1297 ptr += 2;
1299 ptr = bth + 38 + pglast * pgsize;
1302 nentries = GET_USHORT(ptr, 2);
1303 ptr += 8;
1304 for (i = 0; i < nentries; i++)
1306 char* fname = ptr;
1307 ptr += strlen(fname) + 1;
1308 WINE_TRACE("\\- %s\n", fname);
1309 if (strcmp(fname, name) == 0)
1311 *subbuf = file_buffer + GET_UINT(ptr, 0);
1312 *subend = *subbuf + GET_UINT(*subbuf, 0);
1313 if (file_buffer > *subbuf || *subbuf > *subend || *subend > end)
1315 WINE_WARN("size mismatch\n");
1316 return FALSE;
1318 return TRUE;
1320 ptr += 4;
1323 return FALSE;
1326 /***********************************************************************
1328 * HLPFILE_SystemCommands
1330 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1332 BYTE *buf, *ptr, *end;
1333 HLPFILE_MACRO *macro, **m;
1334 LPSTR p;
1335 unsigned short magic, minor, major, flags;
1337 hlpfile->lpszTitle = NULL;
1339 if (!HLPFILE_FindSubFile("|SYSTEM", &buf, &end)) return FALSE;
1341 magic = GET_USHORT(buf + 9, 0);
1342 minor = GET_USHORT(buf + 9, 2);
1343 major = GET_USHORT(buf + 9, 4);
1344 /* gen date on 4 bytes */
1345 flags = GET_USHORT(buf + 9, 10);
1346 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1347 magic, major, minor, flags);
1348 if (magic != 0x036C || major != 1)
1349 {WINE_WARN("Wrong system header\n"); return FALSE;}
1350 if (minor <= 16) {WINE_WARN("too old file format (NIY)\n"); return FALSE;}
1351 if (flags & 8) {WINE_WARN("Unsupported yet page size\n"); return FALSE;}
1353 hlpfile->version = minor;
1354 hlpfile->flags = flags;
1356 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1358 switch (GET_USHORT(ptr, 0))
1360 case 1:
1361 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1362 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(ptr + 4) + 1);
1363 if (!hlpfile->lpszTitle) return FALSE;
1364 lstrcpy(hlpfile->lpszTitle, ptr + 4);
1365 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1366 break;
1368 case 2:
1369 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1370 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(ptr + 4) + 1);
1371 if (!hlpfile->lpszCopyright) return FALSE;
1372 lstrcpy(hlpfile->lpszCopyright, ptr + 4);
1373 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1374 break;
1376 case 3:
1377 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1378 hlpfile->contents_start = GET_UINT(ptr, 4);
1379 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1380 break;
1382 case 4:
1383 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(ptr + 4) + 1);
1384 if (!macro) break;
1385 p = (char*)macro + sizeof(HLPFILE_MACRO);
1386 lstrcpy(p, (LPSTR)ptr + 4);
1387 macro->lpszMacro = p;
1388 macro->next = 0;
1389 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1390 *m = macro;
1391 break;
1393 case 6:
1394 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1395 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1396 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1397 if (hlpfile->windows)
1399 unsigned flags = GET_USHORT(ptr, 4);
1400 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1402 if (flags & 0x0001) strcpy(wi->type, ptr + 6); else wi->type[0] = '\0';
1403 if (flags & 0x0002) strcpy(wi->name, ptr + 16); else wi->name[0] = '\0';
1404 if (flags & 0x0004) strcpy(wi->caption, ptr + 25); else strncpy(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1405 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1406 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1407 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1408 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1409 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1410 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1411 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1412 WINE_TRACE("System-Window: flags=%c%c%c%c%c%c%c%c type=%s name=%s caption=%s (%ld,%ld)x(%ld,%ld)\n",
1413 flags & 0x0001 ? 'T' : 't',
1414 flags & 0x0002 ? 'N' : 'n',
1415 flags & 0x0004 ? 'C' : 'c',
1416 flags & 0x0008 ? 'X' : 'x',
1417 flags & 0x0010 ? 'Y' : 'y',
1418 flags & 0x0020 ? 'W' : 'w',
1419 flags & 0x0040 ? 'H' : 'h',
1420 flags & 0x0080 ? 'S' : 's',
1421 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1422 wi->size.cx, wi->size.cy);
1424 break;
1425 default:
1426 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1429 return TRUE;
1432 /***********************************************************************
1434 * HLPFILE_UncompressedLZ77_Size
1436 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
1438 int i, newsize = 0;
1440 while (ptr < end)
1442 int mask = *ptr++;
1443 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1445 if (mask & 1)
1447 int code = GET_USHORT(ptr, 0);
1448 int len = 3 + (code >> 12);
1449 newsize += len;
1450 ptr += 2;
1452 else newsize++, ptr++;
1456 return newsize;
1459 /***********************************************************************
1461 * HLPFILE_UncompressLZ77
1463 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
1465 int i;
1467 while (ptr < end)
1469 int mask = *ptr++;
1470 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1472 if (mask & 1)
1474 int code = GET_USHORT(ptr, 0);
1475 int len = 3 + (code >> 12);
1476 int offset = code & 0xfff;
1477 memcpy(newptr, newptr - offset - 1, len);
1478 newptr += len;
1479 ptr += 2;
1481 else *newptr++ = *ptr++;
1485 return newptr;
1488 /***********************************************************************
1490 * HLPFILE_UncompressLZ77_Phrases
1492 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
1494 UINT i, num, dec_size;
1495 BYTE *buf, *end;
1497 if (!HLPFILE_FindSubFile("|Phrases", &buf, &end)) return FALSE;
1499 num = phrases.num = GET_USHORT(buf, 9);
1500 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
1502 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
1504 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1505 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1506 if (!phrases.offsets || !phrases.buffer) return FALSE;
1508 for (i = 0; i <= num; i++)
1509 phrases.offsets[i] = GET_USHORT(buf, 0x11 + 2 * i) - 2 * num - 2;
1511 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, phrases.buffer);
1513 hlpfile->hasPhrases = TRUE;
1514 return TRUE;
1517 /***********************************************************************
1519 * HLPFILE_Uncompress_Phrases40
1521 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
1523 UINT num, dec_size, cpr_size;
1524 BYTE *buf_idx, *end_idx;
1525 BYTE *buf_phs, *end_phs;
1526 short i, n;
1527 long* ptr, mask = 0;
1528 unsigned short bc;
1530 if (!HLPFILE_FindSubFile("|PhrIndex", &buf_idx, &end_idx) ||
1531 !HLPFILE_FindSubFile("|PhrImage", &buf_phs, &end_phs)) return FALSE;
1533 ptr = (long*)(buf_idx + 9 + 28);
1534 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
1535 num = phrases.num = GET_USHORT(buf_idx, 9 + 4);
1537 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
1538 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
1539 GET_UINT(buf_idx, 9 + 0),
1540 GET_UINT(buf_idx, 9 + 4),
1541 GET_UINT(buf_idx, 9 + 8),
1542 GET_UINT(buf_idx, 9 + 12),
1543 GET_UINT(buf_idx, 9 + 16),
1544 GET_UINT(buf_idx, 9 + 20),
1545 GET_USHORT(buf_idx, 9 + 24),
1546 GET_USHORT(buf_idx, 9 + 26));
1548 dec_size = GET_UINT(buf_idx, 9 + 12);
1549 cpr_size = GET_UINT(buf_idx, 9 + 16);
1551 if (dec_size != cpr_size &&
1552 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
1554 WINE_WARN("size mismatch %u %u\n",
1555 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1556 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1559 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1560 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1561 if (!phrases.offsets || !phrases.buffer) return FALSE;
1563 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
1565 phrases.offsets[0] = 0;
1566 for (i = 0; i < num; i++)
1568 for (n = 1; getbit(); n += 1 << bc);
1569 if (getbit()) n++;
1570 if (bc > 1 && getbit()) n += 2;
1571 if (bc > 2 && getbit()) n += 4;
1572 if (bc > 3 && getbit()) n += 8;
1573 if (bc > 4 && getbit()) n += 16;
1574 phrases.offsets[i + 1] = phrases.offsets[i] + n;
1576 #undef getbit
1578 if (dec_size == cpr_size)
1579 memcpy(phrases.buffer, buf_phs + 9, dec_size);
1580 else
1581 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, phrases.buffer);
1583 hlpfile->hasPhrases = FALSE;
1584 return TRUE;
1587 /***********************************************************************
1589 * HLPFILE_Uncompress_Topic
1591 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
1593 BYTE *buf, *ptr, *end, *newptr;
1594 int i, newsize = 0;
1596 if (!HLPFILE_FindSubFile("|TOPIC", &buf, &end))
1597 {WINE_WARN("topic0\n"); return FALSE;}
1599 switch (hlpfile->flags & (8|4))
1601 case 8:
1602 WINE_FIXME("Unsupported format\n");
1603 return FALSE;
1604 case 4:
1605 buf += 9;
1606 topic.wMapLen = (end - buf - 1) / 0x1000 + 1;
1608 for (i = 0; i < topic.wMapLen; i++)
1610 ptr = buf + i * 0x1000;
1612 /* I don't know why, it's necessary for printman.hlp */
1613 if (ptr + 0x44 > end) ptr = end - 0x44;
1615 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + 0x1000));
1618 topic.map = HeapAlloc(GetProcessHeap(), 0,
1619 topic.wMapLen * sizeof(topic.map[0]) + newsize);
1620 if (!topic.map) return FALSE;
1621 newptr = (char*)(topic.map + topic.wMapLen);
1622 topic.end = newptr + newsize;
1624 for (i = 0; i < topic.wMapLen; i++)
1626 ptr = buf + i * 0x1000;
1627 if (ptr + 0x44 > end) ptr = end - 0x44;
1629 topic.map[i] = newptr;
1630 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + 0x1000), newptr);
1632 break;
1633 case 0:
1634 /* basically, we need to copy the 0x1000 byte pages (removing the first 0x0C) in
1635 * one single are in memory
1637 #define DST_LEN (0x1000 - 0x0C)
1638 buf += 9;
1639 newsize = end - buf;
1640 /* number of destination pages */
1641 topic.wMapLen = (newsize - 1) / DST_LEN + 1;
1642 topic.map = HeapAlloc(GetProcessHeap(), 0,
1643 topic.wMapLen * (sizeof(topic.map[0]) + DST_LEN));
1644 if (!topic.map) return FALSE;
1645 newptr = (char*)(topic.map + topic.wMapLen);
1646 topic.end = newptr + newsize;
1648 for (i = 0; i < topic.wMapLen; i++)
1650 topic.map[i] = newptr + i * DST_LEN;
1651 memcpy(topic.map[i], buf + i * 0x1000 + 0x0C, DST_LEN);
1653 #undef DST_LEN
1654 break;
1656 return TRUE;
1659 /***********************************************************************
1661 * HLPFILE_Uncompress2
1664 static void HLPFILE_Uncompress2(const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
1666 BYTE *phptr, *phend;
1667 UINT code;
1668 UINT index;
1670 while (ptr < end && newptr < newend)
1672 if (!*ptr || *ptr >= 0x10)
1673 *newptr++ = *ptr++;
1674 else
1676 code = 0x100 * ptr[0] + ptr[1];
1677 index = (code - 0x100) / 2;
1679 phptr = phrases.buffer + phrases.offsets[index];
1680 phend = phrases.buffer + phrases.offsets[index + 1];
1682 if (newptr + (phend - phptr) > newend)
1684 WINE_FIXME("buffer overflow %p > %p for %d bytes\n",
1685 newptr, newend, phend - phptr);
1686 return;
1688 memcpy(newptr, phptr, phend - phptr);
1689 newptr += phend - phptr;
1690 if (code & 1) *newptr++ = ' ';
1692 ptr += 2;
1695 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
1698 /******************************************************************
1699 * HLPFILE_Uncompress3
1703 static BOOL HLPFILE_Uncompress3(char* dst, const char* dst_end,
1704 const BYTE* src, const BYTE* src_end)
1706 int idx, len;
1708 for (; src < src_end; src++)
1710 if ((*src & 1) == 0)
1712 idx = *src / 2;
1713 if (idx > phrases.num)
1715 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1716 len = 0;
1718 else
1720 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1721 if (dst + len <= dst_end)
1722 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1725 else if ((*src & 0x03) == 0x01)
1727 idx = (*src + 1) * 64;
1728 idx += *++src;
1729 if (idx > phrases.num)
1731 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1732 len = 0;
1734 else
1736 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1737 if (dst + len <= dst_end)
1738 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1741 else if ((*src & 0x07) == 0x03)
1743 len = (*src / 8) + 1;
1744 if (dst + len <= dst_end)
1745 memcpy(dst, src + 1, len);
1746 src += len;
1748 else
1750 len = (*src / 16) + 1;
1751 if (dst + len <= dst_end)
1752 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
1754 dst += len;
1757 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
1758 return TRUE;
1761 /******************************************************************
1762 * HLPFILE_UncompressRLE
1766 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
1768 BYTE ch;
1769 BYTE* sdst = *dst + dstsz;
1771 while (src < end)
1773 ch = *src++;
1774 if (ch & 0x80)
1776 ch &= 0x7F;
1777 if (ch == 0) WINE_FIXME("Null length 1, next is %u\n", *src);
1778 if ((*dst) + ch < sdst)
1779 memcpy(*dst, src, ch);
1780 src += ch;
1782 else
1784 if ((*dst) + ch < sdst)
1785 memset(*dst, (char)*src, ch);
1786 src++;
1787 if (ch == 0)
1789 WINE_FIXME("Null length 2, next is %u\n", *src);
1792 *dst += ch;
1794 if (*dst != sdst)
1795 WINE_FIXME("Buffer X-flow: d(%u) instead of d(%u)\n",
1796 *dst - (sdst - dstsz), dstsz);
1799 /******************************************************************
1800 * HLPFILE_EnumBTreeLeaves
1804 static void HLPFILE_EnumBTreeLeaves(const BYTE* buf, const BYTE* end, unsigned (*fn)(const BYTE*, void*), void* user)
1806 unsigned psize, pnext;
1807 unsigned num, nlvl;
1808 const BYTE* ptr;
1810 num = GET_UINT(buf, 9 + 34);
1811 psize = GET_USHORT(buf, 9 + 4);
1812 nlvl = GET_USHORT(buf, 9 + 32);
1813 pnext = GET_USHORT(buf, 9 + 26);
1815 WINE_TRACE("BTree: #entries=%u pagSize=%u #levels=%u #pages=%u root=%u struct%16s\n",
1816 num, psize, nlvl, GET_USHORT(buf, 9 + 30), pnext, buf + 9 + 6);
1817 if (!num) return;
1819 while (--nlvl > 0)
1821 ptr = (buf + 9 + 38) + pnext * psize;
1822 WINE_TRACE("BTree: (index[%u]) unused=%u #entries=%u <%u\n",
1823 pnext, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2), GET_USHORT(ptr, 4));
1824 pnext = GET_USHORT(ptr, 4);
1826 while (pnext != 0xFFFF)
1828 const BYTE* node_page;
1829 unsigned short limit;
1831 node_page = ptr = (buf + 9 + 38) + pnext * psize;
1832 limit = GET_USHORT(ptr, 2);
1833 WINE_TRACE("BTree: (leaf [%u]) unused=%u #entries=%u <%u >%u\n",
1834 pnext, GET_USHORT(ptr, 0), limit, GET_USHORT(ptr, 4), GET_USHORT(ptr, 6));
1835 ptr += 8;
1836 while (limit--)
1837 ptr += (fn)(ptr, user);
1838 pnext = GET_USHORT(node_page, 6);
1842 struct myfncb {
1843 HLPFILE* hlpfile;
1844 int i;
1847 static unsigned myfn(const BYTE* ptr, void* user)
1849 struct myfncb* m = user;
1851 m->hlpfile->Context[m->i].lHash = GET_UINT(ptr, 0);
1852 m->hlpfile->Context[m->i].offset = GET_UINT(ptr, 4);
1853 m->i++;
1854 return 8;
1857 /***********************************************************************
1859 * HLPFILE_GetContext
1861 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
1863 BYTE *cbuf, *cend;
1864 struct myfncb m;
1865 unsigned clen;
1867 if (!HLPFILE_FindSubFile("|CONTEXT", &cbuf, &cend)) {WINE_WARN("context0\n"); return FALSE;}
1869 clen = GET_UINT(cbuf, 0x2b);
1870 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen * sizeof(HLPFILE_CONTEXT));
1871 if (!hlpfile->Context) return FALSE;
1872 hlpfile->wContextLen = clen;
1874 m.hlpfile = hlpfile;
1875 m.i = 0;
1876 HLPFILE_EnumBTreeLeaves(cbuf, cend, myfn, &m);
1878 return TRUE;
1881 /******************************************************************
1882 * HLPFILE_DeleteLink
1886 void HLPFILE_FreeLink(HLPFILE_LINK* link)
1888 if (link && !--link->wRefCount)
1889 HeapFree(GetProcessHeap(), 0, link);
1892 /***********************************************************************
1894 * HLPFILE_DeleteParagraph
1896 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
1898 HLPFILE_PARAGRAPH* next;
1900 while (paragraph)
1902 next = paragraph->next;
1904 if (paragraph->cookie == para_metafile)
1905 DeleteMetaFile(paragraph->u.gfx.u.mf.hMetaFile);
1907 HLPFILE_FreeLink(paragraph->link);
1909 HeapFree(GetProcessHeap(), 0, paragraph);
1910 paragraph = next;
1914 /***********************************************************************
1916 * DeleteMacro
1918 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
1920 HLPFILE_MACRO* next;
1922 while (macro)
1924 next = macro->next;
1925 HeapFree(GetProcessHeap(), 0, macro);
1926 macro = next;
1930 /***********************************************************************
1932 * DeletePage
1934 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
1936 HLPFILE_PAGE* next;
1938 while (page)
1940 next = page->next;
1941 HLPFILE_DeleteParagraph(page->first_paragraph);
1942 HLPFILE_DeleteMacro(page->first_macro);
1943 HeapFree(GetProcessHeap(), 0, page);
1944 page = next;
1948 /***********************************************************************
1950 * HLPFILE_FreeHlpFile
1952 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
1954 unsigned i;
1956 if (!hlpfile || --hlpfile->wRefCount > 0) return;
1958 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
1959 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
1960 else first_hlpfile = hlpfile->next;
1962 if (hlpfile->numFonts)
1964 for (i = 0; i < hlpfile->numFonts; i++)
1966 DeleteObject(hlpfile->fonts[i].hFont);
1968 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
1971 if (hlpfile->numBmps)
1973 for (i = 0; i < hlpfile->numBmps; i++)
1975 DeleteObject(hlpfile->bmps[i]);
1977 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
1980 HLPFILE_DeletePage(hlpfile->first_page);
1981 HLPFILE_DeleteMacro(hlpfile->first_macro);
1983 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
1984 if (hlpfile->Context) HeapFree(GetProcessHeap(), 0, hlpfile->Context);
1985 if (hlpfile->lpszTitle) HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
1986 if (hlpfile->lpszCopyright) HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
1987 HeapFree(GetProcessHeap(), 0, hlpfile);