Fixed 'break NN' command (using dbghelp.SymEnumLines).
[wine.git] / programs / winhelp / hlpfile.c
blob0556b0575560b7911e800342d346556208d86ab2
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 <stdarg.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winhelp.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
36 static inline unsigned short GET_USHORT(const BYTE* buffer, unsigned i)
38 return (BYTE)buffer[i] + 0x100 * (BYTE)buffer[i + 1];
41 static inline short GET_SHORT(const BYTE* buffer, unsigned i)
43 return (BYTE)buffer[i] + 0x100 * (signed char)buffer[i+1];
46 static inline unsigned GET_UINT(const BYTE* buffer, unsigned i)
48 return GET_USHORT(buffer, i) + 0x10000 * GET_USHORT(buffer, i + 2);
51 static HLPFILE *first_hlpfile = 0;
52 static BYTE *file_buffer;
54 static struct
56 UINT num;
57 unsigned* offsets;
58 char* buffer;
59 } phrases;
61 static struct
63 BYTE** map;
64 BYTE* end;
65 UINT wMapLen;
66 } topic;
68 static struct
70 UINT wFont;
71 UINT wIndent;
72 UINT wHSpace;
73 UINT wVSpace;
74 HLPFILE_LINK* link;
75 } attributes;
77 static BOOL HLPFILE_DoReadHlpFile(HLPFILE*, LPCSTR);
78 static BOOL HLPFILE_ReadFileToBuffer(HFILE);
79 static BOOL HLPFILE_FindSubFile(LPCSTR name, BYTE**, BYTE**);
80 static BOOL HLPFILE_SystemCommands(HLPFILE*);
81 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end);
82 static BYTE* HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr);
83 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE*);
84 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE*);
85 static BOOL HLPFILE_Uncompress_Topic(HLPFILE*);
86 static BOOL HLPFILE_GetContext(HLPFILE*);
87 static BOOL HLPFILE_AddPage(HLPFILE*, BYTE*, BYTE*, unsigned);
88 static BOOL HLPFILE_AddParagraph(HLPFILE*, BYTE *, BYTE*, unsigned*);
89 static void HLPFILE_Uncompress2(const BYTE*, const BYTE*, BYTE*, const BYTE*);
90 static BOOL HLPFILE_Uncompress3(char*, const char*, const BYTE*, const BYTE*);
91 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz);
92 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile);
94 #if 0
95 /***********************************************************************
97 * HLPFILE_PageByNumber
99 static HLPFILE_PAGE *HLPFILE_PageByNumber(LPCSTR lpszPath, UINT wNum)
101 HLPFILE_PAGE *page;
102 HLPFILE *hlpfile = HLPFILE_ReadHlpFile(lpszPath);
104 if (!hlpfile) return 0;
106 WINE_TRACE("[%s/%u]\n", lpszPath, wNum);
108 for (page = hlpfile->first_page; page && wNum; page = page->next) wNum--;
110 /* HLPFILE_FreeHlpFile(lpszPath); */
112 return page;
114 #endif
116 /* FIXME:
117 * this finds the page containing the offset. The offset can either
118 * refer to the top of the page (offset == page->offset), or
119 * to some paragraph inside the page...
120 * As of today, we only return the page... we should also return
121 * a paragraph, and then, while opening a new page, compute the
122 * y-offset of the paragraph to be shown and scroll the window
123 * accordinly
125 /******************************************************************
126 * HLPFILE_PageByOffset
130 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset)
132 HLPFILE_PAGE* page;
133 HLPFILE_PAGE* found;
135 if (!hlpfile) return 0;
137 WINE_TRACE("<%s>[%lx]\n", hlpfile->lpszPath, offset);
139 if (offset == 0xFFFFFFFF) return NULL;
140 page = NULL;
142 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
144 if (page->offset <= offset && (!found || found->offset < page->offset))
145 found = page;
147 if (!found)
148 WINE_ERR("Page of offset %lu not found in file %s\n",
149 offset, hlpfile->lpszPath);
150 return found;
153 /***********************************************************************
155 * HLPFILE_HlpFilePageByHash
157 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash)
159 int i;
161 if (!hlpfile) return 0;
163 WINE_TRACE("<%s>[%lx]\n", hlpfile->lpszPath, lHash);
165 for (i = 0; i < hlpfile->wContextLen; i++)
167 if (hlpfile->Context[i].lHash == lHash)
168 return HLPFILE_PageByOffset(hlpfile, hlpfile->Context[i].offset);
171 WINE_ERR("Page of hash %lx not found in file %s\n", lHash, hlpfile->lpszPath);
172 return NULL;
175 /***********************************************************************
177 * HLPFILE_Contents
179 HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile)
181 HLPFILE_PAGE* page = NULL;
183 if (!hlpfile) return NULL;
185 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start);
186 if (!page) page = hlpfile->first_page;
187 return page;
190 /***********************************************************************
192 * HLPFILE_Hash
194 LONG HLPFILE_Hash(LPCSTR lpszContext)
196 LONG lHash = 0;
197 CHAR c;
199 while ((c = *lpszContext++))
201 CHAR x = 0;
202 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
203 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
204 if (c >= '1' && c <= '9') x = c - '0';
205 if (c == '0') x = 10;
206 if (c == '.') x = 12;
207 if (c == '_') x = 13;
208 if (x) lHash = lHash * 43 + x;
210 return lHash;
213 /***********************************************************************
215 * HLPFILE_ReadHlpFile
217 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
219 HLPFILE* hlpfile;
221 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
223 if (!strcmp(lpszPath, hlpfile->lpszPath))
225 hlpfile->wRefCount++;
226 return hlpfile;
230 hlpfile = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
231 if (!hlpfile) return 0;
233 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
234 hlpfile->lpszTitle = NULL;
235 hlpfile->lpszCopyright = NULL;
236 hlpfile->first_page = NULL;
237 hlpfile->first_macro = NULL;
238 hlpfile->wContextLen = 0;
239 hlpfile->Context = NULL;
240 hlpfile->contents_start = 0xFFFFFFFF;
241 hlpfile->prev = NULL;
242 hlpfile->next = first_hlpfile;
243 hlpfile->wRefCount = 1;
245 hlpfile->numBmps = 0;
246 hlpfile->bmps = NULL;
248 hlpfile->numFonts = 0;
249 hlpfile->fonts = NULL;
251 hlpfile->numWindows = 0;
252 hlpfile->windows = NULL;
254 strcpy(hlpfile->lpszPath, lpszPath);
256 first_hlpfile = hlpfile;
257 if (hlpfile->next) hlpfile->next->prev = hlpfile;
259 phrases.offsets = NULL;
260 phrases.buffer = NULL;
261 topic.map = NULL;
262 topic.end = NULL;
263 file_buffer = NULL;
265 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
267 HLPFILE_FreeHlpFile(hlpfile);
268 hlpfile = 0;
271 HeapFree(GetProcessHeap(), 0, phrases.offsets);
272 HeapFree(GetProcessHeap(), 0, phrases.buffer);
273 HeapFree(GetProcessHeap(), 0, topic.map);
274 HeapFree(GetProcessHeap(), 0, file_buffer);
276 return hlpfile;
279 /***********************************************************************
281 * HLPFILE_DoReadHlpFile
283 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
285 BOOL ret;
286 HFILE hFile;
287 OFSTRUCT ofs;
288 BYTE* buf;
289 DWORD ref = 0x0C;
290 unsigned index, old_index, offset, len, offs;
292 hFile = OpenFile(lpszPath, &ofs, OF_READ);
293 if (hFile == HFILE_ERROR) return FALSE;
295 ret = HLPFILE_ReadFileToBuffer(hFile);
296 _lclose(hFile);
297 if (!ret) return FALSE;
299 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
301 /* load phrases support */
302 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
303 HLPFILE_Uncompress_Phrases40(hlpfile);
305 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
306 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
308 buf = topic.map[0];
309 old_index = -1;
310 offs = 0;
313 BYTE* end;
315 /* FIXME this depends on the blocksize, can be 2k in some cases */
316 index = (ref - 0x0C) >> 14;
317 offset = (ref - 0x0C) & 0x3fff;
319 WINE_TRACE("ref=%08lx => [%u/%u]\n", ref, index, offset);
321 if (index >= topic.wMapLen) {WINE_WARN("maplen\n"); break;}
322 buf = topic.map[index] + offset;
323 if (buf + 0x15 >= topic.end) {WINE_WARN("extra\n"); break;}
324 end = min(buf + GET_UINT(buf, 0), topic.end);
325 if (index != old_index) {offs = 0; old_index = index;}
327 switch (buf[0x14])
329 case 0x02:
330 if (!HLPFILE_AddPage(hlpfile, buf, end, index * 0x8000L + offs)) return FALSE;
331 break;
333 case 0x20:
334 if (!HLPFILE_AddParagraph(hlpfile, buf, end, &len)) return FALSE;
335 offs += len;
336 break;
338 case 0x23:
339 if (!HLPFILE_AddParagraph(hlpfile, buf, end, &len)) return FALSE;
340 offs += len;
341 break;
343 default:
344 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
347 ref = GET_UINT(buf, 0xc);
348 } while (ref != 0xffffffff);
350 return HLPFILE_GetContext(hlpfile);
353 /***********************************************************************
355 * HLPFILE_AddPage
357 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned offset)
359 HLPFILE_PAGE* page;
360 BYTE* title;
361 UINT titlesize;
362 char* ptr;
363 HLPFILE_MACRO*macro;
365 if (buf + 0x31 > end) {WINE_WARN("page1\n"); return FALSE;};
366 title = buf + GET_UINT(buf, 0x10);
367 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
369 titlesize = GET_UINT(buf, 4);
370 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
371 if (!page) return FALSE;
372 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
374 if (hlpfile->hasPhrases)
376 HLPFILE_Uncompress2(title, end, (BYTE*)page->lpszTitle, (BYTE*)page->lpszTitle + titlesize);
378 else
380 if (GET_UINT(buf, 0x4) > GET_UINT(buf, 0) - GET_UINT(buf, 0x10))
382 /* need to decompress */
383 HLPFILE_Uncompress3(page->lpszTitle, page->lpszTitle + titlesize,
384 title, end);
386 else
388 memcpy(page->lpszTitle, title, titlesize);
392 page->lpszTitle[titlesize] = '\0';
394 if (hlpfile->first_page)
396 HLPFILE_PAGE *p;
398 for (p = hlpfile->first_page; p->next; p = p->next);
399 page->prev = p;
400 p->next = page;
402 else
404 hlpfile->first_page = page;
405 page->prev = NULL;
408 page->file = hlpfile;
409 page->next = NULL;
410 page->first_paragraph = NULL;
411 page->first_macro = NULL;
412 page->wNumber = GET_UINT(buf, 0x21);
413 page->offset = offset;
415 page->browse_bwd = GET_UINT(buf, 0x19);
416 page->browse_fwd = GET_UINT(buf, 0x1D);
418 WINE_TRACE("Added page[%d]: title='%s' %08lx << %08x >> %08lx\n",
419 page->wNumber, page->lpszTitle,
420 page->browse_bwd, page->offset, page->browse_fwd);
422 memset(&attributes, 0, sizeof(attributes));
424 /* now load macros */
425 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
426 while (ptr < page->lpszTitle + titlesize)
428 unsigned len = strlen(ptr);
429 WINE_TRACE("macro: %s\n", ptr);
430 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
431 macro->lpszMacro = (char*)(macro + 1);
432 memcpy((char*)macro->lpszMacro, ptr, len + 1);
433 /* FIXME: shall we really link macro in reverse order ??
434 * may produce strange results when played at page opening
436 macro->next = page->first_macro;
437 page->first_macro = macro;
438 ptr += len + 1;
441 return TRUE;
444 static long fetch_long(BYTE** ptr)
446 long ret;
448 if (*(*ptr) & 1)
450 ret = (*(unsigned long*)(*ptr) - 0x80000000L) / 2;
451 (*ptr) += 4;
453 else
455 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
456 (*ptr) += 2;
459 return ret;
462 static unsigned long fetch_ulong(BYTE** ptr)
464 unsigned long ret;
466 if (*(*ptr) & 1)
468 ret = *(unsigned long*)(*ptr) / 2;
469 (*ptr) += 4;
471 else
473 ret = *(unsigned short*)(*ptr) / 2;
474 (*ptr) += 2;
476 return ret;
479 static short fetch_short(BYTE** ptr)
481 short ret;
483 if (*(*ptr) & 1)
485 ret = (*(unsigned short*)(*ptr) - 0x8000) / 2;
486 (*ptr) += 2;
488 else
490 ret = (*(unsigned char*)(*ptr) - 0x80) / 2;
491 (*ptr)++;
493 return ret;
496 static unsigned short fetch_ushort(BYTE** ptr)
498 unsigned short ret;
500 if (*(*ptr) & 1)
502 ret = *(unsigned short*)(*ptr) / 2;
503 (*ptr) += 2;
505 else
507 ret = *(unsigned char*)(*ptr) / 2;
508 (*ptr)++;
510 return ret;
513 /******************************************************************
514 * HLPFILE_DecompressGfx
516 * Decompress the data part of a bitmap or a metafile
518 static BYTE* HLPFILE_DecompressGfx(BYTE* src, unsigned csz, unsigned sz, BYTE packing)
520 BYTE* dst;
521 BYTE* tmp;
522 BYTE* tmp2;
523 unsigned sz77;
525 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
527 switch (packing)
529 case 0: /* uncompressed */
530 if (sz != csz)
531 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
532 dst = src;
533 break;
534 case 1: /* RunLen */
535 tmp = dst = HeapAlloc(GetProcessHeap(), 0, sz);
536 if (!dst) return NULL;
537 HLPFILE_UncompressRLE(src, src + csz, &tmp, sz);
538 if (tmp - dst != sz)
539 WINE_WARN("Bogus gfx sizes (RunLen): %u/%u\n", tmp - dst, sz);
540 break;
541 case 2: /* LZ77 */
542 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
543 dst = HeapAlloc(GetProcessHeap(), 0, sz77);
544 if (!dst) return NULL;
545 HLPFILE_UncompressLZ77(src, src + csz, dst);
546 if (sz77 != sz)
547 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
548 break;
549 case 3: /* LZ77 then RLE */
550 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
551 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
552 if (!tmp) return FALSE;
553 HLPFILE_UncompressLZ77(src, src + csz, tmp);
554 dst = tmp2 = HeapAlloc(GetProcessHeap(), 0, sz);
555 if (!dst) return FALSE;
556 HLPFILE_UncompressRLE(tmp, tmp + sz77, &tmp2, sz);
557 if (tmp2 - dst != sz)
558 WINE_WARN("Bogus gfx sizes (LZ77+RunLen): %u / %u\n", tmp2 - dst, sz);
559 HeapFree(GetProcessHeap(), 0, tmp);
560 break;
561 default:
562 WINE_FIXME("Unsupported packing %u\n", packing);
563 return NULL;
565 return dst;
568 /******************************************************************
569 * HLPFILE_LoadBitmap
573 static BOOL HLPFILE_LoadBitmap(BYTE* beg, BYTE type, BYTE pack,
574 HLPFILE_PARAGRAPH* paragraph)
576 BYTE* ptr;
577 BYTE* pict_beg;
578 BITMAPINFO* bi;
579 unsigned long off, csz;
580 HDC hdc;
582 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
583 if (!bi) return FALSE;
585 ptr = beg + 2; /* for type and pack */
587 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
588 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
589 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
590 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
591 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
592 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
593 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
594 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
595 bi->bmiHeader.biClrImportant = fetch_ulong(&ptr);
596 bi->bmiHeader.biCompression = BI_RGB;
597 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
598 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
599 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
600 WINE_TRACE("planes=%d bc=%d size=(%ld,%ld)\n",
601 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
602 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
604 csz = fetch_ulong(&ptr);
605 fetch_ulong(&ptr); /* hotspot size */
607 off = GET_UINT(ptr, 0); ptr += 4;
608 /* GET_UINT(ptr, 0); hotspot offset */ ptr += 4;
610 /* now read palette info */
611 if (type == 0x06)
613 unsigned nc = bi->bmiHeader.biClrUsed;
614 unsigned i;
616 /* not quite right, especially for bitfields type of compression */
617 if (!nc && bi->bmiHeader.biBitCount <= 8)
618 nc = 1 << bi->bmiHeader.biBitCount;
620 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
621 if (!bi) return FALSE;
622 for (i = 0; i < nc; i++)
624 bi->bmiColors[i].rgbBlue = ptr[0];
625 bi->bmiColors[i].rgbGreen = ptr[1];
626 bi->bmiColors[i].rgbRed = ptr[2];
627 bi->bmiColors[i].rgbReserved = 0;
628 ptr += 4;
631 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack);
633 paragraph->u.gfx.u.bmp.hBitmap = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
634 CBM_INIT, pict_beg,
635 bi, DIB_RGB_COLORS);
636 ReleaseDC(0, hdc);
637 if (!paragraph->u.gfx.u.bmp.hBitmap)
638 WINE_ERR("Couldn't create bitmap\n");
640 HeapFree(GetProcessHeap(), 0, bi);
641 if (pict_beg != beg + off) HeapFree(GetProcessHeap(), 0, pict_beg);
643 return TRUE;
646 /******************************************************************
647 * HLPFILE_LoadMetaFile
651 static BOOL HLPFILE_LoadMetaFile(BYTE* beg, BYTE pack, HLPFILE_PARAGRAPH* paragraph)
653 BYTE* ptr;
654 unsigned long size, csize;
655 unsigned long off, hsoff;
656 BYTE* bits;
657 METAFILEPICT mfp;
659 WINE_TRACE("Loading metafile\n");
661 ptr = beg + 2; /* for type and pack */
663 mfp.mm = fetch_ushort(&ptr); /* mapping mode */
665 mfp.xExt = GET_USHORT(ptr, 0);
666 mfp.yExt = GET_USHORT(ptr, 2);
667 ptr += 4;
669 size = fetch_ulong(&ptr); /* decompressed size */
670 csize = fetch_ulong(&ptr); /* compressed size */
671 fetch_ulong(&ptr); /* hotspot size */
672 off = GET_UINT(ptr, 0);
673 hsoff = GET_UINT(ptr, 4);
674 ptr += 8;
676 WINE_TRACE("sz=%lu csz=%lu (%ld,%ld) offs=%lu/%u,%lu\n",
677 size, csize, mfp.xExt, mfp.yExt, off, ptr - beg, hsoff);
679 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack);
680 if (!bits) return FALSE;
682 paragraph->cookie = para_metafile;
684 mfp.hMF = NULL;
686 paragraph->u.gfx.u.mf.hMetaFile = SetMetaFileBitsEx(size, bits);
688 if (!paragraph->u.gfx.u.mf.hMetaFile)
689 WINE_FIXME("Couldn't load metafile\n");
691 if (bits != beg + off) HeapFree(GetProcessHeap(), 0, bits);
693 paragraph->u.gfx.u.mf.mfSize.cx = mfp.xExt;
694 paragraph->u.gfx.u.mf.mfSize.cy = mfp.yExt;
696 return TRUE;
699 /******************************************************************
700 * HLPFILE_LoadGfxByAddr
704 static BOOL HLPFILE_LoadGfxByAddr(HLPFILE *hlpfile, BYTE* ref,
705 unsigned long size,
706 HLPFILE_PARAGRAPH* paragraph)
708 unsigned i, numpict;
710 numpict = GET_USHORT(ref, 2);
711 WINE_TRACE("Got picture magic=%04x #=%d\n",
712 GET_USHORT(ref, 0), numpict);
714 for (i = 0; i < numpict; i++)
716 BYTE* beg;
717 BYTE* ptr;
718 BYTE type, pack;
720 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
721 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
723 type = *ptr++;
724 pack = *ptr++;
726 switch (type)
728 case 5: /* device dependent bmp */
729 case 6: /* device independent bmp */
730 HLPFILE_LoadBitmap(beg, type, pack, paragraph);
731 break;
732 case 8:
733 HLPFILE_LoadMetaFile(beg, pack, paragraph);
734 break;
735 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
738 /* FIXME: hotspots */
740 /* FIXME: implement support for multiple picture format */
741 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
742 break;
744 return TRUE;
747 /******************************************************************
748 * HLPFILE_LoadGfxByIndex
752 static BOOL HLPFILE_LoadGfxByIndex(HLPFILE *hlpfile, unsigned index,
753 HLPFILE_PARAGRAPH* paragraph)
755 char tmp[16];
756 BYTE *ref, *end;
757 BOOL ret;
759 WINE_TRACE("Loading picture #%d\n", index);
761 if (index < hlpfile->numBmps && hlpfile->bmps[index] != NULL)
763 paragraph->u.gfx.u.bmp.hBitmap = hlpfile->bmps[index];
764 return TRUE;
767 sprintf(tmp, "|bm%u", index);
769 if (!HLPFILE_FindSubFile(tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
771 ref += 9;
773 ret = HLPFILE_LoadGfxByAddr(hlpfile, ref, end - ref, paragraph);
775 /* cache bitmap */
776 if (ret && paragraph->cookie == para_bitmap)
778 if (index >= hlpfile->numBmps)
780 hlpfile->numBmps = index + 1;
781 if (hlpfile->bmps)
782 hlpfile->bmps = HeapReAlloc(GetProcessHeap(), 0, hlpfile->bmps,
783 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
784 else
785 hlpfile->bmps = HeapAlloc(GetProcessHeap(), 0,
786 hlpfile->numBmps * sizeof(hlpfile->bmps[0]));
789 hlpfile->bmps[index] = paragraph->u.gfx.u.bmp.hBitmap;
791 return ret;
794 /******************************************************************
795 * HLPFILE_AllocLink
799 static HLPFILE_LINK* HLPFILE_AllocLink(int cookie, const char* str, LONG hash,
800 BOOL clrChange, unsigned wnd)
802 HLPFILE_LINK* link;
804 /* FIXME: should build a string table for the attributes.link.lpszPath
805 * they are reallocated for each link
807 link = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK) + strlen(str) + 1);
808 if (!link) return NULL;
810 link->cookie = cookie;
811 link->lpszString = (char*)link + sizeof(HLPFILE_LINK);
812 strcpy((char*)link->lpszString, str);
813 link->lHash = hash;
814 link->bClrChange = clrChange ? 1 : 0;
815 link->window = wnd;
816 link->wRefCount = 1;
818 WINE_TRACE("Link[%d] to %s@%08lx:%d\n",
819 link->cookie, link->lpszString,
820 link->lHash, link->window);
821 return link;
824 /***********************************************************************
826 * HLPFILE_AddParagraph
828 static BOOL HLPFILE_AddParagraph(HLPFILE *hlpfile, BYTE *buf, BYTE *end, unsigned* len)
830 HLPFILE_PAGE *page;
831 HLPFILE_PARAGRAPH *paragraph, **paragraphptr;
832 UINT textsize;
833 BYTE *format, *format_end;
834 char *text, *text_end;
835 long size;
836 unsigned short bits;
837 unsigned nc, ncol = 1;
839 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
841 for (page = hlpfile->first_page; page->next; page = page->next) /* Nothing */;
842 for (paragraphptr = &page->first_paragraph; *paragraphptr;
843 paragraphptr = &(*paragraphptr)->next) /* Nothing */;
845 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
847 size = GET_UINT(buf, 0x4);
848 text = HeapAlloc(GetProcessHeap(), 0, size);
849 if (!text) return FALSE;
850 if (hlpfile->hasPhrases)
852 HLPFILE_Uncompress2(buf + GET_UINT(buf, 0x10), end, (BYTE*)text, (BYTE*)text + size);
854 else
856 if (GET_UINT(buf, 0x4) > GET_UINT(buf, 0) - GET_UINT(buf, 0x10))
858 /* block is compressed */
859 HLPFILE_Uncompress3(text, text + size, buf + GET_UINT(buf, 0x10), end);
861 else
863 text = (char*)buf + GET_UINT(buf, 0x10);
866 text_end = text + size;
868 format = buf + 0x15;
869 format_end = buf + GET_UINT(buf, 0x10);
871 fetch_long(&format);
872 *len = fetch_ushort(&format);
874 if (buf[0x14] == 0x23)
876 char type;
878 ncol = *format++;
880 WINE_TRACE("#cols %u\n", ncol);
881 type = *format++;
882 if (type == 0 || type == 2)
883 format += 2;
884 format += ncol * 4;
887 for (nc = 0; nc < ncol; nc++)
889 WINE_TRACE("looking for format at offset %u for column %d\n", format - (buf + 0x15), nc);
890 if (buf[0x14] == 0x23)
891 format += 5;
892 format += 4;
893 bits = GET_USHORT(format, 0); format += 2;
894 if (bits & 0x0001) fetch_long(&format);
895 if (bits & 0x0002) fetch_short(&format);
896 if (bits & 0x0004) fetch_short(&format);
897 if (bits & 0x0008) fetch_short(&format);
898 if (bits & 0x0010) fetch_short(&format);
899 if (bits & 0x0020) fetch_short(&format);
900 if (bits & 0x0040) fetch_short(&format);
901 if (bits & 0x0100) format += 3;
902 if (bits & 0x0200)
904 int ntab = fetch_short(&format);
905 unsigned short ts;
907 while (ntab-- > 0)
909 ts = fetch_ushort(&format);
910 if (ts & 0x4000) fetch_ushort(&format);
913 /* 0x0400, 0x0800 and 0x1000 don't need space */
914 if ((bits & 0xE080) != 0)
915 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
917 while (text < text_end && format < format_end)
919 WINE_TRACE("Got text: '%s' (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
920 textsize = strlen(text) + 1;
921 if (textsize > 1)
923 paragraph = HeapAlloc(GetProcessHeap(), 0,
924 sizeof(HLPFILE_PARAGRAPH) + textsize);
925 if (!paragraph) return FALSE;
926 *paragraphptr = paragraph;
927 paragraphptr = &paragraph->next;
929 paragraph->next = NULL;
930 paragraph->link = attributes.link;
931 if (paragraph->link) paragraph->link->wRefCount++;
932 paragraph->cookie = para_normal_text;
933 paragraph->u.text.wFont = attributes.wFont;
934 paragraph->u.text.wVSpace = attributes.wVSpace;
935 paragraph->u.text.wHSpace = attributes.wHSpace;
936 paragraph->u.text.wIndent = attributes.wIndent;
937 paragraph->u.text.lpszText = (char*)paragraph + sizeof(HLPFILE_PARAGRAPH);
938 strcpy(paragraph->u.text.lpszText, text);
940 attributes.wVSpace = 0;
941 attributes.wHSpace = 0;
943 /* else: null text, keep on storing attributes */
944 text += textsize;
946 if (*format == 0xff)
948 format++;
949 break;
952 WINE_TRACE("format=%02x\n", *format);
953 switch (*format)
955 case 0x20:
956 WINE_FIXME("NIY20\n");
957 format += 5;
958 break;
960 case 0x21:
961 WINE_FIXME("NIY21\n");
962 format += 3;
963 break;
965 case 0x80:
966 attributes.wFont = GET_USHORT(format, 1);
967 WINE_TRACE("Changing font to %d\n", attributes.wFont);
968 format += 3;
969 break;
971 case 0x81:
972 attributes.wVSpace++;
973 format += 1;
974 break;
976 case 0x82:
977 attributes.wVSpace++;
978 attributes.wIndent = 0;
979 format += 1;
980 break;
982 case 0x83:
983 attributes.wIndent++;
984 format += 1;
985 break;
987 #if 0
988 case 0x84:
989 format += 3;
990 break;
991 #endif
993 case 0x86:
994 case 0x87:
995 case 0x88:
997 BYTE pos = (*format - 0x86);
998 BYTE type = format[1];
999 long size;
1001 format += 2;
1002 size = fetch_long(&format);
1004 paragraph = HeapAlloc(GetProcessHeap(), 0,
1005 sizeof(HLPFILE_PARAGRAPH) + textsize);
1006 if (!paragraph) return FALSE;
1007 *paragraphptr = paragraph;
1008 paragraphptr = &paragraph->next;
1010 paragraph->next = NULL;
1011 paragraph->link = attributes.link;
1012 if (paragraph->link) paragraph->link->wRefCount++;
1013 paragraph->cookie = para_bitmap;
1014 paragraph->u.gfx.pos = pos;
1015 switch (type)
1017 case 0x22:
1018 fetch_ushort(&format); /* hot spot */
1019 /* fall thru */
1020 case 0x03:
1021 switch (GET_SHORT(format, 0))
1023 case 0:
1024 HLPFILE_LoadGfxByIndex(hlpfile, GET_SHORT(format, 2),
1025 paragraph);
1026 break;
1027 case 1:
1028 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1029 GET_SHORT(format, 0),
1030 size, GET_SHORT(format, 2));
1031 HLPFILE_LoadGfxByAddr(hlpfile, format + 2, size - 4,
1032 paragraph);
1033 break;
1034 default:
1035 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1036 break;
1038 break;
1039 case 0x05:
1040 WINE_FIXME("Got an embedded element %s\n", format + 6);
1041 break;
1042 default:
1043 WINE_FIXME("Got a type %d picture\n", type);
1044 break;
1046 if (attributes.wVSpace) paragraph->u.gfx.pos |= 0x8000;
1048 format += size;
1050 break;
1052 case 0x89:
1053 HLPFILE_FreeLink(attributes.link);
1054 attributes.link = NULL;
1055 format += 1;
1056 break;
1058 case 0x8B:
1059 case 0x8C:
1060 WINE_FIXME("NIY non-break space/hyphen\n");
1061 format += 1;
1062 break;
1064 #if 0
1065 case 0xA9:
1066 format += 2;
1067 break;
1068 #endif
1070 case 0xC8:
1071 case 0xCC:
1072 WINE_TRACE("macro => %s\n", format + 3);
1073 HLPFILE_FreeLink(attributes.link);
1074 attributes.link = HLPFILE_AllocLink(hlp_link_macro, (const char*)format + 3,
1075 0, !(*format & 4), -1);
1076 format += 3 + GET_USHORT(format, 1);
1077 break;
1079 case 0xE0:
1080 case 0xE1:
1081 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1082 format += 5;
1083 break;
1085 case 0xE2:
1086 case 0xE3:
1087 case 0xE6:
1088 case 0xE7:
1089 HLPFILE_FreeLink(attributes.link);
1090 attributes.link = HLPFILE_AllocLink((*format & 1) ? hlp_link_link : hlp_link_popup,
1091 hlpfile->lpszPath,
1092 GET_UINT(format, 1),
1093 !(*format & 4), -1);
1094 format += 5;
1095 break;
1097 case 0xEA:
1098 case 0xEB:
1099 case 0xEE:
1100 case 0xEF:
1102 char* ptr = (char*) format + 8;
1103 BYTE type = format[3];
1104 int wnd = -1;
1105 char* str;
1107 if (type == 1) wnd = *ptr++;
1108 if (type == 4 || type == 6)
1110 str = ptr;
1111 ptr += strlen(ptr) + 1;
1113 else
1114 str = hlpfile->lpszPath;
1115 if (type == 6)
1117 for (wnd = hlpfile->numWindows - 1; wnd >= 0; wnd--)
1119 if (!strcmp(ptr, hlpfile->windows[wnd].name)) break;
1121 if (wnd == -1)
1122 WINE_WARN("Couldn't find window info for %s\n", ptr);
1124 HLPFILE_FreeLink(attributes.link);
1125 attributes.link = HLPFILE_AllocLink((*format & 4) ? hlp_link_link : hlp_link_popup,
1126 str, GET_UINT(format, 4),
1127 !(*format & 1), wnd);
1129 format += 3 + GET_USHORT(format, 1);
1130 break;
1132 default:
1133 WINE_WARN("format %02x\n", *format);
1134 format++;
1138 if (text_end != (char*)buf + GET_UINT(buf, 0x10) + size)
1139 HeapFree(GetProcessHeap(), 0, text_end - size);
1140 return TRUE;
1143 /******************************************************************
1144 * HLPFILE_ReadFont
1148 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1150 BYTE *ref, *end;
1151 unsigned i, len, idx;
1152 unsigned face_num, dscr_num, face_offset, dscr_offset;
1153 BYTE flag, family;
1155 if (!HLPFILE_FindSubFile("|FONT", &ref, &end))
1157 WINE_WARN("no subfile FONT\n");
1158 hlpfile->numFonts = 0;
1159 hlpfile->fonts = NULL;
1160 return FALSE;
1163 ref += 9;
1165 face_num = GET_USHORT(ref, 0);
1166 dscr_num = GET_USHORT(ref, 2);
1167 face_offset = GET_USHORT(ref, 4);
1168 dscr_offset = GET_USHORT(ref, 6);
1170 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1171 face_num, face_offset, dscr_num, dscr_offset);
1173 hlpfile->numFonts = dscr_num;
1174 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1176 len = (dscr_offset - face_offset) / face_num;
1177 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1178 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1179 for (i = 0; i < dscr_num; i++)
1181 flag = ref[dscr_offset + i * 11 + 0];
1182 family = ref[dscr_offset + i * 11 + 2];
1184 hlpfile->fonts[i].LogFont.lfHeight = -ref[dscr_offset + i * 11 + 1] / 2;
1185 hlpfile->fonts[i].LogFont.lfWidth = 0;
1186 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1187 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1188 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1189 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1190 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1191 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1192 hlpfile->fonts[i].LogFont.lfCharSet = ANSI_CHARSET;
1193 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1194 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1195 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1196 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1198 switch (family)
1200 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1201 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1202 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1203 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1204 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1205 default: WINE_FIXME("Unknown family %u\n", family);
1207 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1209 if (idx < face_num)
1211 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1212 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1214 else
1216 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1217 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1219 hlpfile->fonts[i].hFont = 0;
1220 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1221 ref[dscr_offset + i * 11 + 6],
1222 ref[dscr_offset + i * 11 + 7]);
1223 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1224 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1225 i, flag,
1226 X(0, "bold"),
1227 X(1, "italic"),
1228 X(2, "underline"),
1229 X(3, "strikeOut"),
1230 X(4, "dblUnderline"),
1231 X(5, "smallCaps"),
1232 ref[dscr_offset + i * 11 + 1],
1233 family,
1234 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1235 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1237 return TRUE;
1240 /***********************************************************************
1242 * HLPFILE_ReadFileToBuffer
1244 static BOOL HLPFILE_ReadFileToBuffer(HFILE hFile)
1246 BYTE header[16], dummy[1];
1247 UINT size;
1249 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1251 /* sanity checks */
1252 if (GET_UINT(header, 0) != 0x00035F3F)
1253 {WINE_WARN("wrong header\n"); return FALSE;};
1255 size = GET_UINT(header, 12);
1256 file_buffer = HeapAlloc(GetProcessHeap(), 0, size + 1);
1257 if (!file_buffer) return FALSE;
1259 memcpy(file_buffer, header, 16);
1260 if (_hread(hFile, file_buffer + 16, size - 16) != size - 16)
1261 {WINE_WARN("filesize1\n"); return FALSE;};
1263 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1265 file_buffer[size] = '\0'; /* FIXME: was '0', sounds ackward to me */
1267 return TRUE;
1270 /***********************************************************************
1272 * HLPFILE_FindSubFile
1274 static BOOL HLPFILE_FindSubFile(LPCSTR name, BYTE **subbuf, BYTE **subend)
1276 BYTE *root = file_buffer + GET_UINT(file_buffer, 4);
1277 BYTE *end = file_buffer + GET_UINT(file_buffer, 12);
1278 BYTE *ptr;
1279 BYTE *bth;
1281 unsigned pgsize;
1282 unsigned pglast;
1283 unsigned nentries;
1284 unsigned i, n;
1286 bth = root + 9;
1288 /* FIXME: this should be using the EnumBTree functions from this file */
1289 pgsize = GET_USHORT(bth, 4);
1290 WINE_TRACE("%s => pgsize=%u #pg=%u rootpg=%u #lvl=%u\n",
1291 name, pgsize, GET_USHORT(bth, 30), GET_USHORT(bth, 26), GET_USHORT(bth, 32));
1293 ptr = bth + 38 + GET_USHORT(bth, 26) * pgsize;
1295 for (n = 1; n < GET_USHORT(bth, 32); n++)
1297 nentries = GET_USHORT(ptr, 2);
1298 pglast = GET_USHORT(ptr, 4);
1299 WINE_TRACE("[%u]: #entries=%u next=%u\n", n, nentries, pglast);
1301 ptr += 6;
1302 for (i = 0; i < nentries; i++)
1304 char *str = (char*) ptr;
1305 WINE_TRACE("<= %s\n", str);
1306 if (strcmp(name, str) < 0) break;
1307 ptr += strlen(str) + 1;
1308 pglast = GET_USHORT(ptr, 0);
1309 ptr += 2;
1311 ptr = bth + 38 + pglast * pgsize;
1314 nentries = GET_USHORT(ptr, 2);
1315 ptr += 8;
1316 for (i = 0; i < nentries; i++)
1318 char* fname = (char*)ptr;
1319 ptr += strlen(fname) + 1;
1320 WINE_TRACE("\\- %s\n", fname);
1321 if (strcmp(fname, name) == 0)
1323 *subbuf = file_buffer + GET_UINT(ptr, 0);
1324 *subend = *subbuf + GET_UINT(*subbuf, 0);
1325 if (file_buffer > *subbuf || *subbuf > *subend || *subend > end)
1327 WINE_WARN("size mismatch\n");
1328 return FALSE;
1330 return TRUE;
1332 ptr += 4;
1335 return FALSE;
1338 /***********************************************************************
1340 * HLPFILE_SystemCommands
1342 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1344 BYTE *buf, *ptr, *end;
1345 HLPFILE_MACRO *macro, **m;
1346 LPSTR p;
1347 unsigned short magic, minor, major, flags;
1349 hlpfile->lpszTitle = NULL;
1351 if (!HLPFILE_FindSubFile("|SYSTEM", &buf, &end)) return FALSE;
1353 magic = GET_USHORT(buf + 9, 0);
1354 minor = GET_USHORT(buf + 9, 2);
1355 major = GET_USHORT(buf + 9, 4);
1356 /* gen date on 4 bytes */
1357 flags = GET_USHORT(buf + 9, 10);
1358 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1359 magic, major, minor, flags);
1360 if (magic != 0x036C || major != 1)
1361 {WINE_WARN("Wrong system header\n"); return FALSE;}
1362 if (minor <= 16) {WINE_WARN("too old file format (NIY)\n"); return FALSE;}
1363 if (flags & 8) {WINE_WARN("Unsupported yet page size\n"); return FALSE;}
1365 hlpfile->version = minor;
1366 hlpfile->flags = flags;
1368 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
1370 char *str = (char*) ptr + 4;
1371 switch (GET_USHORT(ptr, 0))
1373 case 1:
1374 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
1375 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1376 if (!hlpfile->lpszTitle) return FALSE;
1377 lstrcpy(hlpfile->lpszTitle, str);
1378 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
1379 break;
1381 case 2:
1382 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
1383 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1384 if (!hlpfile->lpszCopyright) return FALSE;
1385 lstrcpy(hlpfile->lpszCopyright, str);
1386 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
1387 break;
1389 case 3:
1390 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
1391 hlpfile->contents_start = GET_UINT(ptr, 4);
1392 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
1393 break;
1395 case 4:
1396 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
1397 if (!macro) break;
1398 p = (char*)macro + sizeof(HLPFILE_MACRO);
1399 lstrcpy(p, str);
1400 macro->lpszMacro = p;
1401 macro->next = 0;
1402 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
1403 *m = macro;
1404 break;
1406 case 6:
1407 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
1409 if (hlpfile->windows)
1410 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
1411 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1412 else
1413 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
1414 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
1416 if (hlpfile->windows)
1418 unsigned flags = GET_USHORT(ptr, 4);
1419 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
1421 if (flags & 0x0001) strcpy(wi->type, &str[2]);
1422 else wi->type[0] = '\0';
1423 if (flags & 0x0002) strcpy(wi->name, &str[12]);
1424 else wi->name[0] = '\0';
1425 if (flags & 0x0004) strcpy(wi->caption, &str[23]);
1426 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
1427 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
1428 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
1429 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
1430 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
1431 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
1432 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
1433 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
1434 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",
1435 flags & 0x0001 ? 'T' : 't',
1436 flags & 0x0002 ? 'N' : 'n',
1437 flags & 0x0004 ? 'C' : 'c',
1438 flags & 0x0008 ? 'X' : 'x',
1439 flags & 0x0010 ? 'Y' : 'y',
1440 flags & 0x0020 ? 'W' : 'w',
1441 flags & 0x0040 ? 'H' : 'h',
1442 flags & 0x0080 ? 'S' : 's',
1443 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
1444 wi->size.cx, wi->size.cy);
1446 break;
1447 default:
1448 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
1451 if (!hlpfile->lpszTitle)
1452 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
1453 return TRUE;
1456 /***********************************************************************
1458 * HLPFILE_UncompressedLZ77_Size
1460 static INT HLPFILE_UncompressedLZ77_Size(BYTE *ptr, BYTE *end)
1462 int i, newsize = 0;
1464 while (ptr < end)
1466 int mask = *ptr++;
1467 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1469 if (mask & 1)
1471 int code = GET_USHORT(ptr, 0);
1472 int len = 3 + (code >> 12);
1473 newsize += len;
1474 ptr += 2;
1476 else newsize++, ptr++;
1480 return newsize;
1483 /***********************************************************************
1485 * HLPFILE_UncompressLZ77
1487 static BYTE *HLPFILE_UncompressLZ77(BYTE *ptr, BYTE *end, BYTE *newptr)
1489 int i;
1491 while (ptr < end)
1493 int mask = *ptr++;
1494 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
1496 if (mask & 1)
1498 int code = GET_USHORT(ptr, 0);
1499 int len = 3 + (code >> 12);
1500 int offset = code & 0xfff;
1501 memcpy(newptr, newptr - offset - 1, len);
1502 newptr += len;
1503 ptr += 2;
1505 else *newptr++ = *ptr++;
1509 return newptr;
1512 /***********************************************************************
1514 * HLPFILE_UncompressLZ77_Phrases
1516 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
1518 UINT i, num, dec_size;
1519 BYTE *buf, *end;
1521 if (!HLPFILE_FindSubFile("|Phrases", &buf, &end)) return FALSE;
1523 num = phrases.num = GET_USHORT(buf, 9);
1524 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
1526 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
1528 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1529 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1530 if (!phrases.offsets || !phrases.buffer) return FALSE;
1532 for (i = 0; i <= num; i++)
1533 phrases.offsets[i] = GET_USHORT(buf, 0x11 + 2 * i) - 2 * num - 2;
1535 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)phrases.buffer);
1537 hlpfile->hasPhrases = TRUE;
1538 return TRUE;
1541 /***********************************************************************
1543 * HLPFILE_Uncompress_Phrases40
1545 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
1547 UINT num, dec_size, cpr_size;
1548 BYTE *buf_idx, *end_idx;
1549 BYTE *buf_phs, *end_phs;
1550 short i, n;
1551 long* ptr, mask = 0;
1552 unsigned short bc;
1554 if (!HLPFILE_FindSubFile("|PhrIndex", &buf_idx, &end_idx) ||
1555 !HLPFILE_FindSubFile("|PhrImage", &buf_phs, &end_phs)) return FALSE;
1557 ptr = (long*)(buf_idx + 9 + 28);
1558 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
1559 num = phrases.num = GET_USHORT(buf_idx, 9 + 4);
1561 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
1562 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
1563 GET_UINT(buf_idx, 9 + 0),
1564 GET_UINT(buf_idx, 9 + 4),
1565 GET_UINT(buf_idx, 9 + 8),
1566 GET_UINT(buf_idx, 9 + 12),
1567 GET_UINT(buf_idx, 9 + 16),
1568 GET_UINT(buf_idx, 9 + 20),
1569 GET_USHORT(buf_idx, 9 + 24),
1570 GET_USHORT(buf_idx, 9 + 26));
1572 dec_size = GET_UINT(buf_idx, 9 + 12);
1573 cpr_size = GET_UINT(buf_idx, 9 + 16);
1575 if (dec_size != cpr_size &&
1576 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
1578 WINE_WARN("size mismatch %u %u\n",
1579 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1580 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
1583 phrases.offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
1584 phrases.buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
1585 if (!phrases.offsets || !phrases.buffer) return FALSE;
1587 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
1589 phrases.offsets[0] = 0;
1590 for (i = 0; i < num; i++)
1592 for (n = 1; getbit(); n += 1 << bc);
1593 if (getbit()) n++;
1594 if (bc > 1 && getbit()) n += 2;
1595 if (bc > 2 && getbit()) n += 4;
1596 if (bc > 3 && getbit()) n += 8;
1597 if (bc > 4 && getbit()) n += 16;
1598 phrases.offsets[i + 1] = phrases.offsets[i] + n;
1600 #undef getbit
1602 if (dec_size == cpr_size)
1603 memcpy(phrases.buffer, buf_phs + 9, dec_size);
1604 else
1605 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)phrases.buffer);
1607 hlpfile->hasPhrases = FALSE;
1608 return TRUE;
1611 /***********************************************************************
1613 * HLPFILE_Uncompress_Topic
1615 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
1617 BYTE *buf, *ptr, *end, *newptr;
1618 int i, newsize = 0;
1620 if (!HLPFILE_FindSubFile("|TOPIC", &buf, &end))
1621 {WINE_WARN("topic0\n"); return FALSE;}
1623 switch (hlpfile->flags & (8|4))
1625 case 8:
1626 WINE_FIXME("Unsupported format\n");
1627 return FALSE;
1628 case 4:
1629 buf += 9;
1630 topic.wMapLen = (end - buf - 1) / 0x1000 + 1;
1632 for (i = 0; i < topic.wMapLen; i++)
1634 ptr = buf + i * 0x1000;
1636 /* I don't know why, it's necessary for printman.hlp */
1637 if (ptr + 0x44 > end) ptr = end - 0x44;
1639 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + 0x1000));
1642 topic.map = HeapAlloc(GetProcessHeap(), 0,
1643 topic.wMapLen * sizeof(topic.map[0]) + newsize);
1644 if (!topic.map) return FALSE;
1645 newptr = (BYTE*)(topic.map + topic.wMapLen);
1646 topic.end = newptr + newsize;
1648 for (i = 0; i < topic.wMapLen; i++)
1650 ptr = buf + i * 0x1000;
1651 if (ptr + 0x44 > end) ptr = end - 0x44;
1653 topic.map[i] = newptr;
1654 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + 0x1000), newptr);
1656 break;
1657 case 0:
1658 /* basically, we need to copy the 0x1000 byte pages (removing the first 0x0C) in
1659 * one single are in memory
1661 #define DST_LEN (0x1000 - 0x0C)
1662 buf += 9;
1663 newsize = end - buf;
1664 /* number of destination pages */
1665 topic.wMapLen = (newsize - 1) / DST_LEN + 1;
1666 topic.map = HeapAlloc(GetProcessHeap(), 0,
1667 topic.wMapLen * (sizeof(topic.map[0]) + DST_LEN));
1668 if (!topic.map) return FALSE;
1669 newptr = (BYTE*)(topic.map + topic.wMapLen);
1670 topic.end = newptr + newsize;
1672 for (i = 0; i < topic.wMapLen; i++)
1674 topic.map[i] = newptr + i * DST_LEN;
1675 memcpy(topic.map[i], buf + i * 0x1000 + 0x0C, DST_LEN);
1677 #undef DST_LEN
1678 break;
1680 return TRUE;
1683 /***********************************************************************
1685 * HLPFILE_Uncompress2
1688 static void HLPFILE_Uncompress2(const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
1690 BYTE *phptr, *phend;
1691 UINT code;
1692 UINT index;
1694 while (ptr < end && newptr < newend)
1696 if (!*ptr || *ptr >= 0x10)
1697 *newptr++ = *ptr++;
1698 else
1700 code = 0x100 * ptr[0] + ptr[1];
1701 index = (code - 0x100) / 2;
1703 phptr = (BYTE*)phrases.buffer + phrases.offsets[index];
1704 phend = (BYTE*)phrases.buffer + phrases.offsets[index + 1];
1706 if (newptr + (phend - phptr) > newend)
1708 WINE_FIXME("buffer overflow %p > %p for %d bytes\n",
1709 newptr, newend, phend - phptr);
1710 return;
1712 memcpy(newptr, phptr, phend - phptr);
1713 newptr += phend - phptr;
1714 if (code & 1) *newptr++ = ' ';
1716 ptr += 2;
1719 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
1722 /******************************************************************
1723 * HLPFILE_Uncompress3
1727 static BOOL HLPFILE_Uncompress3(char* dst, const char* dst_end,
1728 const BYTE* src, const BYTE* src_end)
1730 int idx, len;
1732 for (; src < src_end; src++)
1734 if ((*src & 1) == 0)
1736 idx = *src / 2;
1737 if (idx > phrases.num)
1739 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1740 len = 0;
1742 else
1744 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1745 if (dst + len <= dst_end)
1746 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1749 else if ((*src & 0x03) == 0x01)
1751 idx = (*src + 1) * 64;
1752 idx += *++src;
1753 if (idx > phrases.num)
1755 WINE_ERR("index in phrases %d/%d\n", idx, phrases.num);
1756 len = 0;
1758 else
1760 len = phrases.offsets[idx + 1] - phrases.offsets[idx];
1761 if (dst + len <= dst_end)
1762 memcpy(dst, &phrases.buffer[phrases.offsets[idx]], len);
1765 else if ((*src & 0x07) == 0x03)
1767 len = (*src / 8) + 1;
1768 if (dst + len <= dst_end)
1769 memcpy(dst, src + 1, len);
1770 src += len;
1772 else
1774 len = (*src / 16) + 1;
1775 if (dst + len <= dst_end)
1776 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
1778 dst += len;
1781 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
1782 return TRUE;
1785 /******************************************************************
1786 * HLPFILE_UncompressRLE
1790 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE** dst, unsigned dstsz)
1792 BYTE ch;
1793 BYTE* sdst = *dst + dstsz;
1795 while (src < end)
1797 ch = *src++;
1798 if (ch & 0x80)
1800 ch &= 0x7F;
1801 if ((*dst) + ch <= sdst)
1802 memcpy(*dst, src, ch);
1803 src += ch;
1805 else
1807 if ((*dst) + ch <= sdst)
1808 memset(*dst, (char)*src, ch);
1809 src++;
1811 *dst += ch;
1813 if (*dst != sdst)
1814 WINE_WARN("Buffer X-flow: d(%u) instead of d(%u)\n",
1815 *dst - (sdst - dstsz), dstsz);
1818 /******************************************************************
1819 * HLPFILE_EnumBTreeLeaves
1823 static void HLPFILE_EnumBTreeLeaves(const BYTE* buf, const BYTE* end, unsigned (*fn)(const BYTE*, void*), void* user)
1825 unsigned psize, pnext;
1826 unsigned num, nlvl;
1827 const BYTE* ptr;
1829 num = GET_UINT(buf, 9 + 34);
1830 psize = GET_USHORT(buf, 9 + 4);
1831 nlvl = GET_USHORT(buf, 9 + 32);
1832 pnext = GET_USHORT(buf, 9 + 26);
1834 WINE_TRACE("BTree: #entries=%u pagSize=%u #levels=%u #pages=%u root=%u struct%16s\n",
1835 num, psize, nlvl, GET_USHORT(buf, 9 + 30), pnext, buf + 9 + 6);
1836 if (!num) return;
1838 while (--nlvl > 0)
1840 ptr = (buf + 9 + 38) + pnext * psize;
1841 WINE_TRACE("BTree: (index[%u]) unused=%u #entries=%u <%u\n",
1842 pnext, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2), GET_USHORT(ptr, 4));
1843 pnext = GET_USHORT(ptr, 4);
1845 while (pnext != 0xFFFF)
1847 const BYTE* node_page;
1848 unsigned short limit;
1850 node_page = ptr = (buf + 9 + 38) + pnext * psize;
1851 limit = GET_USHORT(ptr, 2);
1852 WINE_TRACE("BTree: (leaf [%u]) unused=%u #entries=%u <%u >%u\n",
1853 pnext, GET_USHORT(ptr, 0), limit, GET_USHORT(ptr, 4), GET_USHORT(ptr, 6));
1854 ptr += 8;
1855 while (limit--)
1856 ptr += (fn)(ptr, user);
1857 pnext = GET_USHORT(node_page, 6);
1861 struct myfncb {
1862 HLPFILE* hlpfile;
1863 int i;
1866 static unsigned myfn(const BYTE* ptr, void* user)
1868 struct myfncb* m = user;
1870 m->hlpfile->Context[m->i].lHash = GET_UINT(ptr, 0);
1871 m->hlpfile->Context[m->i].offset = GET_UINT(ptr, 4);
1872 m->i++;
1873 return 8;
1876 /***********************************************************************
1878 * HLPFILE_GetContext
1880 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
1882 BYTE *cbuf, *cend;
1883 struct myfncb m;
1884 unsigned clen;
1886 if (!HLPFILE_FindSubFile("|CONTEXT", &cbuf, &cend)) {WINE_WARN("context0\n"); return FALSE;}
1888 clen = GET_UINT(cbuf, 0x2b);
1889 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen * sizeof(HLPFILE_CONTEXT));
1890 if (!hlpfile->Context) return FALSE;
1891 hlpfile->wContextLen = clen;
1893 m.hlpfile = hlpfile;
1894 m.i = 0;
1895 HLPFILE_EnumBTreeLeaves(cbuf, cend, myfn, &m);
1897 return TRUE;
1900 /******************************************************************
1901 * HLPFILE_DeleteLink
1905 void HLPFILE_FreeLink(HLPFILE_LINK* link)
1907 if (link && !--link->wRefCount)
1908 HeapFree(GetProcessHeap(), 0, link);
1911 /***********************************************************************
1913 * HLPFILE_DeleteParagraph
1915 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH* paragraph)
1917 HLPFILE_PARAGRAPH* next;
1919 while (paragraph)
1921 next = paragraph->next;
1923 if (paragraph->cookie == para_metafile)
1924 DeleteMetaFile(paragraph->u.gfx.u.mf.hMetaFile);
1926 HLPFILE_FreeLink(paragraph->link);
1928 HeapFree(GetProcessHeap(), 0, paragraph);
1929 paragraph = next;
1933 /***********************************************************************
1935 * DeleteMacro
1937 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
1939 HLPFILE_MACRO* next;
1941 while (macro)
1943 next = macro->next;
1944 HeapFree(GetProcessHeap(), 0, macro);
1945 macro = next;
1949 /***********************************************************************
1951 * DeletePage
1953 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
1955 HLPFILE_PAGE* next;
1957 while (page)
1959 next = page->next;
1960 HLPFILE_DeleteParagraph(page->first_paragraph);
1961 HLPFILE_DeleteMacro(page->first_macro);
1962 HeapFree(GetProcessHeap(), 0, page);
1963 page = next;
1967 /***********************************************************************
1969 * HLPFILE_FreeHlpFile
1971 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
1973 unsigned i;
1975 if (!hlpfile || --hlpfile->wRefCount > 0) return;
1977 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
1978 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
1979 else first_hlpfile = hlpfile->next;
1981 if (hlpfile->numFonts)
1983 for (i = 0; i < hlpfile->numFonts; i++)
1985 DeleteObject(hlpfile->fonts[i].hFont);
1987 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
1990 if (hlpfile->numBmps)
1992 for (i = 0; i < hlpfile->numBmps; i++)
1994 DeleteObject(hlpfile->bmps[i]);
1996 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
1999 HLPFILE_DeletePage(hlpfile->first_page);
2000 HLPFILE_DeleteMacro(hlpfile->first_macro);
2002 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2003 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2004 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2005 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2006 HeapFree(GetProcessHeap(), 0, hlpfile);