winmm: dwBytesRecorded controls how much is played, not dwBufferLength.
[wine/hramrach.git] / programs / winhlp32 / hlpfile.c
blob2d685ae493af8268d82e15239ad862cae060923b
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 <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winhelp.h"
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;
55 /**************************************************************************
56 * HLPFILE_BPTreeSearch
58 * Searches for an element in B+ tree
60 * PARAMS
61 * buf [I] pointer to the embedded file structured as a B+ tree
62 * key [I] pointer to data to find
63 * comp [I] compare function
65 * RETURNS
66 * Pointer to block identified by key, or NULL if failure.
69 static void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
70 HLPFILE_BPTreeCompare comp)
72 unsigned magic;
73 unsigned page_size;
74 unsigned cur_page;
75 unsigned level;
76 BYTE *pages, *ptr, *newptr;
77 int i, entries;
78 int ret;
80 magic = GET_USHORT(buf, 9);
81 if (magic != 0x293B)
83 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
84 return NULL;
86 page_size = GET_USHORT(buf, 9+4);
87 cur_page = GET_USHORT(buf, 9+26);
88 level = GET_USHORT(buf, 9+32);
89 pages = buf + 9 + 38;
90 while (--level > 0)
92 ptr = pages + cur_page*page_size;
93 entries = GET_SHORT(ptr, 2);
94 ptr += 6;
95 for (i = 0; i < entries; i++)
97 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
98 ptr = newptr;
100 cur_page = GET_USHORT(ptr-2, 0);
102 ptr = pages + cur_page*page_size;
103 entries = GET_SHORT(ptr, 2);
104 ptr += 8;
105 for (i = 0; i < entries; i++)
107 ret = comp(ptr, key, 1, (void **)&newptr);
108 if (ret == 0) return ptr;
109 if (ret > 0) return NULL;
110 ptr = newptr;
112 return NULL;
115 /**************************************************************************
116 * HLPFILE_BPTreeEnum
118 * Enumerates elements in B+ tree.
120 * PARAMS
121 * buf [I] pointer to the embedded file structured as a B+ tree
122 * cb [I] compare function
123 * cookie [IO] cookie for cb function
125 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
127 unsigned magic;
128 unsigned page_size;
129 unsigned cur_page;
130 unsigned level;
131 BYTE *pages, *ptr, *newptr;
132 int i, entries;
134 magic = GET_USHORT(buf, 9);
135 if (magic != 0x293B)
137 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
138 return;
140 page_size = GET_USHORT(buf, 9+4);
141 cur_page = GET_USHORT(buf, 9+26);
142 level = GET_USHORT(buf, 9+32);
143 pages = buf + 9 + 38;
144 while (--level > 0)
146 ptr = pages + cur_page*page_size;
147 cur_page = GET_USHORT(ptr, 4);
149 while (cur_page != 0xFFFF)
151 ptr = pages + cur_page*page_size;
152 entries = GET_SHORT(ptr, 2);
153 ptr += 8;
154 for (i = 0; i < entries; i++)
156 cb(ptr, (void **)&newptr, cookie);
157 ptr = newptr;
159 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
164 /***********************************************************************
166 * HLPFILE_UncompressedLZ77_Size
168 static INT HLPFILE_UncompressedLZ77_Size(const BYTE *ptr, const BYTE *end)
170 int i, newsize = 0;
172 while (ptr < end)
174 int mask = *ptr++;
175 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
177 if (mask & 1)
179 int code = GET_USHORT(ptr, 0);
180 int len = 3 + (code >> 12);
181 newsize += len;
182 ptr += 2;
184 else newsize++, ptr++;
188 return newsize;
191 /***********************************************************************
193 * HLPFILE_UncompressLZ77
195 static BYTE *HLPFILE_UncompressLZ77(const BYTE *ptr, const BYTE *end, BYTE *newptr)
197 int i;
199 while (ptr < end)
201 int mask = *ptr++;
202 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
204 if (mask & 1)
206 int code = GET_USHORT(ptr, 0);
207 int len = 3 + (code >> 12);
208 int offset = code & 0xfff;
210 * We must copy byte-by-byte here. We cannot use memcpy nor
211 * memmove here. Just example:
212 * a[]={1,2,3,4,5,6,7,8,9,10}
213 * newptr=a+2;
214 * offset=1;
215 * We expect:
216 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
218 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
219 ptr += 2;
221 else *newptr++ = *ptr++;
225 return newptr;
228 /***********************************************************************
230 * HLPFILE_Uncompress2
233 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
235 BYTE *phptr, *phend;
236 UINT code;
237 UINT index;
239 while (ptr < end && newptr < newend)
241 if (!*ptr || *ptr >= 0x10)
242 *newptr++ = *ptr++;
243 else
245 code = 0x100 * ptr[0] + ptr[1];
246 index = (code - 0x100) / 2;
248 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
249 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
251 if (newptr + (phend - phptr) > newend)
253 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
254 newptr, newend, (SIZE_T)(phend - phptr));
255 return;
257 memcpy(newptr, phptr, phend - phptr);
258 newptr += phend - phptr;
259 if (code & 1) *newptr++ = ' ';
261 ptr += 2;
264 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
267 /******************************************************************
268 * HLPFILE_Uncompress3
272 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
273 const BYTE* src, const BYTE* src_end)
275 unsigned int idx, len;
277 for (; src < src_end; src++)
279 if ((*src & 1) == 0)
281 idx = *src / 2;
282 if (idx > hlpfile->num_phrases)
284 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
285 len = 0;
287 else
289 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
290 if (dst + len <= dst_end)
291 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
294 else if ((*src & 0x03) == 0x01)
296 idx = (*src + 1) * 64;
297 idx += *++src;
298 if (idx > hlpfile->num_phrases)
300 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
301 len = 0;
303 else
305 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
306 if (dst + len <= dst_end)
307 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
310 else if ((*src & 0x07) == 0x03)
312 len = (*src / 8) + 1;
313 if (dst + len <= dst_end)
314 memcpy(dst, src + 1, len);
315 src += len;
317 else
319 len = (*src / 16) + 1;
320 if (dst + len <= dst_end)
321 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
323 dst += len;
326 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
327 return TRUE;
330 /******************************************************************
331 * HLPFILE_UncompressRLE
335 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE* dst, unsigned dstsz)
337 BYTE ch;
338 BYTE* sdst = dst + dstsz;
340 while (src < end)
342 ch = *src++;
343 if (ch & 0x80)
345 ch &= 0x7F;
346 if (dst + ch <= sdst)
347 memcpy(dst, src, ch);
348 src += ch;
350 else
352 if (dst + ch <= sdst)
353 memset(dst, (char)*src, ch);
354 src++;
356 dst += ch;
358 if (dst != sdst)
359 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
360 (SIZE_T)(dst - (sdst - dstsz)), dstsz);
364 /******************************************************************
365 * HLPFILE_PageByOffset
369 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset, ULONG* relative)
371 HLPFILE_PAGE* page;
372 HLPFILE_PAGE* found;
374 if (!hlpfile) return 0;
376 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, offset);
378 if (offset == 0xFFFFFFFF) return NULL;
379 page = NULL;
381 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
383 if (page->offset <= offset && (!found || found->offset < page->offset))
385 *relative = offset - page->offset;
386 found = page;
389 if (!found)
390 WINE_ERR("Page of offset %u not found in file %s\n",
391 offset, hlpfile->lpszPath);
392 return found;
395 /***********************************************************************
397 * HLPFILE_Contents
399 static HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile, ULONG* relative)
401 HLPFILE_PAGE* page = NULL;
403 if (!hlpfile) return NULL;
405 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start, relative);
406 if (!page)
408 page = hlpfile->first_page;
409 *relative = 0;
411 return page;
414 /**************************************************************************
415 * comp_PageByHash
417 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
420 static int comp_PageByHash(void *p, const void *key,
421 int leaf, void** next)
423 LONG lKey = (LONG_PTR)key;
424 LONG lTest = (INT)GET_UINT(p, 0);
426 *next = (char *)p+(leaf?8:6);
427 WINE_TRACE("Comparing '%d' with '%d'\n", lKey, lTest);
428 if (lTest < lKey) return -1;
429 if (lTest > lKey) return 1;
430 return 0;
433 /***********************************************************************
435 * HLPFILE_PageByHash
437 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash, ULONG* relative)
439 BYTE *ptr;
441 if (!hlpfile) return NULL;
442 if (!lHash) return HLPFILE_Contents(hlpfile, relative);
444 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lHash);
446 /* For win 3.0 files hash values are really page numbers */
447 if (hlpfile->version <= 16)
449 if (lHash >= hlpfile->wTOMapLen) return NULL;
450 return HLPFILE_PageByOffset(hlpfile, hlpfile->TOMap[lHash], relative);
453 ptr = HLPFILE_BPTreeSearch(hlpfile->Context, LongToPtr(lHash), comp_PageByHash);
454 if (!ptr)
456 WINE_ERR("Page of hash %x not found in file %s\n", lHash, hlpfile->lpszPath);
457 return NULL;
460 return HLPFILE_PageByOffset(hlpfile, GET_UINT(ptr, 4), relative);
463 /***********************************************************************
465 * HLPFILE_PageByMap
467 HLPFILE_PAGE *HLPFILE_PageByMap(HLPFILE* hlpfile, LONG lMap, ULONG* relative)
469 unsigned int i;
471 if (!hlpfile) return 0;
473 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lMap);
475 for (i = 0; i < hlpfile->wMapLen; i++)
477 if (hlpfile->Map[i].lMap == lMap)
478 return HLPFILE_PageByOffset(hlpfile, hlpfile->Map[i].offset, relative);
481 WINE_ERR("Page of Map %x not found in file %s\n", lMap, hlpfile->lpszPath);
482 return NULL;
485 /**************************************************************************
486 * comp_FindSubFile
488 * HLPFILE_BPTreeCompare function for HLPFILE directory.
491 static int comp_FindSubFile(void *p, const void *key,
492 int leaf, void** next)
494 *next = (char *)p+strlen(p)+(leaf?5:3);
495 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (char *)key);
496 return strcmp(p, key);
499 /***********************************************************************
501 * HLPFILE_FindSubFile
503 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
505 BYTE *ptr;
507 WINE_TRACE("looking for file '%s'\n", name);
508 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
509 name, comp_FindSubFile);
510 if (!ptr) return FALSE;
511 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
512 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
514 WINE_ERR("internal file %s does not fit\n", name);
515 return FALSE;
517 *subend = *subbuf + GET_UINT(*subbuf, 0);
518 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
520 WINE_ERR("internal file %s does not fit\n", name);
521 return FALSE;
523 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
525 WINE_ERR("invalid size provided for internal file %s\n", name);
526 return FALSE;
528 return TRUE;
531 /***********************************************************************
533 * HLPFILE_Hash
535 LONG HLPFILE_Hash(LPCSTR lpszContext)
537 LONG lHash = 0;
538 CHAR c;
540 while ((c = *lpszContext++))
542 CHAR x = 0;
543 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
544 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
545 if (c >= '1' && c <= '9') x = c - '0';
546 if (c == '0') x = 10;
547 if (c == '.') x = 12;
548 if (c == '_') x = 13;
549 if (x) lHash = lHash * 43 + x;
551 return lHash;
554 static LONG fetch_long(const BYTE** ptr)
556 LONG ret;
558 if (*(*ptr) & 1)
560 ret = (*(const ULONG*)(*ptr) - 0x80000000) / 2;
561 (*ptr) += 4;
563 else
565 ret = (*(const USHORT*)(*ptr) - 0x8000) / 2;
566 (*ptr) += 2;
569 return ret;
572 static ULONG fetch_ulong(const BYTE** ptr)
574 ULONG ret;
576 if (*(*ptr) & 1)
578 ret = *(const ULONG*)(*ptr) / 2;
579 (*ptr) += 4;
581 else
583 ret = *(const USHORT*)(*ptr) / 2;
584 (*ptr) += 2;
586 return ret;
589 static short fetch_short(const BYTE** ptr)
591 short ret;
593 if (*(*ptr) & 1)
595 ret = (*(const unsigned short*)(*ptr) - 0x8000) / 2;
596 (*ptr) += 2;
598 else
600 ret = (*(const unsigned char*)(*ptr) - 0x80) / 2;
601 (*ptr)++;
603 return ret;
606 static unsigned short fetch_ushort(const BYTE** ptr)
608 unsigned short ret;
610 if (*(*ptr) & 1)
612 ret = *(const unsigned short*)(*ptr) / 2;
613 (*ptr) += 2;
615 else
617 ret = *(const unsigned char*)(*ptr) / 2;
618 (*ptr)++;
620 return ret;
623 /******************************************************************
624 * HLPFILE_DecompressGfx
626 * Decompress the data part of a bitmap or a metafile
628 static const BYTE* HLPFILE_DecompressGfx(const BYTE* src, unsigned csz, unsigned sz, BYTE packing,
629 BYTE** alloc)
631 const BYTE* dst;
632 BYTE* tmp;
633 unsigned sz77;
635 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
637 switch (packing)
639 case 0: /* uncompressed */
640 if (sz != csz)
641 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
642 dst = src;
643 *alloc = NULL;
644 break;
645 case 1: /* RunLen */
646 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz);
647 if (!dst) return NULL;
648 HLPFILE_UncompressRLE(src, src + csz, *alloc, sz);
649 break;
650 case 2: /* LZ77 */
651 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
652 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz77);
653 if (!dst) return NULL;
654 HLPFILE_UncompressLZ77(src, src + csz, *alloc);
655 if (sz77 != sz)
656 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
657 break;
658 case 3: /* LZ77 then RLE */
659 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
660 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
661 if (!tmp) return FALSE;
662 HLPFILE_UncompressLZ77(src, src + csz, tmp);
663 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz);
664 if (!dst)
666 HeapFree(GetProcessHeap(), 0, tmp);
667 return FALSE;
669 HLPFILE_UncompressRLE(tmp, tmp + sz77, *alloc, sz);
670 HeapFree(GetProcessHeap(), 0, tmp);
671 break;
672 default:
673 WINE_FIXME("Unsupported packing %u\n", packing);
674 return NULL;
676 return dst;
679 static BOOL HLPFILE_RtfAddRawString(struct RtfData* rd, const char* str, size_t sz)
681 if (rd->ptr + sz >= rd->data + rd->allocated)
683 char* new = HeapReAlloc(GetProcessHeap(), 0, rd->data, rd->allocated *= 2);
684 if (!new) return FALSE;
685 rd->ptr = new + (rd->ptr - rd->data);
686 rd->data = new;
688 memcpy(rd->ptr, str, sz);
689 rd->ptr += sz;
691 return TRUE;
694 static BOOL HLPFILE_RtfAddControl(struct RtfData* rd, const char* str)
696 if (*str == '\\' || *str == '{') rd->in_text = FALSE;
697 else if (*str == '}') rd->in_text = TRUE;
698 return HLPFILE_RtfAddRawString(rd, str, strlen(str));
701 static BOOL HLPFILE_RtfAddText(struct RtfData* rd, const char* str)
703 const char* p;
704 const char* last;
705 const char* replace;
706 unsigned rlen;
708 if (!rd->in_text)
710 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
711 rd->in_text = TRUE;
713 for (last = p = str; *p; p++)
715 if (*p < 0) /* escape non ASCII chars */
717 static char xx[8];
718 rlen = sprintf(xx, "\\'%x", *(const BYTE*)p);
719 replace = xx;
721 else switch (*p)
723 case '{': rlen = 2; replace = "\\{"; break;
724 case '}': rlen = 2; replace = "\\}"; break;
725 case '\\': rlen = 2; replace = "\\\\"; break;
726 default: continue;
728 if ((p != last && !HLPFILE_RtfAddRawString(rd, last, p - last)) ||
729 !HLPFILE_RtfAddRawString(rd, replace, rlen)) return FALSE;
730 last = p + 1;
732 return HLPFILE_RtfAddRawString(rd, last, p - last);
735 /******************************************************************
736 * RtfAddHexBytes
739 static BOOL HLPFILE_RtfAddHexBytes(struct RtfData* rd, const void* _ptr, unsigned sz)
741 char tmp[512];
742 unsigned i, step;
743 const BYTE* ptr = _ptr;
744 static const char* _2hex = "0123456789abcdef";
746 if (!rd->in_text)
748 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
749 rd->in_text = TRUE;
751 for (; sz; sz -= step)
753 step = min(256, sz);
754 for (i = 0; i < step; i++)
756 tmp[2 * i + 0] = _2hex[*ptr >> 4];
757 tmp[2 * i + 1] = _2hex[*ptr++ & 0xF];
759 if (!HLPFILE_RtfAddRawString(rd, tmp, 2 * step)) return FALSE;
761 return TRUE;
764 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
765 const char* str, unsigned len, LONG hash,
766 unsigned clrChange, unsigned bHotSpot, unsigned wnd);
768 /******************************************************************
769 * HLPFILE_AddHotSpotLinks
772 static void HLPFILE_AddHotSpotLinks(struct RtfData* rd, HLPFILE* file,
773 const BYTE* start, ULONG hs_size, ULONG hs_offset)
775 unsigned i, hs_num;
776 ULONG hs_macro;
777 const char* str;
779 if (hs_size == 0 || hs_offset == 0) return;
781 start += hs_offset;
782 /* always 1 ?? */
783 hs_num = GET_USHORT(start, 1);
784 hs_macro = GET_UINT(start, 3);
786 str = (const char*)start + 7 + 15 * hs_num + hs_macro;
787 /* FIXME: should use hs_size to prevent out of bounds reads */
788 for (i = 0; i < hs_num; i++)
790 HLPFILE_HOTSPOTLINK* hslink;
792 WINE_TRACE("%02x-%02x%02x {%s,%s}\n",
793 start[7 + 15 * i + 0], start[7 + 15 * i + 1], start[7 + 15 * i + 2],
794 str, str + strlen(str) + 1);
795 /* str points to two null terminated strings:
796 * hotspot name, then link name
798 str += strlen(str) + 1; /* skip hotspot name */
800 hslink = NULL;
801 switch (start[7 + 15 * i + 0])
802 /* The next two chars always look like 0x04 0x00 ???
803 * What are they for ?
806 case 0xC8:
807 hslink = (HLPFILE_HOTSPOTLINK*)
808 HLPFILE_AllocLink(rd, hlp_link_macro, str, -1, 0, 0, 1, -1);
809 break;
811 case 0xE6:
812 case 0xE7:
813 hslink = (HLPFILE_HOTSPOTLINK*)
814 HLPFILE_AllocLink(rd, (start[7 + 15 * i + 0] & 1) ? hlp_link_link : hlp_link_popup,
815 file->lpszPath, -1, HLPFILE_Hash(str),
816 0, 1, -1);
817 break;
818 default:
819 WINE_FIXME("unknown hotsport target 0x%x\n", start[7 + 15 * i + 0]);
821 if (hslink)
823 hslink->x = GET_USHORT(start, 7 + 15 * i + 3);
824 hslink->y = GET_USHORT(start, 7 + 15 * i + 5);
825 hslink->width = GET_USHORT(start, 7 + 15 * i + 7);
826 hslink->height = GET_USHORT(start, 7 + 15 * i + 9);
827 /* target = GET_UINT(start, 7 + 15 * i + 11); */
829 str += strlen(str) + 1;
833 /******************************************************************
834 * HLPFILE_RtfAddTransparentBitmap
836 * We'll transform a transparent bitmap into an metafile that
837 * we then transform into RTF
839 static BOOL HLPFILE_RtfAddTransparentBitmap(struct RtfData* rd, const BITMAPINFO* bi,
840 const void* pict, unsigned nc)
842 HDC hdc, hdcMask, hdcMem, hdcEMF;
843 HBITMAP hbm, hbmMask, hbmOldMask, hbmOldMem;
844 HENHMETAFILE hEMF;
845 BOOL ret = FALSE;
846 void* data;
847 UINT sz;
849 hbm = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
850 CBM_INIT, pict, bi, DIB_RGB_COLORS);
852 hdcMem = CreateCompatibleDC(hdc);
853 hbmOldMem = SelectObject(hdcMem, hbm);
855 /* create the mask bitmap from the main bitmap */
856 hdcMask = CreateCompatibleDC(hdc);
857 hbmMask = CreateBitmap(bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, 1, 1, NULL);
858 hbmOldMask = SelectObject(hdcMask, hbmMask);
859 SetBkColor(hdcMem,
860 RGB(bi->bmiColors[nc - 1].rgbRed,
861 bi->bmiColors[nc - 1].rgbGreen,
862 bi->bmiColors[nc - 1].rgbBlue));
863 BitBlt(hdcMask, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCCOPY);
865 /* sets to RGB(0,0,0) the transparent bits in main bitmap */
866 SetBkColor(hdcMem, RGB(0,0,0));
867 SetTextColor(hdcMem, RGB(255,255,255));
868 BitBlt(hdcMem, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMask, 0, 0, SRCAND);
870 SelectObject(hdcMask, hbmOldMask);
871 DeleteDC(hdcMask);
873 SelectObject(hdcMem, hbmOldMem);
874 DeleteDC(hdcMem);
876 /* we create the bitmap on the fly */
877 hdcEMF = CreateEnhMetaFile(NULL, NULL, NULL, NULL);
878 hdcMem = CreateCompatibleDC(hdcEMF);
880 /* sets to RGB(0,0,0) the transparent bits in final bitmap */
881 hbmOldMem = SelectObject(hdcMem, hbmMask);
882 SetBkColor(hdcEMF, RGB(255, 255, 255));
883 SetTextColor(hdcEMF, RGB(0, 0, 0));
884 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCAND);
886 /* and copy the remaining bits of main bitmap */
887 SelectObject(hdcMem, hbm);
888 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCPAINT);
889 SelectObject(hdcMem, hbmOldMem);
890 DeleteDC(hdcMem);
892 /* do the cleanup */
893 ReleaseDC(0, hdc);
894 DeleteObject(hbmMask);
895 DeleteObject(hbm);
897 hEMF = CloseEnhMetaFile(hdcEMF);
899 /* generate rtf stream */
900 sz = GetEnhMetaFileBits(hEMF, 0, NULL);
901 if (sz && (data = HeapAlloc(GetProcessHeap(), 0, sz)))
903 if (sz == GetEnhMetaFileBits(hEMF, sz, data))
905 ret = HLPFILE_RtfAddControl(rd, "{\\pict\\emfblip") &&
906 HLPFILE_RtfAddHexBytes(rd, data, sz) &&
907 HLPFILE_RtfAddControl(rd, "}");
909 HeapFree(GetProcessHeap(), 0, data);
911 DeleteEnhMetaFile(hEMF);
913 return ret;
916 /******************************************************************
917 * HLPFILE_RtfAddBitmap
920 static BOOL HLPFILE_RtfAddBitmap(struct RtfData* rd, HLPFILE* file, const BYTE* beg, BYTE type, BYTE pack)
922 const BYTE* ptr;
923 const BYTE* pict_beg;
924 BYTE* alloc = NULL;
925 BITMAPINFO* bi;
926 ULONG off, csz;
927 unsigned nc = 0;
928 BOOL clrImportant = FALSE;
929 BOOL ret = FALSE;
930 char tmp[256];
931 unsigned hs_size, hs_offset;
933 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
934 if (!bi) return FALSE;
936 ptr = beg + 2; /* for type and pack */
938 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
939 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
940 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
941 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
942 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
943 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
944 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
945 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
946 clrImportant = fetch_ulong(&ptr);
947 bi->bmiHeader.biClrImportant = (clrImportant > 1) ? clrImportant : 0;
948 bi->bmiHeader.biCompression = BI_RGB;
949 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
950 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
951 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
952 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
953 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
954 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
956 csz = fetch_ulong(&ptr);
957 hs_size = fetch_ulong(&ptr);
959 off = GET_UINT(ptr, 0); ptr += 4;
960 hs_offset = GET_UINT(ptr, 0); ptr += 4;
961 HLPFILE_AddHotSpotLinks(rd, file, beg, hs_size, hs_offset);
963 /* now read palette info */
964 if (type == 0x06)
966 unsigned i;
968 nc = bi->bmiHeader.biClrUsed;
969 /* not quite right, especially for bitfields type of compression */
970 if (!nc && bi->bmiHeader.biBitCount <= 8)
971 nc = 1 << bi->bmiHeader.biBitCount;
973 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
974 if (!bi) return FALSE;
975 for (i = 0; i < nc; i++)
977 bi->bmiColors[i].rgbBlue = ptr[0];
978 bi->bmiColors[i].rgbGreen = ptr[1];
979 bi->bmiColors[i].rgbRed = ptr[2];
980 bi->bmiColors[i].rgbReserved = 0;
981 ptr += 4;
984 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack, &alloc);
986 if (clrImportant == 1 && nc > 0)
988 ret = HLPFILE_RtfAddTransparentBitmap(rd, bi, pict_beg, nc);
989 goto done;
991 if (!HLPFILE_RtfAddControl(rd, "{\\pict")) goto done;
992 if (type == 0x06)
994 sprintf(tmp, "\\dibitmap0\\picw%d\\pich%d",
995 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
996 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
997 if (!HLPFILE_RtfAddHexBytes(rd, bi, sizeof(*bi) + nc * sizeof(RGBQUAD))) goto done;
999 else
1001 sprintf(tmp, "\\wbitmap0\\wbmbitspixel%d\\wbmplanes%d\\picw%d\\pich%d",
1002 bi->bmiHeader.biBitCount, bi->bmiHeader.biPlanes,
1003 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
1004 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1006 if (!HLPFILE_RtfAddHexBytes(rd, pict_beg, bi->bmiHeader.biSizeImage)) goto done;
1007 if (!HLPFILE_RtfAddControl(rd, "}")) goto done;
1009 ret = TRUE;
1010 done:
1011 HeapFree(GetProcessHeap(), 0, bi);
1012 HeapFree(GetProcessHeap(), 0, alloc);
1014 return ret;
1017 /******************************************************************
1018 * HLPFILE_RtfAddMetaFile
1021 static BOOL HLPFILE_RtfAddMetaFile(struct RtfData* rd, HLPFILE* file, const BYTE* beg, BYTE pack)
1023 ULONG size, csize, off, hs_offset, hs_size;
1024 const BYTE* ptr;
1025 const BYTE* bits;
1026 BYTE* alloc = NULL;
1027 char tmp[256];
1028 unsigned mm;
1029 BOOL ret;
1031 WINE_TRACE("Loading metafile\n");
1033 ptr = beg + 2; /* for type and pack */
1035 mm = fetch_ushort(&ptr); /* mapping mode */
1036 sprintf(tmp, "{\\pict\\wmetafile%d\\picw%d\\pich%d",
1037 mm, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2));
1038 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1039 ptr += 4;
1041 size = fetch_ulong(&ptr); /* decompressed size */
1042 csize = fetch_ulong(&ptr); /* compressed size */
1043 hs_size = fetch_ulong(&ptr); /* hotspot size */
1044 off = GET_UINT(ptr, 0);
1045 hs_offset = GET_UINT(ptr, 4);
1046 ptr += 8;
1048 HLPFILE_AddHotSpotLinks(rd, file, beg, hs_size, hs_offset);
1050 WINE_TRACE("sz=%u csz=%u offs=%u/%u,%u/%u\n",
1051 size, csize, off, (ULONG)(ptr - beg), hs_size, hs_offset);
1053 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack, &alloc);
1054 if (!bits) return FALSE;
1056 ret = HLPFILE_RtfAddHexBytes(rd, bits, size) &&
1057 HLPFILE_RtfAddControl(rd, "}");
1059 HeapFree(GetProcessHeap(), 0, alloc);
1061 return ret;
1064 /******************************************************************
1065 * HLPFILE_RtfAddGfxByAddr
1068 static BOOL HLPFILE_RtfAddGfxByAddr(struct RtfData* rd, HLPFILE *hlpfile,
1069 const BYTE* ref, ULONG size)
1071 unsigned i, numpict;
1073 numpict = GET_USHORT(ref, 2);
1074 WINE_TRACE("Got picture magic=%04x #=%d\n", GET_USHORT(ref, 0), numpict);
1076 for (i = 0; i < numpict; i++)
1078 const BYTE* beg;
1079 const BYTE* ptr;
1080 BYTE type, pack;
1082 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
1083 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
1085 type = *ptr++;
1086 pack = *ptr++;
1088 switch (type)
1090 case 5: /* device dependent bmp */
1091 case 6: /* device independent bmp */
1092 HLPFILE_RtfAddBitmap(rd, hlpfile, beg, type, pack);
1093 break;
1094 case 8:
1095 HLPFILE_RtfAddMetaFile(rd, hlpfile, beg, pack);
1096 break;
1097 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
1100 /* FIXME: hotspots */
1102 /* FIXME: implement support for multiple picture format */
1103 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
1104 break;
1106 return TRUE;
1109 /******************************************************************
1110 * HLPFILE_RtfAddGfxByIndex
1114 static BOOL HLPFILE_RtfAddGfxByIndex(struct RtfData* rd, HLPFILE *hlpfile,
1115 unsigned index)
1117 char tmp[16];
1118 BYTE *ref, *end;
1120 WINE_TRACE("Loading picture #%d\n", index);
1122 sprintf(tmp, "|bm%u", index);
1124 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
1126 ref += 9;
1127 return HLPFILE_RtfAddGfxByAddr(rd, hlpfile, ref, end - ref);
1130 /******************************************************************
1131 * HLPFILE_AllocLink
1135 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
1136 const char* str, unsigned len, LONG hash,
1137 unsigned clrChange, unsigned bHotSpot, unsigned wnd)
1139 HLPFILE_LINK* link;
1140 char* link_str;
1141 unsigned asz = bHotSpot ? sizeof(HLPFILE_HOTSPOTLINK) : sizeof(HLPFILE_LINK);
1143 /* FIXME: should build a string table for the attributes.link.lpszPath
1144 * they are reallocated for each link
1146 if (len == -1) len = strlen(str);
1147 link = HeapAlloc(GetProcessHeap(), 0, asz + len + 1);
1148 if (!link) return NULL;
1150 link->cookie = cookie;
1151 link->string = link_str = (char*)link + asz;
1152 memcpy(link_str, str, len);
1153 link_str[len] = '\0';
1154 link->hash = hash;
1155 link->bClrChange = clrChange ? 1 : 0;
1156 link->bHotSpot = bHotSpot;
1157 link->window = wnd;
1158 link->next = rd->first_link;
1159 rd->first_link = link;
1160 link->cpMin = rd->char_pos;
1161 rd->force_color = clrChange;
1162 if (rd->current_link) WINE_FIXME("Pending link\n");
1163 if (bHotSpot)
1164 link->cpMax = rd->char_pos;
1165 else
1166 rd->current_link = link;
1168 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
1169 link->cookie, link->string, link->hash, link->window);
1170 return link;
1173 static unsigned HLPFILE_HalfPointsToTwips(unsigned pts)
1175 static unsigned logPxY;
1176 if (!logPxY)
1178 HDC hdc = GetDC(NULL);
1179 logPxY = GetDeviceCaps(hdc, LOGPIXELSY);
1180 ReleaseDC(NULL, hdc);
1182 return MulDiv(pts, 72 * 10, logPxY);
1185 /***********************************************************************
1187 * HLPFILE_BrowseParagraph
1189 static BOOL HLPFILE_BrowseParagraph(HLPFILE_PAGE* page, struct RtfData* rd,
1190 BYTE *buf, BYTE* end, unsigned* parlen)
1192 UINT textsize;
1193 const BYTE *format, *format_end;
1194 char *text, *text_base, *text_end;
1195 LONG size, blocksize, datalen;
1196 unsigned short bits;
1197 unsigned nc, ncol = 1;
1198 short table_width;
1199 BOOL in_table = FALSE;
1200 char tmp[256];
1201 BOOL ret = FALSE;
1203 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
1205 *parlen = 0;
1206 blocksize = GET_UINT(buf, 0);
1207 size = GET_UINT(buf, 0x4);
1208 datalen = GET_UINT(buf, 0x10);
1209 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
1210 if (!text) return FALSE;
1211 if (size > blocksize - datalen)
1213 /* need to decompress */
1214 if (page->file->hasPhrases)
1215 HLPFILE_Uncompress2(page->file, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
1216 else if (page->file->hasPhrases40)
1217 HLPFILE_Uncompress3(page->file, text, text + size, buf + datalen, end);
1218 else
1220 WINE_FIXME("Text size is too long, splitting\n");
1221 size = blocksize - datalen;
1222 memcpy(text, buf + datalen, size);
1225 else
1226 memcpy(text, buf + datalen, size);
1228 text_end = text + size;
1230 format = buf + 0x15;
1231 format_end = buf + GET_UINT(buf, 0x10);
1233 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
1235 fetch_long(&format);
1236 *parlen = fetch_ushort(&format);
1239 if (buf[0x14] == 0x23)
1241 char type;
1243 in_table = TRUE;
1244 ncol = *format++;
1246 if (!HLPFILE_RtfAddControl(rd, "\\trowd")) goto done;
1247 type = *format++;
1248 if (type == 0 || type == 2)
1250 table_width = GET_SHORT(format, 0);
1251 format += 2;
1253 else
1254 table_width = 32767;
1255 WINE_TRACE("New table: cols=%d type=%x width=%d\n",
1256 ncol, type, table_width);
1257 if (ncol > 1)
1259 int pos;
1260 sprintf(tmp, "\\trgaph%d\\trleft%d",
1261 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6), table_width, 32767)),
1262 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)));
1263 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1264 pos = HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6) / 2, table_width, 32767));
1265 for (nc = 0; nc < ncol; nc++)
1267 WINE_TRACE("column(%d/%d) gap=%d width=%d\n",
1268 nc, ncol, GET_SHORT(format, nc*4),
1269 GET_SHORT(format, nc*4+2));
1270 pos += GET_SHORT(format, nc * 4) + GET_SHORT(format, nc * 4 + 2);
1271 sprintf(tmp, "\\cellx%d",
1272 HLPFILE_HalfPointsToTwips(MulDiv(pos, table_width, 32767)));
1273 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1276 else
1278 WINE_TRACE("column(0/%d) gap=%d width=%d\n",
1279 ncol, GET_SHORT(format, 0), GET_SHORT(format, 2));
1280 sprintf(tmp, "\\trleft%d\\cellx%d ",
1281 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)),
1282 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0) + GET_SHORT(format, 2),
1283 table_width, 32767)));
1284 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1286 format += ncol * 4;
1289 for (nc = 0; nc < ncol; /**/)
1291 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
1292 if (!HLPFILE_RtfAddControl(rd, "\\pard")) goto done;
1293 if (in_table)
1295 nc = GET_SHORT(format, 0);
1296 if (nc == -1) break;
1297 format += 5;
1298 if (!HLPFILE_RtfAddControl(rd, "\\intbl")) goto done;
1300 else nc++;
1301 if (buf[0x14] == 0x01)
1302 format += 6;
1303 else
1304 format += 4;
1305 bits = GET_USHORT(format, 0); format += 2;
1306 if (bits & 0x0001) fetch_long(&format);
1307 if (bits & 0x0002)
1309 sprintf(tmp, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1310 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1312 if (bits & 0x0004)
1314 sprintf(tmp, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1315 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1317 if (bits & 0x0008)
1319 sprintf(tmp, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1320 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1322 if (bits & 0x0010)
1324 sprintf(tmp, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1325 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1327 if (bits & 0x0020)
1329 sprintf(tmp, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1330 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1332 if (bits & 0x0040)
1334 sprintf(tmp, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1335 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1337 if (bits & 0x0100)
1339 BYTE brdr = *format++;
1340 short w;
1342 if (brdr & 0x01 && !HLPFILE_RtfAddControl(rd, "\\box")) goto done;
1343 if (brdr & 0x02 && !HLPFILE_RtfAddControl(rd, "\\brdrt")) goto done;
1344 if (brdr & 0x04 && !HLPFILE_RtfAddControl(rd, "\\brdrl")) goto done;
1345 if (brdr & 0x08 && !HLPFILE_RtfAddControl(rd, "\\brdrb")) goto done;
1346 if (brdr & 0x10 && !HLPFILE_RtfAddControl(rd, "\\brdrr")) goto done;
1347 if (brdr & 0x20 && !HLPFILE_RtfAddControl(rd, "\\brdrth")) goto done;
1348 if (!(brdr & 0x20) && !HLPFILE_RtfAddControl(rd, "\\brdrs")) goto done;
1349 if (brdr & 0x40 && !HLPFILE_RtfAddControl(rd, "\\brdrdb")) goto done;
1350 /* 0x80: unknown */
1352 w = GET_SHORT(format, 0); format += 2;
1353 if (w)
1355 sprintf(tmp, "\\brdrw%d", HLPFILE_HalfPointsToTwips(w));
1356 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1359 if (bits & 0x0200)
1361 int i, ntab = fetch_short(&format);
1362 unsigned tab, ts;
1363 const char* kind;
1365 for (i = 0; i < ntab; i++)
1367 tab = fetch_ushort(&format);
1368 ts = (tab & 0x4000) ? fetch_ushort(&format) : 0 /* left */;
1369 switch (ts)
1371 default: WINE_FIXME("Unknown tab style %x\n", ts);
1372 /* fall through */
1373 case 0: kind = ""; break;
1374 case 1: kind = "\\tqr"; break;
1375 case 2: kind = "\\tqc"; break;
1377 /* FIXME: do kind */
1378 sprintf(tmp, "%s\\tx%d",
1379 kind, HLPFILE_HalfPointsToTwips(tab & 0x3FFF));
1380 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1383 switch (bits & 0xc00)
1385 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1386 case 0: if (!HLPFILE_RtfAddControl(rd, "\\ql")) goto done; break;
1387 case 0x400: if (!HLPFILE_RtfAddControl(rd, "\\qr")) goto done; break;
1388 case 0x800: if (!HLPFILE_RtfAddControl(rd, "\\qc")) goto done; break;
1391 /* 0x1000 doesn't need space */
1392 if ((bits & 0x1000) && !HLPFILE_RtfAddControl(rd, "\\keep")) goto done;
1393 if ((bits & 0xE080) != 0)
1394 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
1396 while (text < text_end && format < format_end)
1398 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
1399 textsize = strlen(text);
1400 if (textsize)
1402 if (rd->force_color)
1404 if ((rd->current_link->cookie == hlp_link_popup) ?
1405 !HLPFILE_RtfAddControl(rd, "{\\uld\\cf1") :
1406 !HLPFILE_RtfAddControl(rd, "{\\ul\\cf1")) goto done;
1408 if (!HLPFILE_RtfAddText(rd, text)) goto done;
1409 if (rd->force_color && !HLPFILE_RtfAddControl(rd, "}")) goto done;
1410 rd->char_pos += textsize;
1412 /* else: null text, keep on storing attributes */
1413 text += textsize + 1;
1415 if (*format == 0xff)
1417 format++;
1418 break;
1421 WINE_TRACE("format=%02x\n", *format);
1422 switch (*format)
1424 case 0x20:
1425 WINE_FIXME("NIY20\n");
1426 format += 5;
1427 break;
1429 case 0x21:
1430 WINE_FIXME("NIY21\n");
1431 format += 3;
1432 break;
1434 case 0x80:
1436 unsigned font = GET_USHORT(format, 1);
1437 unsigned fs;
1439 WINE_TRACE("Changing font to %d\n", font);
1440 format += 3;
1441 /* Font size in hlpfile is given in the same units as
1442 rtf control word \fs uses (half-points). */
1443 switch (rd->font_scale)
1445 case 0: fs = page->file->fonts[font].LogFont.lfHeight - 4; break;
1446 default:
1447 case 1: fs = page->file->fonts[font].LogFont.lfHeight; break;
1448 case 2: fs = page->file->fonts[font].LogFont.lfHeight + 4; break;
1450 /* FIXME: missing at least colors, also bold attribute looses information */
1452 sprintf(tmp, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1453 font, font + 2, fs,
1454 page->file->fonts[font].LogFont.lfWeight > 400 ? "\\b" : "\\b0",
1455 page->file->fonts[font].LogFont.lfItalic ? "\\i" : "\\i0",
1456 page->file->fonts[font].LogFont.lfUnderline ? "\\ul" : "\\ul0",
1457 page->file->fonts[font].LogFont.lfStrikeOut ? "\\strike" : "\\strike0");
1458 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1460 break;
1462 case 0x81:
1463 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1464 format += 1;
1465 rd->char_pos++;
1466 break;
1468 case 0x82:
1469 if (in_table)
1471 if (format[1] != 0xFF)
1473 if (!HLPFILE_RtfAddControl(rd, "\\par\\intbl")) goto done;
1475 else
1477 if (!HLPFILE_RtfAddControl(rd, "\\cell\\pard\\intbl")) goto done;
1480 else if (!HLPFILE_RtfAddControl(rd, "\\par")) goto done;
1481 format += 1;
1482 rd->char_pos++;
1483 break;
1485 case 0x83:
1486 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1487 format += 1;
1488 rd->char_pos++;
1489 break;
1491 #if 0
1492 case 0x84:
1493 format += 3;
1494 break;
1495 #endif
1497 case 0x86:
1498 case 0x87:
1499 case 0x88:
1501 BYTE type = format[1];
1502 LONG size;
1504 /* FIXME: we don't use 'BYTE pos = (*format - 0x86);' for the image position */
1505 format += 2;
1506 size = fetch_long(&format);
1508 switch (type)
1510 case 0x22:
1511 fetch_ushort(&format); /* hot spot */
1512 /* fall thru */
1513 case 0x03:
1514 switch (GET_SHORT(format, 0))
1516 case 0:
1517 HLPFILE_RtfAddGfxByIndex(rd, page->file, GET_SHORT(format, 2));
1518 rd->char_pos++;
1519 break;
1520 case 1:
1521 WINE_FIXME("does it work ??? %x<%u>#%u\n",
1522 GET_SHORT(format, 0),
1523 size, GET_SHORT(format, 2));
1524 HLPFILE_RtfAddGfxByAddr(rd, page->file, format + 2, size - 4);
1525 rd->char_pos++;
1526 break;
1527 default:
1528 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1529 break;
1531 break;
1532 case 0x05:
1533 WINE_FIXME("Got an embedded element %s\n", format + 6);
1534 break;
1535 default:
1536 WINE_FIXME("Got a type %d picture\n", type);
1537 break;
1539 format += size;
1541 break;
1543 case 0x89:
1544 format += 1;
1545 if (!rd->current_link)
1546 WINE_FIXME("No existing link\n");
1547 rd->current_link->cpMax = rd->char_pos;
1548 rd->current_link = NULL;
1549 rd->force_color = FALSE;
1550 break;
1552 case 0x8B:
1553 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1554 format += 1;
1555 rd->char_pos++;
1556 break;
1558 case 0x8C:
1559 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1560 /* FIXME: it could be that hypen is also in input stream !! */
1561 format += 1;
1562 rd->char_pos++;
1563 break;
1565 #if 0
1566 case 0xA9:
1567 format += 2;
1568 break;
1569 #endif
1571 case 0xC8:
1572 case 0xCC:
1573 WINE_TRACE("macro => %s\n", format + 3);
1574 HLPFILE_AllocLink(rd, hlp_link_macro, (const char*)format + 3,
1575 GET_USHORT(format, 1), 0, !(*format & 4), 0, -1);
1576 format += 3 + GET_USHORT(format, 1);
1577 break;
1579 case 0xE0:
1580 case 0xE1:
1581 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1582 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1583 page->file->lpszPath, -1, GET_UINT(format, 1), 1, 0, -1);
1586 format += 5;
1587 break;
1589 case 0xE2:
1590 case 0xE3:
1591 case 0xE6:
1592 case 0xE7:
1593 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1594 page->file->lpszPath, -1, GET_UINT(format, 1),
1595 !(*format & 4), 0, -1);
1596 format += 5;
1597 break;
1599 case 0xEA:
1600 case 0xEB:
1601 case 0xEE:
1602 case 0xEF:
1604 char* ptr = (char*) format + 8;
1605 BYTE type = format[3];
1606 int wnd = -1;
1608 switch (type)
1610 case 1:
1611 wnd = *ptr;
1612 /* fall through */
1613 case 0:
1614 ptr = page->file->lpszPath;
1615 break;
1616 case 6:
1617 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1619 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1621 if (wnd == -1)
1622 WINE_WARN("Couldn't find window info for %s\n", ptr);
1623 ptr += strlen(ptr) + 1;
1624 /* fall through */
1625 case 4:
1626 break;
1627 default:
1628 WINE_WARN("Unknown link type %d\n", type);
1629 break;
1631 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1632 ptr, -1, GET_UINT(format, 4), !(*format & 4), 0, wnd);
1634 format += 3 + GET_USHORT(format, 1);
1635 break;
1637 default:
1638 WINE_WARN("format %02x\n", *format);
1639 format++;
1643 if (in_table)
1645 if (!HLPFILE_RtfAddControl(rd, "\\row\\par\\pard\\plain")) goto done;
1646 rd->char_pos += 2;
1648 ret = TRUE;
1649 done:
1651 HeapFree(GetProcessHeap(), 0, text_base);
1652 return ret;
1655 /******************************************************************
1656 * HLPFILE_BrowsePage
1659 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page, struct RtfData* rd,
1660 unsigned font_scale, unsigned relative)
1662 HLPFILE *hlpfile = page->file;
1663 BYTE *buf, *end;
1664 DWORD ref = page->reference;
1665 unsigned index, old_index = -1, offset, count = 0, offs = 0;
1666 unsigned cpg, parlen;
1667 char tmp[1024];
1668 const char* ck = NULL;
1670 rd->in_text = TRUE;
1671 rd->data = rd->ptr = HeapAlloc(GetProcessHeap(), 0, rd->allocated = 32768);
1672 rd->char_pos = 0;
1673 rd->first_link = rd->current_link = NULL;
1674 rd->force_color = FALSE;
1675 rd->font_scale = font_scale;
1676 rd->relative = relative;
1677 rd->char_pos_rel = 0;
1679 switch (hlpfile->charset)
1681 case DEFAULT_CHARSET:
1682 case ANSI_CHARSET: cpg = 1252; break;
1683 case SHIFTJIS_CHARSET: cpg = 932; break;
1684 case HANGEUL_CHARSET: cpg = 949; break;
1685 case GB2312_CHARSET: cpg = 936; break;
1686 case CHINESEBIG5_CHARSET: cpg = 950; break;
1687 case GREEK_CHARSET: cpg = 1253; break;
1688 case TURKISH_CHARSET: cpg = 1254; break;
1689 case HEBREW_CHARSET: cpg = 1255; break;
1690 case ARABIC_CHARSET: cpg = 1256; break;
1691 case BALTIC_CHARSET: cpg = 1257; break;
1692 case VIETNAMESE_CHARSET: cpg = 1258; break;
1693 case RUSSIAN_CHARSET: cpg = 1251; break;
1694 case EE_CHARSET: cpg = 1250; break;
1695 case THAI_CHARSET: cpg = 874; break;
1696 case JOHAB_CHARSET: cpg = 1361; break;
1697 case MAC_CHARSET: ck = "mac"; break;
1698 default:
1699 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1700 cpg = 1252;
1702 if (ck)
1704 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1705 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1707 else
1709 sprintf(tmp, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg);
1710 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1713 /* generate font table */
1714 if (!HLPFILE_RtfAddControl(rd, "{\\fonttbl")) return FALSE;
1715 for (index = 0; index < hlpfile->numFonts; index++)
1717 const char* family;
1718 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1720 case FF_MODERN: family = "modern"; break;
1721 case FF_ROMAN: family = "roman"; break;
1722 case FF_SWISS: family = "swiss"; break;
1723 case FF_SCRIPT: family = "script"; break;
1724 case FF_DECORATIVE: family = "decor"; break;
1725 default: family = "nil"; break;
1727 sprintf(tmp, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1728 index, family,
1729 hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0x0F,
1730 hlpfile->fonts[index].LogFont.lfCharSet,
1731 hlpfile->fonts[index].LogFont.lfFaceName);
1732 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1734 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1735 /* generate color table */
1736 if (!HLPFILE_RtfAddControl(rd, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE;
1737 for (index = 0; index < hlpfile->numFonts; index++)
1739 const char* family;
1740 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1742 case FF_MODERN: family = "modern"; break;
1743 case FF_ROMAN: family = "roman"; break;
1744 case FF_SWISS: family = "swiss"; break;
1745 case FF_SCRIPT: family = "script"; break;
1746 case FF_DECORATIVE: family = "decor"; break;
1747 default: family = "nil"; break;
1749 sprintf(tmp, "\\red%d\\green%d\\blue%d;",
1750 GetRValue(hlpfile->fonts[index].color),
1751 GetGValue(hlpfile->fonts[index].color),
1752 GetBValue(hlpfile->fonts[index].color));
1753 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1755 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1759 if (hlpfile->version <= 16)
1761 index = (ref - 0x0C) / hlpfile->dsize;
1762 offset = (ref - 0x0C) % hlpfile->dsize;
1764 else
1766 index = (ref - 0x0C) >> 14;
1767 offset = (ref - 0x0C) & 0x3FFF;
1770 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
1772 /* we jumped to the next block, adjust pointers */
1773 ref -= 12;
1774 offset -= 12;
1777 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1778 buf = hlpfile->topic_map[index] + offset;
1779 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1780 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1781 if (index != old_index) {offs = 0; old_index = index;}
1783 switch (buf[0x14])
1785 case 0x02:
1786 if (count++) goto done;
1787 break;
1788 case 0x01:
1789 case 0x20:
1790 case 0x23:
1791 if (!HLPFILE_BrowseParagraph(page, rd, buf, end, &parlen)) return FALSE;
1792 if (relative > index * 0x8000 + offs)
1793 rd->char_pos_rel = rd->char_pos;
1794 offs += parlen;
1795 break;
1796 default:
1797 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1799 if (hlpfile->version <= 16)
1801 ref += GET_UINT(buf, 0xc);
1802 if (GET_UINT(buf, 0xc) == 0)
1803 break;
1805 else
1806 ref = GET_UINT(buf, 0xc);
1807 } while (ref != 0xffffffff);
1808 done:
1809 page->first_link = rd->first_link;
1810 return HLPFILE_RtfAddControl(rd, "}");
1813 /******************************************************************
1814 * HLPFILE_ReadFont
1818 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1820 BYTE *ref, *end;
1821 unsigned i, len, idx;
1822 unsigned face_num, dscr_num, face_offset, dscr_offset;
1823 BYTE flag, family;
1825 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1827 WINE_WARN("no subfile FONT\n");
1828 hlpfile->numFonts = 0;
1829 hlpfile->fonts = NULL;
1830 return FALSE;
1833 ref += 9;
1835 face_num = GET_USHORT(ref, 0);
1836 dscr_num = GET_USHORT(ref, 2);
1837 face_offset = GET_USHORT(ref, 4);
1838 dscr_offset = GET_USHORT(ref, 6);
1840 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1841 face_num, face_offset, dscr_num, dscr_offset);
1843 hlpfile->numFonts = dscr_num;
1844 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1846 len = (dscr_offset - face_offset) / face_num;
1847 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1848 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1849 for (i = 0; i < dscr_num; i++)
1851 flag = ref[dscr_offset + i * 11 + 0];
1852 family = ref[dscr_offset + i * 11 + 2];
1854 hlpfile->fonts[i].LogFont.lfHeight = ref[dscr_offset + i * 11 + 1];
1855 hlpfile->fonts[i].LogFont.lfWidth = 0;
1856 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1857 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1858 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1859 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) ? TRUE : FALSE;
1860 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) ? TRUE : FALSE;
1861 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) ? TRUE : FALSE;
1862 hlpfile->fonts[i].LogFont.lfCharSet = hlpfile->charset;
1863 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1864 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1865 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1866 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1868 switch (family)
1870 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1871 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1872 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1873 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1874 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1875 default: WINE_FIXME("Unknown family %u\n", family);
1877 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1879 if (idx < face_num)
1881 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1882 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1884 else
1886 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1887 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1889 hlpfile->fonts[i].hFont = 0;
1890 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1891 ref[dscr_offset + i * 11 + 6],
1892 ref[dscr_offset + i * 11 + 7]);
1893 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1894 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1895 i, flag,
1896 X(0, "bold"),
1897 X(1, "italic"),
1898 X(2, "underline"),
1899 X(3, "strikeOut"),
1900 X(4, "dblUnderline"),
1901 X(5, "smallCaps"),
1902 ref[dscr_offset + i * 11 + 1],
1903 family,
1904 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1905 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1907 return TRUE;
1910 /***********************************************************************
1912 * HLPFILE_ReadFileToBuffer
1914 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1916 BYTE header[16], dummy[1];
1918 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1920 /* sanity checks */
1921 if (GET_UINT(header, 0) != 0x00035F3F)
1922 {WINE_WARN("wrong header\n"); return FALSE;};
1924 hlpfile->file_buffer_size = GET_UINT(header, 12);
1925 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1926 if (!hlpfile->file_buffer) return FALSE;
1928 memcpy(hlpfile->file_buffer, header, 16);
1929 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1930 {WINE_WARN("filesize1\n"); return FALSE;};
1932 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1934 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1936 return TRUE;
1939 /***********************************************************************
1941 * HLPFILE_SystemCommands
1943 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1945 BYTE *buf, *ptr, *end;
1946 HLPFILE_MACRO *macro, **m;
1947 LPSTR p;
1948 unsigned short magic, minor, major, flags;
1950 hlpfile->lpszTitle = NULL;
1952 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1954 magic = GET_USHORT(buf + 9, 0);
1955 minor = GET_USHORT(buf + 9, 2);
1956 major = GET_USHORT(buf + 9, 4);
1957 /* gen date on 4 bytes */
1958 flags = GET_USHORT(buf + 9, 10);
1959 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1960 magic, major, minor, flags);
1961 if (magic != 0x036C || major != 1)
1962 {WINE_WARN("Wrong system header\n"); return FALSE;}
1963 if (minor <= 16)
1965 hlpfile->tbsize = 0x800;
1966 hlpfile->compressed = 0;
1968 else if (flags == 0)
1970 hlpfile->tbsize = 0x1000;
1971 hlpfile->compressed = 0;
1973 else if (flags == 4)
1975 hlpfile->tbsize = 0x1000;
1976 hlpfile->compressed = 1;
1978 else
1980 hlpfile->tbsize = 0x800;
1981 hlpfile->compressed = 1;
1984 if (hlpfile->compressed)
1985 hlpfile->dsize = 0x4000;
1986 else
1987 hlpfile->dsize = hlpfile->tbsize - 0x0C;
1989 hlpfile->version = minor;
1990 hlpfile->flags = flags;
1991 hlpfile->charset = DEFAULT_CHARSET;
1993 if (hlpfile->version <= 16)
1995 char *str = (char*)buf + 0x15;
1997 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
1998 if (!hlpfile->lpszTitle) return FALSE;
1999 lstrcpy(hlpfile->lpszTitle, str);
2000 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
2001 /* Nothing more to parse */
2002 return TRUE;
2004 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
2006 char *str = (char*) ptr + 4;
2007 switch (GET_USHORT(ptr, 0))
2009 case 1:
2010 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
2011 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
2012 if (!hlpfile->lpszTitle) return FALSE;
2013 lstrcpy(hlpfile->lpszTitle, str);
2014 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
2015 break;
2017 case 2:
2018 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
2019 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
2020 if (!hlpfile->lpszCopyright) return FALSE;
2021 lstrcpy(hlpfile->lpszCopyright, str);
2022 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
2023 break;
2025 case 3:
2026 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
2027 hlpfile->contents_start = GET_UINT(ptr, 4);
2028 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
2029 break;
2031 case 4:
2032 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + lstrlen(str) + 1);
2033 if (!macro) break;
2034 p = (char*)macro + sizeof(HLPFILE_MACRO);
2035 lstrcpy(p, str);
2036 macro->lpszMacro = p;
2037 macro->next = 0;
2038 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
2039 *m = macro;
2040 break;
2042 case 5:
2043 if (GET_USHORT(ptr, 4 + 4) != 1)
2044 WINE_FIXME("More than one icon, picking up first\n");
2045 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
2046 hlpfile->hIcon = CreateIconFromResourceEx(ptr + 4 + 0x16,
2047 GET_USHORT(ptr, 2) - 0x16, TRUE,
2048 0x30000, 0, 0, 0);
2049 break;
2051 case 6:
2052 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
2054 if (hlpfile->windows)
2055 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
2056 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
2057 else
2058 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
2059 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
2061 if (hlpfile->windows)
2063 unsigned flags = GET_USHORT(ptr, 4);
2064 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
2066 if (flags & 0x0001) strcpy(wi->type, &str[2]);
2067 else wi->type[0] = '\0';
2068 if (flags & 0x0002) strcpy(wi->name, &str[12]);
2069 else wi->name[0] = '\0';
2070 if (flags & 0x0004) strcpy(wi->caption, &str[21]);
2071 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
2072 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
2073 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
2074 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
2075 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
2076 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
2077 wi->win_style = WS_OVERLAPPEDWINDOW;
2078 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
2079 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
2080 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",
2081 flags & 0x0001 ? 'T' : 't',
2082 flags & 0x0002 ? 'N' : 'n',
2083 flags & 0x0004 ? 'C' : 'c',
2084 flags & 0x0008 ? 'X' : 'x',
2085 flags & 0x0010 ? 'Y' : 'y',
2086 flags & 0x0020 ? 'W' : 'w',
2087 flags & 0x0040 ? 'H' : 'h',
2088 flags & 0x0080 ? 'S' : 's',
2089 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
2090 wi->size.cx, wi->size.cy);
2092 break;
2093 case 8:
2094 WINE_WARN("Citation: '%s'\n", ptr + 4);
2095 break;
2096 case 11:
2097 hlpfile->charset = ptr[4];
2098 WINE_TRACE("Charset: %d\n", hlpfile->charset);
2099 break;
2100 default:
2101 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
2104 if (!hlpfile->lpszTitle)
2105 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
2106 return TRUE;
2109 /***********************************************************************
2111 * HLPFILE_GetContext
2113 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2115 BYTE *cbuf, *cend;
2116 unsigned clen;
2118 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2119 {WINE_WARN("context0\n"); return FALSE;}
2121 clen = cend - cbuf;
2122 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2123 if (!hlpfile->Context) return FALSE;
2124 memcpy(hlpfile->Context, cbuf, clen);
2126 return TRUE;
2129 /***********************************************************************
2131 * HLPFILE_GetKeywords
2133 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2135 BYTE *cbuf, *cend;
2136 unsigned clen;
2138 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2139 clen = cend - cbuf;
2140 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2141 if (!hlpfile->kwbtree) return FALSE;
2142 memcpy(hlpfile->kwbtree, cbuf, clen);
2144 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2146 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2147 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2148 return FALSE;
2150 clen = cend - cbuf;
2151 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2152 if (!hlpfile->kwdata)
2154 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2155 return FALSE;
2157 memcpy(hlpfile->kwdata, cbuf, clen);
2159 return TRUE;
2162 /***********************************************************************
2164 * HLPFILE_GetMap
2166 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2168 BYTE *cbuf, *cend;
2169 unsigned entries, i;
2171 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2172 {WINE_WARN("no map section\n"); return FALSE;}
2174 entries = GET_USHORT(cbuf, 9);
2175 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2176 if (!hlpfile->Map) return FALSE;
2177 hlpfile->wMapLen = entries;
2178 for (i = 0; i < entries; i++)
2180 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2181 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2183 return TRUE;
2186 /***********************************************************************
2188 * HLPFILE_GetTOMap
2190 static BOOL HLPFILE_GetTOMap(HLPFILE *hlpfile)
2192 BYTE *cbuf, *cend;
2193 unsigned clen;
2195 if (!HLPFILE_FindSubFile(hlpfile, "|TOMAP", &cbuf, &cend))
2196 {WINE_WARN("no tomap section\n"); return FALSE;}
2198 clen = cend - cbuf - 9;
2199 hlpfile->TOMap = HeapAlloc(GetProcessHeap(), 0, clen);
2200 if (!hlpfile->TOMap) return FALSE;
2201 memcpy(hlpfile->TOMap, cbuf+9, clen);
2202 hlpfile->wTOMapLen = clen/4;
2203 return TRUE;
2206 /***********************************************************************
2208 * DeleteMacro
2210 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2212 HLPFILE_MACRO* next;
2214 while (macro)
2216 next = macro->next;
2217 HeapFree(GetProcessHeap(), 0, macro);
2218 macro = next;
2222 /***********************************************************************
2224 * DeletePage
2226 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2228 HLPFILE_PAGE* next;
2230 while (page)
2232 next = page->next;
2233 HLPFILE_DeleteMacro(page->first_macro);
2234 HeapFree(GetProcessHeap(), 0, page);
2235 page = next;
2239 /***********************************************************************
2241 * HLPFILE_FreeHlpFile
2243 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2245 unsigned i;
2247 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2249 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2250 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2251 else first_hlpfile = hlpfile->next;
2253 if (hlpfile->numFonts)
2255 for (i = 0; i < hlpfile->numFonts; i++)
2257 DeleteObject(hlpfile->fonts[i].hFont);
2259 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2262 if (hlpfile->numBmps)
2264 for (i = 0; i < hlpfile->numBmps; i++)
2266 DeleteObject(hlpfile->bmps[i]);
2268 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2271 HLPFILE_DeletePage(hlpfile->first_page);
2272 HLPFILE_DeleteMacro(hlpfile->first_macro);
2274 DestroyIcon(hlpfile->hIcon);
2275 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2276 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2277 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2278 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2279 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2280 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2281 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2282 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2283 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2284 HeapFree(GetProcessHeap(), 0, hlpfile->help_on_file);
2285 HeapFree(GetProcessHeap(), 0, hlpfile);
2288 /***********************************************************************
2290 * HLPFILE_UncompressLZ77_Phrases
2292 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
2294 UINT i, num, dec_size, head_size;
2295 BYTE *buf, *end;
2297 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
2299 if (hlpfile->version <= 16)
2300 head_size = 13;
2301 else
2302 head_size = 17;
2304 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
2305 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
2307 if (hlpfile->version <= 16)
2308 dec_size = end - buf - 15 - 2 * num;
2309 else
2310 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
2312 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2313 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2314 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2316 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2317 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2318 return FALSE;
2321 for (i = 0; i <= num; i++)
2322 hlpfile->phrases_offsets[i] = GET_USHORT(buf, head_size + 2 * i) - 2 * num - 2;
2324 if (hlpfile->version <= 16)
2325 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
2326 else
2327 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
2329 hlpfile->hasPhrases = TRUE;
2330 return TRUE;
2333 /***********************************************************************
2335 * HLPFILE_Uncompress_Phrases40
2337 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
2339 UINT num;
2340 INT dec_size, cpr_size;
2341 BYTE *buf_idx, *end_idx;
2342 BYTE *buf_phs, *end_phs;
2343 LONG* ptr, mask = 0;
2344 unsigned int i;
2345 unsigned short bc, n;
2347 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
2348 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
2350 ptr = (LONG*)(buf_idx + 9 + 28);
2351 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
2352 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
2354 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2355 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2356 GET_UINT(buf_idx, 9 + 0),
2357 GET_UINT(buf_idx, 9 + 4),
2358 GET_UINT(buf_idx, 9 + 8),
2359 GET_UINT(buf_idx, 9 + 12),
2360 GET_UINT(buf_idx, 9 + 16),
2361 GET_UINT(buf_idx, 9 + 20),
2362 GET_USHORT(buf_idx, 9 + 24),
2363 GET_USHORT(buf_idx, 9 + 26));
2365 dec_size = GET_UINT(buf_idx, 9 + 12);
2366 cpr_size = GET_UINT(buf_idx, 9 + 16);
2368 if (dec_size != cpr_size &&
2369 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
2371 WINE_WARN("size mismatch %u %u\n",
2372 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2373 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2376 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2377 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2378 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2380 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2381 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2382 return FALSE;
2385 #define getbit() (ptr += (mask < 0), mask = mask*2 + (mask<=0), (*ptr & mask) != 0)
2387 hlpfile->phrases_offsets[0] = 0;
2388 for (i = 0; i < num; i++)
2390 for (n = 1; getbit(); n += 1 << bc);
2391 if (getbit()) n++;
2392 if (bc > 1 && getbit()) n += 2;
2393 if (bc > 2 && getbit()) n += 4;
2394 if (bc > 3 && getbit()) n += 8;
2395 if (bc > 4 && getbit()) n += 16;
2396 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
2398 #undef getbit
2400 if (dec_size == cpr_size)
2401 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2402 else
2403 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2405 hlpfile->hasPhrases40 = TRUE;
2406 return TRUE;
2409 /***********************************************************************
2411 * HLPFILE_Uncompress_Topic
2413 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
2415 BYTE *buf, *ptr, *end, *newptr;
2416 unsigned int i, newsize = 0;
2417 unsigned int topic_size;
2419 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
2420 {WINE_WARN("topic0\n"); return FALSE;}
2422 buf += 9; /* Skip file header */
2423 topic_size = end - buf;
2424 if (hlpfile->compressed)
2426 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2428 for (i = 0; i < hlpfile->topic_maplen; i++)
2430 ptr = buf + i * hlpfile->tbsize;
2432 /* I don't know why, it's necessary for printman.hlp */
2433 if (ptr + 0x44 > end) ptr = end - 0x44;
2435 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
2438 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2439 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
2440 if (!hlpfile->topic_map) return FALSE;
2441 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2442 hlpfile->topic_end = newptr + newsize;
2444 for (i = 0; i < hlpfile->topic_maplen; i++)
2446 ptr = buf + i * hlpfile->tbsize;
2447 if (ptr + 0x44 > end) ptr = end - 0x44;
2449 hlpfile->topic_map[i] = newptr;
2450 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
2453 else
2455 /* basically, we need to copy the TopicBlockSize byte pages
2456 * (removing the first 0x0C) in one single area in memory
2458 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2459 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2460 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
2461 if (!hlpfile->topic_map) return FALSE;
2462 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2463 hlpfile->topic_end = newptr + topic_size;
2465 for (i = 0; i < hlpfile->topic_maplen; i++)
2467 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
2468 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
2471 return TRUE;
2474 /***********************************************************************
2476 * HLPFILE_AddPage
2478 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, const BYTE *buf, const BYTE *end, unsigned ref, unsigned offset)
2480 HLPFILE_PAGE* page;
2481 const BYTE* title;
2482 UINT titlesize, blocksize, datalen;
2483 char* ptr;
2484 HLPFILE_MACRO*macro;
2486 blocksize = GET_UINT(buf, 0);
2487 datalen = GET_UINT(buf, 0x10);
2488 title = buf + datalen;
2489 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
2491 titlesize = GET_UINT(buf, 4);
2492 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
2493 if (!page) return FALSE;
2494 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
2496 if (titlesize > blocksize - datalen)
2498 /* need to decompress */
2499 if (hlpfile->hasPhrases)
2500 HLPFILE_Uncompress2(hlpfile, title, end, (BYTE*)page->lpszTitle, (BYTE*)page->lpszTitle + titlesize);
2501 else if (hlpfile->hasPhrases40)
2502 HLPFILE_Uncompress3(hlpfile, page->lpszTitle, page->lpszTitle + titlesize, title, end);
2503 else
2505 WINE_FIXME("Text size is too long, splitting\n");
2506 titlesize = blocksize - datalen;
2507 memcpy(page->lpszTitle, title, titlesize);
2510 else
2511 memcpy(page->lpszTitle, title, titlesize);
2513 page->lpszTitle[titlesize] = '\0';
2515 if (hlpfile->first_page)
2517 hlpfile->last_page->next = page;
2518 page->prev = hlpfile->last_page;
2519 hlpfile->last_page = page;
2521 else
2523 hlpfile->first_page = page;
2524 hlpfile->last_page = page;
2525 page->prev = NULL;
2528 page->file = hlpfile;
2529 page->next = NULL;
2530 page->first_macro = NULL;
2531 page->first_link = NULL;
2532 page->wNumber = GET_UINT(buf, 0x21);
2533 page->offset = offset;
2534 page->reference = ref;
2536 page->browse_bwd = GET_UINT(buf, 0x19);
2537 page->browse_fwd = GET_UINT(buf, 0x1D);
2539 if (hlpfile->version <= 16)
2541 if (page->browse_bwd == 0xFFFF || page->browse_bwd == 0xFFFFFFFF)
2542 page->browse_bwd = 0xFFFFFFFF;
2543 else
2544 page->browse_bwd = hlpfile->TOMap[page->browse_bwd];
2546 if (page->browse_fwd == 0xFFFF || page->browse_fwd == 0xFFFFFFFF)
2547 page->browse_fwd = 0xFFFFFFFF;
2548 else
2549 page->browse_fwd = hlpfile->TOMap[page->browse_fwd];
2552 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
2553 page->wNumber, page->lpszTitle,
2554 page->browse_bwd, page->offset, page->browse_fwd);
2556 /* now load macros */
2557 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
2558 while (ptr < page->lpszTitle + titlesize)
2560 unsigned len = strlen(ptr);
2561 char* macro_str;
2563 WINE_TRACE("macro: %s\n", ptr);
2564 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
2565 macro->lpszMacro = macro_str = (char*)(macro + 1);
2566 memcpy(macro_str, ptr, len + 1);
2567 /* FIXME: shall we really link macro in reverse order ??
2568 * may produce strange results when played at page opening
2570 macro->next = page->first_macro;
2571 page->first_macro = macro;
2572 ptr += len + 1;
2575 return TRUE;
2578 /***********************************************************************
2580 * HLPFILE_SkipParagraph
2582 static BOOL HLPFILE_SkipParagraph(HLPFILE *hlpfile, const BYTE *buf, const BYTE *end, unsigned* len)
2584 const BYTE *tmp;
2586 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
2587 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
2589 tmp = buf + 0x15;
2590 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
2592 fetch_long(&tmp);
2593 *len = fetch_ushort(&tmp);
2595 else *len = end-buf-15;
2597 return TRUE;
2600 /***********************************************************************
2602 * HLPFILE_DoReadHlpFile
2604 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
2606 BOOL ret;
2607 HFILE hFile;
2608 OFSTRUCT ofs;
2609 BYTE* buf;
2610 DWORD ref = 0x0C;
2611 unsigned index, old_index, offset, len, offs, topicoffset;
2613 hFile = OpenFile(lpszPath, &ofs, OF_READ);
2614 if (hFile == HFILE_ERROR) return FALSE;
2616 ret = HLPFILE_ReadFileToBuffer(hlpfile, hFile);
2617 _lclose(hFile);
2618 if (!ret) return FALSE;
2620 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
2622 if (hlpfile->version <= 16 && !HLPFILE_GetTOMap(hlpfile)) return FALSE;
2624 /* load phrases support */
2625 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
2626 HLPFILE_Uncompress_Phrases40(hlpfile);
2628 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
2629 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
2631 buf = hlpfile->topic_map[0];
2632 old_index = -1;
2633 offs = 0;
2636 BYTE* end;
2638 if (hlpfile->version <= 16)
2640 index = (ref - 0x0C) / hlpfile->dsize;
2641 offset = (ref - 0x0C) % hlpfile->dsize;
2643 else
2645 index = (ref - 0x0C) >> 14;
2646 offset = (ref - 0x0C) & 0x3FFF;
2649 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
2651 /* we jumped to the next block, adjust pointers */
2652 ref -= 12;
2653 offset -= 12;
2656 WINE_TRACE("ref=%08x => [%u/%u]\n", ref, index, offset);
2658 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
2659 buf = hlpfile->topic_map[index] + offset;
2660 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
2661 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
2662 if (index != old_index) {offs = 0; old_index = index;}
2664 switch (buf[0x14])
2666 case 0x02:
2667 if (hlpfile->version <= 16)
2668 topicoffset = ref + index * 12;
2669 else
2670 topicoffset = index * 0x8000 + offs;
2671 if (!HLPFILE_AddPage(hlpfile, buf, end, ref, topicoffset)) return FALSE;
2672 break;
2674 case 0x01:
2675 case 0x20:
2676 case 0x23:
2677 if (!HLPFILE_SkipParagraph(hlpfile, buf, end, &len)) return FALSE;
2678 offs += len;
2679 break;
2681 default:
2682 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
2685 if (hlpfile->version <= 16)
2687 ref += GET_UINT(buf, 0xc);
2688 if (GET_UINT(buf, 0xc) == 0)
2689 break;
2691 else
2692 ref = GET_UINT(buf, 0xc);
2693 } while (ref != 0xffffffff);
2695 HLPFILE_GetKeywords(hlpfile);
2696 HLPFILE_GetMap(hlpfile);
2697 if (hlpfile->version <= 16) return TRUE;
2698 return HLPFILE_GetContext(hlpfile);
2701 /***********************************************************************
2703 * HLPFILE_ReadHlpFile
2705 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
2707 HLPFILE* hlpfile;
2709 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
2711 if (!strcmp(lpszPath, hlpfile->lpszPath))
2713 hlpfile->wRefCount++;
2714 return hlpfile;
2718 hlpfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2719 sizeof(HLPFILE) + lstrlen(lpszPath) + 1);
2720 if (!hlpfile) return 0;
2722 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
2723 hlpfile->contents_start = 0xFFFFFFFF;
2724 hlpfile->next = first_hlpfile;
2725 hlpfile->wRefCount = 1;
2727 strcpy(hlpfile->lpszPath, lpszPath);
2729 first_hlpfile = hlpfile;
2730 if (hlpfile->next) hlpfile->next->prev = hlpfile;
2732 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
2734 HLPFILE_FreeHlpFile(hlpfile);
2735 hlpfile = 0;
2738 return hlpfile;