4 * Copyright 1996 Ulrich Schmid
5 * 2002, 2008 Eric Pouech
6 * 2007 Kirill K. Smirnov
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(winhelp
);
37 static inline unsigned short GET_USHORT(const BYTE
* buffer
, unsigned i
)
39 return (BYTE
)buffer
[i
] + 0x100 * (BYTE
)buffer
[i
+ 1];
42 static inline short GET_SHORT(const BYTE
* buffer
, unsigned i
)
44 return (BYTE
)buffer
[i
] + 0x100 * (signed char)buffer
[i
+1];
47 static inline unsigned GET_UINT(const BYTE
* buffer
, unsigned i
)
49 return GET_USHORT(buffer
, i
) + 0x10000 * GET_USHORT(buffer
, i
+ 2);
52 static HLPFILE
*first_hlpfile
= 0;
63 static BOOL
HLPFILE_DoReadHlpFile(HLPFILE
*, LPCSTR
);
64 static BOOL
HLPFILE_ReadFileToBuffer(HLPFILE
*, HFILE
);
65 static BOOL
HLPFILE_FindSubFile(HLPFILE
*, LPCSTR
, BYTE
**, BYTE
**);
66 static BOOL
HLPFILE_SystemCommands(HLPFILE
*);
67 static INT
HLPFILE_UncompressedLZ77_Size(BYTE
*ptr
, BYTE
*end
);
68 static BYTE
* HLPFILE_UncompressLZ77(BYTE
*ptr
, BYTE
*end
, BYTE
*newptr
);
69 static BOOL
HLPFILE_UncompressLZ77_Phrases(HLPFILE
*);
70 static BOOL
HLPFILE_Uncompress_Phrases40(HLPFILE
*);
71 static BOOL
HLPFILE_Uncompress_Topic(HLPFILE
*);
72 static BOOL
HLPFILE_GetContext(HLPFILE
*);
73 static BOOL
HLPFILE_GetKeywords(HLPFILE
*);
74 static BOOL
HLPFILE_GetMap(HLPFILE
*);
75 static BOOL
HLPFILE_AddPage(HLPFILE
*, BYTE
*, BYTE
*, unsigned, unsigned);
76 static BOOL
HLPFILE_SkipParagraph(HLPFILE
*, BYTE
*, BYTE
*, unsigned*);
77 static void HLPFILE_Uncompress2(HLPFILE
*, const BYTE
*, const BYTE
*, BYTE
*, const BYTE
*);
78 static BOOL
HLPFILE_Uncompress3(HLPFILE
*, char*, const char*, const BYTE
*, const BYTE
*);
79 static void HLPFILE_UncompressRLE(const BYTE
* src
, const BYTE
* end
, BYTE
** dst
, unsigned dstsz
);
80 static BOOL
HLPFILE_ReadFont(HLPFILE
* hlpfile
);
82 /***********************************************************************
84 * HLPFILE_PageByNumber
86 static HLPFILE_PAGE
*HLPFILE_PageByNumber(HLPFILE
* hlpfile
, UINT wNum
)
91 WINE_TRACE("<%s>[%u]\n", hlpfile
->lpszPath
, wNum
);
93 for (page
= hlpfile
->first_page
; page
&& temp
; page
= page
->next
) temp
--;
95 WINE_ERR("Page of number %u not found in file %s\n", wNum
, hlpfile
->lpszPath
);
99 /******************************************************************
100 * HLPFILE_PageByOffset
104 HLPFILE_PAGE
*HLPFILE_PageByOffset(HLPFILE
* hlpfile
, LONG offset
, ULONG
* relative
)
109 if (!hlpfile
) return 0;
111 WINE_TRACE("<%s>[%x]\n", hlpfile
->lpszPath
, offset
);
113 if (offset
== 0xFFFFFFFF) return NULL
;
116 for (found
= NULL
, page
= hlpfile
->first_page
; page
; page
= page
->next
)
118 if (page
->offset
<= offset
&& (!found
|| found
->offset
< page
->offset
))
120 *relative
= offset
- page
->offset
;
125 WINE_ERR("Page of offset %u not found in file %s\n",
126 offset
, hlpfile
->lpszPath
);
130 /**************************************************************************
133 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
136 static int comp_PageByHash(void *p
, const void *key
,
137 int leaf
, void** next
)
139 LONG lKey
= (LONG_PTR
)key
;
140 LONG lTest
= (INT
)GET_UINT(p
, 0);
142 *next
= (char *)p
+(leaf
?8:6);
143 WINE_TRACE("Comparing '%d' with '%d'\n", lKey
, lTest
);
144 if (lTest
< lKey
) return -1;
145 if (lTest
> lKey
) return 1;
149 /***********************************************************************
153 HLPFILE_PAGE
*HLPFILE_PageByHash(HLPFILE
* hlpfile
, LONG lHash
, ULONG
* relative
)
157 if (!hlpfile
) return NULL
;
158 if (!lHash
) return HLPFILE_Contents(hlpfile
, relative
);
160 WINE_TRACE("<%s>[%x]\n", hlpfile
->lpszPath
, lHash
);
162 /* For win 3.0 files hash values are really page numbers */
163 if (hlpfile
->version
<= 16)
166 return HLPFILE_PageByNumber(hlpfile
, lHash
);
169 ptr
= HLPFILE_BPTreeSearch(hlpfile
->Context
, LongToPtr(lHash
), comp_PageByHash
);
172 WINE_ERR("Page of hash %x not found in file %s\n", lHash
, hlpfile
->lpszPath
);
176 return HLPFILE_PageByOffset(hlpfile
, GET_UINT(ptr
, 4), relative
);
179 /***********************************************************************
183 HLPFILE_PAGE
*HLPFILE_PageByMap(HLPFILE
* hlpfile
, LONG lMap
, ULONG
* relative
)
187 if (!hlpfile
) return 0;
189 WINE_TRACE("<%s>[%x]\n", hlpfile
->lpszPath
, lMap
);
191 for (i
= 0; i
< hlpfile
->wMapLen
; i
++)
193 if (hlpfile
->Map
[i
].lMap
== lMap
)
194 return HLPFILE_PageByOffset(hlpfile
, hlpfile
->Map
[i
].offset
, relative
);
197 WINE_ERR("Page of Map %x not found in file %s\n", lMap
, hlpfile
->lpszPath
);
201 /***********************************************************************
205 HLPFILE_PAGE
* HLPFILE_Contents(HLPFILE
*hlpfile
, ULONG
* relative
)
207 HLPFILE_PAGE
* page
= NULL
;
209 if (!hlpfile
) return NULL
;
211 page
= HLPFILE_PageByOffset(hlpfile
, hlpfile
->contents_start
, relative
);
214 page
= hlpfile
->first_page
;
220 /***********************************************************************
224 LONG
HLPFILE_Hash(LPCSTR lpszContext
)
229 while ((c
= *lpszContext
++))
232 if (c
>= 'A' && c
<= 'Z') x
= c
- 'A' + 17;
233 if (c
>= 'a' && c
<= 'z') x
= c
- 'a' + 17;
234 if (c
>= '1' && c
<= '9') x
= c
- '0';
235 if (c
== '0') x
= 10;
236 if (c
== '.') x
= 12;
237 if (c
== '_') x
= 13;
238 if (x
) lHash
= lHash
* 43 + x
;
243 /***********************************************************************
245 * HLPFILE_ReadHlpFile
247 HLPFILE
*HLPFILE_ReadHlpFile(LPCSTR lpszPath
)
251 for (hlpfile
= first_hlpfile
; hlpfile
; hlpfile
= hlpfile
->next
)
253 if (!strcmp(lpszPath
, hlpfile
->lpszPath
))
255 hlpfile
->wRefCount
++;
260 hlpfile
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
261 sizeof(HLPFILE
) + lstrlen(lpszPath
) + 1);
262 if (!hlpfile
) return 0;
264 hlpfile
->lpszPath
= (char*)hlpfile
+ sizeof(HLPFILE
);
265 hlpfile
->contents_start
= 0xFFFFFFFF;
266 hlpfile
->next
= first_hlpfile
;
267 hlpfile
->wRefCount
= 1;
269 strcpy(hlpfile
->lpszPath
, lpszPath
);
271 first_hlpfile
= hlpfile
;
272 if (hlpfile
->next
) hlpfile
->next
->prev
= hlpfile
;
274 if (!HLPFILE_DoReadHlpFile(hlpfile
, lpszPath
))
276 HLPFILE_FreeHlpFile(hlpfile
);
283 /***********************************************************************
285 * HLPFILE_DoReadHlpFile
287 static BOOL
HLPFILE_DoReadHlpFile(HLPFILE
*hlpfile
, LPCSTR lpszPath
)
294 unsigned index
, old_index
, offset
, len
, offs
;
296 hFile
= OpenFile(lpszPath
, &ofs
, OF_READ
);
297 if (hFile
== HFILE_ERROR
) return FALSE
;
299 ret
= HLPFILE_ReadFileToBuffer(hlpfile
, hFile
);
301 if (!ret
) return FALSE
;
303 if (!HLPFILE_SystemCommands(hlpfile
)) return FALSE
;
305 /* load phrases support */
306 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile
))
307 HLPFILE_Uncompress_Phrases40(hlpfile
);
309 if (!HLPFILE_Uncompress_Topic(hlpfile
)) return FALSE
;
310 if (!HLPFILE_ReadFont(hlpfile
)) return FALSE
;
312 buf
= hlpfile
->topic_map
[0];
319 if (hlpfile
->version
<= 16)
321 index
= (ref
- 0x0C) / hlpfile
->dsize
;
322 offset
= (ref
- 0x0C) % hlpfile
->dsize
;
326 index
= (ref
- 0x0C) >> 14;
327 offset
= (ref
- 0x0C) & 0x3FFF;
330 if (hlpfile
->version
<= 16 && index
!= old_index
&& index
!= 0)
332 /* we jumped to the next block, adjust pointers */
337 WINE_TRACE("ref=%08x => [%u/%u]\n", ref
, index
, offset
);
339 if (index
>= hlpfile
->topic_maplen
) {WINE_WARN("maplen\n"); break;}
340 buf
= hlpfile
->topic_map
[index
] + offset
;
341 if (buf
+ 0x15 >= hlpfile
->topic_end
) {WINE_WARN("extra\n"); break;}
342 end
= min(buf
+ GET_UINT(buf
, 0), hlpfile
->topic_end
);
343 if (index
!= old_index
) {offs
= 0; old_index
= index
;}
348 if (!HLPFILE_AddPage(hlpfile
, buf
, end
, ref
, index
* 0x8000L
+ offs
)) return FALSE
;
354 if (!HLPFILE_SkipParagraph(hlpfile
, buf
, end
, &len
)) return FALSE
;
359 WINE_ERR("buf[0x14] = %x\n", buf
[0x14]);
362 if (hlpfile
->version
<= 16)
364 ref
+= GET_UINT(buf
, 0xc);
365 if (GET_UINT(buf
, 0xc) == 0)
369 ref
= GET_UINT(buf
, 0xc);
370 } while (ref
!= 0xffffffff);
372 HLPFILE_GetKeywords(hlpfile
);
373 HLPFILE_GetMap(hlpfile
);
374 if (hlpfile
->version
<= 16) return TRUE
;
375 return HLPFILE_GetContext(hlpfile
);
378 /***********************************************************************
382 static BOOL
HLPFILE_AddPage(HLPFILE
*hlpfile
, BYTE
*buf
, BYTE
*end
, unsigned ref
, unsigned offset
)
386 UINT titlesize
, blocksize
, datalen
;
390 blocksize
= GET_UINT(buf
, 0);
391 datalen
= GET_UINT(buf
, 0x10);
392 title
= buf
+ datalen
;
393 if (title
> end
) {WINE_WARN("page2\n"); return FALSE
;};
395 titlesize
= GET_UINT(buf
, 4);
396 page
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE
) + titlesize
+ 1);
397 if (!page
) return FALSE
;
398 page
->lpszTitle
= (char*)page
+ sizeof(HLPFILE_PAGE
);
400 if (titlesize
> blocksize
- datalen
)
402 /* need to decompress */
403 if (hlpfile
->hasPhrases
)
404 HLPFILE_Uncompress2(hlpfile
, title
, end
, (BYTE
*)page
->lpszTitle
, (BYTE
*)page
->lpszTitle
+ titlesize
);
405 else if (hlpfile
->hasPhrases40
)
406 HLPFILE_Uncompress3(hlpfile
, page
->lpszTitle
, page
->lpszTitle
+ titlesize
, title
, end
);
409 WINE_FIXME("Text size is too long, splitting\n");
410 titlesize
= blocksize
- datalen
;
411 memcpy(page
->lpszTitle
, title
, titlesize
);
415 memcpy(page
->lpszTitle
, title
, titlesize
);
417 page
->lpszTitle
[titlesize
] = '\0';
419 if (hlpfile
->first_page
)
421 hlpfile
->last_page
->next
= page
;
422 page
->prev
= hlpfile
->last_page
;
423 hlpfile
->last_page
= page
;
427 hlpfile
->first_page
= page
;
428 hlpfile
->last_page
= page
;
432 page
->file
= hlpfile
;
434 page
->first_paragraph
= NULL
;
435 page
->first_macro
= NULL
;
436 page
->first_link
= NULL
;
437 page
->wNumber
= GET_UINT(buf
, 0x21);
438 page
->offset
= offset
;
439 page
->reference
= ref
;
441 page
->browse_bwd
= GET_UINT(buf
, 0x19);
442 page
->browse_fwd
= GET_UINT(buf
, 0x1D);
444 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
445 page
->wNumber
, page
->lpszTitle
,
446 page
->browse_bwd
, page
->offset
, page
->browse_fwd
);
448 memset(&attributes
, 0, sizeof(attributes
));
450 /* now load macros */
451 ptr
= page
->lpszTitle
+ strlen(page
->lpszTitle
) + 1;
452 while (ptr
< page
->lpszTitle
+ titlesize
)
454 unsigned len
= strlen(ptr
);
457 WINE_TRACE("macro: %s\n", ptr
);
458 macro
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO
) + len
+ 1);
459 macro
->lpszMacro
= macro_str
= (char*)(macro
+ 1);
460 memcpy(macro_str
, ptr
, len
+ 1);
461 /* FIXME: shall we really link macro in reverse order ??
462 * may produce strange results when played at page opening
464 macro
->next
= page
->first_macro
;
465 page
->first_macro
= macro
;
472 static long fetch_long(BYTE
** ptr
)
478 ret
= (*(unsigned long*)(*ptr
) - 0x80000000L
) / 2;
483 ret
= (*(unsigned short*)(*ptr
) - 0x8000) / 2;
490 static unsigned long fetch_ulong(BYTE
** ptr
)
496 ret
= *(unsigned long*)(*ptr
) / 2;
501 ret
= *(unsigned short*)(*ptr
) / 2;
507 static short fetch_short(BYTE
** ptr
)
513 ret
= (*(unsigned short*)(*ptr
) - 0x8000) / 2;
518 ret
= (*(unsigned char*)(*ptr
) - 0x80) / 2;
524 static unsigned short fetch_ushort(BYTE
** ptr
)
530 ret
= *(unsigned short*)(*ptr
) / 2;
535 ret
= *(unsigned char*)(*ptr
) / 2;
541 /***********************************************************************
543 * HLPFILE_SkipParagraph
545 static BOOL
HLPFILE_SkipParagraph(HLPFILE
*hlpfile
, BYTE
*buf
, BYTE
*end
, unsigned* len
)
549 if (!hlpfile
->first_page
) {WINE_WARN("no page\n"); return FALSE
;};
550 if (buf
+ 0x19 > end
) {WINE_WARN("header too small\n"); return FALSE
;};
553 if (buf
[0x14] == 0x20 || buf
[0x14] == 0x23)
556 *len
= fetch_ushort(&tmp
);
558 else *len
= end
-buf
-15;
563 /******************************************************************
564 * HLPFILE_DecompressGfx
566 * Decompress the data part of a bitmap or a metafile
568 static BYTE
* HLPFILE_DecompressGfx(BYTE
* src
, unsigned csz
, unsigned sz
, BYTE packing
)
575 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing
, csz
, sz
);
579 case 0: /* uncompressed */
581 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz
, csz
);
585 tmp
= dst
= HeapAlloc(GetProcessHeap(), 0, sz
);
586 if (!dst
) return NULL
;
587 HLPFILE_UncompressRLE(src
, src
+ csz
, &tmp
, sz
);
589 WINE_WARN("Bogus gfx sizes (RunLen): %lu/%u\n", (SIZE_T
)(tmp
- dst
), sz
);
592 sz77
= HLPFILE_UncompressedLZ77_Size(src
, src
+ csz
);
593 dst
= HeapAlloc(GetProcessHeap(), 0, sz77
);
594 if (!dst
) return NULL
;
595 HLPFILE_UncompressLZ77(src
, src
+ csz
, dst
);
597 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77
, sz
);
599 case 3: /* LZ77 then RLE */
600 sz77
= HLPFILE_UncompressedLZ77_Size(src
, src
+ csz
);
601 tmp
= HeapAlloc(GetProcessHeap(), 0, sz77
);
602 if (!tmp
) return FALSE
;
603 HLPFILE_UncompressLZ77(src
, src
+ csz
, tmp
);
604 dst
= tmp2
= HeapAlloc(GetProcessHeap(), 0, sz
);
607 HeapFree(GetProcessHeap(), 0, tmp
);
610 HLPFILE_UncompressRLE(tmp
, tmp
+ sz77
, &tmp2
, sz
);
611 if (tmp2
- dst
!= sz
)
612 WINE_WARN("Bogus gfx sizes (LZ77+RunLen): %lu / %u\n", (SIZE_T
)(tmp2
- dst
), sz
);
613 HeapFree(GetProcessHeap(), 0, tmp
);
616 WINE_FIXME("Unsupported packing %u\n", packing
);
622 static BOOL
HLPFILE_RtfAddRawString(struct RtfData
* rd
, const char* str
, size_t sz
)
624 if (!rd
) return TRUE
; /* FIXME: TEMP */
625 if (rd
->ptr
+ sz
>= rd
->data
+ rd
->allocated
)
627 char* new = HeapReAlloc(GetProcessHeap(), 0, rd
->data
, rd
->allocated
*= 2);
628 if (!new) return FALSE
;
629 rd
->ptr
= new + (rd
->ptr
- rd
->data
);
632 memcpy(rd
->ptr
, str
, sz
);
638 static BOOL
HLPFILE_RtfAddControl(struct RtfData
* rd
, const char* str
)
640 if (!rd
) return TRUE
; /* FIXME: TEMP */
641 if (*str
== '\\' || *str
== '{') rd
->in_text
= FALSE
;
642 else if (*str
== '}') rd
->in_text
= TRUE
;
643 return HLPFILE_RtfAddRawString(rd
, str
, strlen(str
));
646 static BOOL
HLPFILE_RtfAddText(struct RtfData
* rd
, const char* str
)
653 if (!rd
) return TRUE
; /* FIXME: TEMP */
656 if (!HLPFILE_RtfAddRawString(rd
, " ", 1)) return FALSE
;
659 for (last
= p
= str
; *p
; p
++)
661 if (*p
< 0) /* escape non ASCII chars */
664 rlen
= sprintf(xx
, "\\'%x", *(const BYTE
*)p
);
669 case '{': rlen
= 2; replace
= "\\{"; break;
670 case '}': rlen
= 2; replace
= "\\}"; break;
671 case '\\': rlen
= 2; replace
= "\\\\"; break;
674 if ((p
!= last
&& !HLPFILE_RtfAddRawString(rd
, last
, p
- last
)) ||
675 !HLPFILE_RtfAddRawString(rd
, replace
, rlen
)) return FALSE
;
678 return HLPFILE_RtfAddRawString(rd
, last
, p
- last
);
681 /******************************************************************
686 static BOOL
HLPFILE_LoadBitmap(BYTE
* beg
, BYTE type
, BYTE pack
,
687 HLPFILE_PARAGRAPH
* paragraph
)
692 unsigned long off
, csz
;
695 bi
= HeapAlloc(GetProcessHeap(), 0, sizeof(*bi
));
696 if (!bi
) return FALSE
;
698 ptr
= beg
+ 2; /* for type and pack */
700 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
701 bi
->bmiHeader
.biXPelsPerMeter
= fetch_ulong(&ptr
);
702 bi
->bmiHeader
.biYPelsPerMeter
= fetch_ulong(&ptr
);
703 bi
->bmiHeader
.biPlanes
= fetch_ushort(&ptr
);
704 bi
->bmiHeader
.biBitCount
= fetch_ushort(&ptr
);
705 bi
->bmiHeader
.biWidth
= fetch_ulong(&ptr
);
706 bi
->bmiHeader
.biHeight
= fetch_ulong(&ptr
);
707 bi
->bmiHeader
.biClrUsed
= fetch_ulong(&ptr
);
708 bi
->bmiHeader
.biClrImportant
= fetch_ulong(&ptr
);
709 bi
->bmiHeader
.biCompression
= BI_RGB
;
710 if (bi
->bmiHeader
.biBitCount
> 32) WINE_FIXME("Unknown bit count %u\n", bi
->bmiHeader
.biBitCount
);
711 if (bi
->bmiHeader
.biPlanes
!= 1) WINE_FIXME("Unsupported planes %u\n", bi
->bmiHeader
.biPlanes
);
712 bi
->bmiHeader
.biSizeImage
= (((bi
->bmiHeader
.biWidth
* bi
->bmiHeader
.biBitCount
+ 31) & ~31) / 8) * bi
->bmiHeader
.biHeight
;
713 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
714 bi
->bmiHeader
.biPlanes
, bi
->bmiHeader
.biBitCount
,
715 bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
);
717 csz
= fetch_ulong(&ptr
);
718 fetch_ulong(&ptr
); /* hotspot size */
720 off
= GET_UINT(ptr
, 0); ptr
+= 4;
721 /* GET_UINT(ptr, 0); hotspot offset */ ptr
+= 4;
723 /* now read palette info */
726 unsigned nc
= bi
->bmiHeader
.biClrUsed
;
729 /* not quite right, especially for bitfields type of compression */
730 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
731 nc
= 1 << bi
->bmiHeader
.biBitCount
;
733 bi
= HeapReAlloc(GetProcessHeap(), 0, bi
, sizeof(*bi
) + nc
* sizeof(RGBQUAD
));
734 if (!bi
) return FALSE
;
735 for (i
= 0; i
< nc
; i
++)
737 bi
->bmiColors
[i
].rgbBlue
= ptr
[0];
738 bi
->bmiColors
[i
].rgbGreen
= ptr
[1];
739 bi
->bmiColors
[i
].rgbRed
= ptr
[2];
740 bi
->bmiColors
[i
].rgbReserved
= 0;
744 pict_beg
= HLPFILE_DecompressGfx(beg
+ off
, csz
, bi
->bmiHeader
.biSizeImage
, pack
);
746 paragraph
->u
.gfx
.u
.bmp
.hBitmap
= CreateDIBitmap(hdc
= GetDC(0), &bi
->bmiHeader
,
750 if (!paragraph
->u
.gfx
.u
.bmp
.hBitmap
)
751 WINE_ERR("Couldn't create bitmap\n");
753 HeapFree(GetProcessHeap(), 0, bi
);
754 if (pict_beg
!= beg
+ off
) HeapFree(GetProcessHeap(), 0, pict_beg
);
759 /******************************************************************
760 * HLPFILE_LoadMetaFile
764 static BOOL
HLPFILE_LoadMetaFile(BYTE
* beg
, BYTE pack
, HLPFILE_PARAGRAPH
* paragraph
)
767 unsigned long size
, csize
;
768 unsigned long off
, hsoff
;
770 LPMETAFILEPICT lpmfp
;
772 WINE_TRACE("Loading metafile\n");
774 ptr
= beg
+ 2; /* for type and pack */
776 lpmfp
= ¶graph
->u
.gfx
.u
.mfp
;
777 lpmfp
->mm
= fetch_ushort(&ptr
); /* mapping mode */
779 lpmfp
->xExt
= GET_USHORT(ptr
, 0);
780 lpmfp
->yExt
= GET_USHORT(ptr
, 2);
783 size
= fetch_ulong(&ptr
); /* decompressed size */
784 csize
= fetch_ulong(&ptr
); /* compressed size */
785 fetch_ulong(&ptr
); /* hotspot size */
786 off
= GET_UINT(ptr
, 0);
787 hsoff
= GET_UINT(ptr
, 4);
790 WINE_TRACE("sz=%lu csz=%lu (%d,%d) offs=%lu/%lu,%lu\n",
791 size
, csize
, lpmfp
->xExt
, lpmfp
->yExt
, off
, (SIZE_T
)(ptr
- beg
), hsoff
);
793 bits
= HLPFILE_DecompressGfx(beg
+ off
, csize
, size
, pack
);
794 if (!bits
) return FALSE
;
796 paragraph
->cookie
= para_metafile
;
798 lpmfp
->hMF
= SetMetaFileBitsEx(size
, bits
);
801 WINE_FIXME("Couldn't load metafile\n");
803 if (bits
!= beg
+ off
) HeapFree(GetProcessHeap(), 0, bits
);
808 /******************************************************************
809 * HLPFILE_LoadGfxByAddr
813 static BOOL
HLPFILE_LoadGfxByAddr(HLPFILE
*hlpfile
, BYTE
* ref
,
815 HLPFILE_PARAGRAPH
* paragraph
)
819 numpict
= GET_USHORT(ref
, 2);
820 WINE_TRACE("Got picture magic=%04x #=%d\n",
821 GET_USHORT(ref
, 0), numpict
);
823 for (i
= 0; i
< numpict
; i
++)
829 WINE_TRACE("Offset[%d] = %x\n", i
, GET_UINT(ref
, (1 + i
) * 4));
830 beg
= ptr
= ref
+ GET_UINT(ref
, (1 + i
) * 4);
837 case 5: /* device dependent bmp */
838 case 6: /* device independent bmp */
839 HLPFILE_LoadBitmap(beg
, type
, pack
, paragraph
);
842 HLPFILE_LoadMetaFile(beg
, pack
, paragraph
);
844 default: WINE_FIXME("Unknown type %u\n", type
); return FALSE
;
847 /* FIXME: hotspots */
849 /* FIXME: implement support for multiple picture format */
850 if (numpict
!= 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
856 /******************************************************************
857 * HLPFILE_LoadGfxByIndex
861 static BOOL
HLPFILE_LoadGfxByIndex(HLPFILE
*hlpfile
, unsigned index
,
862 HLPFILE_PARAGRAPH
* paragraph
)
868 WINE_TRACE("Loading picture #%d\n", index
);
870 if (index
< hlpfile
->numBmps
&& hlpfile
->bmps
[index
] != NULL
)
872 paragraph
->u
.gfx
.u
.bmp
.hBitmap
= hlpfile
->bmps
[index
];
876 sprintf(tmp
, "|bm%u", index
);
878 if (!HLPFILE_FindSubFile(hlpfile
, tmp
, &ref
, &end
)) {WINE_WARN("no sub file\n"); return FALSE
;}
882 ret
= HLPFILE_LoadGfxByAddr(hlpfile
, ref
, end
- ref
, paragraph
);
885 if (ret
&& paragraph
->cookie
== para_bitmap
)
887 if (index
>= hlpfile
->numBmps
)
889 hlpfile
->numBmps
= index
+ 1;
891 hlpfile
->bmps
= HeapReAlloc(GetProcessHeap(), 0, hlpfile
->bmps
,
892 hlpfile
->numBmps
* sizeof(hlpfile
->bmps
[0]));
894 hlpfile
->bmps
= HeapAlloc(GetProcessHeap(), 0,
895 hlpfile
->numBmps
* sizeof(hlpfile
->bmps
[0]));
898 hlpfile
->bmps
[index
] = paragraph
->u
.gfx
.u
.bmp
.hBitmap
;
903 /******************************************************************
908 static HLPFILE_LINK
* HLPFILE_AllocLink(struct RtfData
* rd
, int cookie
,
909 const char* str
, unsigned len
, LONG hash
,
910 unsigned clrChange
, unsigned wnd
)
915 /* FIXME: should build a string table for the attributes.link.lpszPath
916 * they are reallocated for each link
918 if (len
== -1) len
= strlen(str
);
919 link
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK
) + len
+ 1);
920 if (!link
) return NULL
;
922 link
->cookie
= cookie
;
923 link
->string
= link_str
= (char*)(link
+ 1);
924 memcpy(link_str
, str
, len
);
925 link_str
[len
] = '\0';
927 link
->bClrChange
= clrChange
? 1 : 0;
931 link
->next
= rd
->first_link
;
932 rd
->first_link
= link
;
933 link
->cpMin
= rd
->char_pos
;
935 rd
->force_color
= clrChange
;
937 if (rd
->current_link
) WINE_FIXME("Pending link\n");
938 rd
->current_link
= link
;
941 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
942 link
->cookie
, link
->string
, link
->hash
, link
->window
);
946 unsigned HLPFILE_HalfPointsToTwips(unsigned pts
)
948 static unsigned logPxY
;
951 HDC hdc
= GetDC(NULL
);
952 logPxY
= GetDeviceCaps(hdc
, LOGPIXELSY
);
953 ReleaseDC(NULL
, hdc
);
955 return MulDiv(pts
, 72 * 10, logPxY
);
958 /***********************************************************************
960 * HLPFILE_BrowseParagraph
962 static BOOL
HLPFILE_BrowseParagraph(HLPFILE_PAGE
* page
, struct RtfData
* rd
, BYTE
*buf
, BYTE
* end
)
964 HLPFILE_PARAGRAPH
*paragraph
, **paragraphptr
;
966 BYTE
*format
, *format_end
;
967 char *text
, *text_base
, *text_end
;
968 long size
, blocksize
, datalen
;
970 unsigned nc
, ncol
= 1;
971 BOOL in_table
= FALSE
;
975 for (paragraphptr
= &page
->first_paragraph
; *paragraphptr
;
976 paragraphptr
= &(*paragraphptr
)->next
) /* Nothing */;
978 if (buf
+ 0x19 > end
) {WINE_WARN("header too small\n"); return FALSE
;};
980 blocksize
= GET_UINT(buf
, 0);
981 size
= GET_UINT(buf
, 0x4);
982 datalen
= GET_UINT(buf
, 0x10);
983 text
= text_base
= HeapAlloc(GetProcessHeap(), 0, size
);
984 if (!text
) return FALSE
;
985 if (size
> blocksize
- datalen
)
987 /* need to decompress */
988 if (page
->file
->hasPhrases
)
989 HLPFILE_Uncompress2(page
->file
, buf
+ datalen
, end
, (BYTE
*)text
, (BYTE
*)text
+ size
);
990 else if (page
->file
->hasPhrases40
)
991 HLPFILE_Uncompress3(page
->file
, text
, text
+ size
, buf
+ datalen
, end
);
994 WINE_FIXME("Text size is too long, splitting\n");
995 size
= blocksize
- datalen
;
996 memcpy(text
, buf
+ datalen
, size
);
1000 memcpy(text
, buf
+ datalen
, size
);
1002 text_end
= text
+ size
;
1004 format
= buf
+ 0x15;
1005 format_end
= buf
+ GET_UINT(buf
, 0x10);
1007 if (buf
[0x14] == 0x20 || buf
[0x14] == 0x23)
1009 fetch_long(&format
);
1010 fetch_ushort(&format
);
1013 if (buf
[0x14] == 0x23)
1020 WINE_TRACE("#cols %u\n", ncol
);
1022 if (type
== 0 || type
== 2)
1027 for (nc
= 0; nc
< ncol
; /**/)
1029 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T
)(format
- (buf
+ 0x15)), nc
);
1032 nc
= GET_SHORT(format
, 0);
1033 if (nc
== -1) break;
1038 bits
= GET_USHORT(format
, 0); format
+= 2;
1039 if (bits
& 0x0001) fetch_long(&format
);
1042 sprintf(tmp
, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1043 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1047 sprintf(tmp
, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1048 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1052 sprintf(tmp
, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1053 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1057 sprintf(tmp
, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1058 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1062 sprintf(tmp
, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1063 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1067 sprintf(tmp
, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1068 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1070 if (bits
& 0x0100) format
+= 3;
1073 int i
, ntab
= fetch_short(&format
);
1077 for (i
= 0; i
< ntab
; i
++)
1079 tab
= fetch_ushort(&format
);
1080 ts
= (tab
& 0x4000) ? fetch_ushort(&format
) : 0 /* left */;
1083 default: WINE_FIXME("Unknown tab style %x\n", ts
);
1085 case 0: kind
= ""; break;
1086 case 1: kind
= "\\tqr"; break;
1087 case 2: kind
= "\\tqc"; break;
1089 /* FIXME: do kind */
1090 sprintf(tmp
, "%s\\tx%d",
1091 kind
, HLPFILE_HalfPointsToTwips(tab
& 0x3FFF));
1092 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1095 switch (bits
& 0xc00)
1097 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1098 case 0: if (!HLPFILE_RtfAddControl(rd
, "\\ql")) goto done
; break;
1099 case 0x400: if (!HLPFILE_RtfAddControl(rd
, "\\qr")) goto done
; break;
1100 case 0x800: if (!HLPFILE_RtfAddControl(rd
, "\\qc")) goto done
; break;
1103 /* 0x1000 doesn't need space */
1104 if ((bits
& 0xE080) != 0)
1105 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits
);
1107 while (text
< text_end
&& format
< format_end
)
1109 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text
), text
, text_end
, format
, format_end
);
1110 textsize
= strlen(text
);
1113 paragraph
= HeapAlloc(GetProcessHeap(), 0,
1114 sizeof(HLPFILE_PARAGRAPH
) + textsize
+ 1);
1115 if (!paragraph
) return FALSE
;
1116 *paragraphptr
= paragraph
;
1117 paragraphptr
= ¶graph
->next
;
1119 paragraph
->next
= NULL
;
1122 paragraph
->link
= attributes
.link
;
1123 if (paragraph
->link
) paragraph
->link
->wRefCount
++;
1125 else paragraph
->link
= NULL
;
1126 paragraph
->cookie
= para_normal_text
;
1127 paragraph
->u
.text
.wFont
= attributes
.wFont
;
1128 paragraph
->u
.text
.wVSpace
= attributes
.wVSpace
;
1129 paragraph
->u
.text
.wHSpace
= attributes
.wHSpace
;
1130 paragraph
->u
.text
.wIndent
= attributes
.wIndent
;
1131 paragraph
->u
.text
.lpszText
= (char*)paragraph
+ sizeof(HLPFILE_PARAGRAPH
);
1132 strcpy(paragraph
->u
.text
.lpszText
, text
);
1134 attributes
.wVSpace
= 0;
1135 attributes
.wHSpace
= 0;
1136 if (rd
) /* FIXME: TEMP */ {
1137 if (rd
->force_color
)
1139 if ((rd
->current_link
->cookie
== hlp_link_popup
) ?
1140 !HLPFILE_RtfAddControl(rd
, "{\\uld\\cf1") :
1141 !HLPFILE_RtfAddControl(rd
, "{\\ul\\cf1")) goto done
;
1143 if (!HLPFILE_RtfAddText(rd
, text
)) goto done
;
1144 if (rd
->force_color
&& !HLPFILE_RtfAddControl(rd
, "}")) goto done
;
1145 rd
->char_pos
+= textsize
;
1148 /* else: null text, keep on storing attributes */
1149 text
+= textsize
+ 1;
1151 if (*format
== 0xff)
1157 WINE_TRACE("format=%02x\n", *format
);
1161 WINE_FIXME("NIY20\n");
1166 WINE_FIXME("NIY21\n");
1172 unsigned font
= GET_USHORT(format
, 1);
1174 attributes
.wFont
= font
;
1175 WINE_TRACE("Changing font to %d\n", attributes
.wFont
);
1177 fs
= (4 * page
->file
->fonts
[font
].LogFont
.lfHeight
- 3) / 5;
1178 /* FIXME: missing at least colors, also bold attribute looses information */
1180 sprintf(tmp
, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1182 page
->file
->fonts
[font
].LogFont
.lfWeight
> 400 ? "\\b" : "\\b0",
1183 page
->file
->fonts
[font
].LogFont
.lfItalic
? "\\i" : "\\i0",
1184 page
->file
->fonts
[font
].LogFont
.lfUnderline
? "\\ul" : "\\ul0",
1185 page
->file
->fonts
[font
].LogFont
.lfStrikeOut
? "\\strike" : "\\strike0");
1186 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1191 if (!HLPFILE_RtfAddControl(rd
, "\\line")) goto done
;
1192 attributes
.wVSpace
++;
1194 if (rd
) /* FIXME: TEMP */ rd
->char_pos
++;
1198 if (!HLPFILE_RtfAddControl(rd
, "\\par\\pard")) goto done
;
1199 attributes
.wVSpace
++;
1200 attributes
.wIndent
= 0;
1202 if (rd
) /* FIXME: TEMP */ rd
->char_pos
++;
1206 if (!HLPFILE_RtfAddControl(rd
, "\\tab")) goto done
;
1207 attributes
.wIndent
++;
1209 if (rd
) /* FIXME: TEMP */ rd
->char_pos
++;
1222 BYTE pos
= (*format
- 0x86);
1223 BYTE type
= format
[1];
1227 size
= fetch_long(&format
);
1229 paragraph
= HeapAlloc(GetProcessHeap(), 0,
1230 sizeof(HLPFILE_PARAGRAPH
) + textsize
);
1231 if (!paragraph
) return FALSE
;
1232 *paragraphptr
= paragraph
;
1233 paragraphptr
= ¶graph
->next
;
1235 paragraph
->next
= NULL
;
1237 paragraph
->link
= attributes
.link
;
1238 if (paragraph
->link
) paragraph
->link
->wRefCount
++;
1240 else paragraph
->link
= NULL
;
1241 paragraph
->cookie
= para_bitmap
;
1242 paragraph
->u
.gfx
.pos
= pos
;
1246 fetch_ushort(&format
); /* hot spot */
1249 switch (GET_SHORT(format
, 0))
1252 HLPFILE_LoadGfxByIndex(page
->file
, GET_SHORT(format
, 2),
1256 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1257 GET_SHORT(format
, 0),
1258 size
, GET_SHORT(format
, 2));
1259 HLPFILE_LoadGfxByAddr(page
->file
, format
+ 2, size
- 4,
1263 WINE_FIXME("??? %u\n", GET_SHORT(format
, 0));
1268 WINE_FIXME("Got an embedded element %s\n", format
+ 6);
1271 WINE_FIXME("Got a type %d picture\n", type
);
1274 if (attributes
.wVSpace
) paragraph
->u
.gfx
.pos
|= 0x8000;
1281 HLPFILE_FreeLink(attributes
.link
);
1282 attributes
.link
= NULL
;
1285 if (!rd
->current_link
)
1286 WINE_FIXME("No existing link\n");
1288 rd
->current_link
->cpMax
= rd
->char_pos
;
1289 rd
->current_link
= NULL
;
1290 rd
->force_color
= FALSE
;
1295 if (!HLPFILE_RtfAddControl(rd
, "\\~")) goto done
;
1297 if (rd
) /* FIXME: TEMP */ rd
->char_pos
++;
1301 if (!HLPFILE_RtfAddControl(rd
, "\\_")) goto done
;
1302 /* FIXME: it could be that hypen is also in input stream !! */
1304 if (rd
) /* FIXME: TEMP */ rd
->char_pos
++;
1315 WINE_TRACE("macro => %s\n", format
+ 3);
1316 HLPFILE_FreeLink(attributes
.link
);
1317 attributes
.link
= HLPFILE_AllocLink(rd
, hlp_link_macro
, (const char*)format
+ 3,
1318 GET_USHORT(format
, 1), 0, !(*format
& 4), -1);
1319 format
+= 3 + GET_USHORT(format
, 1);
1324 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format
, 1));
1325 HLPFILE_FreeLink(attributes
.link
);
1326 attributes
.link
= HLPFILE_AllocLink(rd
, (*format
& 1) ? hlp_link_link
: hlp_link_popup
,
1327 page
->file
->lpszPath
, -1,
1328 GET_UINT(format
, 1)-16,
1339 attributes
.link
= HLPFILE_AllocLink(rd
, (*format
& 1) ? hlp_link_link
: hlp_link_popup
,
1340 page
->file
->lpszPath
, -1,
1341 GET_UINT(format
, 1),
1342 !(*format
& 4), -1);
1351 char* ptr
= (char*) format
+ 8;
1352 BYTE type
= format
[3];
1361 ptr
= page
->file
->lpszPath
;
1364 for (wnd
= page
->file
->numWindows
- 1; wnd
>= 0; wnd
--)
1366 if (!strcmp(ptr
, page
->file
->windows
[wnd
].name
)) break;
1369 WINE_WARN("Couldn't find window info for %s\n", ptr
);
1370 ptr
+= strlen(ptr
) + 1;
1375 WINE_WARN("Unknown link type %d\n", type
);
1378 HLPFILE_FreeLink(attributes
.link
);
1379 attributes
.link
= HLPFILE_AllocLink(rd
, (*format
& 1) ? hlp_link_link
: hlp_link_popup
,
1380 ptr
, -1, GET_UINT(format
, 4),
1381 !(*format
& 4), wnd
);
1383 format
+= 3 + GET_USHORT(format
, 1);
1387 WINE_WARN("format %02x\n", *format
);
1394 HeapFree(GetProcessHeap(), 0, text_base
);
1398 /******************************************************************
1399 * HLPFILE_BrowsePage
1402 BOOL
HLPFILE_BrowsePage(HLPFILE_PAGE
* page
, struct RtfData
* rd
)
1404 HLPFILE
*hlpfile
= page
->file
;
1406 DWORD ref
= page
->reference
;
1407 unsigned index
, old_index
= -1, offset
, count
= 0, cpg
;
1409 const char* ck
= NULL
;
1411 if (rd
) { /* FIXME: TEMP */
1413 rd
->data
= rd
->ptr
= HeapAlloc(GetProcessHeap(), 0, rd
->allocated
= 32768);
1415 rd
->first_link
= rd
->current_link
= NULL
;
1416 rd
->force_color
= FALSE
;
1419 switch (hlpfile
->charset
)
1421 case DEFAULT_CHARSET
:
1422 case ANSI_CHARSET
: cpg
= 1252; break;
1423 case SHIFTJIS_CHARSET
: cpg
= 932; break;
1424 case HANGEUL_CHARSET
: cpg
= 949; break;
1425 case GB2312_CHARSET
: cpg
= 936; break;
1426 case CHINESEBIG5_CHARSET
: cpg
= 950; break;
1427 case GREEK_CHARSET
: cpg
= 1253; break;
1428 case TURKISH_CHARSET
: cpg
= 1254; break;
1429 case HEBREW_CHARSET
: cpg
= 1255; break;
1430 case ARABIC_CHARSET
: cpg
= 1256; break;
1431 case BALTIC_CHARSET
: cpg
= 1257; break;
1432 case VIETNAMESE_CHARSET
: cpg
= 1258; break;
1433 case RUSSIAN_CHARSET
: cpg
= 1251; break;
1434 case EE_CHARSET
: cpg
= 1250; break;
1435 case THAI_CHARSET
: cpg
= 874; break;
1436 case JOHAB_CHARSET
: cpg
= 1361; break;
1437 case MAC_CHARSET
: ck
= "mac"; break;
1439 WINE_FIXME("Unsupported charset %u\n", hlpfile
->charset
);
1444 sprintf(tmp
, "{\\rtf1\\%s\\deff0", ck
);
1445 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1449 sprintf(tmp
, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg
);
1450 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1453 /* generate font table */
1454 if (!HLPFILE_RtfAddControl(rd
, "{\\fonttbl")) return FALSE
;
1455 for (index
= 0; index
< hlpfile
->numFonts
; index
++)
1458 switch (hlpfile
->fonts
[index
].LogFont
.lfPitchAndFamily
& 0xF0)
1460 case FF_MODERN
: family
= "modern"; break;
1461 case FF_ROMAN
: family
= "roman"; break;
1462 case FF_SWISS
: family
= "swiss"; break;
1463 case FF_SCRIPT
: family
= "script"; break;
1464 case FF_DECORATIVE
: family
= "decor"; break;
1465 default: family
= "nil"; break;
1467 sprintf(tmp
, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1469 hlpfile
->fonts
[index
].LogFont
.lfPitchAndFamily
& 0x0F,
1470 hlpfile
->fonts
[index
].LogFont
.lfCharSet
,
1471 hlpfile
->fonts
[index
].LogFont
.lfFaceName
);
1472 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1474 if (!HLPFILE_RtfAddControl(rd
, "}")) return FALSE
;
1475 /* generate color table */
1476 if (!HLPFILE_RtfAddControl(rd
, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE
;
1477 for (index
= 0; index
< hlpfile
->numFonts
; index
++)
1480 switch (hlpfile
->fonts
[index
].LogFont
.lfPitchAndFamily
& 0xF0)
1482 case FF_MODERN
: family
= "modern"; break;
1483 case FF_ROMAN
: family
= "roman"; break;
1484 case FF_SWISS
: family
= "swiss"; break;
1485 case FF_SCRIPT
: family
= "script"; break;
1486 case FF_DECORATIVE
: family
= "decor"; break;
1487 default: family
= "nil"; break;
1489 sprintf(tmp
, "\\red%d\\green%d\\blue%d;",
1490 GetRValue(hlpfile
->fonts
[index
].color
),
1491 GetGValue(hlpfile
->fonts
[index
].color
),
1492 GetBValue(hlpfile
->fonts
[index
].color
));
1493 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1495 if (!HLPFILE_RtfAddControl(rd
, "}")) return FALSE
;
1499 if (hlpfile
->version
<= 16)
1501 index
= (ref
- 0x0C) / hlpfile
->dsize
;
1502 offset
= (ref
- 0x0C) % hlpfile
->dsize
;
1506 index
= (ref
- 0x0C) >> 14;
1507 offset
= (ref
- 0x0C) & 0x3FFF;
1510 if (hlpfile
->version
<= 16 && index
!= old_index
&& index
!= 0)
1512 /* we jumped to the next block, adjust pointers */
1517 if (index
>= hlpfile
->topic_maplen
) {WINE_WARN("maplen\n"); break;}
1518 buf
= hlpfile
->topic_map
[index
] + offset
;
1519 if (buf
+ 0x15 >= hlpfile
->topic_end
) {WINE_WARN("extra\n"); break;}
1520 end
= min(buf
+ GET_UINT(buf
, 0), hlpfile
->topic_end
);
1521 if (index
!= old_index
) {old_index
= index
;}
1526 if (count
++) goto done
;
1531 if (!HLPFILE_BrowseParagraph(page
, rd
, buf
, end
)) return FALSE
;
1534 WINE_ERR("buf[0x14] = %x\n", buf
[0x14]);
1536 if (hlpfile
->version
<= 16)
1538 ref
+= GET_UINT(buf
, 0xc);
1539 if (GET_UINT(buf
, 0xc) == 0)
1543 ref
= GET_UINT(buf
, 0xc);
1544 } while (ref
!= 0xffffffff);
1547 page
->first_link
= rd
->first_link
;
1548 return HLPFILE_RtfAddControl(rd
, "}");
1551 /******************************************************************
1556 static BOOL
HLPFILE_ReadFont(HLPFILE
* hlpfile
)
1559 unsigned i
, len
, idx
;
1560 unsigned face_num
, dscr_num
, face_offset
, dscr_offset
;
1563 if (!HLPFILE_FindSubFile(hlpfile
, "|FONT", &ref
, &end
))
1565 WINE_WARN("no subfile FONT\n");
1566 hlpfile
->numFonts
= 0;
1567 hlpfile
->fonts
= NULL
;
1573 face_num
= GET_USHORT(ref
, 0);
1574 dscr_num
= GET_USHORT(ref
, 2);
1575 face_offset
= GET_USHORT(ref
, 4);
1576 dscr_offset
= GET_USHORT(ref
, 6);
1578 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1579 face_num
, face_offset
, dscr_num
, dscr_offset
);
1581 hlpfile
->numFonts
= dscr_num
;
1582 hlpfile
->fonts
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT
) * dscr_num
);
1584 len
= (dscr_offset
- face_offset
) / face_num
;
1585 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1586 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1587 for (i
= 0; i
< dscr_num
; i
++)
1589 flag
= ref
[dscr_offset
+ i
* 11 + 0];
1590 family
= ref
[dscr_offset
+ i
* 11 + 2];
1592 hlpfile
->fonts
[i
].LogFont
.lfHeight
= ref
[dscr_offset
+ i
* 11 + 1];
1593 hlpfile
->fonts
[i
].LogFont
.lfWidth
= 0;
1594 hlpfile
->fonts
[i
].LogFont
.lfEscapement
= 0;
1595 hlpfile
->fonts
[i
].LogFont
.lfOrientation
= 0;
1596 hlpfile
->fonts
[i
].LogFont
.lfWeight
= (flag
& 1) ? 700 : 400;
1597 hlpfile
->fonts
[i
].LogFont
.lfItalic
= (flag
& 2) ? TRUE
: FALSE
;
1598 hlpfile
->fonts
[i
].LogFont
.lfUnderline
= (flag
& 4) ? TRUE
: FALSE
;
1599 hlpfile
->fonts
[i
].LogFont
.lfStrikeOut
= (flag
& 8) ? TRUE
: FALSE
;
1600 hlpfile
->fonts
[i
].LogFont
.lfCharSet
= hlpfile
->charset
;
1601 hlpfile
->fonts
[i
].LogFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
1602 hlpfile
->fonts
[i
].LogFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
1603 hlpfile
->fonts
[i
].LogFont
.lfQuality
= DEFAULT_QUALITY
;
1604 hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
= DEFAULT_PITCH
;
1608 case 0x01: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_MODERN
; break;
1609 case 0x02: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_ROMAN
; break;
1610 case 0x03: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_SWISS
; break;
1611 case 0x04: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_SCRIPT
; break;
1612 case 0x05: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_DECORATIVE
; break;
1613 default: WINE_FIXME("Unknown family %u\n", family
);
1615 idx
= GET_USHORT(ref
, dscr_offset
+ i
* 11 + 3);
1619 memcpy(hlpfile
->fonts
[i
].LogFont
.lfFaceName
, ref
+ face_offset
+ idx
* len
, min(len
, LF_FACESIZE
- 1));
1620 hlpfile
->fonts
[i
].LogFont
.lfFaceName
[min(len
, LF_FACESIZE
- 1)] = '\0';
1624 WINE_FIXME("Too high face ref (%u/%u)\n", idx
, face_num
);
1625 strcpy(hlpfile
->fonts
[i
].LogFont
.lfFaceName
, "Helv");
1627 hlpfile
->fonts
[i
].hFont
= 0;
1628 hlpfile
->fonts
[i
].color
= RGB(ref
[dscr_offset
+ i
* 11 + 5],
1629 ref
[dscr_offset
+ i
* 11 + 6],
1630 ref
[dscr_offset
+ i
* 11 + 7]);
1631 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1632 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1638 X(4, "dblUnderline"),
1640 ref
[dscr_offset
+ i
* 11 + 1],
1642 hlpfile
->fonts
[i
].LogFont
.lfFaceName
, idx
,
1643 GET_UINT(ref
, dscr_offset
+ i
* 11 + 5) & 0x00FFFFFF);
1648 /***********************************************************************
1650 * HLPFILE_ReadFileToBuffer
1652 static BOOL
HLPFILE_ReadFileToBuffer(HLPFILE
* hlpfile
, HFILE hFile
)
1654 BYTE header
[16], dummy
[1];
1656 if (_hread(hFile
, header
, 16) != 16) {WINE_WARN("header\n"); return FALSE
;};
1659 if (GET_UINT(header
, 0) != 0x00035F3F)
1660 {WINE_WARN("wrong header\n"); return FALSE
;};
1662 hlpfile
->file_buffer_size
= GET_UINT(header
, 12);
1663 hlpfile
->file_buffer
= HeapAlloc(GetProcessHeap(), 0, hlpfile
->file_buffer_size
+ 1);
1664 if (!hlpfile
->file_buffer
) return FALSE
;
1666 memcpy(hlpfile
->file_buffer
, header
, 16);
1667 if (_hread(hFile
, hlpfile
->file_buffer
+ 16, hlpfile
->file_buffer_size
- 16) !=hlpfile
->file_buffer_size
- 16)
1668 {WINE_WARN("filesize1\n"); return FALSE
;};
1670 if (_hread(hFile
, dummy
, 1) != 0) WINE_WARN("filesize2\n");
1672 hlpfile
->file_buffer
[hlpfile
->file_buffer_size
] = '\0'; /* FIXME: was '0', sounds backwards to me */
1677 /**************************************************************************
1680 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1683 static int comp_FindSubFile(void *p
, const void *key
,
1684 int leaf
, void** next
)
1686 *next
= (char *)p
+strlen(p
)+(leaf
?5:3);
1687 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p
, (char *)key
);
1688 return strcmp(p
, key
);
1691 /***********************************************************************
1693 * HLPFILE_FindSubFile
1695 static BOOL
HLPFILE_FindSubFile(HLPFILE
* hlpfile
, LPCSTR name
, BYTE
**subbuf
, BYTE
**subend
)
1699 WINE_TRACE("looking for file '%s'\n", name
);
1700 ptr
= HLPFILE_BPTreeSearch(hlpfile
->file_buffer
+ GET_UINT(hlpfile
->file_buffer
, 4),
1701 name
, comp_FindSubFile
);
1702 if (!ptr
) return FALSE
;
1703 *subbuf
= hlpfile
->file_buffer
+ GET_UINT(ptr
, strlen(name
)+1);
1704 if (*subbuf
>= hlpfile
->file_buffer
+ hlpfile
->file_buffer_size
)
1706 WINE_ERR("internal file %s does not fit\n", name
);
1709 *subend
= *subbuf
+ GET_UINT(*subbuf
, 0);
1710 if (*subend
> hlpfile
->file_buffer
+ hlpfile
->file_buffer_size
)
1712 WINE_ERR("internal file %s does not fit\n", name
);
1715 if (GET_UINT(*subbuf
, 0) < GET_UINT(*subbuf
, 4) + 9)
1717 WINE_ERR("invalid size provided for internal file %s\n", name
);
1723 /***********************************************************************
1725 * HLPFILE_SystemCommands
1727 static BOOL
HLPFILE_SystemCommands(HLPFILE
* hlpfile
)
1729 BYTE
*buf
, *ptr
, *end
;
1730 HLPFILE_MACRO
*macro
, **m
;
1732 unsigned short magic
, minor
, major
, flags
;
1734 hlpfile
->lpszTitle
= NULL
;
1736 if (!HLPFILE_FindSubFile(hlpfile
, "|SYSTEM", &buf
, &end
)) return FALSE
;
1738 magic
= GET_USHORT(buf
+ 9, 0);
1739 minor
= GET_USHORT(buf
+ 9, 2);
1740 major
= GET_USHORT(buf
+ 9, 4);
1741 /* gen date on 4 bytes */
1742 flags
= GET_USHORT(buf
+ 9, 10);
1743 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1744 magic
, major
, minor
, flags
);
1745 if (magic
!= 0x036C || major
!= 1)
1746 {WINE_WARN("Wrong system header\n"); return FALSE
;}
1749 hlpfile
->tbsize
= 0x800;
1750 hlpfile
->compressed
= 0;
1752 else if (flags
== 0)
1754 hlpfile
->tbsize
= 0x1000;
1755 hlpfile
->compressed
= 0;
1757 else if (flags
== 4)
1759 hlpfile
->tbsize
= 0x1000;
1760 hlpfile
->compressed
= 1;
1764 hlpfile
->tbsize
= 0x800;
1765 hlpfile
->compressed
= 1;
1768 if (hlpfile
->compressed
)
1769 hlpfile
->dsize
= 0x4000;
1771 hlpfile
->dsize
= hlpfile
->tbsize
- 0x0C;
1773 hlpfile
->version
= minor
;
1774 hlpfile
->flags
= flags
;
1775 hlpfile
->charset
= DEFAULT_CHARSET
;
1777 for (ptr
= buf
+ 0x15; ptr
+ 4 <= end
; ptr
+= GET_USHORT(ptr
, 2) + 4)
1779 char *str
= (char*) ptr
+ 4;
1780 switch (GET_USHORT(ptr
, 0))
1783 if (hlpfile
->lpszTitle
) {WINE_WARN("title\n"); break;}
1784 hlpfile
->lpszTitle
= HeapAlloc(GetProcessHeap(), 0, strlen(str
) + 1);
1785 if (!hlpfile
->lpszTitle
) return FALSE
;
1786 lstrcpy(hlpfile
->lpszTitle
, str
);
1787 WINE_TRACE("Title: %s\n", hlpfile
->lpszTitle
);
1791 if (hlpfile
->lpszCopyright
) {WINE_WARN("copyright\n"); break;}
1792 hlpfile
->lpszCopyright
= HeapAlloc(GetProcessHeap(), 0, strlen(str
) + 1);
1793 if (!hlpfile
->lpszCopyright
) return FALSE
;
1794 lstrcpy(hlpfile
->lpszCopyright
, str
);
1795 WINE_TRACE("Copyright: %s\n", hlpfile
->lpszCopyright
);
1799 if (GET_USHORT(ptr
, 2) != 4) {WINE_WARN("system3\n");break;}
1800 hlpfile
->contents_start
= GET_UINT(ptr
, 4);
1801 WINE_TRACE("Setting contents start at %08lx\n", hlpfile
->contents_start
);
1805 macro
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO
) + lstrlen(str
) + 1);
1807 p
= (char*)macro
+ sizeof(HLPFILE_MACRO
);
1809 macro
->lpszMacro
= p
;
1811 for (m
= &hlpfile
->first_macro
; *m
; m
= &(*m
)->next
);
1816 if (GET_USHORT(ptr
, 4 + 4) != 1)
1817 WINE_FIXME("More than one icon, picking up first\n");
1818 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
1819 hlpfile
->hIcon
= CreateIconFromResourceEx(ptr
+ 4 + 0x16,
1820 GET_USHORT(ptr
, 2) - 0x16, TRUE
,
1825 if (GET_USHORT(ptr
, 2) != 90) {WINE_WARN("system6\n");break;}
1827 if (hlpfile
->windows
)
1828 hlpfile
->windows
= HeapReAlloc(GetProcessHeap(), 0, hlpfile
->windows
,
1829 sizeof(HLPFILE_WINDOWINFO
) * ++hlpfile
->numWindows
);
1831 hlpfile
->windows
= HeapAlloc(GetProcessHeap(), 0,
1832 sizeof(HLPFILE_WINDOWINFO
) * ++hlpfile
->numWindows
);
1834 if (hlpfile
->windows
)
1836 unsigned flags
= GET_USHORT(ptr
, 4);
1837 HLPFILE_WINDOWINFO
* wi
= &hlpfile
->windows
[hlpfile
->numWindows
- 1];
1839 if (flags
& 0x0001) strcpy(wi
->type
, &str
[2]);
1840 else wi
->type
[0] = '\0';
1841 if (flags
& 0x0002) strcpy(wi
->name
, &str
[12]);
1842 else wi
->name
[0] = '\0';
1843 if (flags
& 0x0004) strcpy(wi
->caption
, &str
[21]);
1844 else lstrcpynA(wi
->caption
, hlpfile
->lpszTitle
, sizeof(wi
->caption
));
1845 wi
->origin
.x
= (flags
& 0x0008) ? GET_USHORT(ptr
, 76) : CW_USEDEFAULT
;
1846 wi
->origin
.y
= (flags
& 0x0010) ? GET_USHORT(ptr
, 78) : CW_USEDEFAULT
;
1847 wi
->size
.cx
= (flags
& 0x0020) ? GET_USHORT(ptr
, 80) : CW_USEDEFAULT
;
1848 wi
->size
.cy
= (flags
& 0x0040) ? GET_USHORT(ptr
, 82) : CW_USEDEFAULT
;
1849 wi
->style
= (flags
& 0x0080) ? GET_USHORT(ptr
, 84) : SW_SHOW
;
1850 wi
->win_style
= WS_OVERLAPPEDWINDOW
;
1851 wi
->sr_color
= (flags
& 0x0100) ? GET_UINT(ptr
, 86) : 0xFFFFFF;
1852 wi
->nsr_color
= (flags
& 0x0200) ? GET_UINT(ptr
, 90) : 0xFFFFFF;
1853 WINE_TRACE("System-Window: flags=%c%c%c%c%c%c%c%c type=%s name=%s caption=%s (%d,%d)x(%d,%d)\n",
1854 flags
& 0x0001 ? 'T' : 't',
1855 flags
& 0x0002 ? 'N' : 'n',
1856 flags
& 0x0004 ? 'C' : 'c',
1857 flags
& 0x0008 ? 'X' : 'x',
1858 flags
& 0x0010 ? 'Y' : 'y',
1859 flags
& 0x0020 ? 'W' : 'w',
1860 flags
& 0x0040 ? 'H' : 'h',
1861 flags
& 0x0080 ? 'S' : 's',
1862 wi
->type
, wi
->name
, wi
->caption
, wi
->origin
.x
, wi
->origin
.y
,
1863 wi
->size
.cx
, wi
->size
.cy
);
1867 WINE_WARN("Citation: '%s'\n", ptr
+ 4);
1870 hlpfile
->charset
= ptr
[4];
1871 WINE_TRACE("Charset: %d\n", hlpfile
->charset
);
1874 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr
, 0));
1877 if (!hlpfile
->lpszTitle
)
1878 hlpfile
->lpszTitle
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 1);
1882 /***********************************************************************
1884 * HLPFILE_UncompressedLZ77_Size
1886 static INT
HLPFILE_UncompressedLZ77_Size(BYTE
*ptr
, BYTE
*end
)
1893 for (i
= 0; i
< 8 && ptr
< end
; i
++, mask
>>= 1)
1897 int code
= GET_USHORT(ptr
, 0);
1898 int len
= 3 + (code
>> 12);
1902 else newsize
++, ptr
++;
1909 /***********************************************************************
1911 * HLPFILE_UncompressLZ77
1913 static BYTE
*HLPFILE_UncompressLZ77(BYTE
*ptr
, BYTE
*end
, BYTE
*newptr
)
1920 for (i
= 0; i
< 8 && ptr
< end
; i
++, mask
>>= 1)
1924 int code
= GET_USHORT(ptr
, 0);
1925 int len
= 3 + (code
>> 12);
1926 int offset
= code
& 0xfff;
1928 * We must copy byte-by-byte here. We cannot use memcpy nor
1929 * memmove here. Just example:
1930 * a[]={1,2,3,4,5,6,7,8,9,10}
1934 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
1936 for (; len
>0; len
--, newptr
++) *newptr
= *(newptr
-offset
-1);
1939 else *newptr
++ = *ptr
++;
1946 /***********************************************************************
1948 * HLPFILE_UncompressLZ77_Phrases
1950 static BOOL
HLPFILE_UncompressLZ77_Phrases(HLPFILE
* hlpfile
)
1952 UINT i
, num
, dec_size
, head_size
;
1955 if (!HLPFILE_FindSubFile(hlpfile
, "|Phrases", &buf
, &end
)) return FALSE
;
1957 if (hlpfile
->version
<= 16)
1962 num
= hlpfile
->num_phrases
= GET_USHORT(buf
, 9);
1963 if (buf
+ 2 * num
+ 0x13 >= end
) {WINE_WARN("1a\n"); return FALSE
;};
1965 if (hlpfile
->version
<= 16)
1966 dec_size
= end
- buf
- 15 - 2 * num
;
1968 dec_size
= HLPFILE_UncompressedLZ77_Size(buf
+ 0x13 + 2 * num
, end
);
1970 hlpfile
->phrases_offsets
= HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num
+ 1));
1971 hlpfile
->phrases_buffer
= HeapAlloc(GetProcessHeap(), 0, dec_size
);
1972 if (!hlpfile
->phrases_offsets
|| !hlpfile
->phrases_buffer
)
1974 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_offsets
);
1975 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_buffer
);
1979 for (i
= 0; i
<= num
; i
++)
1980 hlpfile
->phrases_offsets
[i
] = GET_USHORT(buf
, 0x11 + 2 * i
) - 2 * num
- 2;
1982 if (hlpfile
->version
<= 16)
1983 memcpy(hlpfile
->phrases_buffer
, buf
+ 15 + 2*num
, dec_size
);
1985 HLPFILE_UncompressLZ77(buf
+ 0x13 + 2 * num
, end
, (BYTE
*)hlpfile
->phrases_buffer
);
1987 hlpfile
->hasPhrases
= TRUE
;
1991 /***********************************************************************
1993 * HLPFILE_Uncompress_Phrases40
1995 static BOOL
HLPFILE_Uncompress_Phrases40(HLPFILE
* hlpfile
)
1998 INT dec_size
, cpr_size
;
1999 BYTE
*buf_idx
, *end_idx
;
2000 BYTE
*buf_phs
, *end_phs
;
2001 long* ptr
, mask
= 0;
2003 unsigned short bc
, n
;
2005 if (!HLPFILE_FindSubFile(hlpfile
, "|PhrIndex", &buf_idx
, &end_idx
) ||
2006 !HLPFILE_FindSubFile(hlpfile
, "|PhrImage", &buf_phs
, &end_phs
)) return FALSE
;
2008 ptr
= (long*)(buf_idx
+ 9 + 28);
2009 bc
= GET_USHORT(buf_idx
, 9 + 24) & 0x0F;
2010 num
= hlpfile
->num_phrases
= GET_USHORT(buf_idx
, 9 + 4);
2012 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2013 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2014 GET_UINT(buf_idx
, 9 + 0),
2015 GET_UINT(buf_idx
, 9 + 4),
2016 GET_UINT(buf_idx
, 9 + 8),
2017 GET_UINT(buf_idx
, 9 + 12),
2018 GET_UINT(buf_idx
, 9 + 16),
2019 GET_UINT(buf_idx
, 9 + 20),
2020 GET_USHORT(buf_idx
, 9 + 24),
2021 GET_USHORT(buf_idx
, 9 + 26));
2023 dec_size
= GET_UINT(buf_idx
, 9 + 12);
2024 cpr_size
= GET_UINT(buf_idx
, 9 + 16);
2026 if (dec_size
!= cpr_size
&&
2027 dec_size
!= HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
))
2029 WINE_WARN("size mismatch %u %u\n",
2030 dec_size
, HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
));
2031 dec_size
= max(dec_size
, HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
));
2034 hlpfile
->phrases_offsets
= HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num
+ 1));
2035 hlpfile
->phrases_buffer
= HeapAlloc(GetProcessHeap(), 0, dec_size
);
2036 if (!hlpfile
->phrases_offsets
|| !hlpfile
->phrases_buffer
)
2038 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_offsets
);
2039 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_buffer
);
2043 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
2045 hlpfile
->phrases_offsets
[0] = 0;
2046 for (i
= 0; i
< num
; i
++)
2048 for (n
= 1; getbit(); n
+= 1 << bc
);
2050 if (bc
> 1 && getbit()) n
+= 2;
2051 if (bc
> 2 && getbit()) n
+= 4;
2052 if (bc
> 3 && getbit()) n
+= 8;
2053 if (bc
> 4 && getbit()) n
+= 16;
2054 hlpfile
->phrases_offsets
[i
+ 1] = hlpfile
->phrases_offsets
[i
] + n
;
2058 if (dec_size
== cpr_size
)
2059 memcpy(hlpfile
->phrases_buffer
, buf_phs
+ 9, dec_size
);
2061 HLPFILE_UncompressLZ77(buf_phs
+ 9, end_phs
, (BYTE
*)hlpfile
->phrases_buffer
);
2063 hlpfile
->hasPhrases40
= TRUE
;
2067 /***********************************************************************
2069 * HLPFILE_Uncompress_Topic
2071 static BOOL
HLPFILE_Uncompress_Topic(HLPFILE
* hlpfile
)
2073 BYTE
*buf
, *ptr
, *end
, *newptr
;
2074 unsigned int i
, newsize
= 0;
2075 unsigned int topic_size
;
2077 if (!HLPFILE_FindSubFile(hlpfile
, "|TOPIC", &buf
, &end
))
2078 {WINE_WARN("topic0\n"); return FALSE
;}
2080 buf
+= 9; /* Skip file header */
2081 topic_size
= end
- buf
;
2082 if (hlpfile
->compressed
)
2084 hlpfile
->topic_maplen
= (topic_size
- 1) / hlpfile
->tbsize
+ 1;
2086 for (i
= 0; i
< hlpfile
->topic_maplen
; i
++)
2088 ptr
= buf
+ i
* hlpfile
->tbsize
;
2090 /* I don't know why, it's necessary for printman.hlp */
2091 if (ptr
+ 0x44 > end
) ptr
= end
- 0x44;
2093 newsize
+= HLPFILE_UncompressedLZ77_Size(ptr
+ 0xc, min(end
, ptr
+ hlpfile
->tbsize
));
2096 hlpfile
->topic_map
= HeapAlloc(GetProcessHeap(), 0,
2097 hlpfile
->topic_maplen
* sizeof(hlpfile
->topic_map
[0]) + newsize
);
2098 if (!hlpfile
->topic_map
) return FALSE
;
2099 newptr
= (BYTE
*)(hlpfile
->topic_map
+ hlpfile
->topic_maplen
);
2100 hlpfile
->topic_end
= newptr
+ newsize
;
2102 for (i
= 0; i
< hlpfile
->topic_maplen
; i
++)
2104 ptr
= buf
+ i
* hlpfile
->tbsize
;
2105 if (ptr
+ 0x44 > end
) ptr
= end
- 0x44;
2107 hlpfile
->topic_map
[i
] = newptr
;
2108 newptr
= HLPFILE_UncompressLZ77(ptr
+ 0xc, min(end
, ptr
+ hlpfile
->tbsize
), newptr
);
2113 /* basically, we need to copy the TopicBlockSize byte pages
2114 * (removing the first 0x0C) in one single area in memory
2116 hlpfile
->topic_maplen
= (topic_size
- 1) / hlpfile
->tbsize
+ 1;
2117 hlpfile
->topic_map
= HeapAlloc(GetProcessHeap(), 0,
2118 hlpfile
->topic_maplen
* (sizeof(hlpfile
->topic_map
[0]) + hlpfile
->dsize
));
2119 if (!hlpfile
->topic_map
) return FALSE
;
2120 newptr
= (BYTE
*)(hlpfile
->topic_map
+ hlpfile
->topic_maplen
);
2121 hlpfile
->topic_end
= newptr
+ topic_size
;
2123 for (i
= 0; i
< hlpfile
->topic_maplen
; i
++)
2125 hlpfile
->topic_map
[i
] = newptr
+ i
* hlpfile
->dsize
;
2126 memcpy(hlpfile
->topic_map
[i
], buf
+ i
* hlpfile
->tbsize
+ 0x0C, hlpfile
->dsize
);
2132 /***********************************************************************
2134 * HLPFILE_Uncompress2
2137 static void HLPFILE_Uncompress2(HLPFILE
* hlpfile
, const BYTE
*ptr
, const BYTE
*end
, BYTE
*newptr
, const BYTE
*newend
)
2139 BYTE
*phptr
, *phend
;
2143 while (ptr
< end
&& newptr
< newend
)
2145 if (!*ptr
|| *ptr
>= 0x10)
2149 code
= 0x100 * ptr
[0] + ptr
[1];
2150 index
= (code
- 0x100) / 2;
2152 phptr
= (BYTE
*)hlpfile
->phrases_buffer
+ hlpfile
->phrases_offsets
[index
];
2153 phend
= (BYTE
*)hlpfile
->phrases_buffer
+ hlpfile
->phrases_offsets
[index
+ 1];
2155 if (newptr
+ (phend
- phptr
) > newend
)
2157 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
2158 newptr
, newend
, (SIZE_T
)(phend
- phptr
));
2161 memcpy(newptr
, phptr
, phend
- phptr
);
2162 newptr
+= phend
- phptr
;
2163 if (code
& 1) *newptr
++ = ' ';
2168 if (newptr
> newend
) WINE_FIXME("buffer overflow %p > %p\n", newptr
, newend
);
2171 /******************************************************************
2172 * HLPFILE_Uncompress3
2176 static BOOL
HLPFILE_Uncompress3(HLPFILE
* hlpfile
, char* dst
, const char* dst_end
,
2177 const BYTE
* src
, const BYTE
* src_end
)
2179 unsigned int idx
, len
;
2181 for (; src
< src_end
; src
++)
2183 if ((*src
& 1) == 0)
2186 if (idx
> hlpfile
->num_phrases
)
2188 WINE_ERR("index in phrases %d/%d\n", idx
, hlpfile
->num_phrases
);
2193 len
= hlpfile
->phrases_offsets
[idx
+ 1] - hlpfile
->phrases_offsets
[idx
];
2194 if (dst
+ len
<= dst_end
)
2195 memcpy(dst
, &hlpfile
->phrases_buffer
[hlpfile
->phrases_offsets
[idx
]], len
);
2198 else if ((*src
& 0x03) == 0x01)
2200 idx
= (*src
+ 1) * 64;
2202 if (idx
> hlpfile
->num_phrases
)
2204 WINE_ERR("index in phrases %d/%d\n", idx
, hlpfile
->num_phrases
);
2209 len
= hlpfile
->phrases_offsets
[idx
+ 1] - hlpfile
->phrases_offsets
[idx
];
2210 if (dst
+ len
<= dst_end
)
2211 memcpy(dst
, &hlpfile
->phrases_buffer
[hlpfile
->phrases_offsets
[idx
]], len
);
2214 else if ((*src
& 0x07) == 0x03)
2216 len
= (*src
/ 8) + 1;
2217 if (dst
+ len
<= dst_end
)
2218 memcpy(dst
, src
+ 1, len
);
2223 len
= (*src
/ 16) + 1;
2224 if (dst
+ len
<= dst_end
)
2225 memset(dst
, ((*src
& 0x0F) == 0x07) ? ' ' : 0, len
);
2230 if (dst
> dst_end
) WINE_ERR("buffer overflow (%p > %p)\n", dst
, dst_end
);
2234 /******************************************************************
2235 * HLPFILE_UncompressRLE
2239 static void HLPFILE_UncompressRLE(const BYTE
* src
, const BYTE
* end
, BYTE
** dst
, unsigned dstsz
)
2242 BYTE
* sdst
= *dst
+ dstsz
;
2250 if ((*dst
) + ch
<= sdst
)
2251 memcpy(*dst
, src
, ch
);
2256 if ((*dst
) + ch
<= sdst
)
2257 memset(*dst
, (char)*src
, ch
);
2263 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
2264 (SIZE_T
)(*dst
- (sdst
- dstsz
)), dstsz
);
2267 /**************************************************************************
2268 * HLPFILE_BPTreeSearch
2270 * Searches for an element in B+ tree
2273 * buf [I] pointer to the embedded file structured as a B+ tree
2274 * key [I] pointer to data to find
2275 * comp [I] compare function
2278 * Pointer to block identified by key, or NULL if failure.
2281 void* HLPFILE_BPTreeSearch(BYTE
* buf
, const void* key
,
2282 HLPFILE_BPTreeCompare comp
)
2288 BYTE
*pages
, *ptr
, *newptr
;
2292 magic
= GET_USHORT(buf
, 9);
2293 if (magic
!= 0x293B)
2295 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic
);
2298 page_size
= GET_USHORT(buf
, 9+4);
2299 cur_page
= GET_USHORT(buf
, 9+26);
2300 level
= GET_USHORT(buf
, 9+32);
2301 pages
= buf
+ 9 + 38;
2304 ptr
= pages
+ cur_page
*page_size
;
2305 entries
= GET_SHORT(ptr
, 2);
2307 for (i
= 0; i
< entries
; i
++)
2309 if (comp(ptr
, key
, 0, (void **)&newptr
) > 0) break;
2312 cur_page
= GET_USHORT(ptr
-2, 0);
2314 ptr
= pages
+ cur_page
*page_size
;
2315 entries
= GET_SHORT(ptr
, 2);
2317 for (i
= 0; i
< entries
; i
++)
2319 ret
= comp(ptr
, key
, 1, (void **)&newptr
);
2320 if (ret
== 0) return ptr
;
2321 if (ret
> 0) return NULL
;
2327 /**************************************************************************
2328 * HLPFILE_BPTreeEnum
2330 * Enumerates elements in B+ tree.
2333 * buf [I] pointer to the embedded file structured as a B+ tree
2334 * cb [I] compare function
2335 * cookie [IO] cookie for cb function
2337 void HLPFILE_BPTreeEnum(BYTE
* buf
, HLPFILE_BPTreeCallback cb
, void* cookie
)
2343 BYTE
*pages
, *ptr
, *newptr
;
2346 magic
= GET_USHORT(buf
, 9);
2347 if (magic
!= 0x293B)
2349 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic
);
2352 page_size
= GET_USHORT(buf
, 9+4);
2353 cur_page
= GET_USHORT(buf
, 9+26);
2354 level
= GET_USHORT(buf
, 9+32);
2355 pages
= buf
+ 9 + 38;
2358 ptr
= pages
+ cur_page
*page_size
;
2359 cur_page
= GET_USHORT(ptr
, 4);
2361 while (cur_page
!= 0xFFFF)
2363 ptr
= pages
+ cur_page
*page_size
;
2364 entries
= GET_SHORT(ptr
, 2);
2366 for (i
= 0; i
< entries
; i
++)
2368 cb(ptr
, (void **)&newptr
, cookie
);
2371 cur_page
= GET_USHORT(pages
+cur_page
*page_size
, 6);
2376 /***********************************************************************
2378 * HLPFILE_GetContext
2380 static BOOL
HLPFILE_GetContext(HLPFILE
*hlpfile
)
2385 if (!HLPFILE_FindSubFile(hlpfile
, "|CONTEXT", &cbuf
, &cend
))
2386 {WINE_WARN("context0\n"); return FALSE
;}
2389 hlpfile
->Context
= HeapAlloc(GetProcessHeap(), 0, clen
);
2390 if (!hlpfile
->Context
) return FALSE
;
2391 memcpy(hlpfile
->Context
, cbuf
, clen
);
2396 /***********************************************************************
2398 * HLPFILE_GetKeywords
2400 static BOOL
HLPFILE_GetKeywords(HLPFILE
*hlpfile
)
2405 if (!HLPFILE_FindSubFile(hlpfile
, "|KWBTREE", &cbuf
, &cend
)) return FALSE
;
2407 hlpfile
->kwbtree
= HeapAlloc(GetProcessHeap(), 0, clen
);
2408 if (!hlpfile
->kwbtree
) return FALSE
;
2409 memcpy(hlpfile
->kwbtree
, cbuf
, clen
);
2411 if (!HLPFILE_FindSubFile(hlpfile
, "|KWDATA", &cbuf
, &cend
))
2413 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2414 HeapFree(GetProcessHeap(), 0, hlpfile
->kwbtree
);
2418 hlpfile
->kwdata
= HeapAlloc(GetProcessHeap(), 0, clen
);
2419 if (!hlpfile
->kwdata
)
2421 HeapFree(GetProcessHeap(), 0, hlpfile
->kwdata
);
2424 memcpy(hlpfile
->kwdata
, cbuf
, clen
);
2429 /***********************************************************************
2433 static BOOL
HLPFILE_GetMap(HLPFILE
*hlpfile
)
2436 unsigned entries
, i
;
2438 if (!HLPFILE_FindSubFile(hlpfile
, "|CTXOMAP", &cbuf
, &cend
))
2439 {WINE_WARN("no map section\n"); return FALSE
;}
2441 entries
= GET_USHORT(cbuf
, 9);
2442 hlpfile
->Map
= HeapAlloc(GetProcessHeap(), 0, entries
* sizeof(HLPFILE_MAP
));
2443 if (!hlpfile
->Map
) return FALSE
;
2444 hlpfile
->wMapLen
= entries
;
2445 for (i
= 0; i
< entries
; i
++)
2447 hlpfile
->Map
[i
].lMap
= GET_UINT(cbuf
+11,i
*8);
2448 hlpfile
->Map
[i
].offset
= GET_UINT(cbuf
+11,i
*8+4);
2453 /******************************************************************
2454 * HLPFILE_DeleteLink
2458 void HLPFILE_FreeLink(HLPFILE_LINK
* link
)
2460 if (link
&& !--link
->wRefCount
)
2462 HeapFree(GetProcessHeap(), 0, link
);
2466 /***********************************************************************
2468 * HLPFILE_DeleteParagraph
2470 static void HLPFILE_DeleteParagraph(HLPFILE_PARAGRAPH
* paragraph
)
2472 HLPFILE_PARAGRAPH
* next
;
2476 next
= paragraph
->next
;
2478 if (paragraph
->cookie
== para_metafile
)
2479 DeleteMetaFile(paragraph
->u
.gfx
.u
.mfp
.hMF
);
2481 HLPFILE_FreeLink(paragraph
->link
);
2483 HeapFree(GetProcessHeap(), 0, paragraph
);
2488 /***********************************************************************
2492 static void HLPFILE_DeleteMacro(HLPFILE_MACRO
* macro
)
2494 HLPFILE_MACRO
* next
;
2499 HeapFree(GetProcessHeap(), 0, macro
);
2504 /***********************************************************************
2508 static void HLPFILE_DeletePage(HLPFILE_PAGE
* page
)
2515 HLPFILE_DeleteParagraph(page
->first_paragraph
);
2516 HLPFILE_DeleteMacro(page
->first_macro
);
2517 HeapFree(GetProcessHeap(), 0, page
);
2522 /***********************************************************************
2524 * HLPFILE_FreeHlpFile
2526 void HLPFILE_FreeHlpFile(HLPFILE
* hlpfile
)
2530 if (!hlpfile
|| --hlpfile
->wRefCount
> 0) return;
2532 if (hlpfile
->next
) hlpfile
->next
->prev
= hlpfile
->prev
;
2533 if (hlpfile
->prev
) hlpfile
->prev
->next
= hlpfile
->next
;
2534 else first_hlpfile
= hlpfile
->next
;
2536 if (hlpfile
->numFonts
)
2538 for (i
= 0; i
< hlpfile
->numFonts
; i
++)
2540 DeleteObject(hlpfile
->fonts
[i
].hFont
);
2542 HeapFree(GetProcessHeap(), 0, hlpfile
->fonts
);
2545 if (hlpfile
->numBmps
)
2547 for (i
= 0; i
< hlpfile
->numBmps
; i
++)
2549 DeleteObject(hlpfile
->bmps
[i
]);
2551 HeapFree(GetProcessHeap(), 0, hlpfile
->bmps
);
2554 HLPFILE_DeletePage(hlpfile
->first_page
);
2555 HLPFILE_DeleteMacro(hlpfile
->first_macro
);
2557 DestroyIcon(hlpfile
->hIcon
);
2558 if (hlpfile
->numWindows
) HeapFree(GetProcessHeap(), 0, hlpfile
->windows
);
2559 HeapFree(GetProcessHeap(), 0, hlpfile
->Context
);
2560 HeapFree(GetProcessHeap(), 0, hlpfile
->Map
);
2561 HeapFree(GetProcessHeap(), 0, hlpfile
->lpszTitle
);
2562 HeapFree(GetProcessHeap(), 0, hlpfile
->lpszCopyright
);
2563 HeapFree(GetProcessHeap(), 0, hlpfile
->file_buffer
);
2564 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_offsets
);
2565 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_buffer
);
2566 HeapFree(GetProcessHeap(), 0, hlpfile
->topic_map
);
2567 HeapFree(GetProcessHeap(), 0, hlpfile
);