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
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(winhelp
);
38 static inline unsigned short GET_USHORT(const BYTE
* buffer
, unsigned i
)
40 return (BYTE
)buffer
[i
] + 0x100 * (BYTE
)buffer
[i
+ 1];
43 static inline short GET_SHORT(const BYTE
* buffer
, unsigned i
)
45 return (BYTE
)buffer
[i
] + 0x100 * (signed char)buffer
[i
+1];
48 static inline unsigned GET_UINT(const BYTE
* buffer
, unsigned i
)
50 return GET_USHORT(buffer
, i
) + 0x10000 * GET_USHORT(buffer
, i
+ 2);
53 static HLPFILE
*first_hlpfile
= 0;
56 /**************************************************************************
57 * HLPFILE_BPTreeSearch
59 * Searches for an element in B+ tree
62 * buf [I] pointer to the embedded file structured as a B+ tree
63 * key [I] pointer to data to find
64 * comp [I] compare function
67 * Pointer to block identified by key, or NULL if failure.
70 static void* HLPFILE_BPTreeSearch(BYTE
* buf
, const void* key
,
71 HLPFILE_BPTreeCompare comp
)
77 BYTE
*pages
, *ptr
, *newptr
;
81 magic
= GET_USHORT(buf
, 9);
84 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic
);
87 page_size
= GET_USHORT(buf
, 9+4);
88 cur_page
= GET_USHORT(buf
, 9+26);
89 level
= GET_USHORT(buf
, 9+32);
93 ptr
= pages
+ cur_page
*page_size
;
94 entries
= GET_SHORT(ptr
, 2);
96 for (i
= 0; i
< entries
; i
++)
98 if (comp(ptr
, key
, 0, (void **)&newptr
) > 0) break;
101 cur_page
= GET_USHORT(ptr
-2, 0);
103 ptr
= pages
+ cur_page
*page_size
;
104 entries
= GET_SHORT(ptr
, 2);
106 for (i
= 0; i
< entries
; i
++)
108 ret
= comp(ptr
, key
, 1, (void **)&newptr
);
109 if (ret
== 0) return ptr
;
110 if (ret
> 0) return NULL
;
116 /**************************************************************************
119 * Enumerates elements in B+ tree.
122 * buf [I] pointer to the embedded file structured as a B+ tree
123 * cb [I] compare function
124 * cookie [IO] cookie for cb function
126 void HLPFILE_BPTreeEnum(BYTE
* buf
, HLPFILE_BPTreeCallback cb
, void* cookie
)
132 BYTE
*pages
, *ptr
, *newptr
;
135 magic
= GET_USHORT(buf
, 9);
138 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic
);
141 page_size
= GET_USHORT(buf
, 9+4);
142 cur_page
= GET_USHORT(buf
, 9+26);
143 level
= GET_USHORT(buf
, 9+32);
144 pages
= buf
+ 9 + 38;
147 ptr
= pages
+ cur_page
*page_size
;
148 cur_page
= GET_USHORT(ptr
, 4);
150 while (cur_page
!= 0xFFFF)
152 ptr
= pages
+ cur_page
*page_size
;
153 entries
= GET_SHORT(ptr
, 2);
155 for (i
= 0; i
< entries
; i
++)
157 cb(ptr
, (void **)&newptr
, cookie
);
160 cur_page
= GET_USHORT(pages
+cur_page
*page_size
, 6);
165 /***********************************************************************
167 * HLPFILE_UncompressedLZ77_Size
169 static INT
HLPFILE_UncompressedLZ77_Size(const BYTE
*ptr
, const BYTE
*end
)
176 for (i
= 0; i
< 8 && ptr
< end
; i
++, mask
>>= 1)
180 int code
= GET_USHORT(ptr
, 0);
181 int len
= 3 + (code
>> 12);
185 else newsize
++, ptr
++;
192 /***********************************************************************
194 * HLPFILE_UncompressLZ77
196 static BYTE
*HLPFILE_UncompressLZ77(const BYTE
*ptr
, const BYTE
*end
, BYTE
*newptr
)
203 for (i
= 0; i
< 8 && ptr
< end
; i
++, mask
>>= 1)
207 int code
= GET_USHORT(ptr
, 0);
208 int len
= 3 + (code
>> 12);
209 int offset
= code
& 0xfff;
211 * We must copy byte-by-byte here. We cannot use memcpy nor
212 * memmove here. Just example:
213 * a[]={1,2,3,4,5,6,7,8,9,10}
217 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
219 for (; len
>0; len
--, newptr
++) *newptr
= *(newptr
-offset
-1);
222 else *newptr
++ = *ptr
++;
229 /***********************************************************************
231 * HLPFILE_Uncompress2
234 static void HLPFILE_Uncompress2(HLPFILE
* hlpfile
, const BYTE
*ptr
, const BYTE
*end
, BYTE
*newptr
, const BYTE
*newend
)
240 while (ptr
< end
&& newptr
< newend
)
242 if (!*ptr
|| *ptr
>= 0x10)
246 code
= 0x100 * ptr
[0] + ptr
[1];
247 index
= (code
- 0x100) / 2;
249 phptr
= (BYTE
*)hlpfile
->phrases_buffer
+ hlpfile
->phrases_offsets
[index
];
250 phend
= (BYTE
*)hlpfile
->phrases_buffer
+ hlpfile
->phrases_offsets
[index
+ 1];
252 if (newptr
+ (phend
- phptr
) > newend
)
254 WINE_FIXME("buffer overflow %p > %p for %Iu bytes\n",
255 newptr
, newend
, (SIZE_T
)(phend
- phptr
));
258 memcpy(newptr
, phptr
, phend
- phptr
);
259 newptr
+= phend
- phptr
;
260 if (code
& 1) *newptr
++ = ' ';
265 if (newptr
> newend
) WINE_FIXME("buffer overflow %p > %p\n", newptr
, newend
);
268 /******************************************************************
269 * HLPFILE_Uncompress3
273 static BOOL
HLPFILE_Uncompress3(HLPFILE
* hlpfile
, char* dst
, const char* dst_end
,
274 const BYTE
* src
, const BYTE
* src_end
)
276 unsigned int idx
, len
;
278 for (; src
< src_end
; src
++)
283 if (idx
> hlpfile
->num_phrases
)
285 WINE_ERR("index in phrases %d/%d\n", idx
, hlpfile
->num_phrases
);
290 len
= hlpfile
->phrases_offsets
[idx
+ 1] - hlpfile
->phrases_offsets
[idx
];
291 if (dst
+ len
<= dst_end
)
292 memcpy(dst
, &hlpfile
->phrases_buffer
[hlpfile
->phrases_offsets
[idx
]], len
);
295 else if ((*src
& 0x03) == 0x01)
297 idx
= (*src
+ 1) * 64;
299 if (idx
> hlpfile
->num_phrases
)
301 WINE_ERR("index in phrases %d/%d\n", idx
, hlpfile
->num_phrases
);
306 len
= hlpfile
->phrases_offsets
[idx
+ 1] - hlpfile
->phrases_offsets
[idx
];
307 if (dst
+ len
<= dst_end
)
308 memcpy(dst
, &hlpfile
->phrases_buffer
[hlpfile
->phrases_offsets
[idx
]], len
);
311 else if ((*src
& 0x07) == 0x03)
313 len
= (*src
/ 8) + 1;
314 if (dst
+ len
<= dst_end
)
315 memcpy(dst
, src
+ 1, len
);
320 len
= (*src
/ 16) + 1;
321 if (dst
+ len
<= dst_end
)
322 memset(dst
, ((*src
& 0x0F) == 0x07) ? ' ' : 0, len
);
327 if (dst
> dst_end
) WINE_ERR("buffer overflow (%p > %p)\n", dst
, dst_end
);
331 /******************************************************************
332 * HLPFILE_UncompressRLE
336 static void HLPFILE_UncompressRLE(const BYTE
* src
, const BYTE
* end
, BYTE
* dst
, unsigned dstsz
)
339 BYTE
* sdst
= dst
+ dstsz
;
347 if (dst
+ ch
<= sdst
)
348 memcpy(dst
, src
, ch
);
353 if (dst
+ ch
<= sdst
)
354 memset(dst
, (char)*src
, ch
);
360 WINE_WARN("Buffer X-flow: d(%Iu) instead of d(%u)\n",
361 (SIZE_T
)(dst
- (sdst
- dstsz
)), dstsz
);
365 /******************************************************************
366 * HLPFILE_PageByOffset
370 HLPFILE_PAGE
*HLPFILE_PageByOffset(HLPFILE
* hlpfile
, LONG offset
, ULONG
* relative
)
375 if (!hlpfile
) return 0;
377 WINE_TRACE("<%s>[%lx]\n", debugstr_a(hlpfile
->lpszPath
), offset
);
379 if (offset
== 0xFFFFFFFF) return NULL
;
382 for (found
= NULL
, page
= hlpfile
->first_page
; page
; page
= page
->next
)
384 if (page
->offset
<= offset
&& (!found
|| found
->offset
< page
->offset
))
386 *relative
= offset
- page
->offset
;
391 WINE_ERR("Page of offset %lu not found in file %s\n",
392 offset
, debugstr_a(hlpfile
->lpszPath
));
396 /***********************************************************************
400 static HLPFILE_PAGE
* HLPFILE_Contents(HLPFILE
*hlpfile
, ULONG
* relative
)
402 HLPFILE_PAGE
* page
= NULL
;
404 if (!hlpfile
) return NULL
;
406 page
= HLPFILE_PageByOffset(hlpfile
, hlpfile
->contents_start
, relative
);
409 page
= hlpfile
->first_page
;
415 /**************************************************************************
418 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
421 static int comp_PageByHash(void *p
, const void *key
,
422 int leaf
, void** next
)
424 LONG lKey
= (LONG_PTR
)key
;
425 LONG lTest
= (INT
)GET_UINT(p
, 0);
427 *next
= (char *)p
+(leaf
?8:6);
428 WINE_TRACE("Comparing '%ld' with '%ld'\n", lKey
, lTest
);
429 if (lTest
< lKey
) return -1;
430 if (lTest
> lKey
) return 1;
434 /***********************************************************************
438 HLPFILE_PAGE
*HLPFILE_PageByHash(HLPFILE
* hlpfile
, LONG lHash
, ULONG
* relative
)
442 if (!hlpfile
) return NULL
;
443 if (!lHash
) return HLPFILE_Contents(hlpfile
, relative
);
445 WINE_TRACE("<%s>[%lx]\n", debugstr_a(hlpfile
->lpszPath
), lHash
);
447 /* For win 3.0 files hash values are really page numbers */
448 if (hlpfile
->version
<= 16)
450 if (lHash
>= hlpfile
->wTOMapLen
) return NULL
;
451 return HLPFILE_PageByOffset(hlpfile
, hlpfile
->TOMap
[lHash
], relative
);
454 ptr
= HLPFILE_BPTreeSearch(hlpfile
->Context
, LongToPtr(lHash
), comp_PageByHash
);
457 WINE_ERR("Page of hash %lx not found in file %s\n", lHash
, debugstr_a(hlpfile
->lpszPath
));
461 return HLPFILE_PageByOffset(hlpfile
, GET_UINT(ptr
, 4), relative
);
464 /***********************************************************************
468 HLPFILE_PAGE
*HLPFILE_PageByMap(HLPFILE
* hlpfile
, LONG lMap
, ULONG
* relative
)
472 if (!hlpfile
) return 0;
474 WINE_TRACE("<%s>[%lx]\n", debugstr_a(hlpfile
->lpszPath
), lMap
);
476 for (i
= 0; i
< hlpfile
->wMapLen
; i
++)
478 if (hlpfile
->Map
[i
].lMap
== lMap
)
479 return HLPFILE_PageByOffset(hlpfile
, hlpfile
->Map
[i
].offset
, relative
);
482 WINE_ERR("Page of Map %lx not found in file %s\n", lMap
, debugstr_a(hlpfile
->lpszPath
));
486 /**************************************************************************
489 * HLPFILE_BPTreeCompare function for HLPFILE directory.
492 static int comp_FindSubFile(void *p
, const void *key
,
493 int leaf
, void** next
)
495 *next
= (char *)p
+strlen(p
)+(leaf
?5:3);
496 WINE_TRACE("Comparing %s with %s\n", debugstr_a((char *)p
), debugstr_a((const char *)key
));
497 return strcmp(p
, key
);
500 /***********************************************************************
502 * HLPFILE_FindSubFile
504 static BOOL
HLPFILE_FindSubFile(HLPFILE
* hlpfile
, LPCSTR name
, BYTE
**subbuf
, BYTE
**subend
)
508 WINE_TRACE("looking for file %s\n", debugstr_a(name
));
509 ptr
= HLPFILE_BPTreeSearch(hlpfile
->file_buffer
+ GET_UINT(hlpfile
->file_buffer
, 4),
510 name
, comp_FindSubFile
);
512 { /* Subfiles with bitmap images are usually prefixed with '|', but sometimes not.
513 Unfortunately, there is no consensus among different pieces of unofficial
514 documentation. So remove leading '|' and try again. */
518 WINE_TRACE("not found. try %s\n", debugstr_a(name
));
519 ptr
= HLPFILE_BPTreeSearch(hlpfile
->file_buffer
+ GET_UINT(hlpfile
->file_buffer
, 4),
520 name
, comp_FindSubFile
);
523 if (!ptr
) return FALSE
;
524 *subbuf
= hlpfile
->file_buffer
+ GET_UINT(ptr
, strlen(name
)+1);
525 if (*subbuf
>= hlpfile
->file_buffer
+ hlpfile
->file_buffer_size
)
527 WINE_ERR("internal file %s does not fit\n", debugstr_a(name
));
530 *subend
= *subbuf
+ GET_UINT(*subbuf
, 0);
531 if (*subend
> hlpfile
->file_buffer
+ hlpfile
->file_buffer_size
)
533 WINE_ERR("internal file %s does not fit\n", debugstr_a(name
));
536 if (GET_UINT(*subbuf
, 0) < GET_UINT(*subbuf
, 4) + 9)
538 WINE_ERR("invalid size provided for internal file %s\n", debugstr_a(name
));
544 /***********************************************************************
548 LONG
HLPFILE_Hash(LPCSTR lpszContext
)
553 while ((c
= *lpszContext
++))
556 if (c
>= 'A' && c
<= 'Z') x
= c
- 'A' + 17;
557 if (c
>= 'a' && c
<= 'z') x
= c
- 'a' + 17;
558 if (c
>= '1' && c
<= '9') x
= c
- '0';
559 if (c
== '0') x
= 10;
560 if (c
== '.') x
= 12;
561 if (c
== '_') x
= 13;
562 if (x
) lHash
= lHash
* 43 + x
;
567 static LONG
fetch_long(const BYTE
** ptr
)
573 ret
= (*(const ULONG
*)(*ptr
) - 0x80000000) / 2;
578 ret
= (*(const USHORT
*)(*ptr
) - 0x8000) / 2;
585 static ULONG
fetch_ulong(const BYTE
** ptr
)
591 ret
= *(const ULONG
*)(*ptr
) / 2;
596 ret
= *(const USHORT
*)(*ptr
) / 2;
602 static short fetch_short(const BYTE
** ptr
)
608 ret
= (*(const unsigned short*)(*ptr
) - 0x8000) / 2;
613 ret
= (*(const unsigned char*)(*ptr
) - 0x80) / 2;
619 static unsigned short fetch_ushort(const BYTE
** ptr
)
625 ret
= *(const unsigned short*)(*ptr
) / 2;
630 ret
= *(const unsigned char*)(*ptr
) / 2;
636 /******************************************************************
637 * HLPFILE_DecompressGfx
639 * Decompress the data part of a bitmap or a metafile
641 static const BYTE
* HLPFILE_DecompressGfx(const BYTE
* src
, unsigned csz
, unsigned sz
, BYTE packing
,
648 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing
, csz
, sz
);
652 case 0: /* uncompressed */
654 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz
, csz
);
659 dst
= *alloc
= malloc(sz
);
660 if (!dst
) return NULL
;
661 HLPFILE_UncompressRLE(src
, src
+ csz
, *alloc
, sz
);
664 sz77
= HLPFILE_UncompressedLZ77_Size(src
, src
+ csz
);
665 dst
= *alloc
= malloc(sz77
);
666 if (!dst
) return NULL
;
667 HLPFILE_UncompressLZ77(src
, src
+ csz
, *alloc
);
669 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77
, sz
);
671 case 3: /* LZ77 then RLE */
672 sz77
= HLPFILE_UncompressedLZ77_Size(src
, src
+ csz
);
674 if (!tmp
) return FALSE
;
675 HLPFILE_UncompressLZ77(src
, src
+ csz
, tmp
);
676 dst
= *alloc
= malloc(sz
);
682 HLPFILE_UncompressRLE(tmp
, tmp
+ sz77
, *alloc
, sz
);
686 WINE_FIXME("Unsupported packing %u\n", packing
);
692 static BOOL
HLPFILE_RtfAddRawString(struct RtfData
* rd
, const char* str
, size_t sz
)
694 if (rd
->ptr
+ sz
>= rd
->data
+ rd
->allocated
)
696 char* new = realloc(rd
->data
, rd
->allocated
* 2);
697 if (!new) return FALSE
;
698 rd
->ptr
= new + (rd
->ptr
- rd
->data
);
702 memcpy(rd
->ptr
, str
, sz
);
708 static BOOL
HLPFILE_RtfAddControl(struct RtfData
* rd
, const char* str
)
710 WINE_TRACE("%s\n", debugstr_a(str
));
711 if (*str
== '\\' || *str
== '{') rd
->in_text
= FALSE
;
712 else if (*str
== '}') rd
->in_text
= TRUE
;
713 return HLPFILE_RtfAddRawString(rd
, str
, strlen(str
));
716 static BOOL
HLPFILE_RtfAddText(struct RtfData
* rd
, const char* str
)
725 if (!HLPFILE_RtfAddRawString(rd
, " ", 1)) return FALSE
;
728 for (last
= p
= str
; *p
; p
++)
730 if (*p
& 0x80) /* escape non-ASCII chars */
733 rlen
= sprintf(xx
, "\\'%x", *(const BYTE
*)p
);
738 case '{': rlen
= 2; replace
= "\\{"; break;
739 case '}': rlen
= 2; replace
= "\\}"; break;
740 case '\\': rlen
= 2; replace
= "\\\\"; break;
743 if ((p
!= last
&& !HLPFILE_RtfAddRawString(rd
, last
, p
- last
)) ||
744 !HLPFILE_RtfAddRawString(rd
, replace
, rlen
)) return FALSE
;
747 return HLPFILE_RtfAddRawString(rd
, last
, p
- last
);
750 /******************************************************************
754 static BOOL
HLPFILE_RtfAddHexBytes(struct RtfData
* rd
, const void* _ptr
, unsigned sz
)
758 const BYTE
* ptr
= _ptr
;
759 static const char* _2hex
= "0123456789abcdef";
763 if (!HLPFILE_RtfAddRawString(rd
, " ", 1)) return FALSE
;
766 for (; sz
; sz
-= step
)
769 for (i
= 0; i
< step
; i
++)
771 tmp
[2 * i
+ 0] = _2hex
[*ptr
>> 4];
772 tmp
[2 * i
+ 1] = _2hex
[*ptr
++ & 0xF];
774 if (!HLPFILE_RtfAddRawString(rd
, tmp
, 2 * step
)) return FALSE
;
779 static HLPFILE_LINK
* HLPFILE_AllocLink(struct RtfData
* rd
, int cookie
,
780 const char* str
, unsigned len
, LONG hash
,
781 BOOL clrChange
, BOOL bHotSpot
, unsigned wnd
);
783 /******************************************************************
784 * HLPFILE_AddHotSpotLinks
787 static void HLPFILE_AddHotSpotLinks(struct RtfData
* rd
, HLPFILE
* file
,
788 const BYTE
* start
, ULONG hs_size
, ULONG hs_offset
)
794 if (hs_size
== 0 || hs_offset
== 0) return;
798 hs_num
= GET_USHORT(start
, 1);
799 hs_macro
= GET_UINT(start
, 3);
801 str
= (const char*)start
+ 7 + 15 * hs_num
+ hs_macro
;
802 /* FIXME: should use hs_size to prevent out of bounds reads */
803 for (i
= 0; i
< hs_num
; i
++)
805 HLPFILE_HOTSPOTLINK
* hslink
;
807 WINE_TRACE("%02x-%02x%02x {%s,%s}\n",
808 start
[7 + 15 * i
+ 0], start
[7 + 15 * i
+ 1], start
[7 + 15 * i
+ 2],
809 debugstr_a(str
), debugstr_a(str
+ strlen(str
) + 1));
810 /* str points to two null terminated strings:
811 * hotspot name, then link name
813 str
+= strlen(str
) + 1; /* skip hotspot name */
816 switch (start
[7 + 15 * i
+ 0])
817 /* The next two chars always look like 0x04 0x00 ???
818 * What are they for ?
822 hslink
= (HLPFILE_HOTSPOTLINK
*)
823 HLPFILE_AllocLink(rd
, hlp_link_macro
, str
, -1, 0, FALSE
, TRUE
, -1);
828 hslink
= (HLPFILE_HOTSPOTLINK
*)
829 HLPFILE_AllocLink(rd
, (start
[7 + 15 * i
+ 0] & 1) ? hlp_link_link
: hlp_link_popup
,
830 file
->lpszPath
, -1, HLPFILE_Hash(str
),
837 const char* win
= strchr(str
, '>');
843 for (wnd
= file
->numWindows
- 1; wnd
>= 0; wnd
--)
845 if (!strcmp(win
+ 1, file
->windows
[wnd
].name
)) break;
848 WINE_WARN("Couldn't find window info for %s\n", debugstr_a(win
));
849 if ((tgt
= malloc(win
- str
+ 1)))
851 memcpy(tgt
, str
, win
- str
);
852 tgt
[win
- str
] = '\0';
855 hslink
= (HLPFILE_HOTSPOTLINK
*)
856 HLPFILE_AllocLink(rd
, (start
[7 + 15 * i
+ 0] & 1) ? hlp_link_link
: hlp_link_popup
,
857 file
->lpszPath
, -1, HLPFILE_Hash(tgt
? tgt
: str
), FALSE
, TRUE
, wnd
);
862 WINE_FIXME("unknown hotsport target 0x%x\n", start
[7 + 15 * i
+ 0]);
866 hslink
->x
= GET_USHORT(start
, 7 + 15 * i
+ 3);
867 hslink
->y
= GET_USHORT(start
, 7 + 15 * i
+ 5);
868 hslink
->width
= GET_USHORT(start
, 7 + 15 * i
+ 7);
869 hslink
->height
= GET_USHORT(start
, 7 + 15 * i
+ 9);
870 /* target = GET_UINT(start, 7 + 15 * i + 11); */
872 str
+= strlen(str
) + 1;
876 /******************************************************************
877 * HLPFILE_RtfAddTransparentBitmap
879 * We'll transform a transparent bitmap into an metafile that
880 * we then transform into RTF
882 static BOOL
HLPFILE_RtfAddTransparentBitmap(struct RtfData
* rd
, const BITMAPINFO
* bi
,
883 const void* pict
, unsigned nc
)
885 HDC hdc
, hdcMask
, hdcMem
, hdcEMF
;
886 HBITMAP hbm
, hbmMask
, hbmOldMask
, hbmOldMem
;
892 hbm
= CreateDIBitmap(hdc
= GetDC(0), &bi
->bmiHeader
,
893 CBM_INIT
, pict
, bi
, DIB_RGB_COLORS
);
895 hdcMem
= CreateCompatibleDC(hdc
);
896 hbmOldMem
= SelectObject(hdcMem
, hbm
);
898 /* create the mask bitmap from the main bitmap */
899 hdcMask
= CreateCompatibleDC(hdc
);
900 hbmMask
= CreateBitmap(bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
, 1, 1, NULL
);
901 hbmOldMask
= SelectObject(hdcMask
, hbmMask
);
903 RGB(bi
->bmiColors
[nc
- 1].rgbRed
,
904 bi
->bmiColors
[nc
- 1].rgbGreen
,
905 bi
->bmiColors
[nc
- 1].rgbBlue
));
906 BitBlt(hdcMask
, 0, 0, bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
, hdcMem
, 0, 0, SRCCOPY
);
908 /* sets to RGB(0,0,0) the transparent bits in main bitmap */
909 SetBkColor(hdcMem
, RGB(0,0,0));
910 SetTextColor(hdcMem
, RGB(255,255,255));
911 BitBlt(hdcMem
, 0, 0, bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
, hdcMask
, 0, 0, SRCAND
);
913 SelectObject(hdcMask
, hbmOldMask
);
916 SelectObject(hdcMem
, hbmOldMem
);
919 /* we create the bitmap on the fly */
920 hdcEMF
= CreateEnhMetaFileW(NULL
, NULL
, NULL
, NULL
);
921 hdcMem
= CreateCompatibleDC(hdcEMF
);
923 /* sets to RGB(0,0,0) the transparent bits in final bitmap */
924 hbmOldMem
= SelectObject(hdcMem
, hbmMask
);
925 SetBkColor(hdcEMF
, RGB(255, 255, 255));
926 SetTextColor(hdcEMF
, RGB(0, 0, 0));
927 BitBlt(hdcEMF
, 0, 0, bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
, hdcMem
, 0, 0, SRCAND
);
929 /* and copy the remaining bits of main bitmap */
930 SelectObject(hdcMem
, hbm
);
931 BitBlt(hdcEMF
, 0, 0, bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
, hdcMem
, 0, 0, SRCPAINT
);
932 SelectObject(hdcMem
, hbmOldMem
);
937 DeleteObject(hbmMask
);
940 hEMF
= CloseEnhMetaFile(hdcEMF
);
942 /* generate rtf stream */
943 sz
= GetEnhMetaFileBits(hEMF
, 0, NULL
);
944 if (sz
&& (data
= malloc(sz
)))
946 if (sz
== GetEnhMetaFileBits(hEMF
, sz
, data
))
948 ret
= HLPFILE_RtfAddControl(rd
, "{\\pict\\emfblip") &&
949 HLPFILE_RtfAddHexBytes(rd
, data
, sz
) &&
950 HLPFILE_RtfAddControl(rd
, "}");
954 DeleteEnhMetaFile(hEMF
);
959 /******************************************************************
960 * HLPFILE_RtfAddBitmap
963 static BOOL
HLPFILE_RtfAddBitmap(struct RtfData
* rd
, HLPFILE
* file
, const BYTE
* beg
, BYTE type
, BYTE pack
)
966 const BYTE
* pict_beg
;
972 BOOL clrImportant
= FALSE
;
975 unsigned hs_size
, hs_offset
;
977 bi
= malloc(sizeof(*bi
));
978 if (!bi
) return FALSE
;
980 ptr
= beg
+ 2; /* for type and pack */
982 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
983 bi
->bmiHeader
.biXPelsPerMeter
= fetch_ulong(&ptr
);
984 bi
->bmiHeader
.biYPelsPerMeter
= fetch_ulong(&ptr
);
985 bi
->bmiHeader
.biPlanes
= fetch_ushort(&ptr
);
986 bi
->bmiHeader
.biBitCount
= fetch_ushort(&ptr
);
987 bi
->bmiHeader
.biWidth
= fetch_ulong(&ptr
);
988 bi
->bmiHeader
.biHeight
= fetch_ulong(&ptr
);
989 bi
->bmiHeader
.biClrUsed
= fetch_ulong(&ptr
);
990 clrImportant
= fetch_ulong(&ptr
);
991 bi
->bmiHeader
.biClrImportant
= (clrImportant
> 1) ? clrImportant
: 0;
992 bi
->bmiHeader
.biCompression
= BI_RGB
;
993 if (bi
->bmiHeader
.biBitCount
> 32) WINE_FIXME("Unknown bit count %u\n", bi
->bmiHeader
.biBitCount
);
994 if (bi
->bmiHeader
.biPlanes
!= 1) WINE_FIXME("Unsupported planes %u\n", bi
->bmiHeader
.biPlanes
);
995 bi
->bmiHeader
.biSizeImage
= (((bi
->bmiHeader
.biWidth
* bi
->bmiHeader
.biBitCount
+ 31) & ~31) / 8) * bi
->bmiHeader
.biHeight
;
996 WINE_TRACE("planes=%d bc=%d size=(%ld,%ld)\n",
997 bi
->bmiHeader
.biPlanes
, bi
->bmiHeader
.biBitCount
,
998 bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
);
1000 csz
= fetch_ulong(&ptr
);
1001 hs_size
= fetch_ulong(&ptr
);
1003 off
= GET_UINT(ptr
, 0); ptr
+= 4;
1004 hs_offset
= GET_UINT(ptr
, 0); ptr
+= 4;
1005 HLPFILE_AddHotSpotLinks(rd
, file
, beg
, hs_size
, hs_offset
);
1007 /* now read palette info */
1012 nc
= bi
->bmiHeader
.biClrUsed
;
1013 /* not quite right, especially for bitfields type of compression */
1014 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
1015 nc
= 1 << bi
->bmiHeader
.biBitCount
;
1017 new_bi
= realloc(bi
, sizeof(*bi
) + nc
* sizeof(RGBQUAD
));
1024 for (i
= 0; i
< nc
; i
++)
1026 bi
->bmiColors
[i
].rgbBlue
= ptr
[0];
1027 bi
->bmiColors
[i
].rgbGreen
= ptr
[1];
1028 bi
->bmiColors
[i
].rgbRed
= ptr
[2];
1029 bi
->bmiColors
[i
].rgbReserved
= 0;
1033 pict_beg
= HLPFILE_DecompressGfx(beg
+ off
, csz
, bi
->bmiHeader
.biSizeImage
, pack
, &alloc
);
1035 if (clrImportant
== 1 && nc
> 0)
1037 ret
= HLPFILE_RtfAddTransparentBitmap(rd
, bi
, pict_beg
, nc
);
1040 if (!HLPFILE_RtfAddControl(rd
, "{\\pict")) goto done
;
1043 sprintf(tmp
, "\\dibitmap0\\picw%ld\\pich%ld",
1044 bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
);
1045 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1046 if (!HLPFILE_RtfAddHexBytes(rd
, bi
, sizeof(*bi
) + nc
* sizeof(RGBQUAD
))) goto done
;
1050 sprintf(tmp
, "\\wbitmap0\\wbmbitspixel%d\\wbmplanes%d\\picw%ld\\pich%ld",
1051 bi
->bmiHeader
.biBitCount
, bi
->bmiHeader
.biPlanes
,
1052 bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
);
1053 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1055 if (!HLPFILE_RtfAddHexBytes(rd
, pict_beg
, bi
->bmiHeader
.biSizeImage
)) goto done
;
1056 if (!HLPFILE_RtfAddControl(rd
, "}")) goto done
;
1066 /******************************************************************
1067 * HLPFILE_RtfAddMetaFile
1070 static BOOL
HLPFILE_RtfAddMetaFile(struct RtfData
* rd
, HLPFILE
* file
, const BYTE
* beg
, BYTE pack
)
1072 ULONG size
, csize
, off
, hs_offset
, hs_size
;
1080 WINE_TRACE("Loading metafile\n");
1082 ptr
= beg
+ 2; /* for type and pack */
1084 mm
= fetch_ushort(&ptr
); /* mapping mode */
1085 sprintf(tmp
, "{\\pict\\wmetafile%d\\picw%d\\pich%d",
1086 mm
, GET_USHORT(ptr
, 0), GET_USHORT(ptr
, 2));
1087 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1090 size
= fetch_ulong(&ptr
); /* decompressed size */
1091 csize
= fetch_ulong(&ptr
); /* compressed size */
1092 hs_size
= fetch_ulong(&ptr
); /* hotspot size */
1093 off
= GET_UINT(ptr
, 0);
1094 hs_offset
= GET_UINT(ptr
, 4);
1097 HLPFILE_AddHotSpotLinks(rd
, file
, beg
, hs_size
, hs_offset
);
1099 WINE_TRACE("sz=%lu csz=%lu offs=%lu/%lu,%lu/%lu\n",
1100 size
, csize
, off
, (ULONG
)(ptr
- beg
), hs_size
, hs_offset
);
1102 bits
= HLPFILE_DecompressGfx(beg
+ off
, csize
, size
, pack
, &alloc
);
1103 if (!bits
) return FALSE
;
1105 ret
= HLPFILE_RtfAddHexBytes(rd
, bits
, size
) &&
1106 HLPFILE_RtfAddControl(rd
, "}");
1113 /******************************************************************
1114 * HLPFILE_RtfAddGfxByAddr
1117 static BOOL
HLPFILE_RtfAddGfxByAddr(struct RtfData
* rd
, HLPFILE
*hlpfile
,
1118 const BYTE
* ref
, ULONG size
)
1120 unsigned i
, numpict
;
1122 numpict
= GET_USHORT(ref
, 2);
1123 WINE_TRACE("Got picture magic=%04x #=%d\n", GET_USHORT(ref
, 0), numpict
);
1125 for (i
= 0; i
< numpict
; i
++)
1131 WINE_TRACE("Offset[%d] = %x\n", i
, GET_UINT(ref
, (1 + i
) * 4));
1132 beg
= ptr
= ref
+ GET_UINT(ref
, (1 + i
) * 4);
1139 case 5: /* device dependent bmp */
1140 case 6: /* device independent bmp */
1141 HLPFILE_RtfAddBitmap(rd
, hlpfile
, beg
, type
, pack
);
1144 HLPFILE_RtfAddMetaFile(rd
, hlpfile
, beg
, pack
);
1146 default: WINE_FIXME("Unknown type %u\n", type
); return FALSE
;
1149 /* FIXME: hotspots */
1151 /* FIXME: implement support for multiple picture format */
1152 if (numpict
!= 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
1158 /******************************************************************
1159 * HLPFILE_RtfAddGfxByIndex
1163 static BOOL
HLPFILE_RtfAddGfxByIndex(struct RtfData
* rd
, HLPFILE
*hlpfile
,
1169 WINE_TRACE("Loading picture #%d\n", index
);
1171 sprintf(tmp
, "|bm%u", index
);
1173 if (!HLPFILE_FindSubFile(hlpfile
, tmp
, &ref
, &end
)) {WINE_WARN("no sub file\n"); return FALSE
;}
1176 return HLPFILE_RtfAddGfxByAddr(rd
, hlpfile
, ref
, end
- ref
);
1179 /******************************************************************
1184 static HLPFILE_LINK
* HLPFILE_AllocLink(struct RtfData
* rd
, int cookie
,
1185 const char* str
, unsigned len
, LONG hash
,
1186 BOOL clrChange
, BOOL bHotSpot
, unsigned wnd
)
1190 unsigned asz
= bHotSpot
? sizeof(HLPFILE_HOTSPOTLINK
) : sizeof(HLPFILE_LINK
);
1192 /* FIXME: should build a string table for the attributes.link.lpszPath
1193 * they are reallocated for each link
1195 if (len
== -1) len
= strlen(str
);
1196 link
= malloc(asz
+ len
+ 1);
1197 if (!link
) return NULL
;
1199 link
->cookie
= cookie
;
1200 link
->string
= link_str
= (char*)link
+ asz
;
1201 memcpy(link_str
, str
, len
);
1202 link_str
[len
] = '\0';
1204 link
->bClrChange
= clrChange
;
1205 link
->bHotSpot
= bHotSpot
;
1207 link
->next
= rd
->first_link
;
1208 rd
->first_link
= link
;
1209 link
->cpMin
= rd
->char_pos
;
1210 rd
->force_color
= clrChange
;
1211 if (rd
->current_link
) WINE_FIXME("Pending link\n");
1213 link
->cpMax
= rd
->char_pos
;
1215 rd
->current_link
= link
;
1217 WINE_TRACE("Link[%d] to %s@%08lx:%d\n",
1218 link
->cookie
, debugstr_a(link
->string
), link
->hash
, link
->window
);
1222 static unsigned HLPFILE_HalfPointsScale(HLPFILE_PAGE
* page
, unsigned pts
)
1224 return pts
* page
->file
->scale
- page
->file
->rounderr
;
1227 /***********************************************************************
1229 * HLPFILE_BrowseParagraph
1231 static BOOL
HLPFILE_BrowseParagraph(HLPFILE_PAGE
* page
, struct RtfData
* rd
,
1232 BYTE
*buf
, BYTE
* end
, unsigned* parlen
)
1235 const BYTE
*format
, *format_end
;
1236 char *text
, *text_base
, *text_end
;
1237 LONG size
, blocksize
, datalen
;
1238 unsigned short bits
;
1240 short nc
, lastcol
, table_width
;
1244 if (buf
+ 0x19 > end
) {WINE_WARN("header too small\n"); return FALSE
;};
1247 blocksize
= GET_UINT(buf
, 0);
1248 size
= GET_UINT(buf
, 0x4);
1249 datalen
= GET_UINT(buf
, 0x10);
1250 text
= text_base
= malloc(size
);
1251 if (!text
) return FALSE
;
1252 if (size
> blocksize
- datalen
)
1254 /* need to decompress */
1255 if (page
->file
->hasPhrases
)
1256 HLPFILE_Uncompress2(page
->file
, buf
+ datalen
, end
, (BYTE
*)text
, (BYTE
*)text
+ size
);
1257 else if (page
->file
->hasPhrases40
)
1258 HLPFILE_Uncompress3(page
->file
, text
, text
+ size
, buf
+ datalen
, end
);
1261 WINE_FIXME("Text size is too long, splitting\n");
1262 size
= blocksize
- datalen
;
1263 memcpy(text
, buf
+ datalen
, size
);
1267 memcpy(text
, buf
+ datalen
, size
);
1269 text_end
= text
+ size
;
1271 format
= buf
+ 0x15;
1272 format_end
= buf
+ GET_UINT(buf
, 0x10);
1274 WINE_TRACE("Record type (buf[0x14]) = 0x%x\n", buf
[0x14]);
1276 if (buf
[0x14] == HLP_DISPLAY
|| buf
[0x14] == HLP_TABLE
)
1278 fetch_long(&format
);
1279 *parlen
= fetch_ushort(&format
);
1282 if (buf
[0x14] == HLP_TABLE
)
1288 if (!HLPFILE_RtfAddControl(rd
, "\\trowd")) goto done
;
1290 if (type
== 0 || type
== 2)
1292 table_width
= GET_SHORT(format
, 0);
1294 if (!HLPFILE_RtfAddControl(rd
, "\\trqc")) goto done
;
1297 table_width
= 32767;
1298 WINE_TRACE("New table: cols=%d type=%x width=%d\n",
1299 ncol
, type
, table_width
);
1303 sprintf(tmp
, "\\trgaph%d\\trleft%d",
1304 MulDiv(HLPFILE_HalfPointsScale(page
, GET_SHORT(format
, 6)), table_width
, 32767),
1305 MulDiv(HLPFILE_HalfPointsScale(page
, GET_SHORT(format
, 2) - GET_SHORT(format
, 6)), table_width
, 32767) - 1);
1306 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1307 pos
= GET_SHORT(format
, 6) / 2;
1308 for (nc
= 0; nc
< ncol
; nc
++)
1310 WINE_TRACE("column(%d/%d) gap=%d width=%d\n",
1311 nc
, ncol
, GET_SHORT(format
, nc
*4),
1312 GET_SHORT(format
, nc
*4+2));
1313 pos
+= GET_SHORT(format
, nc
* 4) + GET_SHORT(format
, nc
* 4 + 2);
1314 sprintf(tmp
, "\\cellx%d",
1315 MulDiv(HLPFILE_HalfPointsScale(page
, pos
), table_width
, 32767));
1316 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1321 WINE_TRACE("column(0/%d) gap=%d width=%d\n",
1322 ncol
, GET_SHORT(format
, 0), GET_SHORT(format
, 2));
1323 sprintf(tmp
, "\\trleft%d\\cellx%d ",
1324 MulDiv(HLPFILE_HalfPointsScale(page
, GET_SHORT(format
, 2)), table_width
, 32767) - 1,
1325 MulDiv(HLPFILE_HalfPointsScale(page
, GET_SHORT(format
, 0)), table_width
, 32767));
1326 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1332 for (nc
= 0; nc
< ncol
; /**/)
1334 WINE_TRACE("looking for format at offset %Iu in column %d\n", (SIZE_T
)(format
- (buf
+ 0x15)), nc
);
1335 if (!HLPFILE_RtfAddControl(rd
, "\\pard")) goto done
;
1336 if (buf
[0x14] == HLP_TABLE
)
1338 nc
= lastcol
= GET_SHORT(format
, 0);
1339 if (nc
== -1) /* last column */
1341 if (!HLPFILE_RtfAddControl(rd
, "\\row")) goto done
;
1346 if (!HLPFILE_RtfAddControl(rd
, "\\intbl")) goto done
;
1349 if (buf
[0x14] == HLP_DISPLAY30
)
1353 bits
= GET_USHORT(format
, 0); format
+= 2;
1354 if (bits
& 0x0001) fetch_long(&format
);
1357 sprintf(tmp
, "\\sb%d", HLPFILE_HalfPointsScale(page
, fetch_short(&format
)));
1358 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1362 sprintf(tmp
, "\\sa%d", HLPFILE_HalfPointsScale(page
, fetch_short(&format
)));
1363 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1367 sprintf(tmp
, "\\sl%d", HLPFILE_HalfPointsScale(page
, fetch_short(&format
)));
1368 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1372 sprintf(tmp
, "\\li%d", HLPFILE_HalfPointsScale(page
, fetch_short(&format
)));
1373 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1377 sprintf(tmp
, "\\ri%d", HLPFILE_HalfPointsScale(page
, fetch_short(&format
)));
1378 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1382 sprintf(tmp
, "\\fi%d", HLPFILE_HalfPointsScale(page
, fetch_short(&format
)));
1383 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1387 BYTE brdr
= *format
++;
1390 if ((brdr
& 0x01) && !HLPFILE_RtfAddControl(rd
, "\\box")) goto done
;
1391 if ((brdr
& 0x02) && !HLPFILE_RtfAddControl(rd
, "\\brdrt")) goto done
;
1392 if ((brdr
& 0x04) && !HLPFILE_RtfAddControl(rd
, "\\brdrl")) goto done
;
1393 if ((brdr
& 0x08) && !HLPFILE_RtfAddControl(rd
, "\\brdrb")) goto done
;
1394 if ((brdr
& 0x10) && !HLPFILE_RtfAddControl(rd
, "\\brdrr")) goto done
;
1395 if ((brdr
& 0x20) && !HLPFILE_RtfAddControl(rd
, "\\brdrth")) goto done
;
1396 if (!(brdr
& 0x20) && !HLPFILE_RtfAddControl(rd
, "\\brdrs")) goto done
;
1397 if ((brdr
& 0x40) && !HLPFILE_RtfAddControl(rd
, "\\brdrdb")) goto done
;
1400 w
= GET_SHORT(format
, 0); format
+= 2;
1403 sprintf(tmp
, "\\brdrw%d", HLPFILE_HalfPointsScale(page
, w
));
1404 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1409 int i
, ntab
= fetch_short(&format
);
1413 for (i
= 0; i
< ntab
; i
++)
1415 tab
= fetch_ushort(&format
);
1416 ts
= (tab
& 0x4000) ? fetch_ushort(&format
) : 0 /* left */;
1419 default: WINE_FIXME("Unknown tab style %x\n", ts
);
1421 case 0: kind
= ""; break;
1422 case 1: kind
= "\\tqr"; break;
1423 case 2: kind
= "\\tqc"; break;
1425 /* FIXME: do kind */
1426 sprintf(tmp
, "%s\\tx%d",
1427 kind
, HLPFILE_HalfPointsScale(page
, tab
& 0x3FFF));
1428 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1431 switch (bits
& 0xc00)
1433 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1434 case 0: if (!HLPFILE_RtfAddControl(rd
, "\\ql")) goto done
; break;
1435 case 0x400: if (!HLPFILE_RtfAddControl(rd
, "\\qr")) goto done
; break;
1436 case 0x800: if (!HLPFILE_RtfAddControl(rd
, "\\qc")) goto done
; break;
1439 /* 0x1000 doesn't need space */
1440 if ((bits
& 0x1000) && !HLPFILE_RtfAddControl(rd
, "\\keep")) goto done
;
1441 if ((bits
& 0xE080) != 0)
1442 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits
);
1444 while (text
< text_end
&& format
< format_end
)
1446 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", debugstr_a(text
), text
, text_end
, format
, format_end
);
1447 textsize
= strlen(text
);
1450 if (rd
->force_color
)
1452 if ((rd
->current_link
->cookie
== hlp_link_popup
) ?
1453 !HLPFILE_RtfAddControl(rd
, "{\\uld\\cf1") :
1454 !HLPFILE_RtfAddControl(rd
, "{\\ul\\cf1")) goto done
;
1456 if (!HLPFILE_RtfAddText(rd
, text
)) goto done
;
1457 if (rd
->force_color
&& !HLPFILE_RtfAddControl(rd
, "}")) goto done
;
1458 rd
->char_pos
+= textsize
;
1460 /* else: null text, keep on storing attributes */
1461 text
+= textsize
+ 1;
1463 WINE_TRACE("format=0x%02x\n", *format
);
1464 if (*format
== 0xff)
1473 WINE_FIXME("NIY20\n");
1478 WINE_FIXME("NIY21\n");
1484 unsigned font
= GET_USHORT(format
, 1);
1487 WINE_TRACE("Changing font to %d\n", font
);
1489 /* Font size in hlpfile is given in the same units as
1490 rtf control word \fs uses (half-points). */
1491 switch (rd
->font_scale
)
1493 case 0: fs
= page
->file
->fonts
[font
].LogFont
.lfHeight
- 4; break;
1495 case 1: fs
= page
->file
->fonts
[font
].LogFont
.lfHeight
; break;
1496 case 2: fs
= page
->file
->fonts
[font
].LogFont
.lfHeight
+ 4; break;
1498 /* FIXME: colors are missing, at a minimum; also, the bold attribute loses information */
1500 sprintf(tmp
, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1502 page
->file
->fonts
[font
].LogFont
.lfWeight
> 400 ? "\\b" : "\\b0",
1503 page
->file
->fonts
[font
].LogFont
.lfItalic
? "\\i" : "\\i0",
1504 page
->file
->fonts
[font
].LogFont
.lfUnderline
? "\\ul" : "\\ul0",
1505 page
->file
->fonts
[font
].LogFont
.lfStrikeOut
? "\\strike" : "\\strike0");
1506 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1511 if (!HLPFILE_RtfAddControl(rd
, "\\line")) goto done
;
1517 if (buf
[0x14] == HLP_TABLE
)
1519 if (format
[1] != 0xFF)
1521 if (!HLPFILE_RtfAddControl(rd
, "\\par\\intbl")) goto done
;
1523 else if (GET_SHORT(format
, 2) == -1)
1525 if (!HLPFILE_RtfAddControl(rd
, "\\cell\\intbl\\row")) goto done
;
1528 else if (GET_SHORT(format
, 2) == lastcol
)
1530 if (!HLPFILE_RtfAddControl(rd
, "\\par\\pard")) goto done
;
1534 if (!HLPFILE_RtfAddControl(rd
, "\\cell\\pard")) goto done
;
1537 else if (!HLPFILE_RtfAddControl(rd
, "\\par")) goto done
;
1543 if (!HLPFILE_RtfAddControl(rd
, "\\tab")) goto done
;
1558 BYTE type
= format
[1];
1560 /* FIXME: we don't use 'BYTE pos = (*format - 0x86);' for the image position */
1562 size
= fetch_long(&format
);
1567 fetch_ushort(&format
); /* hot spot */
1570 switch (GET_SHORT(format
, 0))
1573 HLPFILE_RtfAddGfxByIndex(rd
, page
->file
, GET_SHORT(format
, 2));
1577 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1578 GET_SHORT(format
, 0),
1579 size
, GET_SHORT(format
, 2));
1580 HLPFILE_RtfAddGfxByAddr(rd
, page
->file
, format
+ 2, size
- 4);
1584 WINE_FIXME("??? %u\n", GET_SHORT(format
, 0));
1589 WINE_FIXME("Got an embedded element %s\n", debugstr_a((char *)format
+ 6));
1592 WINE_FIXME("Got a type %d picture\n", type
);
1601 if (!rd
->current_link
)
1602 WINE_FIXME("No existing link\n");
1603 rd
->current_link
->cpMax
= rd
->char_pos
;
1604 rd
->current_link
= NULL
;
1605 rd
->force_color
= FALSE
;
1609 if (!HLPFILE_RtfAddControl(rd
, "\\~")) goto done
;
1615 if (!HLPFILE_RtfAddControl(rd
, "\\_")) goto done
;
1616 /* FIXME: it could be that hyphen is also in input stream !! */
1629 WINE_TRACE("macro => %s\n", debugstr_a((char *)format
+ 3));
1630 HLPFILE_AllocLink(rd
, hlp_link_macro
, (const char*)format
+ 3,
1631 GET_USHORT(format
, 1), 0, !(*format
& 4), FALSE
, -1);
1632 format
+= 3 + GET_USHORT(format
, 1);
1637 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format
, 1));
1638 HLPFILE_AllocLink(rd
, (*format
& 1) ? hlp_link_link
: hlp_link_popup
,
1639 page
->file
->lpszPath
, -1, GET_UINT(format
, 1), TRUE
, FALSE
, -1);
1649 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format
, 1));
1650 HLPFILE_AllocLink(rd
, (*format
& 1) ? hlp_link_link
: hlp_link_popup
,
1651 page
->file
->lpszPath
, -1, GET_UINT(format
, 1),
1652 !(*format
& 4), FALSE
, -1);
1661 const char* ptr
= (const char*) format
+ 8;
1662 BYTE type
= format
[3];
1671 ptr
= page
->file
->lpszPath
;
1674 for (wnd
= page
->file
->numWindows
- 1; wnd
>= 0; wnd
--)
1676 if (!strcmp(ptr
, page
->file
->windows
[wnd
].name
)) break;
1679 WINE_WARN("Couldn't find window info for %s\n", debugstr_a(ptr
));
1680 ptr
+= strlen(ptr
) + 1;
1685 WINE_WARN("Unknown link type %d\n", type
);
1688 HLPFILE_AllocLink(rd
, (*format
& 1) ? hlp_link_link
: hlp_link_popup
,
1689 ptr
, -1, GET_UINT(format
, 4), !(*format
& 4), FALSE
, wnd
);
1691 format
+= 3 + GET_USHORT(format
, 1);
1695 WINE_WARN("format %02x\n", *format
);
1707 /******************************************************************
1708 * HLPFILE_BrowsePage
1711 BOOL
HLPFILE_BrowsePage(HLPFILE_PAGE
* page
, struct RtfData
* rd
,
1712 unsigned font_scale
, unsigned relative
)
1714 HLPFILE
*hlpfile
= page
->file
;
1716 DWORD ref
= page
->reference
;
1717 unsigned index
, old_index
= -1, offset
, count
= 0, offs
= 0;
1718 unsigned cpg
, parlen
;
1720 const char* ck
= NULL
;
1723 rd
->data
= rd
->ptr
= malloc(rd
->allocated
= 32768);
1725 rd
->first_link
= rd
->current_link
= NULL
;
1726 rd
->force_color
= FALSE
;
1727 rd
->font_scale
= font_scale
;
1728 rd
->relative
= relative
;
1729 rd
->char_pos_rel
= 0;
1731 switch (hlpfile
->charset
)
1733 case DEFAULT_CHARSET
:
1734 case ANSI_CHARSET
: cpg
= 1252; break;
1735 case SHIFTJIS_CHARSET
: cpg
= 932; break;
1736 case HANGEUL_CHARSET
: cpg
= 949; break;
1737 case GB2312_CHARSET
: cpg
= 936; break;
1738 case CHINESEBIG5_CHARSET
: cpg
= 950; break;
1739 case GREEK_CHARSET
: cpg
= 1253; break;
1740 case TURKISH_CHARSET
: cpg
= 1254; break;
1741 case HEBREW_CHARSET
: cpg
= 1255; break;
1742 case ARABIC_CHARSET
: cpg
= 1256; break;
1743 case BALTIC_CHARSET
: cpg
= 1257; break;
1744 case VIETNAMESE_CHARSET
: cpg
= 1258; break;
1745 case RUSSIAN_CHARSET
: cpg
= 1251; break;
1746 case EE_CHARSET
: cpg
= 1250; break;
1747 case THAI_CHARSET
: cpg
= 874; break;
1748 case JOHAB_CHARSET
: cpg
= 1361; break;
1749 case MAC_CHARSET
: ck
= "mac"; break;
1751 WINE_FIXME("Unsupported charset %u\n", hlpfile
->charset
);
1756 sprintf(tmp
, "{\\rtf1\\%s\\deff0", ck
);
1757 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1761 sprintf(tmp
, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg
);
1762 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1765 /* generate font table */
1766 if (!HLPFILE_RtfAddControl(rd
, "{\\fonttbl")) return FALSE
;
1767 for (index
= 0; index
< hlpfile
->numFonts
; index
++)
1770 switch (hlpfile
->fonts
[index
].LogFont
.lfPitchAndFamily
& 0xF0)
1772 case FF_MODERN
: family
= "modern"; break;
1773 case FF_ROMAN
: family
= "roman"; break;
1774 case FF_SWISS
: family
= "swiss"; break;
1775 case FF_SCRIPT
: family
= "script"; break;
1776 case FF_DECORATIVE
: family
= "decor"; break;
1777 default: family
= "nil"; break;
1779 sprintf(tmp
, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1781 hlpfile
->fonts
[index
].LogFont
.lfPitchAndFamily
& 0x0F,
1782 hlpfile
->fonts
[index
].LogFont
.lfCharSet
,
1783 hlpfile
->fonts
[index
].LogFont
.lfFaceName
);
1784 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1786 if (!HLPFILE_RtfAddControl(rd
, "}")) return FALSE
;
1787 /* generate color table */
1788 if (!HLPFILE_RtfAddControl(rd
, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE
;
1789 for (index
= 0; index
< hlpfile
->numFonts
; index
++)
1791 sprintf(tmp
, "\\red%d\\green%d\\blue%d;",
1792 GetRValue(hlpfile
->fonts
[index
].color
),
1793 GetGValue(hlpfile
->fonts
[index
].color
),
1794 GetBValue(hlpfile
->fonts
[index
].color
));
1795 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1797 if (!HLPFILE_RtfAddControl(rd
, "}")) return FALSE
;
1801 if (hlpfile
->version
<= 16)
1803 index
= (ref
- 0x0C) / hlpfile
->dsize
;
1804 offset
= (ref
- 0x0C) % hlpfile
->dsize
;
1808 index
= (ref
- 0x0C) >> 14;
1809 offset
= (ref
- 0x0C) & 0x3FFF;
1812 if (hlpfile
->version
<= 16 && index
!= old_index
&& old_index
!= -1)
1814 /* we jumped to the next block, adjust pointers */
1819 if (index
>= hlpfile
->topic_maplen
) {WINE_WARN("maplen\n"); break;}
1820 buf
= hlpfile
->topic_map
[index
] + offset
;
1821 if (buf
+ 0x15 >= hlpfile
->topic_end
) {WINE_WARN("extra\n"); break;}
1822 end
= min(buf
+ GET_UINT(buf
, 0), hlpfile
->topic_end
);
1823 if (index
!= old_index
) {offs
= 0; old_index
= index
;}
1828 if (count
++) goto done
;
1833 if (!HLPFILE_BrowseParagraph(page
, rd
, buf
, end
, &parlen
)) return FALSE
;
1834 if (relative
> index
* 0x8000 + offs
)
1835 rd
->char_pos_rel
= rd
->char_pos
;
1839 WINE_ERR("buf[0x14] = %x\n", buf
[0x14]);
1841 if (hlpfile
->version
<= 16)
1843 ref
+= GET_UINT(buf
, 0xc);
1844 if (GET_UINT(buf
, 0xc) == 0)
1848 ref
= GET_UINT(buf
, 0xc);
1849 } while (ref
!= 0xffffffff);
1851 page
->first_link
= rd
->first_link
;
1852 return HLPFILE_RtfAddControl(rd
, "}");
1855 /******************************************************************
1860 static BOOL
HLPFILE_ReadFont(HLPFILE
* hlpfile
)
1863 unsigned i
, len
, idx
;
1864 unsigned face_num
, dscr_num
, face_offset
, dscr_offset
;
1867 if (!HLPFILE_FindSubFile(hlpfile
, "|FONT", &ref
, &end
))
1869 WINE_WARN("no subfile FONT\n");
1870 hlpfile
->numFonts
= 0;
1871 hlpfile
->fonts
= NULL
;
1877 face_num
= GET_USHORT(ref
, 0);
1878 dscr_num
= GET_USHORT(ref
, 2);
1879 face_offset
= GET_USHORT(ref
, 4);
1880 dscr_offset
= GET_USHORT(ref
, 6);
1882 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1883 face_num
, face_offset
, dscr_num
, dscr_offset
);
1885 hlpfile
->numFonts
= dscr_num
;
1886 hlpfile
->fonts
= malloc(sizeof(HLPFILE_FONT
) * dscr_num
);
1888 len
= (dscr_offset
- face_offset
) / face_num
;
1891 if (face_offset
>= 16)
1894 hlpfile
->rounderr
= 0;
1895 WINE_FIXME("mvb font: not implemented\n");
1899 if (face_offset
>= 12)
1902 hlpfile
->rounderr
= 0;
1903 WINE_FIXME("new font: not implemented\n");
1907 hlpfile
->scale
= 10;
1908 hlpfile
->rounderr
= 5;
1909 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1910 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1911 for (i
= 0; i
< dscr_num
; i
++)
1913 flag
= ref
[dscr_offset
+ i
* 11 + 0];
1914 family
= ref
[dscr_offset
+ i
* 11 + 2];
1916 hlpfile
->fonts
[i
].LogFont
.lfHeight
= ref
[dscr_offset
+ i
* 11 + 1];
1917 hlpfile
->fonts
[i
].LogFont
.lfWidth
= 0;
1918 hlpfile
->fonts
[i
].LogFont
.lfEscapement
= 0;
1919 hlpfile
->fonts
[i
].LogFont
.lfOrientation
= 0;
1920 hlpfile
->fonts
[i
].LogFont
.lfWeight
= (flag
& 1) ? 700 : 400;
1921 hlpfile
->fonts
[i
].LogFont
.lfItalic
= (flag
& 2) != 0;
1922 hlpfile
->fonts
[i
].LogFont
.lfUnderline
= (flag
& 4) != 0;
1923 hlpfile
->fonts
[i
].LogFont
.lfStrikeOut
= (flag
& 8) != 0;
1924 hlpfile
->fonts
[i
].LogFont
.lfCharSet
= hlpfile
->charset
;
1925 hlpfile
->fonts
[i
].LogFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
1926 hlpfile
->fonts
[i
].LogFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
1927 hlpfile
->fonts
[i
].LogFont
.lfQuality
= DEFAULT_QUALITY
;
1928 hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
= DEFAULT_PITCH
;
1932 case 0x01: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_MODERN
; break;
1933 case 0x02: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_ROMAN
; break;
1934 case 0x03: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_SWISS
; break;
1935 case 0x04: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_SCRIPT
; break;
1936 case 0x05: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_DECORATIVE
; break;
1937 default: WINE_FIXME("Unknown family %u\n", family
);
1939 idx
= GET_USHORT(ref
, dscr_offset
+ i
* 11 + 3);
1943 memcpy(hlpfile
->fonts
[i
].LogFont
.lfFaceName
, ref
+ face_offset
+ idx
* len
, min(len
, LF_FACESIZE
- 1));
1944 hlpfile
->fonts
[i
].LogFont
.lfFaceName
[min(len
, LF_FACESIZE
- 1)] = '\0';
1948 WINE_FIXME("Too high face ref (%u/%u)\n", idx
, face_num
);
1949 strcpy(hlpfile
->fonts
[i
].LogFont
.lfFaceName
, "Helv");
1951 hlpfile
->fonts
[i
].hFont
= 0;
1952 hlpfile
->fonts
[i
].color
= RGB(ref
[dscr_offset
+ i
* 11 + 5],
1953 ref
[dscr_offset
+ i
* 11 + 6],
1954 ref
[dscr_offset
+ i
* 11 + 7]);
1955 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1956 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1962 X(4, "dblUnderline"),
1964 ref
[dscr_offset
+ i
* 11 + 1],
1966 debugstr_a(hlpfile
->fonts
[i
].LogFont
.lfFaceName
), idx
,
1967 GET_UINT(ref
, dscr_offset
+ i
* 11 + 5) & 0x00FFFFFF);
1972 /***********************************************************************
1974 * HLPFILE_ReadFileToBuffer
1976 static BOOL
HLPFILE_ReadFileToBuffer(HLPFILE
* hlpfile
, HFILE hFile
)
1978 BYTE header
[16], dummy
[1];
1980 if (_hread(hFile
, header
, 16) != 16) {WINE_WARN("header\n"); return FALSE
;};
1983 if (GET_UINT(header
, 0) != 0x00035F3F)
1984 {WINE_WARN("wrong header\n"); return FALSE
;};
1986 hlpfile
->file_buffer_size
= GET_UINT(header
, 12);
1987 hlpfile
->file_buffer
= malloc(hlpfile
->file_buffer_size
+ 1);
1988 if (!hlpfile
->file_buffer
) return FALSE
;
1990 memcpy(hlpfile
->file_buffer
, header
, 16);
1991 if (_hread(hFile
, hlpfile
->file_buffer
+ 16, hlpfile
->file_buffer_size
- 16) !=hlpfile
->file_buffer_size
- 16)
1992 {WINE_WARN("filesize1\n"); return FALSE
;};
1994 if (_hread(hFile
, dummy
, 1) != 0) WINE_WARN("filesize2\n");
1996 hlpfile
->file_buffer
[hlpfile
->file_buffer_size
] = '\0'; /* FIXME: was '0', sounds backwards to me */
2001 /***********************************************************************
2003 * HLPFILE_SystemCommands
2005 static BOOL
HLPFILE_SystemCommands(HLPFILE
* hlpfile
)
2007 BYTE
*buf
, *ptr
, *end
;
2008 HLPFILE_MACRO
*macro
, **m
;
2010 unsigned short magic
, minor
, major
, flags
;
2012 hlpfile
->lpszTitle
= NULL
;
2014 if (!HLPFILE_FindSubFile(hlpfile
, "|SYSTEM", &buf
, &end
)) return FALSE
;
2016 magic
= GET_USHORT(buf
+ 9, 0);
2017 minor
= GET_USHORT(buf
+ 9, 2);
2018 major
= GET_USHORT(buf
+ 9, 4);
2019 /* gen date on 4 bytes */
2020 flags
= GET_USHORT(buf
+ 9, 10);
2021 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
2022 magic
, major
, minor
, flags
);
2023 if (magic
!= 0x036C || major
!= 1)
2024 {WINE_WARN("Wrong system header\n"); return FALSE
;}
2027 hlpfile
->tbsize
= 0x800;
2028 hlpfile
->compressed
= FALSE
;
2030 else if (flags
== 0)
2032 hlpfile
->tbsize
= 0x1000;
2033 hlpfile
->compressed
= FALSE
;
2035 else if (flags
== 4)
2037 hlpfile
->tbsize
= 0x1000;
2038 hlpfile
->compressed
= TRUE
;
2042 hlpfile
->tbsize
= 0x800;
2043 hlpfile
->compressed
= TRUE
;
2046 if (hlpfile
->compressed
)
2047 hlpfile
->dsize
= 0x4000;
2049 hlpfile
->dsize
= hlpfile
->tbsize
- 0x0C;
2051 hlpfile
->version
= minor
;
2052 hlpfile
->flags
= flags
;
2053 hlpfile
->charset
= DEFAULT_CHARSET
;
2055 if (hlpfile
->version
<= 16)
2057 char *str
= (char*)buf
+ 0x15;
2059 hlpfile
->lpszTitle
= strdup(str
);
2060 if (!hlpfile
->lpszTitle
) return FALSE
;
2061 WINE_TRACE("Title: %s\n", debugstr_a(hlpfile
->lpszTitle
));
2062 /* Nothing more to parse */
2065 for (ptr
= buf
+ 0x15; ptr
+ 4 <= end
; ptr
+= GET_USHORT(ptr
, 2) + 4)
2067 char *str
= (char*) ptr
+ 4;
2068 switch (GET_USHORT(ptr
, 0))
2071 if (hlpfile
->lpszTitle
) {WINE_WARN("title\n"); break;}
2072 hlpfile
->lpszTitle
= strdup(str
);
2073 if (!hlpfile
->lpszTitle
) return FALSE
;
2074 WINE_TRACE("Title: %s\n", debugstr_a(hlpfile
->lpszTitle
));
2078 if (hlpfile
->lpszCopyright
) {WINE_WARN("copyright\n"); break;}
2079 hlpfile
->lpszCopyright
= strdup(str
);
2080 if (!hlpfile
->lpszCopyright
) return FALSE
;
2081 WINE_TRACE("Copyright: %s\n", debugstr_a(hlpfile
->lpszCopyright
));
2085 if (GET_USHORT(ptr
, 2) != 4) {WINE_WARN("system3\n");break;}
2086 hlpfile
->contents_start
= GET_UINT(ptr
, 4);
2087 WINE_TRACE("Setting contents start at %08lx\n", hlpfile
->contents_start
);
2091 macro
= malloc(sizeof(HLPFILE_MACRO
) + strlen(str
) + 1);
2093 p
= (char*)macro
+ sizeof(HLPFILE_MACRO
);
2095 macro
->lpszMacro
= p
;
2097 for (m
= &hlpfile
->first_macro
; *m
; m
= &(*m
)->next
);
2102 if (GET_USHORT(ptr
, 4 + 4) != 1)
2103 WINE_FIXME("More than one icon, picking up first\n");
2104 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
2105 hlpfile
->hIcon
= CreateIconFromResourceEx(ptr
+ 4 + 0x16,
2106 GET_USHORT(ptr
, 2) - 0x16, TRUE
,
2111 if (GET_USHORT(ptr
, 2) != 90) {WINE_WARN("system6\n");break;}
2113 hlpfile
->windows
= realloc(hlpfile
->windows
,
2114 sizeof(HLPFILE_WINDOWINFO
) * ++hlpfile
->numWindows
);
2116 if (hlpfile
->windows
)
2118 HLPFILE_WINDOWINFO
* wi
= &hlpfile
->windows
[hlpfile
->numWindows
- 1];
2120 flags
= GET_USHORT(ptr
, 4);
2121 if (flags
& 0x0001) strcpy(wi
->type
, &str
[2]);
2122 else wi
->type
[0] = '\0';
2123 if (flags
& 0x0002) strcpy(wi
->name
, &str
[12]);
2124 else wi
->name
[0] = '\0';
2125 if (flags
& 0x0004) strcpy(wi
->caption
, &str
[21]);
2126 else lstrcpynA(wi
->caption
, hlpfile
->lpszTitle
, sizeof(wi
->caption
));
2127 wi
->origin
.x
= (flags
& 0x0008) ? GET_USHORT(ptr
, 76) : CW_USEDEFAULT
;
2128 wi
->origin
.y
= (flags
& 0x0010) ? GET_USHORT(ptr
, 78) : CW_USEDEFAULT
;
2129 wi
->size
.cx
= (flags
& 0x0020) ? GET_USHORT(ptr
, 80) : CW_USEDEFAULT
;
2130 wi
->size
.cy
= (flags
& 0x0040) ? GET_USHORT(ptr
, 82) : CW_USEDEFAULT
;
2131 wi
->style
= (flags
& 0x0080) ? GET_USHORT(ptr
, 84) : SW_SHOW
;
2132 wi
->win_style
= WS_OVERLAPPEDWINDOW
;
2133 wi
->sr_color
= (flags
& 0x0100) ? GET_UINT(ptr
, 86) : 0xFFFFFF;
2134 wi
->nsr_color
= (flags
& 0x0200) ? GET_UINT(ptr
, 90) : 0xFFFFFF;
2135 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",
2136 flags
& 0x0001 ? 'T' : 't',
2137 flags
& 0x0002 ? 'N' : 'n',
2138 flags
& 0x0004 ? 'C' : 'c',
2139 flags
& 0x0008 ? 'X' : 'x',
2140 flags
& 0x0010 ? 'Y' : 'y',
2141 flags
& 0x0020 ? 'W' : 'w',
2142 flags
& 0x0040 ? 'H' : 'h',
2143 flags
& 0x0080 ? 'S' : 's',
2144 debugstr_a(wi
->type
), debugstr_a(wi
->name
), debugstr_a(wi
->caption
), wi
->origin
.x
, wi
->origin
.y
,
2145 wi
->size
.cx
, wi
->size
.cy
);
2149 WINE_WARN("Citation: %s\n", debugstr_a((char *)ptr
+ 4));
2152 hlpfile
->charset
= ptr
[4];
2153 WINE_TRACE("Charset: %d\n", hlpfile
->charset
);
2156 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr
, 0));
2159 if (!hlpfile
->lpszTitle
)
2160 hlpfile
->lpszTitle
= strdup("");
2164 /***********************************************************************
2166 * HLPFILE_GetContext
2168 static BOOL
HLPFILE_GetContext(HLPFILE
*hlpfile
)
2173 if (!HLPFILE_FindSubFile(hlpfile
, "|CONTEXT", &cbuf
, &cend
))
2174 {WINE_WARN("context0\n"); return FALSE
;}
2177 hlpfile
->Context
= malloc(clen
);
2178 if (!hlpfile
->Context
) return FALSE
;
2179 memcpy(hlpfile
->Context
, cbuf
, clen
);
2184 /***********************************************************************
2186 * HLPFILE_GetKeywords
2188 static BOOL
HLPFILE_GetKeywords(HLPFILE
*hlpfile
)
2193 if (!HLPFILE_FindSubFile(hlpfile
, "|KWBTREE", &cbuf
, &cend
)) return FALSE
;
2195 hlpfile
->kwbtree
= malloc(clen
);
2196 if (!hlpfile
->kwbtree
) return FALSE
;
2197 memcpy(hlpfile
->kwbtree
, cbuf
, clen
);
2199 if (!HLPFILE_FindSubFile(hlpfile
, "|KWDATA", &cbuf
, &cend
))
2201 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2202 free(hlpfile
->kwbtree
);
2206 hlpfile
->kwdata
= malloc(clen
);
2207 if (!hlpfile
->kwdata
)
2209 free(hlpfile
->kwdata
);
2212 memcpy(hlpfile
->kwdata
, cbuf
, clen
);
2217 /***********************************************************************
2221 static BOOL
HLPFILE_GetMap(HLPFILE
*hlpfile
)
2224 unsigned entries
, i
;
2226 if (!HLPFILE_FindSubFile(hlpfile
, "|CTXOMAP", &cbuf
, &cend
))
2227 {WINE_WARN("no map section\n"); return FALSE
;}
2229 entries
= GET_USHORT(cbuf
, 9);
2230 hlpfile
->Map
= malloc(entries
* sizeof(HLPFILE_MAP
));
2231 if (!hlpfile
->Map
) return FALSE
;
2232 hlpfile
->wMapLen
= entries
;
2233 for (i
= 0; i
< entries
; i
++)
2235 hlpfile
->Map
[i
].lMap
= GET_UINT(cbuf
+11,i
*8);
2236 hlpfile
->Map
[i
].offset
= GET_UINT(cbuf
+11,i
*8+4);
2241 /***********************************************************************
2245 static BOOL
HLPFILE_GetTOMap(HLPFILE
*hlpfile
)
2250 if (!HLPFILE_FindSubFile(hlpfile
, "|TOMAP", &cbuf
, &cend
))
2251 {WINE_WARN("no tomap section\n"); return FALSE
;}
2253 clen
= cend
- cbuf
- 9;
2254 hlpfile
->TOMap
= malloc(clen
);
2255 if (!hlpfile
->TOMap
) return FALSE
;
2256 memcpy(hlpfile
->TOMap
, cbuf
+9, clen
);
2257 hlpfile
->wTOMapLen
= clen
/4;
2261 /***********************************************************************
2265 static void HLPFILE_DeleteMacro(HLPFILE_MACRO
* macro
)
2267 HLPFILE_MACRO
* next
;
2277 /***********************************************************************
2281 static void HLPFILE_DeletePage(HLPFILE_PAGE
* page
)
2288 HLPFILE_DeleteMacro(page
->first_macro
);
2294 /***********************************************************************
2296 * HLPFILE_FreeHlpFile
2298 void HLPFILE_FreeHlpFile(HLPFILE
* hlpfile
)
2302 if (!hlpfile
|| --hlpfile
->wRefCount
> 0) return;
2304 if (hlpfile
->next
) hlpfile
->next
->prev
= hlpfile
->prev
;
2305 if (hlpfile
->prev
) hlpfile
->prev
->next
= hlpfile
->next
;
2306 else first_hlpfile
= hlpfile
->next
;
2308 if (hlpfile
->numFonts
)
2310 for (i
= 0; i
< hlpfile
->numFonts
; i
++)
2312 DeleteObject(hlpfile
->fonts
[i
].hFont
);
2314 free(hlpfile
->fonts
);
2317 if (hlpfile
->numBmps
)
2319 for (i
= 0; i
< hlpfile
->numBmps
; i
++)
2321 DeleteObject(hlpfile
->bmps
[i
]);
2323 free(hlpfile
->bmps
);
2326 HLPFILE_DeletePage(hlpfile
->first_page
);
2327 HLPFILE_DeleteMacro(hlpfile
->first_macro
);
2329 DestroyIcon(hlpfile
->hIcon
);
2330 if (hlpfile
->numWindows
) free(hlpfile
->windows
);
2331 free(hlpfile
->Context
);
2333 free(hlpfile
->lpszTitle
);
2334 free(hlpfile
->lpszCopyright
);
2335 free(hlpfile
->file_buffer
);
2336 free(hlpfile
->phrases_offsets
);
2337 free(hlpfile
->phrases_buffer
);
2338 free(hlpfile
->topic_map
);
2339 free(hlpfile
->help_on_file
);
2343 /***********************************************************************
2345 * HLPFILE_UncompressLZ77_Phrases
2347 static BOOL
HLPFILE_UncompressLZ77_Phrases(HLPFILE
* hlpfile
)
2349 UINT i
, num
, dec_size
, head_size
;
2352 if (!HLPFILE_FindSubFile(hlpfile
, "|Phrases", &buf
, &end
)) return FALSE
;
2354 if (hlpfile
->version
<= 16)
2359 num
= hlpfile
->num_phrases
= GET_USHORT(buf
, 9);
2360 if (buf
+ 2 * num
+ 0x13 >= end
) {WINE_WARN("1a\n"); return FALSE
;};
2362 if (hlpfile
->version
<= 16)
2363 dec_size
= end
- buf
- 15 - 2 * num
;
2365 dec_size
= HLPFILE_UncompressedLZ77_Size(buf
+ 0x13 + 2 * num
, end
);
2367 hlpfile
->phrases_offsets
= malloc(sizeof(unsigned) * (num
+ 1));
2368 hlpfile
->phrases_buffer
= malloc(dec_size
);
2369 if (!hlpfile
->phrases_offsets
|| !hlpfile
->phrases_buffer
)
2371 free(hlpfile
->phrases_offsets
);
2372 free(hlpfile
->phrases_buffer
);
2376 for (i
= 0; i
<= num
; i
++)
2377 hlpfile
->phrases_offsets
[i
] = GET_USHORT(buf
, head_size
+ 2 * i
) - 2 * num
- 2;
2379 if (hlpfile
->version
<= 16)
2380 memcpy(hlpfile
->phrases_buffer
, buf
+ 15 + 2*num
, dec_size
);
2382 HLPFILE_UncompressLZ77(buf
+ 0x13 + 2 * num
, end
, (BYTE
*)hlpfile
->phrases_buffer
);
2384 hlpfile
->hasPhrases
= TRUE
;
2388 /***********************************************************************
2390 * HLPFILE_Uncompress_Phrases40
2392 static BOOL
HLPFILE_Uncompress_Phrases40(HLPFILE
* hlpfile
)
2395 INT dec_size
, cpr_size
;
2396 BYTE
*buf_idx
, *end_idx
;
2397 BYTE
*buf_phs
, *end_phs
;
2398 ULONG
* ptr
, mask
= 0;
2400 unsigned short bc
, n
;
2402 if (!HLPFILE_FindSubFile(hlpfile
, "|PhrIndex", &buf_idx
, &end_idx
) ||
2403 !HLPFILE_FindSubFile(hlpfile
, "|PhrImage", &buf_phs
, &end_phs
)) return FALSE
;
2405 ptr
= (ULONG
*)(buf_idx
+ 9 + 28);
2406 bc
= GET_USHORT(buf_idx
, 9 + 24) & 0x0F;
2407 num
= hlpfile
->num_phrases
= GET_USHORT(buf_idx
, 9 + 4);
2409 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2410 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2411 GET_UINT(buf_idx
, 9 + 0),
2412 GET_UINT(buf_idx
, 9 + 4),
2413 GET_UINT(buf_idx
, 9 + 8),
2414 GET_UINT(buf_idx
, 9 + 12),
2415 GET_UINT(buf_idx
, 9 + 16),
2416 GET_UINT(buf_idx
, 9 + 20),
2417 GET_USHORT(buf_idx
, 9 + 24),
2418 GET_USHORT(buf_idx
, 9 + 26));
2420 dec_size
= GET_UINT(buf_idx
, 9 + 12);
2421 cpr_size
= GET_UINT(buf_idx
, 9 + 16);
2423 if (dec_size
!= cpr_size
&&
2424 dec_size
!= HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
))
2426 WINE_WARN("size mismatch %u %u\n",
2427 dec_size
, HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
));
2428 dec_size
= max(dec_size
, HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
));
2431 hlpfile
->phrases_offsets
= malloc(sizeof(unsigned) * (num
+ 1));
2432 hlpfile
->phrases_buffer
= malloc(dec_size
);
2433 if (!hlpfile
->phrases_offsets
|| !hlpfile
->phrases_buffer
)
2435 free(hlpfile
->phrases_offsets
);
2436 free(hlpfile
->phrases_buffer
);
2440 #define getbit() ((mask <<= 1) ? (*ptr & mask) != 0: (*++ptr & (mask=1)) != 0)
2442 hlpfile
->phrases_offsets
[0] = 0;
2443 ptr
--; /* as we'll first increment ptr because mask is 0 on first getbit() call */
2444 for (i
= 0; i
< num
; i
++)
2446 for (n
= 1; getbit(); n
+= 1 << bc
);
2448 if (bc
> 1 && getbit()) n
+= 2;
2449 if (bc
> 2 && getbit()) n
+= 4;
2450 if (bc
> 3 && getbit()) n
+= 8;
2451 if (bc
> 4 && getbit()) n
+= 16;
2452 hlpfile
->phrases_offsets
[i
+ 1] = hlpfile
->phrases_offsets
[i
] + n
;
2456 if (dec_size
== cpr_size
)
2457 memcpy(hlpfile
->phrases_buffer
, buf_phs
+ 9, dec_size
);
2459 HLPFILE_UncompressLZ77(buf_phs
+ 9, end_phs
, (BYTE
*)hlpfile
->phrases_buffer
);
2461 hlpfile
->hasPhrases40
= TRUE
;
2465 /***********************************************************************
2467 * HLPFILE_Uncompress_Topic
2469 static BOOL
HLPFILE_Uncompress_Topic(HLPFILE
* hlpfile
)
2471 BYTE
*buf
, *ptr
, *end
, *newptr
;
2472 unsigned int i
, newsize
= 0;
2473 unsigned int topic_size
;
2475 if (!HLPFILE_FindSubFile(hlpfile
, "|TOPIC", &buf
, &end
))
2476 {WINE_WARN("topic0\n"); return FALSE
;}
2478 buf
+= 9; /* Skip file header */
2479 topic_size
= end
- buf
;
2480 if (hlpfile
->compressed
)
2482 hlpfile
->topic_maplen
= (topic_size
- 1) / hlpfile
->tbsize
+ 1;
2484 for (i
= 0; i
< hlpfile
->topic_maplen
; i
++)
2486 ptr
= buf
+ i
* hlpfile
->tbsize
;
2488 /* I don't know why, it's necessary for printman.hlp */
2489 if (ptr
+ 0x44 > end
) ptr
= end
- 0x44;
2491 newsize
+= HLPFILE_UncompressedLZ77_Size(ptr
+ 0xc, min(end
, ptr
+ hlpfile
->tbsize
));
2494 hlpfile
->topic_map
= malloc(hlpfile
->topic_maplen
* sizeof(hlpfile
->topic_map
[0]) + newsize
);
2495 if (!hlpfile
->topic_map
) return FALSE
;
2496 newptr
= (BYTE
*)(hlpfile
->topic_map
+ hlpfile
->topic_maplen
);
2497 hlpfile
->topic_end
= newptr
+ newsize
;
2499 for (i
= 0; i
< hlpfile
->topic_maplen
; i
++)
2501 ptr
= buf
+ i
* hlpfile
->tbsize
;
2502 if (ptr
+ 0x44 > end
) ptr
= end
- 0x44;
2504 hlpfile
->topic_map
[i
] = newptr
;
2505 newptr
= HLPFILE_UncompressLZ77(ptr
+ 0xc, min(end
, ptr
+ hlpfile
->tbsize
), newptr
);
2510 /* basically, we need to copy the TopicBlockSize byte pages
2511 * (removing the first 0x0C) in one single area in memory
2513 hlpfile
->topic_maplen
= (topic_size
- 1) / hlpfile
->tbsize
+ 1;
2514 hlpfile
->topic_map
= malloc(hlpfile
->topic_maplen
* (sizeof(hlpfile
->topic_map
[0]) + hlpfile
->dsize
));
2515 if (!hlpfile
->topic_map
) return FALSE
;
2516 newptr
= (BYTE
*)(hlpfile
->topic_map
+ hlpfile
->topic_maplen
);
2517 hlpfile
->topic_end
= newptr
+ topic_size
;
2519 for (i
= 0; i
< hlpfile
->topic_maplen
; i
++)
2521 hlpfile
->topic_map
[i
] = newptr
+ i
* hlpfile
->dsize
;
2522 memcpy(hlpfile
->topic_map
[i
], buf
+ i
* hlpfile
->tbsize
+ 0x0C, hlpfile
->dsize
);
2528 /***********************************************************************
2532 static BOOL
HLPFILE_AddPage(HLPFILE
*hlpfile
, const BYTE
*buf
, const BYTE
*end
, unsigned ref
, unsigned offset
)
2536 UINT titlesize
, blocksize
, datalen
;
2538 HLPFILE_MACRO
*macro
;
2540 blocksize
= GET_UINT(buf
, 0);
2541 datalen
= GET_UINT(buf
, 0x10);
2542 title
= buf
+ datalen
;
2543 if (title
> end
) {WINE_WARN("page2\n"); return FALSE
;};
2545 titlesize
= GET_UINT(buf
, 4);
2546 page
= malloc(sizeof(HLPFILE_PAGE
) + titlesize
+ 1);
2547 if (!page
) return FALSE
;
2548 page
->lpszTitle
= (char*)page
+ sizeof(HLPFILE_PAGE
);
2550 if (titlesize
> blocksize
- datalen
)
2552 /* need to decompress */
2553 if (hlpfile
->hasPhrases
)
2554 HLPFILE_Uncompress2(hlpfile
, title
, end
, (BYTE
*)page
->lpszTitle
, (BYTE
*)page
->lpszTitle
+ titlesize
);
2555 else if (hlpfile
->hasPhrases40
)
2556 HLPFILE_Uncompress3(hlpfile
, page
->lpszTitle
, page
->lpszTitle
+ titlesize
, title
, end
);
2559 WINE_FIXME("Text size is too long, splitting\n");
2560 titlesize
= blocksize
- datalen
;
2561 memcpy(page
->lpszTitle
, title
, titlesize
);
2565 memcpy(page
->lpszTitle
, title
, titlesize
);
2567 page
->lpszTitle
[titlesize
] = '\0';
2569 if (hlpfile
->first_page
)
2571 hlpfile
->last_page
->next
= page
;
2572 page
->prev
= hlpfile
->last_page
;
2573 hlpfile
->last_page
= page
;
2577 hlpfile
->first_page
= page
;
2578 hlpfile
->last_page
= page
;
2582 page
->file
= hlpfile
;
2584 page
->first_macro
= NULL
;
2585 page
->first_link
= NULL
;
2586 page
->wNumber
= GET_UINT(buf
, 0x21);
2587 page
->offset
= offset
;
2588 page
->reference
= ref
;
2590 page
->browse_bwd
= GET_UINT(buf
, 0x19);
2591 page
->browse_fwd
= GET_UINT(buf
, 0x1D);
2593 if (hlpfile
->version
<= 16)
2595 if (page
->browse_bwd
== 0xFFFF || page
->browse_bwd
== 0xFFFFFFFF)
2596 page
->browse_bwd
= 0xFFFFFFFF;
2598 page
->browse_bwd
= hlpfile
->TOMap
[page
->browse_bwd
];
2600 if (page
->browse_fwd
== 0xFFFF || page
->browse_fwd
== 0xFFFFFFFF)
2601 page
->browse_fwd
= 0xFFFFFFFF;
2603 page
->browse_fwd
= hlpfile
->TOMap
[page
->browse_fwd
];
2606 WINE_TRACE("Added page[%d]: title=%s %08lx << %08x >> %08lx\n",
2607 page
->wNumber
, debugstr_a(page
->lpszTitle
),
2608 page
->browse_bwd
, page
->offset
, page
->browse_fwd
);
2610 /* now load macros */
2611 ptr
= page
->lpszTitle
+ strlen(page
->lpszTitle
) + 1;
2612 while (ptr
< page
->lpszTitle
+ titlesize
)
2614 unsigned len
= strlen(ptr
);
2617 WINE_TRACE("macro: %s\n", debugstr_a(ptr
));
2618 macro
= malloc(sizeof(HLPFILE_MACRO
) + len
+ 1);
2619 macro
->lpszMacro
= macro_str
= (char*)(macro
+ 1);
2620 memcpy(macro_str
, ptr
, len
+ 1);
2621 /* FIXME: shall we really link macro in reverse order ??
2622 * may produce strange results when played at page opening
2624 macro
->next
= page
->first_macro
;
2625 page
->first_macro
= macro
;
2632 /***********************************************************************
2634 * HLPFILE_SkipParagraph
2636 static BOOL
HLPFILE_SkipParagraph(HLPFILE
*hlpfile
, const BYTE
*buf
, const BYTE
*end
, unsigned* len
)
2640 if (!hlpfile
->first_page
) {WINE_WARN("no page\n"); return FALSE
;};
2641 if (buf
+ 0x19 > end
) {WINE_WARN("header too small\n"); return FALSE
;};
2644 if (buf
[0x14] == HLP_DISPLAY
|| buf
[0x14] == HLP_TABLE
)
2647 *len
= fetch_ushort(&tmp
);
2649 else *len
= end
-buf
-15;
2654 /***********************************************************************
2656 * HLPFILE_DoReadHlpFile
2658 static BOOL
HLPFILE_DoReadHlpFile(HLPFILE
*hlpfile
, LPCSTR lpszPath
)
2665 unsigned index
, old_index
, offset
, len
, offs
, topicoffset
;
2667 hFile
= OpenFile(lpszPath
, &ofs
, OF_READ
);
2668 if (hFile
== HFILE_ERROR
) return FALSE
;
2670 ret
= HLPFILE_ReadFileToBuffer(hlpfile
, hFile
);
2672 if (!ret
) return FALSE
;
2674 if (!HLPFILE_SystemCommands(hlpfile
)) return FALSE
;
2676 if (hlpfile
->version
<= 16 && !HLPFILE_GetTOMap(hlpfile
)) return FALSE
;
2678 /* load phrases support */
2679 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile
))
2680 HLPFILE_Uncompress_Phrases40(hlpfile
);
2682 if (!HLPFILE_Uncompress_Topic(hlpfile
)) return FALSE
;
2683 if (!HLPFILE_ReadFont(hlpfile
)) return FALSE
;
2691 if (hlpfile
->version
<= 16)
2693 index
= (ref
- 0x0C) / hlpfile
->dsize
;
2694 offset
= (ref
- 0x0C) % hlpfile
->dsize
;
2698 index
= (ref
- 0x0C) >> 14;
2699 offset
= (ref
- 0x0C) & 0x3FFF;
2702 if (hlpfile
->version
<= 16 && index
!= old_index
&& old_index
!= -1)
2704 /* we jumped to the next block, adjust pointers */
2709 WINE_TRACE("ref=%08lx => [%u/%u]\n", ref
, index
, offset
);
2711 if (index
>= hlpfile
->topic_maplen
) {WINE_WARN("maplen\n"); break;}
2712 buf
= hlpfile
->topic_map
[index
] + offset
;
2713 if (buf
+ 0x15 >= hlpfile
->topic_end
) {WINE_WARN("extra\n"); break;}
2714 end
= min(buf
+ GET_UINT(buf
, 0), hlpfile
->topic_end
);
2715 if (index
!= old_index
) {offs
= 0; old_index
= index
;}
2719 case HLP_TOPICHDR
: /* Topic Header */
2720 if (hlpfile
->version
<= 16)
2721 topicoffset
= ref
+ index
* 12;
2723 topicoffset
= index
* 0x8000 + offs
;
2724 if (!HLPFILE_AddPage(hlpfile
, buf
, end
, ref
, topicoffset
)) return FALSE
;
2730 if (!HLPFILE_SkipParagraph(hlpfile
, buf
, end
, &len
)) return FALSE
;
2735 WINE_ERR("buf[0x14] = %x\n", buf
[0x14]);
2738 if (hlpfile
->version
<= 16)
2740 ref
+= GET_UINT(buf
, 0xc);
2741 if (GET_UINT(buf
, 0xc) == 0)
2745 ref
= GET_UINT(buf
, 0xc);
2746 } while (ref
!= 0xffffffff);
2748 HLPFILE_GetKeywords(hlpfile
);
2749 HLPFILE_GetMap(hlpfile
);
2750 if (hlpfile
->version
<= 16) return TRUE
;
2751 return HLPFILE_GetContext(hlpfile
);
2754 /***********************************************************************
2756 * HLPFILE_ReadHlpFile
2758 HLPFILE
*HLPFILE_ReadHlpFile(LPCSTR lpszPath
)
2762 for (hlpfile
= first_hlpfile
; hlpfile
; hlpfile
= hlpfile
->next
)
2764 if (!strcmp(lpszPath
, hlpfile
->lpszPath
))
2766 hlpfile
->wRefCount
++;
2771 hlpfile
= calloc(1, sizeof(HLPFILE
) + strlen(lpszPath
) + 1);
2772 if (!hlpfile
) return 0;
2774 hlpfile
->lpszPath
= (char*)hlpfile
+ sizeof(HLPFILE
);
2775 hlpfile
->contents_start
= 0xFFFFFFFF;
2776 hlpfile
->next
= first_hlpfile
;
2777 hlpfile
->wRefCount
= 1;
2779 strcpy(hlpfile
->lpszPath
, lpszPath
);
2781 first_hlpfile
= hlpfile
;
2782 if (hlpfile
->next
) hlpfile
->next
->prev
= hlpfile
;
2784 if (!HLPFILE_DoReadHlpFile(hlpfile
, lpszPath
))
2786 HLPFILE_FreeHlpFile(hlpfile
);