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;
54 static BOOL
HLPFILE_DoReadHlpFile(HLPFILE
*, LPCSTR
);
55 static BOOL
HLPFILE_ReadFileToBuffer(HLPFILE
*, HFILE
);
56 static BOOL
HLPFILE_FindSubFile(HLPFILE
*, LPCSTR
, BYTE
**, BYTE
**);
57 static BOOL
HLPFILE_SystemCommands(HLPFILE
*);
58 static INT
HLPFILE_UncompressedLZ77_Size(const BYTE
*ptr
, const BYTE
*end
);
59 static BYTE
* HLPFILE_UncompressLZ77(const BYTE
*ptr
, const BYTE
*end
, BYTE
*newptr
);
60 static BOOL
HLPFILE_UncompressLZ77_Phrases(HLPFILE
*);
61 static BOOL
HLPFILE_Uncompress_Phrases40(HLPFILE
*);
62 static BOOL
HLPFILE_Uncompress_Topic(HLPFILE
*);
63 static BOOL
HLPFILE_GetContext(HLPFILE
*);
64 static BOOL
HLPFILE_GetKeywords(HLPFILE
*);
65 static BOOL
HLPFILE_GetMap(HLPFILE
*);
66 static BOOL
HLPFILE_AddPage(HLPFILE
*, const BYTE
*, const BYTE
*, unsigned, unsigned);
67 static BOOL
HLPFILE_SkipParagraph(HLPFILE
*, const BYTE
*, const BYTE
*, unsigned*);
68 static void HLPFILE_Uncompress2(HLPFILE
*, const BYTE
*, const BYTE
*, BYTE
*, const BYTE
*);
69 static BOOL
HLPFILE_Uncompress3(HLPFILE
*, char*, const char*, const BYTE
*, const BYTE
*);
70 static void HLPFILE_UncompressRLE(const BYTE
* src
, const BYTE
* end
, BYTE
* dst
, unsigned dstsz
);
71 static BOOL
HLPFILE_ReadFont(HLPFILE
* hlpfile
);
73 /***********************************************************************
75 * HLPFILE_PageByNumber
77 static HLPFILE_PAGE
*HLPFILE_PageByNumber(HLPFILE
* hlpfile
, UINT wNum
)
82 WINE_TRACE("<%s>[%u]\n", hlpfile
->lpszPath
, wNum
);
84 for (page
= hlpfile
->first_page
; page
&& temp
; page
= page
->next
) temp
--;
86 WINE_ERR("Page of number %u not found in file %s\n", wNum
, hlpfile
->lpszPath
);
90 /******************************************************************
91 * HLPFILE_PageByOffset
95 HLPFILE_PAGE
*HLPFILE_PageByOffset(HLPFILE
* hlpfile
, LONG offset
, ULONG
* relative
)
100 if (!hlpfile
) return 0;
102 WINE_TRACE("<%s>[%x]\n", hlpfile
->lpszPath
, offset
);
104 if (offset
== 0xFFFFFFFF) return NULL
;
107 for (found
= NULL
, page
= hlpfile
->first_page
; page
; page
= page
->next
)
109 if (page
->offset
<= offset
&& (!found
|| found
->offset
< page
->offset
))
111 *relative
= offset
- page
->offset
;
116 WINE_ERR("Page of offset %u not found in file %s\n",
117 offset
, hlpfile
->lpszPath
);
121 /**************************************************************************
124 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
127 static int comp_PageByHash(void *p
, const void *key
,
128 int leaf
, void** next
)
130 LONG lKey
= (LONG_PTR
)key
;
131 LONG lTest
= (INT
)GET_UINT(p
, 0);
133 *next
= (char *)p
+(leaf
?8:6);
134 WINE_TRACE("Comparing '%d' with '%d'\n", lKey
, lTest
);
135 if (lTest
< lKey
) return -1;
136 if (lTest
> lKey
) return 1;
140 /***********************************************************************
144 HLPFILE_PAGE
*HLPFILE_PageByHash(HLPFILE
* hlpfile
, LONG lHash
, ULONG
* relative
)
148 if (!hlpfile
) return NULL
;
149 if (!lHash
) return HLPFILE_Contents(hlpfile
, relative
);
151 WINE_TRACE("<%s>[%x]\n", hlpfile
->lpszPath
, lHash
);
153 /* For win 3.0 files hash values are really page numbers */
154 if (hlpfile
->version
<= 16)
157 return HLPFILE_PageByNumber(hlpfile
, lHash
);
160 ptr
= HLPFILE_BPTreeSearch(hlpfile
->Context
, LongToPtr(lHash
), comp_PageByHash
);
163 WINE_ERR("Page of hash %x not found in file %s\n", lHash
, hlpfile
->lpszPath
);
167 return HLPFILE_PageByOffset(hlpfile
, GET_UINT(ptr
, 4), relative
);
170 /***********************************************************************
174 HLPFILE_PAGE
*HLPFILE_PageByMap(HLPFILE
* hlpfile
, LONG lMap
, ULONG
* relative
)
178 if (!hlpfile
) return 0;
180 WINE_TRACE("<%s>[%x]\n", hlpfile
->lpszPath
, lMap
);
182 for (i
= 0; i
< hlpfile
->wMapLen
; i
++)
184 if (hlpfile
->Map
[i
].lMap
== lMap
)
185 return HLPFILE_PageByOffset(hlpfile
, hlpfile
->Map
[i
].offset
, relative
);
188 WINE_ERR("Page of Map %x not found in file %s\n", lMap
, hlpfile
->lpszPath
);
192 /***********************************************************************
196 HLPFILE_PAGE
* HLPFILE_Contents(HLPFILE
*hlpfile
, ULONG
* relative
)
198 HLPFILE_PAGE
* page
= NULL
;
200 if (!hlpfile
) return NULL
;
202 page
= HLPFILE_PageByOffset(hlpfile
, hlpfile
->contents_start
, relative
);
205 page
= hlpfile
->first_page
;
211 /***********************************************************************
215 LONG
HLPFILE_Hash(LPCSTR lpszContext
)
220 while ((c
= *lpszContext
++))
223 if (c
>= 'A' && c
<= 'Z') x
= c
- 'A' + 17;
224 if (c
>= 'a' && c
<= 'z') x
= c
- 'a' + 17;
225 if (c
>= '1' && c
<= '9') x
= c
- '0';
226 if (c
== '0') x
= 10;
227 if (c
== '.') x
= 12;
228 if (c
== '_') x
= 13;
229 if (x
) lHash
= lHash
* 43 + x
;
234 /***********************************************************************
236 * HLPFILE_ReadHlpFile
238 HLPFILE
*HLPFILE_ReadHlpFile(LPCSTR lpszPath
)
242 for (hlpfile
= first_hlpfile
; hlpfile
; hlpfile
= hlpfile
->next
)
244 if (!strcmp(lpszPath
, hlpfile
->lpszPath
))
246 hlpfile
->wRefCount
++;
251 hlpfile
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
252 sizeof(HLPFILE
) + lstrlen(lpszPath
) + 1);
253 if (!hlpfile
) return 0;
255 hlpfile
->lpszPath
= (char*)hlpfile
+ sizeof(HLPFILE
);
256 hlpfile
->contents_start
= 0xFFFFFFFF;
257 hlpfile
->next
= first_hlpfile
;
258 hlpfile
->wRefCount
= 1;
260 strcpy(hlpfile
->lpszPath
, lpszPath
);
262 first_hlpfile
= hlpfile
;
263 if (hlpfile
->next
) hlpfile
->next
->prev
= hlpfile
;
265 if (!HLPFILE_DoReadHlpFile(hlpfile
, lpszPath
))
267 HLPFILE_FreeHlpFile(hlpfile
);
274 /***********************************************************************
276 * HLPFILE_DoReadHlpFile
278 static BOOL
HLPFILE_DoReadHlpFile(HLPFILE
*hlpfile
, LPCSTR lpszPath
)
285 unsigned index
, old_index
, offset
, len
, offs
;
287 hFile
= OpenFile(lpszPath
, &ofs
, OF_READ
);
288 if (hFile
== HFILE_ERROR
) return FALSE
;
290 ret
= HLPFILE_ReadFileToBuffer(hlpfile
, hFile
);
292 if (!ret
) return FALSE
;
294 if (!HLPFILE_SystemCommands(hlpfile
)) return FALSE
;
296 /* load phrases support */
297 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile
))
298 HLPFILE_Uncompress_Phrases40(hlpfile
);
300 if (!HLPFILE_Uncompress_Topic(hlpfile
)) return FALSE
;
301 if (!HLPFILE_ReadFont(hlpfile
)) return FALSE
;
303 buf
= hlpfile
->topic_map
[0];
310 if (hlpfile
->version
<= 16)
312 index
= (ref
- 0x0C) / hlpfile
->dsize
;
313 offset
= (ref
- 0x0C) % hlpfile
->dsize
;
317 index
= (ref
- 0x0C) >> 14;
318 offset
= (ref
- 0x0C) & 0x3FFF;
321 if (hlpfile
->version
<= 16 && index
!= old_index
&& old_index
!= -1)
323 /* we jumped to the next block, adjust pointers */
328 WINE_TRACE("ref=%08x => [%u/%u]\n", ref
, index
, offset
);
330 if (index
>= hlpfile
->topic_maplen
) {WINE_WARN("maplen\n"); break;}
331 buf
= hlpfile
->topic_map
[index
] + offset
;
332 if (buf
+ 0x15 >= hlpfile
->topic_end
) {WINE_WARN("extra\n"); break;}
333 end
= min(buf
+ GET_UINT(buf
, 0), hlpfile
->topic_end
);
334 if (index
!= old_index
) {offs
= 0; old_index
= index
;}
339 if (!HLPFILE_AddPage(hlpfile
, buf
, end
, ref
, index
* 0x8000L
+ offs
)) return FALSE
;
345 if (!HLPFILE_SkipParagraph(hlpfile
, buf
, end
, &len
)) return FALSE
;
350 WINE_ERR("buf[0x14] = %x\n", buf
[0x14]);
353 if (hlpfile
->version
<= 16)
355 ref
+= GET_UINT(buf
, 0xc);
356 if (GET_UINT(buf
, 0xc) == 0)
360 ref
= GET_UINT(buf
, 0xc);
361 } while (ref
!= 0xffffffff);
363 HLPFILE_GetKeywords(hlpfile
);
364 HLPFILE_GetMap(hlpfile
);
365 if (hlpfile
->version
<= 16) return TRUE
;
366 return HLPFILE_GetContext(hlpfile
);
369 /***********************************************************************
373 static BOOL
HLPFILE_AddPage(HLPFILE
*hlpfile
, const BYTE
*buf
, const BYTE
*end
, unsigned ref
, unsigned offset
)
377 UINT titlesize
, blocksize
, datalen
;
381 blocksize
= GET_UINT(buf
, 0);
382 datalen
= GET_UINT(buf
, 0x10);
383 title
= buf
+ datalen
;
384 if (title
> end
) {WINE_WARN("page2\n"); return FALSE
;};
386 titlesize
= GET_UINT(buf
, 4);
387 page
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE
) + titlesize
+ 1);
388 if (!page
) return FALSE
;
389 page
->lpszTitle
= (char*)page
+ sizeof(HLPFILE_PAGE
);
391 if (titlesize
> blocksize
- datalen
)
393 /* need to decompress */
394 if (hlpfile
->hasPhrases
)
395 HLPFILE_Uncompress2(hlpfile
, title
, end
, (BYTE
*)page
->lpszTitle
, (BYTE
*)page
->lpszTitle
+ titlesize
);
396 else if (hlpfile
->hasPhrases40
)
397 HLPFILE_Uncompress3(hlpfile
, page
->lpszTitle
, page
->lpszTitle
+ titlesize
, title
, end
);
400 WINE_FIXME("Text size is too long, splitting\n");
401 titlesize
= blocksize
- datalen
;
402 memcpy(page
->lpszTitle
, title
, titlesize
);
406 memcpy(page
->lpszTitle
, title
, titlesize
);
408 page
->lpszTitle
[titlesize
] = '\0';
410 if (hlpfile
->first_page
)
412 hlpfile
->last_page
->next
= page
;
413 page
->prev
= hlpfile
->last_page
;
414 hlpfile
->last_page
= page
;
418 hlpfile
->first_page
= page
;
419 hlpfile
->last_page
= page
;
423 page
->file
= hlpfile
;
425 page
->first_macro
= NULL
;
426 page
->first_link
= NULL
;
427 page
->wNumber
= GET_UINT(buf
, 0x21);
428 page
->offset
= offset
;
429 page
->reference
= ref
;
431 page
->browse_bwd
= GET_UINT(buf
, 0x19);
432 page
->browse_fwd
= GET_UINT(buf
, 0x1D);
434 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
435 page
->wNumber
, page
->lpszTitle
,
436 page
->browse_bwd
, page
->offset
, page
->browse_fwd
);
438 /* now load macros */
439 ptr
= page
->lpszTitle
+ strlen(page
->lpszTitle
) + 1;
440 while (ptr
< page
->lpszTitle
+ titlesize
)
442 unsigned len
= strlen(ptr
);
445 WINE_TRACE("macro: %s\n", ptr
);
446 macro
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO
) + len
+ 1);
447 macro
->lpszMacro
= macro_str
= (char*)(macro
+ 1);
448 memcpy(macro_str
, ptr
, len
+ 1);
449 /* FIXME: shall we really link macro in reverse order ??
450 * may produce strange results when played at page opening
452 macro
->next
= page
->first_macro
;
453 page
->first_macro
= macro
;
460 static long fetch_long(const BYTE
** ptr
)
466 ret
= (*(const unsigned long*)(*ptr
) - 0x80000000L
) / 2;
471 ret
= (*(const unsigned short*)(*ptr
) - 0x8000) / 2;
478 static unsigned long fetch_ulong(const BYTE
** ptr
)
484 ret
= *(const unsigned long*)(*ptr
) / 2;
489 ret
= *(const unsigned short*)(*ptr
) / 2;
495 static short fetch_short(const BYTE
** ptr
)
501 ret
= (*(const unsigned short*)(*ptr
) - 0x8000) / 2;
506 ret
= (*(const unsigned char*)(*ptr
) - 0x80) / 2;
512 static unsigned short fetch_ushort(const BYTE
** ptr
)
518 ret
= *(const unsigned short*)(*ptr
) / 2;
523 ret
= *(const unsigned char*)(*ptr
) / 2;
529 /***********************************************************************
531 * HLPFILE_SkipParagraph
533 static BOOL
HLPFILE_SkipParagraph(HLPFILE
*hlpfile
, const BYTE
*buf
, const BYTE
*end
, unsigned* len
)
537 if (!hlpfile
->first_page
) {WINE_WARN("no page\n"); return FALSE
;};
538 if (buf
+ 0x19 > end
) {WINE_WARN("header too small\n"); return FALSE
;};
541 if (buf
[0x14] == 0x20 || buf
[0x14] == 0x23)
544 *len
= fetch_ushort(&tmp
);
546 else *len
= end
-buf
-15;
551 /******************************************************************
552 * HLPFILE_DecompressGfx
554 * Decompress the data part of a bitmap or a metafile
556 static const BYTE
* HLPFILE_DecompressGfx(const BYTE
* src
, unsigned csz
, unsigned sz
, BYTE packing
,
563 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing
, csz
, sz
);
567 case 0: /* uncompressed */
569 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz
, csz
);
574 dst
= *alloc
= HeapAlloc(GetProcessHeap(), 0, sz
);
575 if (!dst
) return NULL
;
576 HLPFILE_UncompressRLE(src
, src
+ csz
, *alloc
, sz
);
579 sz77
= HLPFILE_UncompressedLZ77_Size(src
, src
+ csz
);
580 dst
= *alloc
= HeapAlloc(GetProcessHeap(), 0, sz77
);
581 if (!dst
) return NULL
;
582 HLPFILE_UncompressLZ77(src
, src
+ csz
, *alloc
);
584 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77
, sz
);
586 case 3: /* LZ77 then RLE */
587 sz77
= HLPFILE_UncompressedLZ77_Size(src
, src
+ csz
);
588 tmp
= HeapAlloc(GetProcessHeap(), 0, sz77
);
589 if (!tmp
) return FALSE
;
590 HLPFILE_UncompressLZ77(src
, src
+ csz
, tmp
);
591 dst
= *alloc
= HeapAlloc(GetProcessHeap(), 0, sz
);
594 HeapFree(GetProcessHeap(), 0, tmp
);
597 HLPFILE_UncompressRLE(tmp
, tmp
+ sz77
, *alloc
, sz
);
598 HeapFree(GetProcessHeap(), 0, tmp
);
601 WINE_FIXME("Unsupported packing %u\n", packing
);
607 static BOOL
HLPFILE_RtfAddRawString(struct RtfData
* rd
, const char* str
, size_t sz
)
609 if (rd
->ptr
+ sz
>= rd
->data
+ rd
->allocated
)
611 char* new = HeapReAlloc(GetProcessHeap(), 0, rd
->data
, rd
->allocated
*= 2);
612 if (!new) return FALSE
;
613 rd
->ptr
= new + (rd
->ptr
- rd
->data
);
616 memcpy(rd
->ptr
, str
, sz
);
622 static BOOL
HLPFILE_RtfAddControl(struct RtfData
* rd
, const char* str
)
624 if (*str
== '\\' || *str
== '{') rd
->in_text
= FALSE
;
625 else if (*str
== '}') rd
->in_text
= TRUE
;
626 return HLPFILE_RtfAddRawString(rd
, str
, strlen(str
));
629 static BOOL
HLPFILE_RtfAddText(struct RtfData
* rd
, const char* str
)
638 if (!HLPFILE_RtfAddRawString(rd
, " ", 1)) return FALSE
;
641 for (last
= p
= str
; *p
; p
++)
643 if (*p
< 0) /* escape non ASCII chars */
646 rlen
= sprintf(xx
, "\\'%x", *(const BYTE
*)p
);
651 case '{': rlen
= 2; replace
= "\\{"; break;
652 case '}': rlen
= 2; replace
= "\\}"; break;
653 case '\\': rlen
= 2; replace
= "\\\\"; break;
656 if ((p
!= last
&& !HLPFILE_RtfAddRawString(rd
, last
, p
- last
)) ||
657 !HLPFILE_RtfAddRawString(rd
, replace
, rlen
)) return FALSE
;
660 return HLPFILE_RtfAddRawString(rd
, last
, p
- last
);
663 /******************************************************************
667 static BOOL
HLPFILE_RtfAddHexBytes(struct RtfData
* rd
, const void* _ptr
, unsigned sz
)
671 const BYTE
* ptr
= _ptr
;
672 static const char* _2hex
= "0123456789abcdef";
676 if (!HLPFILE_RtfAddRawString(rd
, " ", 1)) return FALSE
;
679 for (; sz
; sz
-= step
)
682 for (i
= 0; i
< step
; i
++)
684 tmp
[2 * i
+ 0] = _2hex
[*ptr
>> 4];
685 tmp
[2 * i
+ 1] = _2hex
[*ptr
++ & 0xF];
687 if (!HLPFILE_RtfAddRawString(rd
, tmp
, 2 * step
)) return FALSE
;
692 /******************************************************************
693 * HLPFILE_RtfAddTransparentBitmap
695 * We'll transform a transparent bitmap into an metafile that
696 * we then transform into RTF
698 static BOOL
HLPFILE_RtfAddTransparentBitmap(struct RtfData
* rd
, const BITMAPINFO
* bi
,
699 const void* pict
, unsigned nc
)
701 HDC hdc
, hdcMask
, hdcMem
, hdcEMF
;
702 HBITMAP hbm
, hbmMask
, hbmOldMask
, hbmOldMem
;
708 hbm
= CreateDIBitmap(hdc
= GetDC(0), &bi
->bmiHeader
,
709 CBM_INIT
, pict
, bi
, DIB_RGB_COLORS
);
711 hdcMem
= CreateCompatibleDC(hdc
);
712 hbmOldMem
= SelectObject(hdcMem
, hbm
);
714 /* create the mask bitmap from the main bitmap */
715 hdcMask
= CreateCompatibleDC(hdc
);
716 hbmMask
= CreateBitmap(bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
, 1, 1, NULL
);
717 hbmOldMask
= SelectObject(hdcMask
, hbmMask
);
719 RGB(bi
->bmiColors
[nc
- 1].rgbRed
,
720 bi
->bmiColors
[nc
- 1].rgbGreen
,
721 bi
->bmiColors
[nc
- 1].rgbBlue
));
722 BitBlt(hdcMask
, 0, 0, bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
, hdcMem
, 0, 0, SRCCOPY
);
724 /* sets to RGB(0,0,0) the transparent bits in main bitmap */
725 SetBkColor(hdcMem
, RGB(0,0,0));
726 SetTextColor(hdcMem
, RGB(255,255,255));
727 BitBlt(hdcMem
, 0, 0, bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
, hdcMask
, 0, 0, SRCAND
);
729 SelectObject(hdcMask
, hbmOldMask
);
732 SelectObject(hdcMem
, hbmOldMem
);
735 /* we create the bitmap on the fly */
736 hdcEMF
= CreateEnhMetaFile(NULL
, NULL
, NULL
, NULL
);
737 hdcMem
= CreateCompatibleDC(hdcEMF
);
739 /* sets to RGB(0,0,0) the transparent bits in final bitmap */
740 hbmOldMem
= SelectObject(hdcMem
, hbmMask
);
741 SetBkColor(hdcEMF
, RGB(255, 255, 255));
742 SetTextColor(hdcEMF
, RGB(0, 0, 0));
743 BitBlt(hdcEMF
, 0, 0, bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
, hdcMem
, 0, 0, SRCAND
);
745 /* and copy the remaining bits of main bitmap */
746 SelectObject(hdcMem
, hbm
);
747 BitBlt(hdcEMF
, 0, 0, bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
, hdcMem
, 0, 0, SRCPAINT
);
748 SelectObject(hdcMem
, hbmOldMem
);
753 DeleteObject(hbmMask
);
756 hEMF
= CloseEnhMetaFile(hdcEMF
);
758 /* generate rtf stream */
759 sz
= GetEnhMetaFileBits(hEMF
, 0, NULL
);
760 if (sz
&& (data
= HeapAlloc(GetProcessHeap(), 0, sz
)))
762 if (sz
== GetEnhMetaFileBits(hEMF
, sz
, data
))
764 ret
= HLPFILE_RtfAddControl(rd
, "{\\pict\\emfblip") &&
765 HLPFILE_RtfAddHexBytes(rd
, data
, sz
) &&
766 HLPFILE_RtfAddControl(rd
, "}");
768 HeapFree(GetProcessHeap(), 0, data
);
770 DeleteEnhMetaFile(hEMF
);
775 /******************************************************************
776 * HLPFILE_RtfAddBitmap
779 static BOOL
HLPFILE_RtfAddBitmap(struct RtfData
* rd
, const BYTE
* beg
, BYTE type
, BYTE pack
)
782 const BYTE
* pict_beg
;
785 unsigned long off
, csz
;
787 BOOL clrImportant
= FALSE
;
791 bi
= HeapAlloc(GetProcessHeap(), 0, sizeof(*bi
));
792 if (!bi
) return FALSE
;
794 ptr
= beg
+ 2; /* for type and pack */
796 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
797 bi
->bmiHeader
.biXPelsPerMeter
= fetch_ulong(&ptr
);
798 bi
->bmiHeader
.biYPelsPerMeter
= fetch_ulong(&ptr
);
799 bi
->bmiHeader
.biPlanes
= fetch_ushort(&ptr
);
800 bi
->bmiHeader
.biBitCount
= fetch_ushort(&ptr
);
801 bi
->bmiHeader
.biWidth
= fetch_ulong(&ptr
);
802 bi
->bmiHeader
.biHeight
= fetch_ulong(&ptr
);
803 bi
->bmiHeader
.biClrUsed
= fetch_ulong(&ptr
);
804 clrImportant
= fetch_ulong(&ptr
);
805 bi
->bmiHeader
.biClrImportant
= (clrImportant
> 1) ? clrImportant
: 0;
806 bi
->bmiHeader
.biCompression
= BI_RGB
;
807 if (bi
->bmiHeader
.biBitCount
> 32) WINE_FIXME("Unknown bit count %u\n", bi
->bmiHeader
.biBitCount
);
808 if (bi
->bmiHeader
.biPlanes
!= 1) WINE_FIXME("Unsupported planes %u\n", bi
->bmiHeader
.biPlanes
);
809 bi
->bmiHeader
.biSizeImage
= (((bi
->bmiHeader
.biWidth
* bi
->bmiHeader
.biBitCount
+ 31) & ~31) / 8) * bi
->bmiHeader
.biHeight
;
810 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
811 bi
->bmiHeader
.biPlanes
, bi
->bmiHeader
.biBitCount
,
812 bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
);
814 csz
= fetch_ulong(&ptr
);
815 fetch_ulong(&ptr
); /* hotspot size */
817 off
= GET_UINT(ptr
, 0); ptr
+= 4;
818 /* GET_UINT(ptr, 0); hotspot offset */ ptr
+= 4;
820 /* now read palette info */
825 nc
= bi
->bmiHeader
.biClrUsed
;
826 /* not quite right, especially for bitfields type of compression */
827 if (!nc
&& bi
->bmiHeader
.biBitCount
<= 8)
828 nc
= 1 << bi
->bmiHeader
.biBitCount
;
830 bi
= HeapReAlloc(GetProcessHeap(), 0, bi
, sizeof(*bi
) + nc
* sizeof(RGBQUAD
));
831 if (!bi
) return FALSE
;
832 for (i
= 0; i
< nc
; i
++)
834 bi
->bmiColors
[i
].rgbBlue
= ptr
[0];
835 bi
->bmiColors
[i
].rgbGreen
= ptr
[1];
836 bi
->bmiColors
[i
].rgbRed
= ptr
[2];
837 bi
->bmiColors
[i
].rgbReserved
= 0;
841 pict_beg
= HLPFILE_DecompressGfx(beg
+ off
, csz
, bi
->bmiHeader
.biSizeImage
, pack
, &alloc
);
843 if (clrImportant
== 1 && nc
> 0)
845 ret
= HLPFILE_RtfAddTransparentBitmap(rd
, bi
, pict_beg
, nc
);
848 if (!HLPFILE_RtfAddControl(rd
, "{\\pict")) goto done
;
851 sprintf(tmp
, "\\dibitmap0\\picw%d\\pich%d",
852 bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
);
853 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
854 if (!HLPFILE_RtfAddHexBytes(rd
, bi
, sizeof(*bi
) + nc
* sizeof(RGBQUAD
))) goto done
;
858 sprintf(tmp
, "\\wbitmap0\\wbmbitspixel%d\\wbmplanes%d\\picw%d\\pich%d",
859 bi
->bmiHeader
.biBitCount
, bi
->bmiHeader
.biPlanes
,
860 bi
->bmiHeader
.biWidth
, bi
->bmiHeader
.biHeight
);
861 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
863 if (!HLPFILE_RtfAddHexBytes(rd
, pict_beg
, bi
->bmiHeader
.biSizeImage
)) goto done
;
864 if (!HLPFILE_RtfAddControl(rd
, "}")) goto done
;
868 HeapFree(GetProcessHeap(), 0, bi
);
869 HeapFree(GetProcessHeap(), 0, alloc
);
874 /******************************************************************
875 * HLPFILE_RtfAddMetaFile
878 static BOOL
HLPFILE_RtfAddMetaFile(struct RtfData
* rd
, const BYTE
* beg
, BYTE pack
)
881 unsigned long size
, csize
;
882 unsigned long off
, hsoff
;
889 WINE_TRACE("Loading metafile\n");
891 ptr
= beg
+ 2; /* for type and pack */
893 mm
= fetch_ushort(&ptr
); /* mapping mode */
894 sprintf(tmp
, "{\\pict\\wmetafile%d\\picw%d\\pich%d",
895 mm
, GET_USHORT(ptr
, 0), GET_USHORT(ptr
, 2));
896 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
899 size
= fetch_ulong(&ptr
); /* decompressed size */
900 csize
= fetch_ulong(&ptr
); /* compressed size */
901 fetch_ulong(&ptr
); /* hotspot size */
902 off
= GET_UINT(ptr
, 0);
903 hsoff
= GET_UINT(ptr
, 4);
906 WINE_TRACE("sz=%lu csz=%lu offs=%lu/%u,%lu\n",
907 size
, csize
, off
, ptr
- beg
, hsoff
);
909 bits
= HLPFILE_DecompressGfx(beg
+ off
, csize
, size
, pack
, &alloc
);
910 if (!bits
) return FALSE
;
912 ret
= HLPFILE_RtfAddHexBytes(rd
, bits
, size
) &&
913 HLPFILE_RtfAddControl(rd
, "}");
915 HeapFree(GetProcessHeap(), 0, alloc
);
920 /******************************************************************
921 * HLPFILE_RtfAddGfxByAddr
924 static BOOL
HLPFILE_RtfAddGfxByAddr(struct RtfData
* rd
, HLPFILE
*hlpfile
,
925 const BYTE
* ref
, unsigned long size
)
929 numpict
= GET_USHORT(ref
, 2);
930 WINE_TRACE("Got picture magic=%04x #=%d\n", GET_USHORT(ref
, 0), numpict
);
932 for (i
= 0; i
< numpict
; i
++)
938 WINE_TRACE("Offset[%d] = %x\n", i
, GET_UINT(ref
, (1 + i
) * 4));
939 beg
= ptr
= ref
+ GET_UINT(ref
, (1 + i
) * 4);
946 case 5: /* device dependent bmp */
947 case 6: /* device independent bmp */
948 HLPFILE_RtfAddBitmap(rd
, beg
, type
, pack
);
951 HLPFILE_RtfAddMetaFile(rd
, beg
, pack
);
953 default: WINE_FIXME("Unknown type %u\n", type
); return FALSE
;
956 /* FIXME: hotspots */
958 /* FIXME: implement support for multiple picture format */
959 if (numpict
!= 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
965 /******************************************************************
966 * HLPFILE_RtfAddGfxByIndex
970 static BOOL
HLPFILE_RtfAddGfxByIndex(struct RtfData
* rd
, HLPFILE
*hlpfile
,
976 WINE_TRACE("Loading picture #%d\n", index
);
978 sprintf(tmp
, "|bm%u", index
);
980 if (!HLPFILE_FindSubFile(hlpfile
, tmp
, &ref
, &end
)) {WINE_WARN("no sub file\n"); return FALSE
;}
983 return HLPFILE_RtfAddGfxByAddr(rd
, hlpfile
, ref
, end
- ref
);
986 /******************************************************************
991 static HLPFILE_LINK
* HLPFILE_AllocLink(struct RtfData
* rd
, int cookie
,
992 const char* str
, unsigned len
, LONG hash
,
993 unsigned clrChange
, unsigned wnd
)
998 /* FIXME: should build a string table for the attributes.link.lpszPath
999 * they are reallocated for each link
1001 if (len
== -1) len
= strlen(str
);
1002 link
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_LINK
) + len
+ 1);
1003 if (!link
) return NULL
;
1005 link
->cookie
= cookie
;
1006 link
->string
= link_str
= (char*)(link
+ 1);
1007 memcpy(link_str
, str
, len
);
1008 link_str
[len
] = '\0';
1010 link
->bClrChange
= clrChange
? 1 : 0;
1012 link
->next
= rd
->first_link
;
1013 rd
->first_link
= link
;
1014 link
->cpMin
= rd
->char_pos
;
1016 rd
->force_color
= clrChange
;
1017 if (rd
->current_link
) WINE_FIXME("Pending link\n");
1018 rd
->current_link
= link
;
1020 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
1021 link
->cookie
, link
->string
, link
->hash
, link
->window
);
1025 unsigned HLPFILE_HalfPointsToTwips(unsigned pts
)
1027 static unsigned logPxY
;
1030 HDC hdc
= GetDC(NULL
);
1031 logPxY
= GetDeviceCaps(hdc
, LOGPIXELSY
);
1032 ReleaseDC(NULL
, hdc
);
1034 return MulDiv(pts
, 72 * 10, logPxY
);
1037 /***********************************************************************
1039 * HLPFILE_BrowseParagraph
1041 static BOOL
HLPFILE_BrowseParagraph(HLPFILE_PAGE
* page
, struct RtfData
* rd
,
1042 BYTE
*buf
, BYTE
* end
, unsigned* parlen
)
1045 const BYTE
*format
, *format_end
;
1046 char *text
, *text_base
, *text_end
;
1047 long size
, blocksize
, datalen
;
1048 unsigned short bits
;
1049 unsigned nc
, ncol
= 1;
1051 BOOL in_table
= FALSE
;
1055 if (buf
+ 0x19 > end
) {WINE_WARN("header too small\n"); return FALSE
;};
1058 blocksize
= GET_UINT(buf
, 0);
1059 size
= GET_UINT(buf
, 0x4);
1060 datalen
= GET_UINT(buf
, 0x10);
1061 text
= text_base
= HeapAlloc(GetProcessHeap(), 0, size
);
1062 if (!text
) return FALSE
;
1063 if (size
> blocksize
- datalen
)
1065 /* need to decompress */
1066 if (page
->file
->hasPhrases
)
1067 HLPFILE_Uncompress2(page
->file
, buf
+ datalen
, end
, (BYTE
*)text
, (BYTE
*)text
+ size
);
1068 else if (page
->file
->hasPhrases40
)
1069 HLPFILE_Uncompress3(page
->file
, text
, text
+ size
, buf
+ datalen
, end
);
1072 WINE_FIXME("Text size is too long, splitting\n");
1073 size
= blocksize
- datalen
;
1074 memcpy(text
, buf
+ datalen
, size
);
1078 memcpy(text
, buf
+ datalen
, size
);
1080 text_end
= text
+ size
;
1082 format
= buf
+ 0x15;
1083 format_end
= buf
+ GET_UINT(buf
, 0x10);
1085 if (buf
[0x14] == 0x20 || buf
[0x14] == 0x23)
1087 fetch_long(&format
);
1088 *parlen
= fetch_ushort(&format
);
1091 if (buf
[0x14] == 0x23)
1098 if (!HLPFILE_RtfAddControl(rd
, "\\trowd")) goto done
;
1100 if (type
== 0 || type
== 2)
1102 table_width
= GET_SHORT(format
, 0);
1106 table_width
= 32767;
1107 WINE_TRACE("New table: cols=%d type=%x width=%d\n",
1108 ncol
, type
, table_width
);
1112 sprintf(tmp
, "\\trgaph%d\\trleft%d",
1113 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format
, 6), table_width
, 32767)),
1114 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format
, 0), table_width
, 32767)));
1115 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1116 pos
= HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format
, 6) / 2, table_width
, 32767));
1117 for (nc
= 0; nc
< ncol
; nc
++)
1119 WINE_TRACE("column(%d/%d) gap=%d width=%d\n",
1120 nc
, ncol
, GET_SHORT(format
, nc
*4),
1121 GET_SHORT(format
, nc
*4+2));
1122 pos
+= GET_SHORT(format
, nc
* 4) + GET_SHORT(format
, nc
* 4 + 2);
1123 sprintf(tmp
, "\\cellx%d",
1124 HLPFILE_HalfPointsToTwips(MulDiv(pos
, table_width
, 32767)));
1125 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1130 WINE_TRACE("column(0/%d) gap=%d width=%d\n",
1131 ncol
, GET_SHORT(format
, 0), GET_SHORT(format
, 2));
1132 sprintf(tmp
, "\\trleft%d\\cellx%d ",
1133 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format
, 0), table_width
, 32767)),
1134 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format
, 0) + GET_SHORT(format
, 2),
1135 table_width
, 32767)));
1136 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1141 for (nc
= 0; nc
< ncol
; /**/)
1143 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T
)(format
- (buf
+ 0x15)), nc
);
1144 if (!HLPFILE_RtfAddControl(rd
, "\\pard")) goto done
;
1147 nc
= GET_SHORT(format
, 0);
1148 if (nc
== -1) break;
1150 if (!HLPFILE_RtfAddControl(rd
, "\\intbl")) goto done
;
1153 if (buf
[0x14] == 0x01)
1157 bits
= GET_USHORT(format
, 0); format
+= 2;
1158 if (bits
& 0x0001) fetch_long(&format
);
1161 sprintf(tmp
, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1162 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1166 sprintf(tmp
, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1167 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1171 sprintf(tmp
, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1172 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1176 sprintf(tmp
, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1177 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1181 sprintf(tmp
, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1182 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1186 sprintf(tmp
, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format
)));
1187 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1191 BYTE brdr
= *format
++;
1194 if (brdr
& 0x01 && !HLPFILE_RtfAddControl(rd
, "\\box")) goto done
;
1195 if (brdr
& 0x02 && !HLPFILE_RtfAddControl(rd
, "\\brdrt")) goto done
;
1196 if (brdr
& 0x04 && !HLPFILE_RtfAddControl(rd
, "\\brdrl")) goto done
;
1197 if (brdr
& 0x08 && !HLPFILE_RtfAddControl(rd
, "\\brdrb")) goto done
;
1198 if (brdr
& 0x10 && !HLPFILE_RtfAddControl(rd
, "\\brdrr")) goto done
;
1199 if (brdr
& 0x20 && !HLPFILE_RtfAddControl(rd
, "\\brdrth")) goto done
;
1200 if (!(brdr
& 0x20) && !HLPFILE_RtfAddControl(rd
, "\\brdrs")) goto done
;
1201 if (brdr
& 0x40 && !HLPFILE_RtfAddControl(rd
, "\\brdrdb")) goto done
;
1204 w
= GET_SHORT(format
, 0); format
+= 2;
1207 sprintf(tmp
, "\\brdrw%d", HLPFILE_HalfPointsToTwips(w
));
1208 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1213 int i
, ntab
= fetch_short(&format
);
1217 for (i
= 0; i
< ntab
; i
++)
1219 tab
= fetch_ushort(&format
);
1220 ts
= (tab
& 0x4000) ? fetch_ushort(&format
) : 0 /* left */;
1223 default: WINE_FIXME("Unknown tab style %x\n", ts
);
1225 case 0: kind
= ""; break;
1226 case 1: kind
= "\\tqr"; break;
1227 case 2: kind
= "\\tqc"; break;
1229 /* FIXME: do kind */
1230 sprintf(tmp
, "%s\\tx%d",
1231 kind
, HLPFILE_HalfPointsToTwips(tab
& 0x3FFF));
1232 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1235 switch (bits
& 0xc00)
1237 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1238 case 0: if (!HLPFILE_RtfAddControl(rd
, "\\ql")) goto done
; break;
1239 case 0x400: if (!HLPFILE_RtfAddControl(rd
, "\\qr")) goto done
; break;
1240 case 0x800: if (!HLPFILE_RtfAddControl(rd
, "\\qc")) goto done
; break;
1243 /* 0x1000 doesn't need space */
1244 if ((bits
& 0x1000) && !HLPFILE_RtfAddControl(rd
, "\\keep")) goto done
;
1245 if ((bits
& 0xE080) != 0)
1246 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits
);
1248 while (text
< text_end
&& format
< format_end
)
1250 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text
), text
, text_end
, format
, format_end
);
1251 textsize
= strlen(text
);
1254 if (rd
->force_color
)
1256 if ((rd
->current_link
->cookie
== hlp_link_popup
) ?
1257 !HLPFILE_RtfAddControl(rd
, "{\\uld\\cf1") :
1258 !HLPFILE_RtfAddControl(rd
, "{\\ul\\cf1")) goto done
;
1260 if (!HLPFILE_RtfAddText(rd
, text
)) goto done
;
1261 if (rd
->force_color
&& !HLPFILE_RtfAddControl(rd
, "}")) goto done
;
1262 rd
->char_pos
+= textsize
;
1264 /* else: null text, keep on storing attributes */
1265 text
+= textsize
+ 1;
1267 if (*format
== 0xff)
1273 WINE_TRACE("format=%02x\n", *format
);
1277 WINE_FIXME("NIY20\n");
1282 WINE_FIXME("NIY21\n");
1288 unsigned font
= GET_USHORT(format
, 1);
1291 WINE_TRACE("Changing font to %d\n", font
);
1293 switch (rd
->font_scale
)
1295 case 0: fs
= (4 * page
->file
->fonts
[font
].LogFont
.lfHeight
- 13) / 5; break;
1297 case 1: fs
= (4 * page
->file
->fonts
[font
].LogFont
.lfHeight
- 3) / 5; break;
1298 case 2: fs
= (4 * page
->file
->fonts
[font
].LogFont
.lfHeight
+ 17) / 5; break;
1300 /* FIXME: missing at least colors, also bold attribute looses information */
1302 sprintf(tmp
, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1304 page
->file
->fonts
[font
].LogFont
.lfWeight
> 400 ? "\\b" : "\\b0",
1305 page
->file
->fonts
[font
].LogFont
.lfItalic
? "\\i" : "\\i0",
1306 page
->file
->fonts
[font
].LogFont
.lfUnderline
? "\\ul" : "\\ul0",
1307 page
->file
->fonts
[font
].LogFont
.lfStrikeOut
? "\\strike" : "\\strike0");
1308 if (!HLPFILE_RtfAddControl(rd
, tmp
)) goto done
;
1313 if (!HLPFILE_RtfAddControl(rd
, "\\line")) goto done
;
1321 if (format
[1] != 0xFF)
1323 if (!HLPFILE_RtfAddControl(rd
, "\\par\\intbl")) goto done
;
1327 if (!HLPFILE_RtfAddControl(rd
, "\\cell\\pard\\intbl")) goto done
;
1330 else if (!HLPFILE_RtfAddControl(rd
, "\\par")) goto done
;
1336 if (!HLPFILE_RtfAddControl(rd
, "\\tab")) goto done
;
1351 BYTE type
= format
[1];
1354 /* FIXME: we don't use 'BYTE pos = (*format - 0x86);' for the image position */
1356 size
= fetch_long(&format
);
1361 fetch_ushort(&format
); /* hot spot */
1364 switch (GET_SHORT(format
, 0))
1367 HLPFILE_RtfAddGfxByIndex(rd
, page
->file
, GET_SHORT(format
, 2));
1371 WINE_FIXME("does it work ??? %x<%lu>#%u\n",
1372 GET_SHORT(format
, 0),
1373 size
, GET_SHORT(format
, 2));
1374 HLPFILE_RtfAddGfxByAddr(rd
, page
->file
, format
+ 2, size
- 4);
1378 WINE_FIXME("??? %u\n", GET_SHORT(format
, 0));
1383 WINE_FIXME("Got an embedded element %s\n", format
+ 6);
1386 WINE_FIXME("Got a type %d picture\n", type
);
1395 if (!rd
->current_link
)
1396 WINE_FIXME("No existing link\n");
1397 rd
->current_link
->cpMax
= rd
->char_pos
;
1398 rd
->current_link
= NULL
;
1399 rd
->force_color
= FALSE
;
1403 if (!HLPFILE_RtfAddControl(rd
, "\\~")) goto done
;
1409 if (!HLPFILE_RtfAddControl(rd
, "\\_")) goto done
;
1410 /* FIXME: it could be that hypen is also in input stream !! */
1423 WINE_TRACE("macro => %s\n", format
+ 3);
1424 HLPFILE_AllocLink(rd
, hlp_link_macro
, (const char*)format
+ 3,
1425 GET_USHORT(format
, 1), 0, !(*format
& 4), -1);
1426 format
+= 3 + GET_USHORT(format
, 1);
1431 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format
, 1));
1432 HLPFILE_AllocLink(rd
, (*format
& 1) ? hlp_link_link
: hlp_link_popup
,
1433 page
->file
->lpszPath
, -1, GET_UINT(format
, 1)-16, 1, -1);
1443 HLPFILE_AllocLink(rd
, (*format
& 1) ? hlp_link_link
: hlp_link_popup
,
1444 page
->file
->lpszPath
, -1, GET_UINT(format
, 1),
1445 !(*format
& 4), -1);
1454 char* ptr
= (char*) format
+ 8;
1455 BYTE type
= format
[3];
1464 ptr
= page
->file
->lpszPath
;
1467 for (wnd
= page
->file
->numWindows
- 1; wnd
>= 0; wnd
--)
1469 if (!strcmp(ptr
, page
->file
->windows
[wnd
].name
)) break;
1472 WINE_WARN("Couldn't find window info for %s\n", ptr
);
1473 ptr
+= strlen(ptr
) + 1;
1478 WINE_WARN("Unknown link type %d\n", type
);
1481 HLPFILE_AllocLink(rd
, (*format
& 1) ? hlp_link_link
: hlp_link_popup
,
1482 ptr
, -1, GET_UINT(format
, 4), !(*format
& 4), wnd
);
1484 format
+= 3 + GET_USHORT(format
, 1);
1488 WINE_WARN("format %02x\n", *format
);
1495 if (!HLPFILE_RtfAddControl(rd
, "\\row\\par\\pard\\plain")) goto done
;
1501 HeapFree(GetProcessHeap(), 0, text_base
);
1505 /******************************************************************
1506 * HLPFILE_BrowsePage
1509 BOOL
HLPFILE_BrowsePage(HLPFILE_PAGE
* page
, struct RtfData
* rd
,
1510 unsigned font_scale
, unsigned relative
)
1512 HLPFILE
*hlpfile
= page
->file
;
1514 DWORD ref
= page
->reference
;
1515 unsigned index
, old_index
= -1, offset
, count
= 0, offs
= 0;
1516 unsigned cpg
, parlen
;
1518 const char* ck
= NULL
;
1521 rd
->data
= rd
->ptr
= HeapAlloc(GetProcessHeap(), 0, rd
->allocated
= 32768);
1523 rd
->first_link
= rd
->current_link
= NULL
;
1524 rd
->force_color
= FALSE
;
1525 rd
->font_scale
= font_scale
;
1526 rd
->relative
= relative
;
1527 rd
->char_pos_rel
= 0;
1529 switch (hlpfile
->charset
)
1531 case DEFAULT_CHARSET
:
1532 case ANSI_CHARSET
: cpg
= 1252; break;
1533 case SHIFTJIS_CHARSET
: cpg
= 932; break;
1534 case HANGEUL_CHARSET
: cpg
= 949; break;
1535 case GB2312_CHARSET
: cpg
= 936; break;
1536 case CHINESEBIG5_CHARSET
: cpg
= 950; break;
1537 case GREEK_CHARSET
: cpg
= 1253; break;
1538 case TURKISH_CHARSET
: cpg
= 1254; break;
1539 case HEBREW_CHARSET
: cpg
= 1255; break;
1540 case ARABIC_CHARSET
: cpg
= 1256; break;
1541 case BALTIC_CHARSET
: cpg
= 1257; break;
1542 case VIETNAMESE_CHARSET
: cpg
= 1258; break;
1543 case RUSSIAN_CHARSET
: cpg
= 1251; break;
1544 case EE_CHARSET
: cpg
= 1250; break;
1545 case THAI_CHARSET
: cpg
= 874; break;
1546 case JOHAB_CHARSET
: cpg
= 1361; break;
1547 case MAC_CHARSET
: ck
= "mac"; break;
1549 WINE_FIXME("Unsupported charset %u\n", hlpfile
->charset
);
1554 sprintf(tmp
, "{\\rtf1\\%s\\deff0", ck
);
1555 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1559 sprintf(tmp
, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg
);
1560 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1563 /* generate font table */
1564 if (!HLPFILE_RtfAddControl(rd
, "{\\fonttbl")) return FALSE
;
1565 for (index
= 0; index
< hlpfile
->numFonts
; index
++)
1568 switch (hlpfile
->fonts
[index
].LogFont
.lfPitchAndFamily
& 0xF0)
1570 case FF_MODERN
: family
= "modern"; break;
1571 case FF_ROMAN
: family
= "roman"; break;
1572 case FF_SWISS
: family
= "swiss"; break;
1573 case FF_SCRIPT
: family
= "script"; break;
1574 case FF_DECORATIVE
: family
= "decor"; break;
1575 default: family
= "nil"; break;
1577 sprintf(tmp
, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1579 hlpfile
->fonts
[index
].LogFont
.lfPitchAndFamily
& 0x0F,
1580 hlpfile
->fonts
[index
].LogFont
.lfCharSet
,
1581 hlpfile
->fonts
[index
].LogFont
.lfFaceName
);
1582 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1584 if (!HLPFILE_RtfAddControl(rd
, "}")) return FALSE
;
1585 /* generate color table */
1586 if (!HLPFILE_RtfAddControl(rd
, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE
;
1587 for (index
= 0; index
< hlpfile
->numFonts
; index
++)
1590 switch (hlpfile
->fonts
[index
].LogFont
.lfPitchAndFamily
& 0xF0)
1592 case FF_MODERN
: family
= "modern"; break;
1593 case FF_ROMAN
: family
= "roman"; break;
1594 case FF_SWISS
: family
= "swiss"; break;
1595 case FF_SCRIPT
: family
= "script"; break;
1596 case FF_DECORATIVE
: family
= "decor"; break;
1597 default: family
= "nil"; break;
1599 sprintf(tmp
, "\\red%d\\green%d\\blue%d;",
1600 GetRValue(hlpfile
->fonts
[index
].color
),
1601 GetGValue(hlpfile
->fonts
[index
].color
),
1602 GetBValue(hlpfile
->fonts
[index
].color
));
1603 if (!HLPFILE_RtfAddControl(rd
, tmp
)) return FALSE
;
1605 if (!HLPFILE_RtfAddControl(rd
, "}")) return FALSE
;
1609 if (hlpfile
->version
<= 16)
1611 index
= (ref
- 0x0C) / hlpfile
->dsize
;
1612 offset
= (ref
- 0x0C) % hlpfile
->dsize
;
1616 index
= (ref
- 0x0C) >> 14;
1617 offset
= (ref
- 0x0C) & 0x3FFF;
1620 if (hlpfile
->version
<= 16 && index
!= old_index
&& old_index
!= -1)
1622 /* we jumped to the next block, adjust pointers */
1627 if (index
>= hlpfile
->topic_maplen
) {WINE_WARN("maplen\n"); break;}
1628 buf
= hlpfile
->topic_map
[index
] + offset
;
1629 if (buf
+ 0x15 >= hlpfile
->topic_end
) {WINE_WARN("extra\n"); break;}
1630 end
= min(buf
+ GET_UINT(buf
, 0), hlpfile
->topic_end
);
1631 if (index
!= old_index
) {offs
= 0; old_index
= index
;}
1636 if (count
++) goto done
;
1641 if (!HLPFILE_BrowseParagraph(page
, rd
, buf
, end
, &parlen
)) return FALSE
;
1642 if (relative
>= index
* 0x8000 + offs
)
1643 rd
->char_pos_rel
= rd
->char_pos
;
1647 WINE_ERR("buf[0x14] = %x\n", buf
[0x14]);
1649 if (hlpfile
->version
<= 16)
1651 ref
+= GET_UINT(buf
, 0xc);
1652 if (GET_UINT(buf
, 0xc) == 0)
1656 ref
= GET_UINT(buf
, 0xc);
1657 } while (ref
!= 0xffffffff);
1659 page
->first_link
= rd
->first_link
;
1660 return HLPFILE_RtfAddControl(rd
, "}");
1663 /******************************************************************
1668 static BOOL
HLPFILE_ReadFont(HLPFILE
* hlpfile
)
1671 unsigned i
, len
, idx
;
1672 unsigned face_num
, dscr_num
, face_offset
, dscr_offset
;
1675 if (!HLPFILE_FindSubFile(hlpfile
, "|FONT", &ref
, &end
))
1677 WINE_WARN("no subfile FONT\n");
1678 hlpfile
->numFonts
= 0;
1679 hlpfile
->fonts
= NULL
;
1685 face_num
= GET_USHORT(ref
, 0);
1686 dscr_num
= GET_USHORT(ref
, 2);
1687 face_offset
= GET_USHORT(ref
, 4);
1688 dscr_offset
= GET_USHORT(ref
, 6);
1690 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1691 face_num
, face_offset
, dscr_num
, dscr_offset
);
1693 hlpfile
->numFonts
= dscr_num
;
1694 hlpfile
->fonts
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT
) * dscr_num
);
1696 len
= (dscr_offset
- face_offset
) / face_num
;
1697 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1698 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1699 for (i
= 0; i
< dscr_num
; i
++)
1701 flag
= ref
[dscr_offset
+ i
* 11 + 0];
1702 family
= ref
[dscr_offset
+ i
* 11 + 2];
1704 hlpfile
->fonts
[i
].LogFont
.lfHeight
= ref
[dscr_offset
+ i
* 11 + 1];
1705 hlpfile
->fonts
[i
].LogFont
.lfWidth
= 0;
1706 hlpfile
->fonts
[i
].LogFont
.lfEscapement
= 0;
1707 hlpfile
->fonts
[i
].LogFont
.lfOrientation
= 0;
1708 hlpfile
->fonts
[i
].LogFont
.lfWeight
= (flag
& 1) ? 700 : 400;
1709 hlpfile
->fonts
[i
].LogFont
.lfItalic
= (flag
& 2) ? TRUE
: FALSE
;
1710 hlpfile
->fonts
[i
].LogFont
.lfUnderline
= (flag
& 4) ? TRUE
: FALSE
;
1711 hlpfile
->fonts
[i
].LogFont
.lfStrikeOut
= (flag
& 8) ? TRUE
: FALSE
;
1712 hlpfile
->fonts
[i
].LogFont
.lfCharSet
= hlpfile
->charset
;
1713 hlpfile
->fonts
[i
].LogFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
1714 hlpfile
->fonts
[i
].LogFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
1715 hlpfile
->fonts
[i
].LogFont
.lfQuality
= DEFAULT_QUALITY
;
1716 hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
= DEFAULT_PITCH
;
1720 case 0x01: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_MODERN
; break;
1721 case 0x02: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_ROMAN
; break;
1722 case 0x03: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_SWISS
; break;
1723 case 0x04: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_SCRIPT
; break;
1724 case 0x05: hlpfile
->fonts
[i
].LogFont
.lfPitchAndFamily
|= FF_DECORATIVE
; break;
1725 default: WINE_FIXME("Unknown family %u\n", family
);
1727 idx
= GET_USHORT(ref
, dscr_offset
+ i
* 11 + 3);
1731 memcpy(hlpfile
->fonts
[i
].LogFont
.lfFaceName
, ref
+ face_offset
+ idx
* len
, min(len
, LF_FACESIZE
- 1));
1732 hlpfile
->fonts
[i
].LogFont
.lfFaceName
[min(len
, LF_FACESIZE
- 1)] = '\0';
1736 WINE_FIXME("Too high face ref (%u/%u)\n", idx
, face_num
);
1737 strcpy(hlpfile
->fonts
[i
].LogFont
.lfFaceName
, "Helv");
1739 hlpfile
->fonts
[i
].hFont
= 0;
1740 hlpfile
->fonts
[i
].color
= RGB(ref
[dscr_offset
+ i
* 11 + 5],
1741 ref
[dscr_offset
+ i
* 11 + 6],
1742 ref
[dscr_offset
+ i
* 11 + 7]);
1743 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1744 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1750 X(4, "dblUnderline"),
1752 ref
[dscr_offset
+ i
* 11 + 1],
1754 hlpfile
->fonts
[i
].LogFont
.lfFaceName
, idx
,
1755 GET_UINT(ref
, dscr_offset
+ i
* 11 + 5) & 0x00FFFFFF);
1760 /***********************************************************************
1762 * HLPFILE_ReadFileToBuffer
1764 static BOOL
HLPFILE_ReadFileToBuffer(HLPFILE
* hlpfile
, HFILE hFile
)
1766 BYTE header
[16], dummy
[1];
1768 if (_hread(hFile
, header
, 16) != 16) {WINE_WARN("header\n"); return FALSE
;};
1771 if (GET_UINT(header
, 0) != 0x00035F3F)
1772 {WINE_WARN("wrong header\n"); return FALSE
;};
1774 hlpfile
->file_buffer_size
= GET_UINT(header
, 12);
1775 hlpfile
->file_buffer
= HeapAlloc(GetProcessHeap(), 0, hlpfile
->file_buffer_size
+ 1);
1776 if (!hlpfile
->file_buffer
) return FALSE
;
1778 memcpy(hlpfile
->file_buffer
, header
, 16);
1779 if (_hread(hFile
, hlpfile
->file_buffer
+ 16, hlpfile
->file_buffer_size
- 16) !=hlpfile
->file_buffer_size
- 16)
1780 {WINE_WARN("filesize1\n"); return FALSE
;};
1782 if (_hread(hFile
, dummy
, 1) != 0) WINE_WARN("filesize2\n");
1784 hlpfile
->file_buffer
[hlpfile
->file_buffer_size
] = '\0'; /* FIXME: was '0', sounds backwards to me */
1789 /**************************************************************************
1792 * HLPFILE_BPTreeCompare function for HLPFILE directory.
1795 static int comp_FindSubFile(void *p
, const void *key
,
1796 int leaf
, void** next
)
1798 *next
= (char *)p
+strlen(p
)+(leaf
?5:3);
1799 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p
, (char *)key
);
1800 return strcmp(p
, key
);
1803 /***********************************************************************
1805 * HLPFILE_FindSubFile
1807 static BOOL
HLPFILE_FindSubFile(HLPFILE
* hlpfile
, LPCSTR name
, BYTE
**subbuf
, BYTE
**subend
)
1811 WINE_TRACE("looking for file '%s'\n", name
);
1812 ptr
= HLPFILE_BPTreeSearch(hlpfile
->file_buffer
+ GET_UINT(hlpfile
->file_buffer
, 4),
1813 name
, comp_FindSubFile
);
1814 if (!ptr
) return FALSE
;
1815 *subbuf
= hlpfile
->file_buffer
+ GET_UINT(ptr
, strlen(name
)+1);
1816 if (*subbuf
>= hlpfile
->file_buffer
+ hlpfile
->file_buffer_size
)
1818 WINE_ERR("internal file %s does not fit\n", name
);
1821 *subend
= *subbuf
+ GET_UINT(*subbuf
, 0);
1822 if (*subend
> hlpfile
->file_buffer
+ hlpfile
->file_buffer_size
)
1824 WINE_ERR("internal file %s does not fit\n", name
);
1827 if (GET_UINT(*subbuf
, 0) < GET_UINT(*subbuf
, 4) + 9)
1829 WINE_ERR("invalid size provided for internal file %s\n", name
);
1835 /***********************************************************************
1837 * HLPFILE_SystemCommands
1839 static BOOL
HLPFILE_SystemCommands(HLPFILE
* hlpfile
)
1841 BYTE
*buf
, *ptr
, *end
;
1842 HLPFILE_MACRO
*macro
, **m
;
1844 unsigned short magic
, minor
, major
, flags
;
1846 hlpfile
->lpszTitle
= NULL
;
1848 if (!HLPFILE_FindSubFile(hlpfile
, "|SYSTEM", &buf
, &end
)) return FALSE
;
1850 magic
= GET_USHORT(buf
+ 9, 0);
1851 minor
= GET_USHORT(buf
+ 9, 2);
1852 major
= GET_USHORT(buf
+ 9, 4);
1853 /* gen date on 4 bytes */
1854 flags
= GET_USHORT(buf
+ 9, 10);
1855 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1856 magic
, major
, minor
, flags
);
1857 if (magic
!= 0x036C || major
!= 1)
1858 {WINE_WARN("Wrong system header\n"); return FALSE
;}
1861 hlpfile
->tbsize
= 0x800;
1862 hlpfile
->compressed
= 0;
1864 else if (flags
== 0)
1866 hlpfile
->tbsize
= 0x1000;
1867 hlpfile
->compressed
= 0;
1869 else if (flags
== 4)
1871 hlpfile
->tbsize
= 0x1000;
1872 hlpfile
->compressed
= 1;
1876 hlpfile
->tbsize
= 0x800;
1877 hlpfile
->compressed
= 1;
1880 if (hlpfile
->compressed
)
1881 hlpfile
->dsize
= 0x4000;
1883 hlpfile
->dsize
= hlpfile
->tbsize
- 0x0C;
1885 hlpfile
->version
= minor
;
1886 hlpfile
->flags
= flags
;
1887 hlpfile
->charset
= DEFAULT_CHARSET
;
1889 for (ptr
= buf
+ 0x15; ptr
+ 4 <= end
; ptr
+= GET_USHORT(ptr
, 2) + 4)
1891 char *str
= (char*) ptr
+ 4;
1892 switch (GET_USHORT(ptr
, 0))
1895 if (hlpfile
->lpszTitle
) {WINE_WARN("title\n"); break;}
1896 hlpfile
->lpszTitle
= HeapAlloc(GetProcessHeap(), 0, strlen(str
) + 1);
1897 if (!hlpfile
->lpszTitle
) return FALSE
;
1898 lstrcpy(hlpfile
->lpszTitle
, str
);
1899 WINE_TRACE("Title: %s\n", hlpfile
->lpszTitle
);
1903 if (hlpfile
->lpszCopyright
) {WINE_WARN("copyright\n"); break;}
1904 hlpfile
->lpszCopyright
= HeapAlloc(GetProcessHeap(), 0, strlen(str
) + 1);
1905 if (!hlpfile
->lpszCopyright
) return FALSE
;
1906 lstrcpy(hlpfile
->lpszCopyright
, str
);
1907 WINE_TRACE("Copyright: %s\n", hlpfile
->lpszCopyright
);
1911 if (GET_USHORT(ptr
, 2) != 4) {WINE_WARN("system3\n");break;}
1912 hlpfile
->contents_start
= GET_UINT(ptr
, 4);
1913 WINE_TRACE("Setting contents start at %08lx\n", hlpfile
->contents_start
);
1917 macro
= HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO
) + lstrlen(str
) + 1);
1919 p
= (char*)macro
+ sizeof(HLPFILE_MACRO
);
1921 macro
->lpszMacro
= p
;
1923 for (m
= &hlpfile
->first_macro
; *m
; m
= &(*m
)->next
);
1928 if (GET_USHORT(ptr
, 4 + 4) != 1)
1929 WINE_FIXME("More than one icon, picking up first\n");
1930 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
1931 hlpfile
->hIcon
= CreateIconFromResourceEx(ptr
+ 4 + 0x16,
1932 GET_USHORT(ptr
, 2) - 0x16, TRUE
,
1937 if (GET_USHORT(ptr
, 2) != 90) {WINE_WARN("system6\n");break;}
1939 if (hlpfile
->windows
)
1940 hlpfile
->windows
= HeapReAlloc(GetProcessHeap(), 0, hlpfile
->windows
,
1941 sizeof(HLPFILE_WINDOWINFO
) * ++hlpfile
->numWindows
);
1943 hlpfile
->windows
= HeapAlloc(GetProcessHeap(), 0,
1944 sizeof(HLPFILE_WINDOWINFO
) * ++hlpfile
->numWindows
);
1946 if (hlpfile
->windows
)
1948 unsigned flags
= GET_USHORT(ptr
, 4);
1949 HLPFILE_WINDOWINFO
* wi
= &hlpfile
->windows
[hlpfile
->numWindows
- 1];
1951 if (flags
& 0x0001) strcpy(wi
->type
, &str
[2]);
1952 else wi
->type
[0] = '\0';
1953 if (flags
& 0x0002) strcpy(wi
->name
, &str
[12]);
1954 else wi
->name
[0] = '\0';
1955 if (flags
& 0x0004) strcpy(wi
->caption
, &str
[21]);
1956 else lstrcpynA(wi
->caption
, hlpfile
->lpszTitle
, sizeof(wi
->caption
));
1957 wi
->origin
.x
= (flags
& 0x0008) ? GET_USHORT(ptr
, 76) : CW_USEDEFAULT
;
1958 wi
->origin
.y
= (flags
& 0x0010) ? GET_USHORT(ptr
, 78) : CW_USEDEFAULT
;
1959 wi
->size
.cx
= (flags
& 0x0020) ? GET_USHORT(ptr
, 80) : CW_USEDEFAULT
;
1960 wi
->size
.cy
= (flags
& 0x0040) ? GET_USHORT(ptr
, 82) : CW_USEDEFAULT
;
1961 wi
->style
= (flags
& 0x0080) ? GET_USHORT(ptr
, 84) : SW_SHOW
;
1962 wi
->win_style
= WS_OVERLAPPEDWINDOW
;
1963 wi
->sr_color
= (flags
& 0x0100) ? GET_UINT(ptr
, 86) : 0xFFFFFF;
1964 wi
->nsr_color
= (flags
& 0x0200) ? GET_UINT(ptr
, 90) : 0xFFFFFF;
1965 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",
1966 flags
& 0x0001 ? 'T' : 't',
1967 flags
& 0x0002 ? 'N' : 'n',
1968 flags
& 0x0004 ? 'C' : 'c',
1969 flags
& 0x0008 ? 'X' : 'x',
1970 flags
& 0x0010 ? 'Y' : 'y',
1971 flags
& 0x0020 ? 'W' : 'w',
1972 flags
& 0x0040 ? 'H' : 'h',
1973 flags
& 0x0080 ? 'S' : 's',
1974 wi
->type
, wi
->name
, wi
->caption
, wi
->origin
.x
, wi
->origin
.y
,
1975 wi
->size
.cx
, wi
->size
.cy
);
1979 WINE_WARN("Citation: '%s'\n", ptr
+ 4);
1982 hlpfile
->charset
= ptr
[4];
1983 WINE_TRACE("Charset: %d\n", hlpfile
->charset
);
1986 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr
, 0));
1989 if (!hlpfile
->lpszTitle
)
1990 hlpfile
->lpszTitle
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 1);
1994 /***********************************************************************
1996 * HLPFILE_UncompressedLZ77_Size
1998 static INT
HLPFILE_UncompressedLZ77_Size(const BYTE
*ptr
, const BYTE
*end
)
2005 for (i
= 0; i
< 8 && ptr
< end
; i
++, mask
>>= 1)
2009 int code
= GET_USHORT(ptr
, 0);
2010 int len
= 3 + (code
>> 12);
2014 else newsize
++, ptr
++;
2021 /***********************************************************************
2023 * HLPFILE_UncompressLZ77
2025 static BYTE
*HLPFILE_UncompressLZ77(const BYTE
*ptr
, const BYTE
*end
, BYTE
*newptr
)
2032 for (i
= 0; i
< 8 && ptr
< end
; i
++, mask
>>= 1)
2036 int code
= GET_USHORT(ptr
, 0);
2037 int len
= 3 + (code
>> 12);
2038 int offset
= code
& 0xfff;
2040 * We must copy byte-by-byte here. We cannot use memcpy nor
2041 * memmove here. Just example:
2042 * a[]={1,2,3,4,5,6,7,8,9,10}
2046 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
2048 for (; len
>0; len
--, newptr
++) *newptr
= *(newptr
-offset
-1);
2051 else *newptr
++ = *ptr
++;
2058 /***********************************************************************
2060 * HLPFILE_UncompressLZ77_Phrases
2062 static BOOL
HLPFILE_UncompressLZ77_Phrases(HLPFILE
* hlpfile
)
2064 UINT i
, num
, dec_size
, head_size
;
2067 if (!HLPFILE_FindSubFile(hlpfile
, "|Phrases", &buf
, &end
)) return FALSE
;
2069 if (hlpfile
->version
<= 16)
2074 num
= hlpfile
->num_phrases
= GET_USHORT(buf
, 9);
2075 if (buf
+ 2 * num
+ 0x13 >= end
) {WINE_WARN("1a\n"); return FALSE
;};
2077 if (hlpfile
->version
<= 16)
2078 dec_size
= end
- buf
- 15 - 2 * num
;
2080 dec_size
= HLPFILE_UncompressedLZ77_Size(buf
+ 0x13 + 2 * num
, end
);
2082 hlpfile
->phrases_offsets
= HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num
+ 1));
2083 hlpfile
->phrases_buffer
= HeapAlloc(GetProcessHeap(), 0, dec_size
);
2084 if (!hlpfile
->phrases_offsets
|| !hlpfile
->phrases_buffer
)
2086 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_offsets
);
2087 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_buffer
);
2091 for (i
= 0; i
<= num
; i
++)
2092 hlpfile
->phrases_offsets
[i
] = GET_USHORT(buf
, head_size
+ 2 * i
) - 2 * num
- 2;
2094 if (hlpfile
->version
<= 16)
2095 memcpy(hlpfile
->phrases_buffer
, buf
+ 15 + 2*num
, dec_size
);
2097 HLPFILE_UncompressLZ77(buf
+ 0x13 + 2 * num
, end
, (BYTE
*)hlpfile
->phrases_buffer
);
2099 hlpfile
->hasPhrases
= TRUE
;
2103 /***********************************************************************
2105 * HLPFILE_Uncompress_Phrases40
2107 static BOOL
HLPFILE_Uncompress_Phrases40(HLPFILE
* hlpfile
)
2110 INT dec_size
, cpr_size
;
2111 BYTE
*buf_idx
, *end_idx
;
2112 BYTE
*buf_phs
, *end_phs
;
2113 long* ptr
, mask
= 0;
2115 unsigned short bc
, n
;
2117 if (!HLPFILE_FindSubFile(hlpfile
, "|PhrIndex", &buf_idx
, &end_idx
) ||
2118 !HLPFILE_FindSubFile(hlpfile
, "|PhrImage", &buf_phs
, &end_phs
)) return FALSE
;
2120 ptr
= (long*)(buf_idx
+ 9 + 28);
2121 bc
= GET_USHORT(buf_idx
, 9 + 24) & 0x0F;
2122 num
= hlpfile
->num_phrases
= GET_USHORT(buf_idx
, 9 + 4);
2124 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2125 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2126 GET_UINT(buf_idx
, 9 + 0),
2127 GET_UINT(buf_idx
, 9 + 4),
2128 GET_UINT(buf_idx
, 9 + 8),
2129 GET_UINT(buf_idx
, 9 + 12),
2130 GET_UINT(buf_idx
, 9 + 16),
2131 GET_UINT(buf_idx
, 9 + 20),
2132 GET_USHORT(buf_idx
, 9 + 24),
2133 GET_USHORT(buf_idx
, 9 + 26));
2135 dec_size
= GET_UINT(buf_idx
, 9 + 12);
2136 cpr_size
= GET_UINT(buf_idx
, 9 + 16);
2138 if (dec_size
!= cpr_size
&&
2139 dec_size
!= HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
))
2141 WINE_WARN("size mismatch %u %u\n",
2142 dec_size
, HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
));
2143 dec_size
= max(dec_size
, HLPFILE_UncompressedLZ77_Size(buf_phs
+ 9, end_phs
));
2146 hlpfile
->phrases_offsets
= HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num
+ 1));
2147 hlpfile
->phrases_buffer
= HeapAlloc(GetProcessHeap(), 0, dec_size
);
2148 if (!hlpfile
->phrases_offsets
|| !hlpfile
->phrases_buffer
)
2150 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_offsets
);
2151 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_buffer
);
2155 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
2157 hlpfile
->phrases_offsets
[0] = 0;
2158 for (i
= 0; i
< num
; i
++)
2160 for (n
= 1; getbit(); n
+= 1 << bc
);
2162 if (bc
> 1 && getbit()) n
+= 2;
2163 if (bc
> 2 && getbit()) n
+= 4;
2164 if (bc
> 3 && getbit()) n
+= 8;
2165 if (bc
> 4 && getbit()) n
+= 16;
2166 hlpfile
->phrases_offsets
[i
+ 1] = hlpfile
->phrases_offsets
[i
] + n
;
2170 if (dec_size
== cpr_size
)
2171 memcpy(hlpfile
->phrases_buffer
, buf_phs
+ 9, dec_size
);
2173 HLPFILE_UncompressLZ77(buf_phs
+ 9, end_phs
, (BYTE
*)hlpfile
->phrases_buffer
);
2175 hlpfile
->hasPhrases40
= TRUE
;
2179 /***********************************************************************
2181 * HLPFILE_Uncompress_Topic
2183 static BOOL
HLPFILE_Uncompress_Topic(HLPFILE
* hlpfile
)
2185 BYTE
*buf
, *ptr
, *end
, *newptr
;
2186 unsigned int i
, newsize
= 0;
2187 unsigned int topic_size
;
2189 if (!HLPFILE_FindSubFile(hlpfile
, "|TOPIC", &buf
, &end
))
2190 {WINE_WARN("topic0\n"); return FALSE
;}
2192 buf
+= 9; /* Skip file header */
2193 topic_size
= end
- buf
;
2194 if (hlpfile
->compressed
)
2196 hlpfile
->topic_maplen
= (topic_size
- 1) / hlpfile
->tbsize
+ 1;
2198 for (i
= 0; i
< hlpfile
->topic_maplen
; i
++)
2200 ptr
= buf
+ i
* hlpfile
->tbsize
;
2202 /* I don't know why, it's necessary for printman.hlp */
2203 if (ptr
+ 0x44 > end
) ptr
= end
- 0x44;
2205 newsize
+= HLPFILE_UncompressedLZ77_Size(ptr
+ 0xc, min(end
, ptr
+ hlpfile
->tbsize
));
2208 hlpfile
->topic_map
= HeapAlloc(GetProcessHeap(), 0,
2209 hlpfile
->topic_maplen
* sizeof(hlpfile
->topic_map
[0]) + newsize
);
2210 if (!hlpfile
->topic_map
) return FALSE
;
2211 newptr
= (BYTE
*)(hlpfile
->topic_map
+ hlpfile
->topic_maplen
);
2212 hlpfile
->topic_end
= newptr
+ newsize
;
2214 for (i
= 0; i
< hlpfile
->topic_maplen
; i
++)
2216 ptr
= buf
+ i
* hlpfile
->tbsize
;
2217 if (ptr
+ 0x44 > end
) ptr
= end
- 0x44;
2219 hlpfile
->topic_map
[i
] = newptr
;
2220 newptr
= HLPFILE_UncompressLZ77(ptr
+ 0xc, min(end
, ptr
+ hlpfile
->tbsize
), newptr
);
2225 /* basically, we need to copy the TopicBlockSize byte pages
2226 * (removing the first 0x0C) in one single area in memory
2228 hlpfile
->topic_maplen
= (topic_size
- 1) / hlpfile
->tbsize
+ 1;
2229 hlpfile
->topic_map
= HeapAlloc(GetProcessHeap(), 0,
2230 hlpfile
->topic_maplen
* (sizeof(hlpfile
->topic_map
[0]) + hlpfile
->dsize
));
2231 if (!hlpfile
->topic_map
) return FALSE
;
2232 newptr
= (BYTE
*)(hlpfile
->topic_map
+ hlpfile
->topic_maplen
);
2233 hlpfile
->topic_end
= newptr
+ topic_size
;
2235 for (i
= 0; i
< hlpfile
->topic_maplen
; i
++)
2237 hlpfile
->topic_map
[i
] = newptr
+ i
* hlpfile
->dsize
;
2238 memcpy(hlpfile
->topic_map
[i
], buf
+ i
* hlpfile
->tbsize
+ 0x0C, hlpfile
->dsize
);
2244 /***********************************************************************
2246 * HLPFILE_Uncompress2
2249 static void HLPFILE_Uncompress2(HLPFILE
* hlpfile
, const BYTE
*ptr
, const BYTE
*end
, BYTE
*newptr
, const BYTE
*newend
)
2251 BYTE
*phptr
, *phend
;
2255 while (ptr
< end
&& newptr
< newend
)
2257 if (!*ptr
|| *ptr
>= 0x10)
2261 code
= 0x100 * ptr
[0] + ptr
[1];
2262 index
= (code
- 0x100) / 2;
2264 phptr
= (BYTE
*)hlpfile
->phrases_buffer
+ hlpfile
->phrases_offsets
[index
];
2265 phend
= (BYTE
*)hlpfile
->phrases_buffer
+ hlpfile
->phrases_offsets
[index
+ 1];
2267 if (newptr
+ (phend
- phptr
) > newend
)
2269 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
2270 newptr
, newend
, (SIZE_T
)(phend
- phptr
));
2273 memcpy(newptr
, phptr
, phend
- phptr
);
2274 newptr
+= phend
- phptr
;
2275 if (code
& 1) *newptr
++ = ' ';
2280 if (newptr
> newend
) WINE_FIXME("buffer overflow %p > %p\n", newptr
, newend
);
2283 /******************************************************************
2284 * HLPFILE_Uncompress3
2288 static BOOL
HLPFILE_Uncompress3(HLPFILE
* hlpfile
, char* dst
, const char* dst_end
,
2289 const BYTE
* src
, const BYTE
* src_end
)
2291 unsigned int idx
, len
;
2293 for (; src
< src_end
; src
++)
2295 if ((*src
& 1) == 0)
2298 if (idx
> hlpfile
->num_phrases
)
2300 WINE_ERR("index in phrases %d/%d\n", idx
, hlpfile
->num_phrases
);
2305 len
= hlpfile
->phrases_offsets
[idx
+ 1] - hlpfile
->phrases_offsets
[idx
];
2306 if (dst
+ len
<= dst_end
)
2307 memcpy(dst
, &hlpfile
->phrases_buffer
[hlpfile
->phrases_offsets
[idx
]], len
);
2310 else if ((*src
& 0x03) == 0x01)
2312 idx
= (*src
+ 1) * 64;
2314 if (idx
> hlpfile
->num_phrases
)
2316 WINE_ERR("index in phrases %d/%d\n", idx
, hlpfile
->num_phrases
);
2321 len
= hlpfile
->phrases_offsets
[idx
+ 1] - hlpfile
->phrases_offsets
[idx
];
2322 if (dst
+ len
<= dst_end
)
2323 memcpy(dst
, &hlpfile
->phrases_buffer
[hlpfile
->phrases_offsets
[idx
]], len
);
2326 else if ((*src
& 0x07) == 0x03)
2328 len
= (*src
/ 8) + 1;
2329 if (dst
+ len
<= dst_end
)
2330 memcpy(dst
, src
+ 1, len
);
2335 len
= (*src
/ 16) + 1;
2336 if (dst
+ len
<= dst_end
)
2337 memset(dst
, ((*src
& 0x0F) == 0x07) ? ' ' : 0, len
);
2342 if (dst
> dst_end
) WINE_ERR("buffer overflow (%p > %p)\n", dst
, dst_end
);
2346 /******************************************************************
2347 * HLPFILE_UncompressRLE
2351 static void HLPFILE_UncompressRLE(const BYTE
* src
, const BYTE
* end
, BYTE
* dst
, unsigned dstsz
)
2354 BYTE
* sdst
= dst
+ dstsz
;
2362 if (dst
+ ch
<= sdst
)
2363 memcpy(dst
, src
, ch
);
2368 if (dst
+ ch
<= sdst
)
2369 memset(dst
, (char)*src
, ch
);
2375 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
2376 (SIZE_T
)(dst
- (sdst
- dstsz
)), dstsz
);
2379 /**************************************************************************
2380 * HLPFILE_BPTreeSearch
2382 * Searches for an element in B+ tree
2385 * buf [I] pointer to the embedded file structured as a B+ tree
2386 * key [I] pointer to data to find
2387 * comp [I] compare function
2390 * Pointer to block identified by key, or NULL if failure.
2393 void* HLPFILE_BPTreeSearch(BYTE
* buf
, const void* key
,
2394 HLPFILE_BPTreeCompare comp
)
2400 BYTE
*pages
, *ptr
, *newptr
;
2404 magic
= GET_USHORT(buf
, 9);
2405 if (magic
!= 0x293B)
2407 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic
);
2410 page_size
= GET_USHORT(buf
, 9+4);
2411 cur_page
= GET_USHORT(buf
, 9+26);
2412 level
= GET_USHORT(buf
, 9+32);
2413 pages
= buf
+ 9 + 38;
2416 ptr
= pages
+ cur_page
*page_size
;
2417 entries
= GET_SHORT(ptr
, 2);
2419 for (i
= 0; i
< entries
; i
++)
2421 if (comp(ptr
, key
, 0, (void **)&newptr
) > 0) break;
2424 cur_page
= GET_USHORT(ptr
-2, 0);
2426 ptr
= pages
+ cur_page
*page_size
;
2427 entries
= GET_SHORT(ptr
, 2);
2429 for (i
= 0; i
< entries
; i
++)
2431 ret
= comp(ptr
, key
, 1, (void **)&newptr
);
2432 if (ret
== 0) return ptr
;
2433 if (ret
> 0) return NULL
;
2439 /**************************************************************************
2440 * HLPFILE_BPTreeEnum
2442 * Enumerates elements in B+ tree.
2445 * buf [I] pointer to the embedded file structured as a B+ tree
2446 * cb [I] compare function
2447 * cookie [IO] cookie for cb function
2449 void HLPFILE_BPTreeEnum(BYTE
* buf
, HLPFILE_BPTreeCallback cb
, void* cookie
)
2455 BYTE
*pages
, *ptr
, *newptr
;
2458 magic
= GET_USHORT(buf
, 9);
2459 if (magic
!= 0x293B)
2461 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic
);
2464 page_size
= GET_USHORT(buf
, 9+4);
2465 cur_page
= GET_USHORT(buf
, 9+26);
2466 level
= GET_USHORT(buf
, 9+32);
2467 pages
= buf
+ 9 + 38;
2470 ptr
= pages
+ cur_page
*page_size
;
2471 cur_page
= GET_USHORT(ptr
, 4);
2473 while (cur_page
!= 0xFFFF)
2475 ptr
= pages
+ cur_page
*page_size
;
2476 entries
= GET_SHORT(ptr
, 2);
2478 for (i
= 0; i
< entries
; i
++)
2480 cb(ptr
, (void **)&newptr
, cookie
);
2483 cur_page
= GET_USHORT(pages
+cur_page
*page_size
, 6);
2488 /***********************************************************************
2490 * HLPFILE_GetContext
2492 static BOOL
HLPFILE_GetContext(HLPFILE
*hlpfile
)
2497 if (!HLPFILE_FindSubFile(hlpfile
, "|CONTEXT", &cbuf
, &cend
))
2498 {WINE_WARN("context0\n"); return FALSE
;}
2501 hlpfile
->Context
= HeapAlloc(GetProcessHeap(), 0, clen
);
2502 if (!hlpfile
->Context
) return FALSE
;
2503 memcpy(hlpfile
->Context
, cbuf
, clen
);
2508 /***********************************************************************
2510 * HLPFILE_GetKeywords
2512 static BOOL
HLPFILE_GetKeywords(HLPFILE
*hlpfile
)
2517 if (!HLPFILE_FindSubFile(hlpfile
, "|KWBTREE", &cbuf
, &cend
)) return FALSE
;
2519 hlpfile
->kwbtree
= HeapAlloc(GetProcessHeap(), 0, clen
);
2520 if (!hlpfile
->kwbtree
) return FALSE
;
2521 memcpy(hlpfile
->kwbtree
, cbuf
, clen
);
2523 if (!HLPFILE_FindSubFile(hlpfile
, "|KWDATA", &cbuf
, &cend
))
2525 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2526 HeapFree(GetProcessHeap(), 0, hlpfile
->kwbtree
);
2530 hlpfile
->kwdata
= HeapAlloc(GetProcessHeap(), 0, clen
);
2531 if (!hlpfile
->kwdata
)
2533 HeapFree(GetProcessHeap(), 0, hlpfile
->kwdata
);
2536 memcpy(hlpfile
->kwdata
, cbuf
, clen
);
2541 /***********************************************************************
2545 static BOOL
HLPFILE_GetMap(HLPFILE
*hlpfile
)
2548 unsigned entries
, i
;
2550 if (!HLPFILE_FindSubFile(hlpfile
, "|CTXOMAP", &cbuf
, &cend
))
2551 {WINE_WARN("no map section\n"); return FALSE
;}
2553 entries
= GET_USHORT(cbuf
, 9);
2554 hlpfile
->Map
= HeapAlloc(GetProcessHeap(), 0, entries
* sizeof(HLPFILE_MAP
));
2555 if (!hlpfile
->Map
) return FALSE
;
2556 hlpfile
->wMapLen
= entries
;
2557 for (i
= 0; i
< entries
; i
++)
2559 hlpfile
->Map
[i
].lMap
= GET_UINT(cbuf
+11,i
*8);
2560 hlpfile
->Map
[i
].offset
= GET_UINT(cbuf
+11,i
*8+4);
2565 /***********************************************************************
2569 static void HLPFILE_DeleteMacro(HLPFILE_MACRO
* macro
)
2571 HLPFILE_MACRO
* next
;
2576 HeapFree(GetProcessHeap(), 0, macro
);
2581 /***********************************************************************
2585 static void HLPFILE_DeletePage(HLPFILE_PAGE
* page
)
2592 HLPFILE_DeleteMacro(page
->first_macro
);
2593 HeapFree(GetProcessHeap(), 0, page
);
2598 /***********************************************************************
2600 * HLPFILE_FreeHlpFile
2602 void HLPFILE_FreeHlpFile(HLPFILE
* hlpfile
)
2606 if (!hlpfile
|| --hlpfile
->wRefCount
> 0) return;
2608 if (hlpfile
->next
) hlpfile
->next
->prev
= hlpfile
->prev
;
2609 if (hlpfile
->prev
) hlpfile
->prev
->next
= hlpfile
->next
;
2610 else first_hlpfile
= hlpfile
->next
;
2612 if (hlpfile
->numFonts
)
2614 for (i
= 0; i
< hlpfile
->numFonts
; i
++)
2616 DeleteObject(hlpfile
->fonts
[i
].hFont
);
2618 HeapFree(GetProcessHeap(), 0, hlpfile
->fonts
);
2621 if (hlpfile
->numBmps
)
2623 for (i
= 0; i
< hlpfile
->numBmps
; i
++)
2625 DeleteObject(hlpfile
->bmps
[i
]);
2627 HeapFree(GetProcessHeap(), 0, hlpfile
->bmps
);
2630 HLPFILE_DeletePage(hlpfile
->first_page
);
2631 HLPFILE_DeleteMacro(hlpfile
->first_macro
);
2633 DestroyIcon(hlpfile
->hIcon
);
2634 if (hlpfile
->numWindows
) HeapFree(GetProcessHeap(), 0, hlpfile
->windows
);
2635 HeapFree(GetProcessHeap(), 0, hlpfile
->Context
);
2636 HeapFree(GetProcessHeap(), 0, hlpfile
->Map
);
2637 HeapFree(GetProcessHeap(), 0, hlpfile
->lpszTitle
);
2638 HeapFree(GetProcessHeap(), 0, hlpfile
->lpszCopyright
);
2639 HeapFree(GetProcessHeap(), 0, hlpfile
->file_buffer
);
2640 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_offsets
);
2641 HeapFree(GetProcessHeap(), 0, hlpfile
->phrases_buffer
);
2642 HeapFree(GetProcessHeap(), 0, hlpfile
->topic_map
);
2643 HeapFree(GetProcessHeap(), 0, hlpfile
->help_on_file
);
2644 HeapFree(GetProcessHeap(), 0, hlpfile
);