win32u: Make NtUserSetWindowPixelFormat() into a proper export.
[wine.git] / programs / winhlp32 / hlpfile.c
blob4fb6853cfe611e448502c424232cdac95c1ba08f
1 /*
2 * Help Viewer
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
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winhelp.h"
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
61 * PARAMS
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
66 * RETURNS
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)
73 unsigned magic;
74 unsigned page_size;
75 unsigned cur_page;
76 unsigned level;
77 BYTE *pages, *ptr, *newptr;
78 int i, entries;
79 int ret;
81 magic = GET_USHORT(buf, 9);
82 if (magic != 0x293B)
84 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
85 return NULL;
87 page_size = GET_USHORT(buf, 9+4);
88 cur_page = GET_USHORT(buf, 9+26);
89 level = GET_USHORT(buf, 9+32);
90 pages = buf + 9 + 38;
91 while (--level > 0)
93 ptr = pages + cur_page*page_size;
94 entries = GET_SHORT(ptr, 2);
95 ptr += 6;
96 for (i = 0; i < entries; i++)
98 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
99 ptr = newptr;
101 cur_page = GET_USHORT(ptr-2, 0);
103 ptr = pages + cur_page*page_size;
104 entries = GET_SHORT(ptr, 2);
105 ptr += 8;
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;
111 ptr = newptr;
113 return NULL;
116 /**************************************************************************
117 * HLPFILE_BPTreeEnum
119 * Enumerates elements in B+ tree.
121 * PARAMS
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)
128 unsigned magic;
129 unsigned page_size;
130 unsigned cur_page;
131 unsigned level;
132 BYTE *pages, *ptr, *newptr;
133 int i, entries;
135 magic = GET_USHORT(buf, 9);
136 if (magic != 0x293B)
138 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
139 return;
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;
145 while (--level > 0)
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);
154 ptr += 8;
155 for (i = 0; i < entries; i++)
157 cb(ptr, (void **)&newptr, cookie);
158 ptr = newptr;
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)
171 int i, newsize = 0;
173 while (ptr < end)
175 int mask = *ptr++;
176 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
178 if (mask & 1)
180 int code = GET_USHORT(ptr, 0);
181 int len = 3 + (code >> 12);
182 newsize += len;
183 ptr += 2;
185 else newsize++, ptr++;
189 return newsize;
192 /***********************************************************************
194 * HLPFILE_UncompressLZ77
196 static BYTE *HLPFILE_UncompressLZ77(const BYTE *ptr, const BYTE *end, BYTE *newptr)
198 int i;
200 while (ptr < end)
202 int mask = *ptr++;
203 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
205 if (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}
214 * newptr=a+2;
215 * offset=1;
216 * We expect:
217 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
219 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
220 ptr += 2;
222 else *newptr++ = *ptr++;
226 return newptr;
229 /***********************************************************************
231 * HLPFILE_Uncompress2
234 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
236 BYTE *phptr, *phend;
237 UINT code;
238 UINT index;
240 while (ptr < end && newptr < newend)
242 if (!*ptr || *ptr >= 0x10)
243 *newptr++ = *ptr++;
244 else
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));
256 return;
258 memcpy(newptr, phptr, phend - phptr);
259 newptr += phend - phptr;
260 if (code & 1) *newptr++ = ' ';
262 ptr += 2;
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++)
280 if ((*src & 1) == 0)
282 idx = *src / 2;
283 if (idx > hlpfile->num_phrases)
285 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
286 len = 0;
288 else
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;
298 idx += *++src;
299 if (idx > hlpfile->num_phrases)
301 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
302 len = 0;
304 else
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);
316 src += len;
318 else
320 len = (*src / 16) + 1;
321 if (dst + len <= dst_end)
322 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
324 dst += len;
327 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
328 return TRUE;
331 /******************************************************************
332 * HLPFILE_UncompressRLE
336 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE* dst, unsigned dstsz)
338 BYTE ch;
339 BYTE* sdst = dst + dstsz;
341 while (src < end)
343 ch = *src++;
344 if (ch & 0x80)
346 ch &= 0x7F;
347 if (dst + ch <= sdst)
348 memcpy(dst, src, ch);
349 src += ch;
351 else
353 if (dst + ch <= sdst)
354 memset(dst, (char)*src, ch);
355 src++;
357 dst += ch;
359 if (dst != sdst)
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)
372 HLPFILE_PAGE* page;
373 HLPFILE_PAGE* found;
375 if (!hlpfile) return 0;
377 WINE_TRACE("<%s>[%lx]\n", debugstr_a(hlpfile->lpszPath), offset);
379 if (offset == 0xFFFFFFFF) return NULL;
380 page = 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;
387 found = page;
390 if (!found)
391 WINE_ERR("Page of offset %lu not found in file %s\n",
392 offset, debugstr_a(hlpfile->lpszPath));
393 return found;
396 /***********************************************************************
398 * HLPFILE_Contents
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);
407 if (!page)
409 page = hlpfile->first_page;
410 *relative = 0;
412 return page;
415 /**************************************************************************
416 * comp_PageByHash
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;
431 return 0;
434 /***********************************************************************
436 * HLPFILE_PageByHash
438 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash, ULONG* relative)
440 BYTE *ptr;
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);
455 if (!ptr)
457 WINE_ERR("Page of hash %lx not found in file %s\n", lHash, debugstr_a(hlpfile->lpszPath));
458 return NULL;
461 return HLPFILE_PageByOffset(hlpfile, GET_UINT(ptr, 4), relative);
464 /***********************************************************************
466 * HLPFILE_PageByMap
468 HLPFILE_PAGE *HLPFILE_PageByMap(HLPFILE* hlpfile, LONG lMap, ULONG* relative)
470 unsigned int i;
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));
483 return NULL;
486 /**************************************************************************
487 * comp_FindSubFile
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)
506 BYTE *ptr;
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);
511 if (!ptr)
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. */
515 CHAR c = *name++;
516 if (c == '|')
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));
528 return FALSE;
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));
534 return FALSE;
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));
539 return FALSE;
541 return TRUE;
544 /***********************************************************************
546 * HLPFILE_Hash
548 LONG HLPFILE_Hash(LPCSTR lpszContext)
550 LONG lHash = 0;
551 CHAR c;
553 while ((c = *lpszContext++))
555 CHAR x = 0;
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;
564 return lHash;
567 static LONG fetch_long(const BYTE** ptr)
569 LONG ret;
571 if (*(*ptr) & 1)
573 ret = (*(const ULONG*)(*ptr) - 0x80000000) / 2;
574 (*ptr) += 4;
576 else
578 ret = (*(const USHORT*)(*ptr) - 0x8000) / 2;
579 (*ptr) += 2;
582 return ret;
585 static ULONG fetch_ulong(const BYTE** ptr)
587 ULONG ret;
589 if (*(*ptr) & 1)
591 ret = *(const ULONG*)(*ptr) / 2;
592 (*ptr) += 4;
594 else
596 ret = *(const USHORT*)(*ptr) / 2;
597 (*ptr) += 2;
599 return ret;
602 static short fetch_short(const BYTE** ptr)
604 short ret;
606 if (*(*ptr) & 1)
608 ret = (*(const unsigned short*)(*ptr) - 0x8000) / 2;
609 (*ptr) += 2;
611 else
613 ret = (*(const unsigned char*)(*ptr) - 0x80) / 2;
614 (*ptr)++;
616 return ret;
619 static unsigned short fetch_ushort(const BYTE** ptr)
621 unsigned short ret;
623 if (*(*ptr) & 1)
625 ret = *(const unsigned short*)(*ptr) / 2;
626 (*ptr) += 2;
628 else
630 ret = *(const unsigned char*)(*ptr) / 2;
631 (*ptr)++;
633 return ret;
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,
642 BYTE** alloc)
644 const BYTE* dst;
645 BYTE* tmp;
646 unsigned sz77;
648 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
650 switch (packing)
652 case 0: /* uncompressed */
653 if (sz != csz)
654 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
655 dst = src;
656 *alloc = NULL;
657 break;
658 case 1: /* RunLen */
659 dst = *alloc = malloc(sz);
660 if (!dst) return NULL;
661 HLPFILE_UncompressRLE(src, src + csz, *alloc, sz);
662 break;
663 case 2: /* LZ77 */
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);
668 if (sz77 != sz)
669 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
670 break;
671 case 3: /* LZ77 then RLE */
672 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
673 tmp = malloc(sz77);
674 if (!tmp) return FALSE;
675 HLPFILE_UncompressLZ77(src, src + csz, tmp);
676 dst = *alloc = malloc(sz);
677 if (!dst)
679 free(tmp);
680 return FALSE;
682 HLPFILE_UncompressRLE(tmp, tmp + sz77, *alloc, sz);
683 free(tmp);
684 break;
685 default:
686 WINE_FIXME("Unsupported packing %u\n", packing);
687 return NULL;
689 return dst;
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);
699 rd->data = new;
700 rd->allocated *= 2;
702 memcpy(rd->ptr, str, sz);
703 rd->ptr += sz;
705 return TRUE;
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)
718 const char* p;
719 const char* last;
720 const char* replace;
721 unsigned rlen;
723 if (!rd->in_text)
725 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
726 rd->in_text = TRUE;
728 for (last = p = str; *p; p++)
730 if (*p & 0x80) /* escape non-ASCII chars */
732 static char xx[8];
733 rlen = sprintf(xx, "\\'%x", *(const BYTE*)p);
734 replace = xx;
736 else switch (*p)
738 case '{': rlen = 2; replace = "\\{"; break;
739 case '}': rlen = 2; replace = "\\}"; break;
740 case '\\': rlen = 2; replace = "\\\\"; break;
741 default: continue;
743 if ((p != last && !HLPFILE_RtfAddRawString(rd, last, p - last)) ||
744 !HLPFILE_RtfAddRawString(rd, replace, rlen)) return FALSE;
745 last = p + 1;
747 return HLPFILE_RtfAddRawString(rd, last, p - last);
750 /******************************************************************
751 * RtfAddHexBytes
754 static BOOL HLPFILE_RtfAddHexBytes(struct RtfData* rd, const void* _ptr, unsigned sz)
756 char tmp[512];
757 unsigned i, step;
758 const BYTE* ptr = _ptr;
759 static const char* _2hex = "0123456789abcdef";
761 if (!rd->in_text)
763 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
764 rd->in_text = TRUE;
766 for (; sz; sz -= step)
768 step = min(256, sz);
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;
776 return TRUE;
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)
790 unsigned i, hs_num;
791 ULONG hs_macro;
792 const char* str;
794 if (hs_size == 0 || hs_offset == 0) return;
796 start += hs_offset;
797 /* always 1 ?? */
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 */
815 hslink = NULL;
816 switch (start[7 + 15 * i + 0])
817 /* The next two chars always look like 0x04 0x00 ???
818 * What are they for ?
821 case 0xC8:
822 hslink = (HLPFILE_HOTSPOTLINK*)
823 HLPFILE_AllocLink(rd, hlp_link_macro, str, -1, 0, FALSE, TRUE, -1);
824 break;
826 case 0xE6:
827 case 0xE7:
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),
831 FALSE, TRUE, -1);
832 break;
834 case 0xEE:
835 case 0xEF:
837 const char* win = strchr(str, '>');
838 int wnd = -1;
839 char* tgt = NULL;
841 if (win)
843 for (wnd = file->numWindows - 1; wnd >= 0; wnd--)
845 if (!strcmp(win + 1, file->windows[wnd].name)) break;
847 if (wnd == -1)
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);
858 free(tgt);
859 break;
861 default:
862 WINE_FIXME("unknown hotsport target 0x%x\n", start[7 + 15 * i + 0]);
864 if (hslink)
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;
887 HENHMETAFILE hEMF;
888 BOOL ret = FALSE;
889 void* data;
890 UINT sz;
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);
902 SetBkColor(hdcMem,
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);
914 DeleteDC(hdcMask);
916 SelectObject(hdcMem, hbmOldMem);
917 DeleteDC(hdcMem);
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);
933 DeleteDC(hdcMem);
935 /* do the cleanup */
936 ReleaseDC(0, hdc);
937 DeleteObject(hbmMask);
938 DeleteObject(hbm);
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, "}");
952 free(data);
954 DeleteEnhMetaFile(hEMF);
956 return ret;
959 /******************************************************************
960 * HLPFILE_RtfAddBitmap
963 static BOOL HLPFILE_RtfAddBitmap(struct RtfData* rd, HLPFILE* file, const BYTE* beg, BYTE type, BYTE pack)
965 const BYTE* ptr;
966 const BYTE* pict_beg;
967 BYTE* alloc = NULL;
968 BITMAPINFO* bi;
969 BITMAPINFO* new_bi;
970 ULONG off, csz;
971 unsigned nc = 0;
972 BOOL clrImportant = FALSE;
973 BOOL ret = FALSE;
974 char tmp[256];
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 */
1008 if (type == 0x06)
1010 unsigned i;
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));
1018 if (!new_bi)
1020 free(bi);
1021 return FALSE;
1023 bi = new_bi;
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;
1030 ptr += 4;
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);
1038 goto done;
1040 if (!HLPFILE_RtfAddControl(rd, "{\\pict")) goto done;
1041 if (type == 0x06)
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;
1048 else
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;
1058 ret = TRUE;
1059 done:
1060 free(bi);
1061 free(alloc);
1063 return ret;
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;
1073 const BYTE* ptr;
1074 const BYTE* bits;
1075 BYTE* alloc = NULL;
1076 char tmp[256];
1077 unsigned mm;
1078 BOOL ret;
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;
1088 ptr += 4;
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);
1095 ptr += 8;
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, "}");
1108 free(alloc);
1110 return ret;
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++)
1127 const BYTE* beg;
1128 const BYTE* ptr;
1129 BYTE type, pack;
1131 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
1132 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
1134 type = *ptr++;
1135 pack = *ptr++;
1137 switch (type)
1139 case 5: /* device dependent bmp */
1140 case 6: /* device independent bmp */
1141 HLPFILE_RtfAddBitmap(rd, hlpfile, beg, type, pack);
1142 break;
1143 case 8:
1144 HLPFILE_RtfAddMetaFile(rd, hlpfile, beg, pack);
1145 break;
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");
1153 break;
1155 return TRUE;
1158 /******************************************************************
1159 * HLPFILE_RtfAddGfxByIndex
1163 static BOOL HLPFILE_RtfAddGfxByIndex(struct RtfData* rd, HLPFILE *hlpfile,
1164 unsigned index)
1166 char tmp[16];
1167 BYTE *ref, *end;
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;}
1175 ref += 9;
1176 return HLPFILE_RtfAddGfxByAddr(rd, hlpfile, ref, end - ref);
1179 /******************************************************************
1180 * HLPFILE_AllocLink
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)
1188 HLPFILE_LINK* link;
1189 char* link_str;
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';
1203 link->hash = hash;
1204 link->bClrChange = clrChange;
1205 link->bHotSpot = bHotSpot;
1206 link->window = wnd;
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");
1212 if (bHotSpot)
1213 link->cpMax = rd->char_pos;
1214 else
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);
1219 return link;
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)
1234 UINT textsize;
1235 const BYTE *format, *format_end;
1236 char *text, *text_base, *text_end;
1237 LONG size, blocksize, datalen;
1238 unsigned short bits;
1239 unsigned ncol = 1;
1240 short nc, lastcol, table_width;
1241 char tmp[256];
1242 BOOL ret = FALSE;
1244 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
1246 *parlen = 0;
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);
1259 else
1261 WINE_FIXME("Text size is too long, splitting\n");
1262 size = blocksize - datalen;
1263 memcpy(text, buf + datalen, size);
1266 else
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)
1284 unsigned char type;
1286 ncol = *format++;
1288 if (!HLPFILE_RtfAddControl(rd, "\\trowd")) goto done;
1289 type = *format++;
1290 if (type == 0 || type == 2)
1292 table_width = GET_SHORT(format, 0);
1293 format += 2;
1294 if (!HLPFILE_RtfAddControl(rd, "\\trqc")) goto done;
1296 else
1297 table_width = 32767;
1298 WINE_TRACE("New table: cols=%d type=%x width=%d\n",
1299 ncol, type, table_width);
1300 if (ncol > 1)
1302 int pos;
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;
1319 else
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;
1328 format += ncol * 4;
1331 lastcol = -1;
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;
1342 rd->char_pos += 2;
1343 break;
1345 format += 5;
1346 if (!HLPFILE_RtfAddControl(rd, "\\intbl")) goto done;
1348 else nc++;
1349 if (buf[0x14] == HLP_DISPLAY30)
1350 format += 6;
1351 else
1352 format += 4;
1353 bits = GET_USHORT(format, 0); format += 2;
1354 if (bits & 0x0001) fetch_long(&format);
1355 if (bits & 0x0002)
1357 sprintf(tmp, "\\sb%d", HLPFILE_HalfPointsScale(page, fetch_short(&format)));
1358 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1360 if (bits & 0x0004)
1362 sprintf(tmp, "\\sa%d", HLPFILE_HalfPointsScale(page, fetch_short(&format)));
1363 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1365 if (bits & 0x0008)
1367 sprintf(tmp, "\\sl%d", HLPFILE_HalfPointsScale(page, fetch_short(&format)));
1368 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1370 if (bits & 0x0010)
1372 sprintf(tmp, "\\li%d", HLPFILE_HalfPointsScale(page, fetch_short(&format)));
1373 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1375 if (bits & 0x0020)
1377 sprintf(tmp, "\\ri%d", HLPFILE_HalfPointsScale(page, fetch_short(&format)));
1378 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1380 if (bits & 0x0040)
1382 sprintf(tmp, "\\fi%d", HLPFILE_HalfPointsScale(page, fetch_short(&format)));
1383 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1385 if (bits & 0x0100)
1387 BYTE brdr = *format++;
1388 short w;
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;
1398 /* 0x80: unknown */
1400 w = GET_SHORT(format, 0); format += 2;
1401 if (w)
1403 sprintf(tmp, "\\brdrw%d", HLPFILE_HalfPointsScale(page, w));
1404 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1407 if (bits & 0x0200)
1409 int i, ntab = fetch_short(&format);
1410 unsigned tab, ts;
1411 const char* kind;
1413 for (i = 0; i < ntab; i++)
1415 tab = fetch_ushort(&format);
1416 ts = (tab & 0x4000) ? fetch_ushort(&format) : 0 /* left */;
1417 switch (ts)
1419 default: WINE_FIXME("Unknown tab style %x\n", ts);
1420 /* fall through */
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);
1448 if (textsize)
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)
1466 format++;
1467 break;
1470 switch (*format)
1472 case 0x20:
1473 WINE_FIXME("NIY20\n");
1474 format += 5;
1475 break;
1477 case 0x21:
1478 WINE_FIXME("NIY21\n");
1479 format += 3;
1480 break;
1482 case 0x80:
1484 unsigned font = GET_USHORT(format, 1);
1485 unsigned fs;
1487 WINE_TRACE("Changing font to %d\n", font);
1488 format += 3;
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;
1494 default:
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",
1501 font, font + 2, fs,
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;
1508 break;
1510 case 0x81:
1511 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1512 format += 1;
1513 rd->char_pos++;
1514 break;
1516 case 0x82:
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;
1526 rd->char_pos += 2;
1528 else if (GET_SHORT(format, 2) == lastcol)
1530 if (!HLPFILE_RtfAddControl(rd, "\\par\\pard")) goto done;
1532 else
1534 if (!HLPFILE_RtfAddControl(rd, "\\cell\\pard")) goto done;
1537 else if (!HLPFILE_RtfAddControl(rd, "\\par")) goto done;
1538 format += 1;
1539 rd->char_pos++;
1540 break;
1542 case 0x83:
1543 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1544 format += 1;
1545 rd->char_pos++;
1546 break;
1548 #if 0
1549 case 0x84:
1550 format += 3;
1551 break;
1552 #endif
1554 case 0x86:
1555 case 0x87:
1556 case 0x88:
1558 BYTE type = format[1];
1560 /* FIXME: we don't use 'BYTE pos = (*format - 0x86);' for the image position */
1561 format += 2;
1562 size = fetch_long(&format);
1564 switch (type)
1566 case 0x22:
1567 fetch_ushort(&format); /* hot spot */
1568 /* fall through */
1569 case 0x03:
1570 switch (GET_SHORT(format, 0))
1572 case 0:
1573 HLPFILE_RtfAddGfxByIndex(rd, page->file, GET_SHORT(format, 2));
1574 rd->char_pos++;
1575 break;
1576 case 1:
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);
1581 rd->char_pos++;
1582 break;
1583 default:
1584 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1585 break;
1587 break;
1588 case 0x05:
1589 WINE_FIXME("Got an embedded element %s\n", debugstr_a((char *)format + 6));
1590 break;
1591 default:
1592 WINE_FIXME("Got a type %d picture\n", type);
1593 break;
1595 format += size;
1597 break;
1599 case 0x89:
1600 format += 1;
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;
1606 break;
1608 case 0x8B:
1609 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1610 format += 1;
1611 rd->char_pos++;
1612 break;
1614 case 0x8C:
1615 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1616 /* FIXME: it could be that hyphen is also in input stream !! */
1617 format += 1;
1618 rd->char_pos++;
1619 break;
1621 #if 0
1622 case 0xA9:
1623 format += 2;
1624 break;
1625 #endif
1627 case 0xC8:
1628 case 0xCC:
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);
1633 break;
1635 case 0xE0:
1636 case 0xE1:
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);
1642 format += 5;
1643 break;
1645 case 0xE2:
1646 case 0xE3:
1647 case 0xE6:
1648 case 0xE7:
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);
1653 format += 5;
1654 break;
1656 case 0xEA:
1657 case 0xEB:
1658 case 0xEE:
1659 case 0xEF:
1661 const char* ptr = (const char*) format + 8;
1662 BYTE type = format[3];
1663 int wnd = -1;
1665 switch (type)
1667 case 1:
1668 wnd = *ptr;
1669 /* fall through */
1670 case 0:
1671 ptr = page->file->lpszPath;
1672 break;
1673 case 6:
1674 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1676 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1678 if (wnd == -1)
1679 WINE_WARN("Couldn't find window info for %s\n", debugstr_a(ptr));
1680 ptr += strlen(ptr) + 1;
1681 /* fall through */
1682 case 4:
1683 break;
1684 default:
1685 WINE_WARN("Unknown link type %d\n", type);
1686 break;
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);
1692 break;
1694 default:
1695 WINE_WARN("format %02x\n", *format);
1696 format++;
1700 ret = TRUE;
1701 done:
1703 free(text_base);
1704 return ret;
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;
1715 BYTE *buf, *end;
1716 DWORD ref = page->reference;
1717 unsigned index, old_index = -1, offset, count = 0, offs = 0;
1718 unsigned cpg, parlen;
1719 char tmp[1024];
1720 const char* ck = NULL;
1722 rd->in_text = TRUE;
1723 rd->data = rd->ptr = malloc(rd->allocated = 32768);
1724 rd->char_pos = 0;
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;
1750 default:
1751 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1752 cpg = 1252;
1754 if (ck)
1756 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1757 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1759 else
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++)
1769 const char* family;
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;}",
1780 index, family,
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;
1806 else
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 */
1815 ref -= 12;
1816 offset -= 12;
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;}
1825 switch (buf[0x14])
1827 case HLP_TOPICHDR:
1828 if (count++) goto done;
1829 break;
1830 case HLP_DISPLAY30:
1831 case HLP_DISPLAY:
1832 case HLP_TABLE:
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;
1836 offs += parlen;
1837 break;
1838 default:
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)
1845 break;
1847 else
1848 ref = GET_UINT(buf, 0xc);
1849 } while (ref != 0xffffffff);
1850 done:
1851 page->first_link = rd->first_link;
1852 return HLPFILE_RtfAddControl(rd, "}");
1855 /******************************************************************
1856 * HLPFILE_ReadFont
1860 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1862 BYTE *ref, *end;
1863 unsigned i, len, idx;
1864 unsigned face_num, dscr_num, face_offset, dscr_offset;
1865 BYTE flag, family;
1867 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1869 WINE_WARN("no subfile FONT\n");
1870 hlpfile->numFonts = 0;
1871 hlpfile->fonts = NULL;
1872 return FALSE;
1875 ref += 9;
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;
1890 /* mvb font */
1891 if (face_offset >= 16)
1893 hlpfile->scale = 1;
1894 hlpfile->rounderr = 0;
1895 WINE_FIXME("mvb font: not implemented\n");
1896 return FALSE;
1898 /* new font */
1899 if (face_offset >= 12)
1901 hlpfile->scale = 1;
1902 hlpfile->rounderr = 0;
1903 WINE_FIXME("new font: not implemented\n");
1904 return FALSE;
1906 /* old font */
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;
1930 switch (family)
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);
1941 if (idx < face_num)
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';
1946 else
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",
1957 i, flag,
1958 X(0, "bold"),
1959 X(1, "italic"),
1960 X(2, "underline"),
1961 X(3, "strikeOut"),
1962 X(4, "dblUnderline"),
1963 X(5, "smallCaps"),
1964 ref[dscr_offset + i * 11 + 1],
1965 family,
1966 debugstr_a(hlpfile->fonts[i].LogFont.lfFaceName), idx,
1967 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1969 return TRUE;
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;};
1982 /* sanity checks */
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 */
1998 return TRUE;
2001 /***********************************************************************
2003 * HLPFILE_SystemCommands
2005 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
2007 BYTE *buf, *ptr, *end;
2008 HLPFILE_MACRO *macro, **m;
2009 LPSTR p;
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;}
2025 if (minor <= 16)
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;
2040 else
2042 hlpfile->tbsize = 0x800;
2043 hlpfile->compressed = TRUE;
2046 if (hlpfile->compressed)
2047 hlpfile->dsize = 0x4000;
2048 else
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 */
2063 return TRUE;
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))
2070 case 1:
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));
2075 break;
2077 case 2:
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));
2082 break;
2084 case 3:
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);
2088 break;
2090 case 4:
2091 macro = malloc(sizeof(HLPFILE_MACRO) + strlen(str) + 1);
2092 if (!macro) break;
2093 p = (char*)macro + sizeof(HLPFILE_MACRO);
2094 strcpy(p, str);
2095 macro->lpszMacro = p;
2096 macro->next = 0;
2097 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
2098 *m = macro;
2099 break;
2101 case 5:
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,
2107 0x30000, 0, 0, 0);
2108 break;
2110 case 6:
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);
2147 break;
2148 case 8:
2149 WINE_WARN("Citation: %s\n", debugstr_a((char *)ptr + 4));
2150 break;
2151 case 11:
2152 hlpfile->charset = ptr[4];
2153 WINE_TRACE("Charset: %d\n", hlpfile->charset);
2154 break;
2155 default:
2156 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
2159 if (!hlpfile->lpszTitle)
2160 hlpfile->lpszTitle = strdup("");
2161 return TRUE;
2164 /***********************************************************************
2166 * HLPFILE_GetContext
2168 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2170 BYTE *cbuf, *cend;
2171 unsigned clen;
2173 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2174 {WINE_WARN("context0\n"); return FALSE;}
2176 clen = cend - cbuf;
2177 hlpfile->Context = malloc(clen);
2178 if (!hlpfile->Context) return FALSE;
2179 memcpy(hlpfile->Context, cbuf, clen);
2181 return TRUE;
2184 /***********************************************************************
2186 * HLPFILE_GetKeywords
2188 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2190 BYTE *cbuf, *cend;
2191 unsigned clen;
2193 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2194 clen = cend - cbuf;
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);
2203 return FALSE;
2205 clen = cend - cbuf;
2206 hlpfile->kwdata = malloc(clen);
2207 if (!hlpfile->kwdata)
2209 free(hlpfile->kwdata);
2210 return FALSE;
2212 memcpy(hlpfile->kwdata, cbuf, clen);
2214 return TRUE;
2217 /***********************************************************************
2219 * HLPFILE_GetMap
2221 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2223 BYTE *cbuf, *cend;
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);
2238 return TRUE;
2241 /***********************************************************************
2243 * HLPFILE_GetTOMap
2245 static BOOL HLPFILE_GetTOMap(HLPFILE *hlpfile)
2247 BYTE *cbuf, *cend;
2248 unsigned clen;
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;
2258 return TRUE;
2261 /***********************************************************************
2263 * DeleteMacro
2265 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2267 HLPFILE_MACRO* next;
2269 while (macro)
2271 next = macro->next;
2272 free(macro);
2273 macro = next;
2277 /***********************************************************************
2279 * DeletePage
2281 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2283 HLPFILE_PAGE* next;
2285 while (page)
2287 next = page->next;
2288 HLPFILE_DeleteMacro(page->first_macro);
2289 free(page);
2290 page = next;
2294 /***********************************************************************
2296 * HLPFILE_FreeHlpFile
2298 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2300 unsigned i;
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);
2332 free(hlpfile->Map);
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);
2340 free(hlpfile);
2343 /***********************************************************************
2345 * HLPFILE_UncompressLZ77_Phrases
2347 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
2349 UINT i, num, dec_size, head_size;
2350 BYTE *buf, *end;
2352 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
2354 if (hlpfile->version <= 16)
2355 head_size = 13;
2356 else
2357 head_size = 17;
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;
2364 else
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);
2373 return FALSE;
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);
2381 else
2382 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
2384 hlpfile->hasPhrases = TRUE;
2385 return TRUE;
2388 /***********************************************************************
2390 * HLPFILE_Uncompress_Phrases40
2392 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
2394 UINT num;
2395 INT dec_size, cpr_size;
2396 BYTE *buf_idx, *end_idx;
2397 BYTE *buf_phs, *end_phs;
2398 ULONG* ptr, mask = 0;
2399 unsigned int i;
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);
2437 return FALSE;
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);
2447 if (getbit()) n++;
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;
2454 #undef getbit
2456 if (dec_size == cpr_size)
2457 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2458 else
2459 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2461 hlpfile->hasPhrases40 = TRUE;
2462 return 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);
2508 else
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);
2525 return TRUE;
2528 /***********************************************************************
2530 * HLPFILE_AddPage
2532 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, const BYTE *buf, const BYTE *end, unsigned ref, unsigned offset)
2534 HLPFILE_PAGE* page;
2535 const BYTE* title;
2536 UINT titlesize, blocksize, datalen;
2537 char* ptr;
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);
2557 else
2559 WINE_FIXME("Text size is too long, splitting\n");
2560 titlesize = blocksize - datalen;
2561 memcpy(page->lpszTitle, title, titlesize);
2564 else
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;
2575 else
2577 hlpfile->first_page = page;
2578 hlpfile->last_page = page;
2579 page->prev = NULL;
2582 page->file = hlpfile;
2583 page->next = NULL;
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;
2597 else
2598 page->browse_bwd = hlpfile->TOMap[page->browse_bwd];
2600 if (page->browse_fwd == 0xFFFF || page->browse_fwd == 0xFFFFFFFF)
2601 page->browse_fwd = 0xFFFFFFFF;
2602 else
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);
2615 char* macro_str;
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;
2626 ptr += len + 1;
2629 return TRUE;
2632 /***********************************************************************
2634 * HLPFILE_SkipParagraph
2636 static BOOL HLPFILE_SkipParagraph(HLPFILE *hlpfile, const BYTE *buf, const BYTE *end, unsigned* len)
2638 const BYTE *tmp;
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;};
2643 tmp = buf + 0x15;
2644 if (buf[0x14] == HLP_DISPLAY || buf[0x14] == HLP_TABLE)
2646 fetch_long(&tmp);
2647 *len = fetch_ushort(&tmp);
2649 else *len = end-buf-15;
2651 return TRUE;
2654 /***********************************************************************
2656 * HLPFILE_DoReadHlpFile
2658 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
2660 BOOL ret;
2661 HFILE hFile;
2662 OFSTRUCT ofs;
2663 BYTE* buf;
2664 DWORD ref = 0x0C;
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);
2671 _lclose(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;
2685 old_index = -1;
2686 offs = 0;
2689 BYTE* end;
2691 if (hlpfile->version <= 16)
2693 index = (ref - 0x0C) / hlpfile->dsize;
2694 offset = (ref - 0x0C) % hlpfile->dsize;
2696 else
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 */
2705 ref -= 12;
2706 offset -= 12;
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;}
2717 switch (buf[0x14])
2719 case HLP_TOPICHDR: /* Topic Header */
2720 if (hlpfile->version <= 16)
2721 topicoffset = ref + index * 12;
2722 else
2723 topicoffset = index * 0x8000 + offs;
2724 if (!HLPFILE_AddPage(hlpfile, buf, end, ref, topicoffset)) return FALSE;
2725 break;
2727 case HLP_DISPLAY30:
2728 case HLP_DISPLAY:
2729 case HLP_TABLE:
2730 if (!HLPFILE_SkipParagraph(hlpfile, buf, end, &len)) return FALSE;
2731 offs += len;
2732 break;
2734 default:
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)
2742 break;
2744 else
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)
2760 HLPFILE* hlpfile;
2762 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
2764 if (!strcmp(lpszPath, hlpfile->lpszPath))
2766 hlpfile->wRefCount++;
2767 return hlpfile;
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);
2787 hlpfile = 0;
2790 return hlpfile;