[NTVDM]
[reactos.git] / base / applications / winhlp32 / hlpfile.c
1 /*
2 * Help Viewer
3 *
4 * Copyright 1996 Ulrich Schmid
5 * 2002, 2008 Eric Pouech
6 * 2007 Kirill K. Smirnov
7 *
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.
12 *
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.
17 *
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
21 */
22
23 //#include <stdarg.h>
24 #include <stdio.h>
25 //#include <string.h>
26 #include <windef.h>
27 //#include <winbase.h>
28 #include <wingdi.h>
29 #include <winuser.h>
30 #include <wine/debug.h>
31
32 #include "winhelp.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
35
36 static inline unsigned short GET_USHORT(const BYTE* buffer, unsigned i)
37 {
38 return (BYTE)buffer[i] + 0x100 * (BYTE)buffer[i + 1];
39 }
40
41 static inline short GET_SHORT(const BYTE* buffer, unsigned i)
42 {
43 return (BYTE)buffer[i] + 0x100 * (signed char)buffer[i+1];
44 }
45
46 static inline unsigned GET_UINT(const BYTE* buffer, unsigned i)
47 {
48 return GET_USHORT(buffer, i) + 0x10000 * GET_USHORT(buffer, i + 2);
49 }
50
51 static HLPFILE *first_hlpfile = 0;
52
53
54 /**************************************************************************
55 * HLPFILE_BPTreeSearch
56 *
57 * Searches for an element in B+ tree
58 *
59 * PARAMS
60 * buf [I] pointer to the embedded file structured as a B+ tree
61 * key [I] pointer to data to find
62 * comp [I] compare function
63 *
64 * RETURNS
65 * Pointer to block identified by key, or NULL if failure.
66 *
67 */
68 static void* HLPFILE_BPTreeSearch(BYTE* buf, const void* key,
69 HLPFILE_BPTreeCompare comp)
70 {
71 unsigned magic;
72 unsigned page_size;
73 unsigned cur_page;
74 unsigned level;
75 BYTE *pages, *ptr, *newptr;
76 int i, entries;
77 int ret;
78
79 magic = GET_USHORT(buf, 9);
80 if (magic != 0x293B)
81 {
82 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
83 return NULL;
84 }
85 page_size = GET_USHORT(buf, 9+4);
86 cur_page = GET_USHORT(buf, 9+26);
87 level = GET_USHORT(buf, 9+32);
88 pages = buf + 9 + 38;
89 while (--level > 0)
90 {
91 ptr = pages + cur_page*page_size;
92 entries = GET_SHORT(ptr, 2);
93 ptr += 6;
94 for (i = 0; i < entries; i++)
95 {
96 if (comp(ptr, key, 0, (void **)&newptr) > 0) break;
97 ptr = newptr;
98 }
99 cur_page = GET_USHORT(ptr-2, 0);
100 }
101 ptr = pages + cur_page*page_size;
102 entries = GET_SHORT(ptr, 2);
103 ptr += 8;
104 for (i = 0; i < entries; i++)
105 {
106 ret = comp(ptr, key, 1, (void **)&newptr);
107 if (ret == 0) return ptr;
108 if (ret > 0) return NULL;
109 ptr = newptr;
110 }
111 return NULL;
112 }
113
114 /**************************************************************************
115 * HLPFILE_BPTreeEnum
116 *
117 * Enumerates elements in B+ tree.
118 *
119 * PARAMS
120 * buf [I] pointer to the embedded file structured as a B+ tree
121 * cb [I] compare function
122 * cookie [IO] cookie for cb function
123 */
124 void HLPFILE_BPTreeEnum(BYTE* buf, HLPFILE_BPTreeCallback cb, void* cookie)
125 {
126 unsigned magic;
127 unsigned page_size;
128 unsigned cur_page;
129 unsigned level;
130 BYTE *pages, *ptr, *newptr;
131 int i, entries;
132
133 magic = GET_USHORT(buf, 9);
134 if (magic != 0x293B)
135 {
136 WINE_ERR("Invalid magic in B+ tree: 0x%x\n", magic);
137 return;
138 }
139 page_size = GET_USHORT(buf, 9+4);
140 cur_page = GET_USHORT(buf, 9+26);
141 level = GET_USHORT(buf, 9+32);
142 pages = buf + 9 + 38;
143 while (--level > 0)
144 {
145 ptr = pages + cur_page*page_size;
146 cur_page = GET_USHORT(ptr, 4);
147 }
148 while (cur_page != 0xFFFF)
149 {
150 ptr = pages + cur_page*page_size;
151 entries = GET_SHORT(ptr, 2);
152 ptr += 8;
153 for (i = 0; i < entries; i++)
154 {
155 cb(ptr, (void **)&newptr, cookie);
156 ptr = newptr;
157 }
158 cur_page = GET_USHORT(pages+cur_page*page_size, 6);
159 }
160 }
161
162
163 /***********************************************************************
164 *
165 * HLPFILE_UncompressedLZ77_Size
166 */
167 static INT HLPFILE_UncompressedLZ77_Size(const BYTE *ptr, const BYTE *end)
168 {
169 int i, newsize = 0;
170
171 while (ptr < end)
172 {
173 int mask = *ptr++;
174 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
175 {
176 if (mask & 1)
177 {
178 int code = GET_USHORT(ptr, 0);
179 int len = 3 + (code >> 12);
180 newsize += len;
181 ptr += 2;
182 }
183 else newsize++, ptr++;
184 }
185 }
186
187 return newsize;
188 }
189
190 /***********************************************************************
191 *
192 * HLPFILE_UncompressLZ77
193 */
194 static BYTE *HLPFILE_UncompressLZ77(const BYTE *ptr, const BYTE *end, BYTE *newptr)
195 {
196 int i;
197
198 while (ptr < end)
199 {
200 int mask = *ptr++;
201 for (i = 0; i < 8 && ptr < end; i++, mask >>= 1)
202 {
203 if (mask & 1)
204 {
205 int code = GET_USHORT(ptr, 0);
206 int len = 3 + (code >> 12);
207 int offset = code & 0xfff;
208 /*
209 * We must copy byte-by-byte here. We cannot use memcpy nor
210 * memmove here. Just example:
211 * a[]={1,2,3,4,5,6,7,8,9,10}
212 * newptr=a+2;
213 * offset=1;
214 * We expect:
215 * {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 11, 12}
216 */
217 for (; len>0; len--, newptr++) *newptr = *(newptr-offset-1);
218 ptr += 2;
219 }
220 else *newptr++ = *ptr++;
221 }
222 }
223
224 return newptr;
225 }
226
227 /***********************************************************************
228 *
229 * HLPFILE_Uncompress2
230 */
231
232 static void HLPFILE_Uncompress2(HLPFILE* hlpfile, const BYTE *ptr, const BYTE *end, BYTE *newptr, const BYTE *newend)
233 {
234 BYTE *phptr, *phend;
235 UINT code;
236 UINT index;
237
238 while (ptr < end && newptr < newend)
239 {
240 if (!*ptr || *ptr >= 0x10)
241 *newptr++ = *ptr++;
242 else
243 {
244 code = 0x100 * ptr[0] + ptr[1];
245 index = (code - 0x100) / 2;
246
247 phptr = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index];
248 phend = (BYTE*)hlpfile->phrases_buffer + hlpfile->phrases_offsets[index + 1];
249
250 if (newptr + (phend - phptr) > newend)
251 {
252 WINE_FIXME("buffer overflow %p > %p for %lu bytes\n",
253 newptr, newend, (SIZE_T)(phend - phptr));
254 return;
255 }
256 memcpy(newptr, phptr, phend - phptr);
257 newptr += phend - phptr;
258 if (code & 1) *newptr++ = ' ';
259
260 ptr += 2;
261 }
262 }
263 if (newptr > newend) WINE_FIXME("buffer overflow %p > %p\n", newptr, newend);
264 }
265
266 /******************************************************************
267 * HLPFILE_Uncompress3
268 *
269 *
270 */
271 static BOOL HLPFILE_Uncompress3(HLPFILE* hlpfile, char* dst, const char* dst_end,
272 const BYTE* src, const BYTE* src_end)
273 {
274 unsigned int idx, len;
275
276 for (; src < src_end; src++)
277 {
278 if ((*src & 1) == 0)
279 {
280 idx = *src / 2;
281 if (idx > hlpfile->num_phrases)
282 {
283 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
284 len = 0;
285 }
286 else
287 {
288 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
289 if (dst + len <= dst_end)
290 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
291 }
292 }
293 else if ((*src & 0x03) == 0x01)
294 {
295 idx = (*src + 1) * 64;
296 idx += *++src;
297 if (idx > hlpfile->num_phrases)
298 {
299 WINE_ERR("index in phrases %d/%d\n", idx, hlpfile->num_phrases);
300 len = 0;
301 }
302 else
303 {
304 len = hlpfile->phrases_offsets[idx + 1] - hlpfile->phrases_offsets[idx];
305 if (dst + len <= dst_end)
306 memcpy(dst, &hlpfile->phrases_buffer[hlpfile->phrases_offsets[idx]], len);
307 }
308 }
309 else if ((*src & 0x07) == 0x03)
310 {
311 len = (*src / 8) + 1;
312 if (dst + len <= dst_end)
313 memcpy(dst, src + 1, len);
314 src += len;
315 }
316 else
317 {
318 len = (*src / 16) + 1;
319 if (dst + len <= dst_end)
320 memset(dst, ((*src & 0x0F) == 0x07) ? ' ' : 0, len);
321 }
322 dst += len;
323 }
324
325 if (dst > dst_end) WINE_ERR("buffer overflow (%p > %p)\n", dst, dst_end);
326 return TRUE;
327 }
328
329 /******************************************************************
330 * HLPFILE_UncompressRLE
331 *
332 *
333 */
334 static void HLPFILE_UncompressRLE(const BYTE* src, const BYTE* end, BYTE* dst, unsigned dstsz)
335 {
336 BYTE ch;
337 BYTE* sdst = dst + dstsz;
338
339 while (src < end)
340 {
341 ch = *src++;
342 if (ch & 0x80)
343 {
344 ch &= 0x7F;
345 if (dst + ch <= sdst)
346 memcpy(dst, src, ch);
347 src += ch;
348 }
349 else
350 {
351 if (dst + ch <= sdst)
352 memset(dst, (char)*src, ch);
353 src++;
354 }
355 dst += ch;
356 }
357 if (dst != sdst)
358 WINE_WARN("Buffer X-flow: d(%lu) instead of d(%u)\n",
359 (SIZE_T)(dst - (sdst - dstsz)), dstsz);
360 }
361
362
363 /******************************************************************
364 * HLPFILE_PageByOffset
365 *
366 *
367 */
368 HLPFILE_PAGE *HLPFILE_PageByOffset(HLPFILE* hlpfile, LONG offset, ULONG* relative)
369 {
370 HLPFILE_PAGE* page;
371 HLPFILE_PAGE* found;
372
373 if (!hlpfile) return 0;
374
375 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, offset);
376
377 if (offset == 0xFFFFFFFF) return NULL;
378 page = NULL;
379
380 for (found = NULL, page = hlpfile->first_page; page; page = page->next)
381 {
382 if (page->offset <= offset && (!found || found->offset < page->offset))
383 {
384 *relative = offset - page->offset;
385 found = page;
386 }
387 }
388 if (!found)
389 WINE_ERR("Page of offset %u not found in file %s\n",
390 offset, hlpfile->lpszPath);
391 return found;
392 }
393
394 /***********************************************************************
395 *
396 * HLPFILE_Contents
397 */
398 static HLPFILE_PAGE* HLPFILE_Contents(HLPFILE *hlpfile, ULONG* relative)
399 {
400 HLPFILE_PAGE* page = NULL;
401
402 if (!hlpfile) return NULL;
403
404 page = HLPFILE_PageByOffset(hlpfile, hlpfile->contents_start, relative);
405 if (!page)
406 {
407 page = hlpfile->first_page;
408 *relative = 0;
409 }
410 return page;
411 }
412
413 /**************************************************************************
414 * comp_PageByHash
415 *
416 * HLPFILE_BPTreeCompare function for '|CONTEXT' B+ tree file
417 *
418 */
419 static int comp_PageByHash(void *p, const void *key,
420 int leaf, void** next)
421 {
422 LONG lKey = (LONG_PTR)key;
423 LONG lTest = (INT)GET_UINT(p, 0);
424
425 *next = (char *)p+(leaf?8:6);
426 WINE_TRACE("Comparing '%d' with '%d'\n", lKey, lTest);
427 if (lTest < lKey) return -1;
428 if (lTest > lKey) return 1;
429 return 0;
430 }
431
432 /***********************************************************************
433 *
434 * HLPFILE_PageByHash
435 */
436 HLPFILE_PAGE *HLPFILE_PageByHash(HLPFILE* hlpfile, LONG lHash, ULONG* relative)
437 {
438 BYTE *ptr;
439
440 if (!hlpfile) return NULL;
441 if (!lHash) return HLPFILE_Contents(hlpfile, relative);
442
443 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lHash);
444
445 /* For win 3.0 files hash values are really page numbers */
446 if (hlpfile->version <= 16)
447 {
448 if (lHash >= hlpfile->wTOMapLen) return NULL;
449 return HLPFILE_PageByOffset(hlpfile, hlpfile->TOMap[lHash], relative);
450 }
451
452 ptr = HLPFILE_BPTreeSearch(hlpfile->Context, LongToPtr(lHash), comp_PageByHash);
453 if (!ptr)
454 {
455 WINE_ERR("Page of hash %x not found in file %s\n", lHash, hlpfile->lpszPath);
456 return NULL;
457 }
458
459 return HLPFILE_PageByOffset(hlpfile, GET_UINT(ptr, 4), relative);
460 }
461
462 /***********************************************************************
463 *
464 * HLPFILE_PageByMap
465 */
466 HLPFILE_PAGE *HLPFILE_PageByMap(HLPFILE* hlpfile, LONG lMap, ULONG* relative)
467 {
468 unsigned int i;
469
470 if (!hlpfile) return 0;
471
472 WINE_TRACE("<%s>[%x]\n", hlpfile->lpszPath, lMap);
473
474 for (i = 0; i < hlpfile->wMapLen; i++)
475 {
476 if (hlpfile->Map[i].lMap == lMap)
477 return HLPFILE_PageByOffset(hlpfile, hlpfile->Map[i].offset, relative);
478 }
479
480 WINE_ERR("Page of Map %x not found in file %s\n", lMap, hlpfile->lpszPath);
481 return NULL;
482 }
483
484 /**************************************************************************
485 * comp_FindSubFile
486 *
487 * HLPFILE_BPTreeCompare function for HLPFILE directory.
488 *
489 */
490 static int comp_FindSubFile(void *p, const void *key,
491 int leaf, void** next)
492 {
493 *next = (char *)p+strlen(p)+(leaf?5:3);
494 WINE_TRACE("Comparing '%s' with '%s'\n", (char *)p, (const char *)key);
495 return strcmp(p, key);
496 }
497
498 /***********************************************************************
499 *
500 * HLPFILE_FindSubFile
501 */
502 static BOOL HLPFILE_FindSubFile(HLPFILE* hlpfile, LPCSTR name, BYTE **subbuf, BYTE **subend)
503 {
504 BYTE *ptr;
505
506 WINE_TRACE("looking for file '%s'\n", name);
507 ptr = HLPFILE_BPTreeSearch(hlpfile->file_buffer + GET_UINT(hlpfile->file_buffer, 4),
508 name, comp_FindSubFile);
509 if (!ptr) return FALSE;
510 *subbuf = hlpfile->file_buffer + GET_UINT(ptr, strlen(name)+1);
511 if (*subbuf >= hlpfile->file_buffer + hlpfile->file_buffer_size)
512 {
513 WINE_ERR("internal file %s does not fit\n", name);
514 return FALSE;
515 }
516 *subend = *subbuf + GET_UINT(*subbuf, 0);
517 if (*subend > hlpfile->file_buffer + hlpfile->file_buffer_size)
518 {
519 WINE_ERR("internal file %s does not fit\n", name);
520 return FALSE;
521 }
522 if (GET_UINT(*subbuf, 0) < GET_UINT(*subbuf, 4) + 9)
523 {
524 WINE_ERR("invalid size provided for internal file %s\n", name);
525 return FALSE;
526 }
527 return TRUE;
528 }
529
530 /***********************************************************************
531 *
532 * HLPFILE_Hash
533 */
534 LONG HLPFILE_Hash(LPCSTR lpszContext)
535 {
536 LONG lHash = 0;
537 CHAR c;
538
539 while ((c = *lpszContext++))
540 {
541 CHAR x = 0;
542 if (c >= 'A' && c <= 'Z') x = c - 'A' + 17;
543 if (c >= 'a' && c <= 'z') x = c - 'a' + 17;
544 if (c >= '1' && c <= '9') x = c - '0';
545 if (c == '0') x = 10;
546 if (c == '.') x = 12;
547 if (c == '_') x = 13;
548 if (x) lHash = lHash * 43 + x;
549 }
550 return lHash;
551 }
552
553 static LONG fetch_long(const BYTE** ptr)
554 {
555 LONG ret;
556
557 if (*(*ptr) & 1)
558 {
559 ret = (*(const ULONG*)(*ptr) - 0x80000000) / 2;
560 (*ptr) += 4;
561 }
562 else
563 {
564 ret = (*(const USHORT*)(*ptr) - 0x8000) / 2;
565 (*ptr) += 2;
566 }
567
568 return ret;
569 }
570
571 static ULONG fetch_ulong(const BYTE** ptr)
572 {
573 ULONG ret;
574
575 if (*(*ptr) & 1)
576 {
577 ret = *(const ULONG*)(*ptr) / 2;
578 (*ptr) += 4;
579 }
580 else
581 {
582 ret = *(const USHORT*)(*ptr) / 2;
583 (*ptr) += 2;
584 }
585 return ret;
586 }
587
588 static short fetch_short(const BYTE** ptr)
589 {
590 short ret;
591
592 if (*(*ptr) & 1)
593 {
594 ret = (*(const unsigned short*)(*ptr) - 0x8000) / 2;
595 (*ptr) += 2;
596 }
597 else
598 {
599 ret = (*(const unsigned char*)(*ptr) - 0x80) / 2;
600 (*ptr)++;
601 }
602 return ret;
603 }
604
605 static unsigned short fetch_ushort(const BYTE** ptr)
606 {
607 unsigned short ret;
608
609 if (*(*ptr) & 1)
610 {
611 ret = *(const unsigned short*)(*ptr) / 2;
612 (*ptr) += 2;
613 }
614 else
615 {
616 ret = *(const unsigned char*)(*ptr) / 2;
617 (*ptr)++;
618 }
619 return ret;
620 }
621
622 /******************************************************************
623 * HLPFILE_DecompressGfx
624 *
625 * Decompress the data part of a bitmap or a metafile
626 */
627 static const BYTE* HLPFILE_DecompressGfx(const BYTE* src, unsigned csz, unsigned sz, BYTE packing,
628 BYTE** alloc)
629 {
630 const BYTE* dst;
631 BYTE* tmp;
632 unsigned sz77;
633
634 WINE_TRACE("Unpacking (%d) from %u bytes to %u bytes\n", packing, csz, sz);
635
636 switch (packing)
637 {
638 case 0: /* uncompressed */
639 if (sz != csz)
640 WINE_WARN("Bogus gfx sizes (uncompressed): %u / %u\n", sz, csz);
641 dst = src;
642 *alloc = NULL;
643 break;
644 case 1: /* RunLen */
645 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz);
646 if (!dst) return NULL;
647 HLPFILE_UncompressRLE(src, src + csz, *alloc, sz);
648 break;
649 case 2: /* LZ77 */
650 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
651 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz77);
652 if (!dst) return NULL;
653 HLPFILE_UncompressLZ77(src, src + csz, *alloc);
654 if (sz77 != sz)
655 WINE_WARN("Bogus gfx sizes (LZ77): %u / %u\n", sz77, sz);
656 break;
657 case 3: /* LZ77 then RLE */
658 sz77 = HLPFILE_UncompressedLZ77_Size(src, src + csz);
659 tmp = HeapAlloc(GetProcessHeap(), 0, sz77);
660 if (!tmp) return FALSE;
661 HLPFILE_UncompressLZ77(src, src + csz, tmp);
662 dst = *alloc = HeapAlloc(GetProcessHeap(), 0, sz);
663 if (!dst)
664 {
665 HeapFree(GetProcessHeap(), 0, tmp);
666 return FALSE;
667 }
668 HLPFILE_UncompressRLE(tmp, tmp + sz77, *alloc, sz);
669 HeapFree(GetProcessHeap(), 0, tmp);
670 break;
671 default:
672 WINE_FIXME("Unsupported packing %u\n", packing);
673 return NULL;
674 }
675 return dst;
676 }
677
678 static BOOL HLPFILE_RtfAddRawString(struct RtfData* rd, const char* str, size_t sz)
679 {
680 if (rd->ptr + sz >= rd->data + rd->allocated)
681 {
682 char* new = HeapReAlloc(GetProcessHeap(), 0, rd->data, rd->allocated *= 2);
683 if (!new) return FALSE;
684 rd->ptr = new + (rd->ptr - rd->data);
685 rd->data = new;
686 }
687 memcpy(rd->ptr, str, sz);
688 rd->ptr += sz;
689
690 return TRUE;
691 }
692
693 static BOOL HLPFILE_RtfAddControl(struct RtfData* rd, const char* str)
694 {
695 if (*str == '\\' || *str == '{') rd->in_text = FALSE;
696 else if (*str == '}') rd->in_text = TRUE;
697 return HLPFILE_RtfAddRawString(rd, str, strlen(str));
698 }
699
700 static BOOL HLPFILE_RtfAddText(struct RtfData* rd, const char* str)
701 {
702 const char* p;
703 const char* last;
704 const char* replace;
705 unsigned rlen;
706
707 if (!rd->in_text)
708 {
709 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
710 rd->in_text = TRUE;
711 }
712 for (last = p = str; *p; p++)
713 {
714 if (*p & 0x80) /* escape non-ASCII chars */
715 {
716 static char xx[8];
717 rlen = sprintf(xx, "\\'%x", *(const BYTE*)p);
718 replace = xx;
719 }
720 else switch (*p)
721 {
722 case '{': rlen = 2; replace = "\\{"; break;
723 case '}': rlen = 2; replace = "\\}"; break;
724 case '\\': rlen = 2; replace = "\\\\"; break;
725 default: continue;
726 }
727 if ((p != last && !HLPFILE_RtfAddRawString(rd, last, p - last)) ||
728 !HLPFILE_RtfAddRawString(rd, replace, rlen)) return FALSE;
729 last = p + 1;
730 }
731 return HLPFILE_RtfAddRawString(rd, last, p - last);
732 }
733
734 /******************************************************************
735 * RtfAddHexBytes
736 *
737 */
738 static BOOL HLPFILE_RtfAddHexBytes(struct RtfData* rd, const void* _ptr, unsigned sz)
739 {
740 char tmp[512];
741 unsigned i, step;
742 const BYTE* ptr = _ptr;
743 static const char* _2hex = "0123456789abcdef";
744
745 if (!rd->in_text)
746 {
747 if (!HLPFILE_RtfAddRawString(rd, " ", 1)) return FALSE;
748 rd->in_text = TRUE;
749 }
750 for (; sz; sz -= step)
751 {
752 step = min(256, sz);
753 for (i = 0; i < step; i++)
754 {
755 tmp[2 * i + 0] = _2hex[*ptr >> 4];
756 tmp[2 * i + 1] = _2hex[*ptr++ & 0xF];
757 }
758 if (!HLPFILE_RtfAddRawString(rd, tmp, 2 * step)) return FALSE;
759 }
760 return TRUE;
761 }
762
763 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
764 const char* str, unsigned len, LONG hash,
765 unsigned clrChange, unsigned bHotSpot, unsigned wnd);
766
767 /******************************************************************
768 * HLPFILE_AddHotSpotLinks
769 *
770 */
771 static void HLPFILE_AddHotSpotLinks(struct RtfData* rd, HLPFILE* file,
772 const BYTE* start, ULONG hs_size, ULONG hs_offset)
773 {
774 unsigned i, hs_num;
775 ULONG hs_macro;
776 const char* str;
777
778 if (hs_size == 0 || hs_offset == 0) return;
779
780 start += hs_offset;
781 /* always 1 ?? */
782 hs_num = GET_USHORT(start, 1);
783 hs_macro = GET_UINT(start, 3);
784
785 str = (const char*)start + 7 + 15 * hs_num + hs_macro;
786 /* FIXME: should use hs_size to prevent out of bounds reads */
787 for (i = 0; i < hs_num; i++)
788 {
789 HLPFILE_HOTSPOTLINK* hslink;
790
791 WINE_TRACE("%02x-%02x%02x {%s,%s}\n",
792 start[7 + 15 * i + 0], start[7 + 15 * i + 1], start[7 + 15 * i + 2],
793 str, str + strlen(str) + 1);
794 /* str points to two null terminated strings:
795 * hotspot name, then link name
796 */
797 str += strlen(str) + 1; /* skip hotspot name */
798
799 hslink = NULL;
800 switch (start[7 + 15 * i + 0])
801 /* The next two chars always look like 0x04 0x00 ???
802 * What are they for ?
803 */
804 {
805 case 0xC8:
806 hslink = (HLPFILE_HOTSPOTLINK*)
807 HLPFILE_AllocLink(rd, hlp_link_macro, str, -1, 0, 0, 1, -1);
808 break;
809
810 case 0xE6:
811 case 0xE7:
812 hslink = (HLPFILE_HOTSPOTLINK*)
813 HLPFILE_AllocLink(rd, (start[7 + 15 * i + 0] & 1) ? hlp_link_link : hlp_link_popup,
814 file->lpszPath, -1, HLPFILE_Hash(str),
815 0, 1, -1);
816 break;
817
818 case 0xEE:
819 case 0xEF:
820 {
821 const char* win = strchr(str, '>');
822 int wnd = -1;
823 char* tgt = NULL;
824
825 if (win)
826 {
827 for (wnd = file->numWindows - 1; wnd >= 0; wnd--)
828 {
829 if (!strcmp(win + 1, file->windows[wnd].name)) break;
830 }
831 if (wnd == -1)
832 WINE_WARN("Couldn't find window info for %s\n", win);
833 if ((tgt = HeapAlloc(GetProcessHeap(), 0, win - str + 1)))
834 {
835 memcpy(tgt, str, win - str);
836 tgt[win - str] = '\0';
837 }
838 }
839 hslink = (HLPFILE_HOTSPOTLINK*)
840 HLPFILE_AllocLink(rd, (start[7 + 15 * i + 0] & 1) ? hlp_link_link : hlp_link_popup,
841 file->lpszPath, -1, HLPFILE_Hash(tgt ? tgt : str), 0, 1, wnd);
842 HeapFree(GetProcessHeap(), 0, tgt);
843 break;
844 }
845 default:
846 WINE_FIXME("unknown hotsport target 0x%x\n", start[7 + 15 * i + 0]);
847 }
848 if (hslink)
849 {
850 hslink->x = GET_USHORT(start, 7 + 15 * i + 3);
851 hslink->y = GET_USHORT(start, 7 + 15 * i + 5);
852 hslink->width = GET_USHORT(start, 7 + 15 * i + 7);
853 hslink->height = GET_USHORT(start, 7 + 15 * i + 9);
854 /* target = GET_UINT(start, 7 + 15 * i + 11); */
855 }
856 str += strlen(str) + 1;
857 }
858 }
859
860 /******************************************************************
861 * HLPFILE_RtfAddTransparentBitmap
862 *
863 * We'll transform a transparent bitmap into an metafile that
864 * we then transform into RTF
865 */
866 static BOOL HLPFILE_RtfAddTransparentBitmap(struct RtfData* rd, const BITMAPINFO* bi,
867 const void* pict, unsigned nc)
868 {
869 HDC hdc, hdcMask, hdcMem, hdcEMF;
870 HBITMAP hbm, hbmMask, hbmOldMask, hbmOldMem;
871 HENHMETAFILE hEMF;
872 BOOL ret = FALSE;
873 void* data;
874 UINT sz;
875
876 hbm = CreateDIBitmap(hdc = GetDC(0), &bi->bmiHeader,
877 CBM_INIT, pict, bi, DIB_RGB_COLORS);
878
879 hdcMem = CreateCompatibleDC(hdc);
880 hbmOldMem = SelectObject(hdcMem, hbm);
881
882 /* create the mask bitmap from the main bitmap */
883 hdcMask = CreateCompatibleDC(hdc);
884 hbmMask = CreateBitmap(bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, 1, 1, NULL);
885 hbmOldMask = SelectObject(hdcMask, hbmMask);
886 SetBkColor(hdcMem,
887 RGB(bi->bmiColors[nc - 1].rgbRed,
888 bi->bmiColors[nc - 1].rgbGreen,
889 bi->bmiColors[nc - 1].rgbBlue));
890 BitBlt(hdcMask, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCCOPY);
891
892 /* sets to RGB(0,0,0) the transparent bits in main bitmap */
893 SetBkColor(hdcMem, RGB(0,0,0));
894 SetTextColor(hdcMem, RGB(255,255,255));
895 BitBlt(hdcMem, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMask, 0, 0, SRCAND);
896
897 SelectObject(hdcMask, hbmOldMask);
898 DeleteDC(hdcMask);
899
900 SelectObject(hdcMem, hbmOldMem);
901 DeleteDC(hdcMem);
902
903 /* we create the bitmap on the fly */
904 hdcEMF = CreateEnhMetaFileW(NULL, NULL, NULL, NULL);
905 hdcMem = CreateCompatibleDC(hdcEMF);
906
907 /* sets to RGB(0,0,0) the transparent bits in final bitmap */
908 hbmOldMem = SelectObject(hdcMem, hbmMask);
909 SetBkColor(hdcEMF, RGB(255, 255, 255));
910 SetTextColor(hdcEMF, RGB(0, 0, 0));
911 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCAND);
912
913 /* and copy the remaining bits of main bitmap */
914 SelectObject(hdcMem, hbm);
915 BitBlt(hdcEMF, 0, 0, bi->bmiHeader.biWidth, bi->bmiHeader.biHeight, hdcMem, 0, 0, SRCPAINT);
916 SelectObject(hdcMem, hbmOldMem);
917 DeleteDC(hdcMem);
918
919 /* do the cleanup */
920 ReleaseDC(0, hdc);
921 DeleteObject(hbmMask);
922 DeleteObject(hbm);
923
924 hEMF = CloseEnhMetaFile(hdcEMF);
925
926 /* generate rtf stream */
927 sz = GetEnhMetaFileBits(hEMF, 0, NULL);
928 if (sz && (data = HeapAlloc(GetProcessHeap(), 0, sz)))
929 {
930 if (sz == GetEnhMetaFileBits(hEMF, sz, data))
931 {
932 ret = HLPFILE_RtfAddControl(rd, "{\\pict\\emfblip") &&
933 HLPFILE_RtfAddHexBytes(rd, data, sz) &&
934 HLPFILE_RtfAddControl(rd, "}");
935 }
936 HeapFree(GetProcessHeap(), 0, data);
937 }
938 DeleteEnhMetaFile(hEMF);
939
940 return ret;
941 }
942
943 /******************************************************************
944 * HLPFILE_RtfAddBitmap
945 *
946 */
947 static BOOL HLPFILE_RtfAddBitmap(struct RtfData* rd, HLPFILE* file, const BYTE* beg, BYTE type, BYTE pack)
948 {
949 const BYTE* ptr;
950 const BYTE* pict_beg;
951 BYTE* alloc = NULL;
952 BITMAPINFO* bi;
953 ULONG off, csz;
954 unsigned nc = 0;
955 BOOL clrImportant = FALSE;
956 BOOL ret = FALSE;
957 char tmp[256];
958 unsigned hs_size, hs_offset;
959
960 bi = HeapAlloc(GetProcessHeap(), 0, sizeof(*bi));
961 if (!bi) return FALSE;
962
963 ptr = beg + 2; /* for type and pack */
964
965 bi->bmiHeader.biSize = sizeof(bi->bmiHeader);
966 bi->bmiHeader.biXPelsPerMeter = fetch_ulong(&ptr);
967 bi->bmiHeader.biYPelsPerMeter = fetch_ulong(&ptr);
968 bi->bmiHeader.biPlanes = fetch_ushort(&ptr);
969 bi->bmiHeader.biBitCount = fetch_ushort(&ptr);
970 bi->bmiHeader.biWidth = fetch_ulong(&ptr);
971 bi->bmiHeader.biHeight = fetch_ulong(&ptr);
972 bi->bmiHeader.biClrUsed = fetch_ulong(&ptr);
973 clrImportant = fetch_ulong(&ptr);
974 bi->bmiHeader.biClrImportant = (clrImportant > 1) ? clrImportant : 0;
975 bi->bmiHeader.biCompression = BI_RGB;
976 if (bi->bmiHeader.biBitCount > 32) WINE_FIXME("Unknown bit count %u\n", bi->bmiHeader.biBitCount);
977 if (bi->bmiHeader.biPlanes != 1) WINE_FIXME("Unsupported planes %u\n", bi->bmiHeader.biPlanes);
978 bi->bmiHeader.biSizeImage = (((bi->bmiHeader.biWidth * bi->bmiHeader.biBitCount + 31) & ~31) / 8) * bi->bmiHeader.biHeight;
979 WINE_TRACE("planes=%d bc=%d size=(%d,%d)\n",
980 bi->bmiHeader.biPlanes, bi->bmiHeader.biBitCount,
981 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
982
983 csz = fetch_ulong(&ptr);
984 hs_size = fetch_ulong(&ptr);
985
986 off = GET_UINT(ptr, 0); ptr += 4;
987 hs_offset = GET_UINT(ptr, 0); ptr += 4;
988 HLPFILE_AddHotSpotLinks(rd, file, beg, hs_size, hs_offset);
989
990 /* now read palette info */
991 if (type == 0x06)
992 {
993 unsigned i;
994
995 nc = bi->bmiHeader.biClrUsed;
996 /* not quite right, especially for bitfields type of compression */
997 if (!nc && bi->bmiHeader.biBitCount <= 8)
998 nc = 1 << bi->bmiHeader.biBitCount;
999
1000 bi = HeapReAlloc(GetProcessHeap(), 0, bi, sizeof(*bi) + nc * sizeof(RGBQUAD));
1001 if (!bi) return FALSE;
1002 for (i = 0; i < nc; i++)
1003 {
1004 bi->bmiColors[i].rgbBlue = ptr[0];
1005 bi->bmiColors[i].rgbGreen = ptr[1];
1006 bi->bmiColors[i].rgbRed = ptr[2];
1007 bi->bmiColors[i].rgbReserved = 0;
1008 ptr += 4;
1009 }
1010 }
1011 pict_beg = HLPFILE_DecompressGfx(beg + off, csz, bi->bmiHeader.biSizeImage, pack, &alloc);
1012
1013 if (clrImportant == 1 && nc > 0)
1014 {
1015 ret = HLPFILE_RtfAddTransparentBitmap(rd, bi, pict_beg, nc);
1016 goto done;
1017 }
1018 if (!HLPFILE_RtfAddControl(rd, "{\\pict")) goto done;
1019 if (type == 0x06)
1020 {
1021 sprintf(tmp, "\\dibitmap0\\picw%d\\pich%d",
1022 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
1023 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1024 if (!HLPFILE_RtfAddHexBytes(rd, bi, sizeof(*bi) + nc * sizeof(RGBQUAD))) goto done;
1025 }
1026 else
1027 {
1028 sprintf(tmp, "\\wbitmap0\\wbmbitspixel%d\\wbmplanes%d\\picw%d\\pich%d",
1029 bi->bmiHeader.biBitCount, bi->bmiHeader.biPlanes,
1030 bi->bmiHeader.biWidth, bi->bmiHeader.biHeight);
1031 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1032 }
1033 if (!HLPFILE_RtfAddHexBytes(rd, pict_beg, bi->bmiHeader.biSizeImage)) goto done;
1034 if (!HLPFILE_RtfAddControl(rd, "}")) goto done;
1035
1036 ret = TRUE;
1037 done:
1038 HeapFree(GetProcessHeap(), 0, bi);
1039 HeapFree(GetProcessHeap(), 0, alloc);
1040
1041 return ret;
1042 }
1043
1044 /******************************************************************
1045 * HLPFILE_RtfAddMetaFile
1046 *
1047 */
1048 static BOOL HLPFILE_RtfAddMetaFile(struct RtfData* rd, HLPFILE* file, const BYTE* beg, BYTE pack)
1049 {
1050 ULONG size, csize, off, hs_offset, hs_size;
1051 const BYTE* ptr;
1052 const BYTE* bits;
1053 BYTE* alloc = NULL;
1054 char tmp[256];
1055 unsigned mm;
1056 BOOL ret;
1057
1058 WINE_TRACE("Loading metafile\n");
1059
1060 ptr = beg + 2; /* for type and pack */
1061
1062 mm = fetch_ushort(&ptr); /* mapping mode */
1063 sprintf(tmp, "{\\pict\\wmetafile%d\\picw%d\\pich%d",
1064 mm, GET_USHORT(ptr, 0), GET_USHORT(ptr, 2));
1065 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1066 ptr += 4;
1067
1068 size = fetch_ulong(&ptr); /* decompressed size */
1069 csize = fetch_ulong(&ptr); /* compressed size */
1070 hs_size = fetch_ulong(&ptr); /* hotspot size */
1071 off = GET_UINT(ptr, 0);
1072 hs_offset = GET_UINT(ptr, 4);
1073 ptr += 8;
1074
1075 HLPFILE_AddHotSpotLinks(rd, file, beg, hs_size, hs_offset);
1076
1077 WINE_TRACE("sz=%u csz=%u offs=%u/%u,%u/%u\n",
1078 size, csize, off, (ULONG)(ptr - beg), hs_size, hs_offset);
1079
1080 bits = HLPFILE_DecompressGfx(beg + off, csize, size, pack, &alloc);
1081 if (!bits) return FALSE;
1082
1083 ret = HLPFILE_RtfAddHexBytes(rd, bits, size) &&
1084 HLPFILE_RtfAddControl(rd, "}");
1085
1086 HeapFree(GetProcessHeap(), 0, alloc);
1087
1088 return ret;
1089 }
1090
1091 /******************************************************************
1092 * HLPFILE_RtfAddGfxByAddr
1093 *
1094 */
1095 static BOOL HLPFILE_RtfAddGfxByAddr(struct RtfData* rd, HLPFILE *hlpfile,
1096 const BYTE* ref, ULONG size)
1097 {
1098 unsigned i, numpict;
1099
1100 numpict = GET_USHORT(ref, 2);
1101 WINE_TRACE("Got picture magic=%04x #=%d\n", GET_USHORT(ref, 0), numpict);
1102
1103 for (i = 0; i < numpict; i++)
1104 {
1105 const BYTE* beg;
1106 const BYTE* ptr;
1107 BYTE type, pack;
1108
1109 WINE_TRACE("Offset[%d] = %x\n", i, GET_UINT(ref, (1 + i) * 4));
1110 beg = ptr = ref + GET_UINT(ref, (1 + i) * 4);
1111
1112 type = *ptr++;
1113 pack = *ptr++;
1114
1115 switch (type)
1116 {
1117 case 5: /* device dependent bmp */
1118 case 6: /* device independent bmp */
1119 HLPFILE_RtfAddBitmap(rd, hlpfile, beg, type, pack);
1120 break;
1121 case 8:
1122 HLPFILE_RtfAddMetaFile(rd, hlpfile, beg, pack);
1123 break;
1124 default: WINE_FIXME("Unknown type %u\n", type); return FALSE;
1125 }
1126
1127 /* FIXME: hotspots */
1128
1129 /* FIXME: implement support for multiple picture format */
1130 if (numpict != 1) WINE_FIXME("Supporting only one bitmap format per logical bitmap (for now). Using first format\n");
1131 break;
1132 }
1133 return TRUE;
1134 }
1135
1136 /******************************************************************
1137 * HLPFILE_RtfAddGfxByIndex
1138 *
1139 *
1140 */
1141 static BOOL HLPFILE_RtfAddGfxByIndex(struct RtfData* rd, HLPFILE *hlpfile,
1142 unsigned index)
1143 {
1144 char tmp[16];
1145 BYTE *ref, *end;
1146
1147 WINE_TRACE("Loading picture #%d\n", index);
1148
1149 sprintf(tmp, "|bm%u", index);
1150
1151 if (!HLPFILE_FindSubFile(hlpfile, tmp, &ref, &end)) {WINE_WARN("no sub file\n"); return FALSE;}
1152
1153 ref += 9;
1154 return HLPFILE_RtfAddGfxByAddr(rd, hlpfile, ref, end - ref);
1155 }
1156
1157 /******************************************************************
1158 * HLPFILE_AllocLink
1159 *
1160 *
1161 */
1162 static HLPFILE_LINK* HLPFILE_AllocLink(struct RtfData* rd, int cookie,
1163 const char* str, unsigned len, LONG hash,
1164 unsigned clrChange, unsigned bHotSpot, unsigned wnd)
1165 {
1166 HLPFILE_LINK* link;
1167 char* link_str;
1168 unsigned asz = bHotSpot ? sizeof(HLPFILE_HOTSPOTLINK) : sizeof(HLPFILE_LINK);
1169
1170 /* FIXME: should build a string table for the attributes.link.lpszPath
1171 * they are reallocated for each link
1172 */
1173 if (len == -1) len = strlen(str);
1174 link = HeapAlloc(GetProcessHeap(), 0, asz + len + 1);
1175 if (!link) return NULL;
1176
1177 link->cookie = cookie;
1178 link->string = link_str = (char*)link + asz;
1179 memcpy(link_str, str, len);
1180 link_str[len] = '\0';
1181 link->hash = hash;
1182 link->bClrChange = clrChange ? 1 : 0;
1183 link->bHotSpot = bHotSpot;
1184 link->window = wnd;
1185 link->next = rd->first_link;
1186 rd->first_link = link;
1187 link->cpMin = rd->char_pos;
1188 rd->force_color = clrChange;
1189 if (rd->current_link) WINE_FIXME("Pending link\n");
1190 if (bHotSpot)
1191 link->cpMax = rd->char_pos;
1192 else
1193 rd->current_link = link;
1194
1195 WINE_TRACE("Link[%d] to %s@%08x:%d\n",
1196 link->cookie, link->string, link->hash, link->window);
1197 return link;
1198 }
1199
1200 static unsigned HLPFILE_HalfPointsToTwips(unsigned pts)
1201 {
1202 static unsigned logPxY;
1203 if (!logPxY)
1204 {
1205 HDC hdc = GetDC(NULL);
1206 logPxY = GetDeviceCaps(hdc, LOGPIXELSY);
1207 ReleaseDC(NULL, hdc);
1208 }
1209 return MulDiv(pts, 72 * 10, logPxY);
1210 }
1211
1212 /***********************************************************************
1213 *
1214 * HLPFILE_BrowseParagraph
1215 */
1216 static BOOL HLPFILE_BrowseParagraph(HLPFILE_PAGE* page, struct RtfData* rd,
1217 BYTE *buf, BYTE* end, unsigned* parlen)
1218 {
1219 UINT textsize;
1220 const BYTE *format, *format_end;
1221 char *text, *text_base, *text_end;
1222 LONG size, blocksize, datalen;
1223 unsigned short bits;
1224 unsigned nc, ncol = 1;
1225 short table_width;
1226 BOOL in_table = FALSE;
1227 char tmp[256];
1228 BOOL ret = FALSE;
1229
1230 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
1231
1232 *parlen = 0;
1233 blocksize = GET_UINT(buf, 0);
1234 size = GET_UINT(buf, 0x4);
1235 datalen = GET_UINT(buf, 0x10);
1236 text = text_base = HeapAlloc(GetProcessHeap(), 0, size);
1237 if (!text) return FALSE;
1238 if (size > blocksize - datalen)
1239 {
1240 /* need to decompress */
1241 if (page->file->hasPhrases)
1242 HLPFILE_Uncompress2(page->file, buf + datalen, end, (BYTE*)text, (BYTE*)text + size);
1243 else if (page->file->hasPhrases40)
1244 HLPFILE_Uncompress3(page->file, text, text + size, buf + datalen, end);
1245 else
1246 {
1247 WINE_FIXME("Text size is too long, splitting\n");
1248 size = blocksize - datalen;
1249 memcpy(text, buf + datalen, size);
1250 }
1251 }
1252 else
1253 memcpy(text, buf + datalen, size);
1254
1255 text_end = text + size;
1256
1257 format = buf + 0x15;
1258 format_end = buf + GET_UINT(buf, 0x10);
1259
1260 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
1261 {
1262 fetch_long(&format);
1263 *parlen = fetch_ushort(&format);
1264 }
1265
1266 if (buf[0x14] == 0x23)
1267 {
1268 char type;
1269
1270 in_table = TRUE;
1271 ncol = *format++;
1272
1273 if (!HLPFILE_RtfAddControl(rd, "\\trowd")) goto done;
1274 type = *format++;
1275 if (type == 0 || type == 2)
1276 {
1277 table_width = GET_SHORT(format, 0);
1278 format += 2;
1279 }
1280 else
1281 table_width = 32767;
1282 WINE_TRACE("New table: cols=%d type=%x width=%d\n",
1283 ncol, type, table_width);
1284 if (ncol > 1)
1285 {
1286 int pos;
1287 sprintf(tmp, "\\trgaph%d\\trleft%d",
1288 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6), table_width, 32767)),
1289 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)));
1290 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1291 pos = HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 6) / 2, table_width, 32767));
1292 for (nc = 0; nc < ncol; nc++)
1293 {
1294 WINE_TRACE("column(%d/%d) gap=%d width=%d\n",
1295 nc, ncol, GET_SHORT(format, nc*4),
1296 GET_SHORT(format, nc*4+2));
1297 pos += GET_SHORT(format, nc * 4) + GET_SHORT(format, nc * 4 + 2);
1298 sprintf(tmp, "\\cellx%d",
1299 HLPFILE_HalfPointsToTwips(MulDiv(pos, table_width, 32767)));
1300 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1301 }
1302 }
1303 else
1304 {
1305 WINE_TRACE("column(0/%d) gap=%d width=%d\n",
1306 ncol, GET_SHORT(format, 0), GET_SHORT(format, 2));
1307 sprintf(tmp, "\\trleft%d\\cellx%d ",
1308 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0), table_width, 32767)),
1309 HLPFILE_HalfPointsToTwips(MulDiv(GET_SHORT(format, 0) + GET_SHORT(format, 2),
1310 table_width, 32767)));
1311 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1312 }
1313 format += ncol * 4;
1314 }
1315
1316 for (nc = 0; nc < ncol; /**/)
1317 {
1318 WINE_TRACE("looking for format at offset %lu in column %d\n", (SIZE_T)(format - (buf + 0x15)), nc);
1319 if (!HLPFILE_RtfAddControl(rd, "\\pard")) goto done;
1320 if (in_table)
1321 {
1322 nc = GET_SHORT(format, 0);
1323 if (nc == -1) break;
1324 format += 5;
1325 if (!HLPFILE_RtfAddControl(rd, "\\intbl")) goto done;
1326 }
1327 else nc++;
1328 if (buf[0x14] == 0x01)
1329 format += 6;
1330 else
1331 format += 4;
1332 bits = GET_USHORT(format, 0); format += 2;
1333 if (bits & 0x0001) fetch_long(&format);
1334 if (bits & 0x0002)
1335 {
1336 sprintf(tmp, "\\sb%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1337 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1338 }
1339 if (bits & 0x0004)
1340 {
1341 sprintf(tmp, "\\sa%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1342 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1343 }
1344 if (bits & 0x0008)
1345 {
1346 sprintf(tmp, "\\sl%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1347 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1348 }
1349 if (bits & 0x0010)
1350 {
1351 sprintf(tmp, "\\li%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1352 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1353 }
1354 if (bits & 0x0020)
1355 {
1356 sprintf(tmp, "\\ri%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1357 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1358 }
1359 if (bits & 0x0040)
1360 {
1361 sprintf(tmp, "\\fi%d", HLPFILE_HalfPointsToTwips(fetch_short(&format)));
1362 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1363 }
1364 if (bits & 0x0100)
1365 {
1366 BYTE brdr = *format++;
1367 short w;
1368
1369 if ((brdr & 0x01) && !HLPFILE_RtfAddControl(rd, "\\box")) goto done;
1370 if ((brdr & 0x02) && !HLPFILE_RtfAddControl(rd, "\\brdrt")) goto done;
1371 if ((brdr & 0x04) && !HLPFILE_RtfAddControl(rd, "\\brdrl")) goto done;
1372 if ((brdr & 0x08) && !HLPFILE_RtfAddControl(rd, "\\brdrb")) goto done;
1373 if ((brdr & 0x10) && !HLPFILE_RtfAddControl(rd, "\\brdrr")) goto done;
1374 if ((brdr & 0x20) && !HLPFILE_RtfAddControl(rd, "\\brdrth")) goto done;
1375 if (!(brdr & 0x20) && !HLPFILE_RtfAddControl(rd, "\\brdrs")) goto done;
1376 if ((brdr & 0x40) && !HLPFILE_RtfAddControl(rd, "\\brdrdb")) goto done;
1377 /* 0x80: unknown */
1378
1379 w = GET_SHORT(format, 0); format += 2;
1380 if (w)
1381 {
1382 sprintf(tmp, "\\brdrw%d", HLPFILE_HalfPointsToTwips(w));
1383 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1384 }
1385 }
1386 if (bits & 0x0200)
1387 {
1388 int i, ntab = fetch_short(&format);
1389 unsigned tab, ts;
1390 const char* kind;
1391
1392 for (i = 0; i < ntab; i++)
1393 {
1394 tab = fetch_ushort(&format);
1395 ts = (tab & 0x4000) ? fetch_ushort(&format) : 0 /* left */;
1396 switch (ts)
1397 {
1398 default: WINE_FIXME("Unknown tab style %x\n", ts);
1399 /* fall through */
1400 case 0: kind = ""; break;
1401 case 1: kind = "\\tqr"; break;
1402 case 2: kind = "\\tqc"; break;
1403 }
1404 /* FIXME: do kind */
1405 sprintf(tmp, "%s\\tx%d",
1406 kind, HLPFILE_HalfPointsToTwips(tab & 0x3FFF));
1407 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1408 }
1409 }
1410 switch (bits & 0xc00)
1411 {
1412 default: WINE_FIXME("Unsupported alignment 0xC00\n"); break;
1413 case 0: if (!HLPFILE_RtfAddControl(rd, "\\ql")) goto done; break;
1414 case 0x400: if (!HLPFILE_RtfAddControl(rd, "\\qr")) goto done; break;
1415 case 0x800: if (!HLPFILE_RtfAddControl(rd, "\\qc")) goto done; break;
1416 }
1417
1418 /* 0x1000 doesn't need space */
1419 if ((bits & 0x1000) && !HLPFILE_RtfAddControl(rd, "\\keep")) goto done;
1420 if ((bits & 0xE080) != 0)
1421 WINE_FIXME("Unsupported bits %04x, potential trouble ahead\n", bits);
1422
1423 while (text < text_end && format < format_end)
1424 {
1425 WINE_TRACE("Got text: %s (%p/%p - %p/%p)\n", wine_dbgstr_a(text), text, text_end, format, format_end);
1426 textsize = strlen(text);
1427 if (textsize)
1428 {
1429 if (rd->force_color)
1430 {
1431 if ((rd->current_link->cookie == hlp_link_popup) ?
1432 !HLPFILE_RtfAddControl(rd, "{\\uld\\cf1") :
1433 !HLPFILE_RtfAddControl(rd, "{\\ul\\cf1")) goto done;
1434 }
1435 if (!HLPFILE_RtfAddText(rd, text)) goto done;
1436 if (rd->force_color && !HLPFILE_RtfAddControl(rd, "}")) goto done;
1437 rd->char_pos += textsize;
1438 }
1439 /* else: null text, keep on storing attributes */
1440 text += textsize + 1;
1441
1442 if (*format == 0xff)
1443 {
1444 format++;
1445 break;
1446 }
1447
1448 WINE_TRACE("format=%02x\n", *format);
1449 switch (*format)
1450 {
1451 case 0x20:
1452 WINE_FIXME("NIY20\n");
1453 format += 5;
1454 break;
1455
1456 case 0x21:
1457 WINE_FIXME("NIY21\n");
1458 format += 3;
1459 break;
1460
1461 case 0x80:
1462 {
1463 unsigned font = GET_USHORT(format, 1);
1464 unsigned fs;
1465
1466 WINE_TRACE("Changing font to %d\n", font);
1467 format += 3;
1468 /* Font size in hlpfile is given in the same units as
1469 rtf control word \fs uses (half-points). */
1470 switch (rd->font_scale)
1471 {
1472 case 0: fs = page->file->fonts[font].LogFont.lfHeight - 4; break;
1473 default:
1474 case 1: fs = page->file->fonts[font].LogFont.lfHeight; break;
1475 case 2: fs = page->file->fonts[font].LogFont.lfHeight + 4; break;
1476 }
1477 /* FIXME: missing at least colors, also bold attribute looses information */
1478
1479 sprintf(tmp, "\\f%d\\cf%d\\fs%d%s%s%s%s",
1480 font, font + 2, fs,
1481 page->file->fonts[font].LogFont.lfWeight > 400 ? "\\b" : "\\b0",
1482 page->file->fonts[font].LogFont.lfItalic ? "\\i" : "\\i0",
1483 page->file->fonts[font].LogFont.lfUnderline ? "\\ul" : "\\ul0",
1484 page->file->fonts[font].LogFont.lfStrikeOut ? "\\strike" : "\\strike0");
1485 if (!HLPFILE_RtfAddControl(rd, tmp)) goto done;
1486 }
1487 break;
1488
1489 case 0x81:
1490 if (!HLPFILE_RtfAddControl(rd, "\\line")) goto done;
1491 format += 1;
1492 rd->char_pos++;
1493 break;
1494
1495 case 0x82:
1496 if (in_table)
1497 {
1498 if (format[1] != 0xFF)
1499 {
1500 if (!HLPFILE_RtfAddControl(rd, "\\par\\intbl")) goto done;
1501 }
1502 else
1503 {
1504 if (!HLPFILE_RtfAddControl(rd, "\\cell\\pard\\intbl")) goto done;
1505 }
1506 }
1507 else if (!HLPFILE_RtfAddControl(rd, "\\par")) goto done;
1508 format += 1;
1509 rd->char_pos++;
1510 break;
1511
1512 case 0x83:
1513 if (!HLPFILE_RtfAddControl(rd, "\\tab")) goto done;
1514 format += 1;
1515 rd->char_pos++;
1516 break;
1517
1518 #if 0
1519 case 0x84:
1520 format += 3;
1521 break;
1522 #endif
1523
1524 case 0x86:
1525 case 0x87:
1526 case 0x88:
1527 {
1528 BYTE type = format[1];
1529
1530 /* FIXME: we don't use 'BYTE pos = (*format - 0x86);' for the image position */
1531 format += 2;
1532 size = fetch_long(&format);
1533
1534 switch (type)
1535 {
1536 case 0x22:
1537 fetch_ushort(&format); /* hot spot */
1538 /* fall through */
1539 case 0x03:
1540 switch (GET_SHORT(format, 0))
1541 {
1542 case 0:
1543 HLPFILE_RtfAddGfxByIndex(rd, page->file, GET_SHORT(format, 2));
1544 rd->char_pos++;
1545 break;
1546 case 1:
1547 WINE_FIXME("does it work ??? %x<%u>#%u\n",
1548 GET_SHORT(format, 0),
1549 size, GET_SHORT(format, 2));
1550 HLPFILE_RtfAddGfxByAddr(rd, page->file, format + 2, size - 4);
1551 rd->char_pos++;
1552 break;
1553 default:
1554 WINE_FIXME("??? %u\n", GET_SHORT(format, 0));
1555 break;
1556 }
1557 break;
1558 case 0x05:
1559 WINE_FIXME("Got an embedded element %s\n", format + 6);
1560 break;
1561 default:
1562 WINE_FIXME("Got a type %d picture\n", type);
1563 break;
1564 }
1565 format += size;
1566 }
1567 break;
1568
1569 case 0x89:
1570 format += 1;
1571 if (!rd->current_link)
1572 WINE_FIXME("No existing link\n");
1573 rd->current_link->cpMax = rd->char_pos;
1574 rd->current_link = NULL;
1575 rd->force_color = FALSE;
1576 break;
1577
1578 case 0x8B:
1579 if (!HLPFILE_RtfAddControl(rd, "\\~")) goto done;
1580 format += 1;
1581 rd->char_pos++;
1582 break;
1583
1584 case 0x8C:
1585 if (!HLPFILE_RtfAddControl(rd, "\\_")) goto done;
1586 /* FIXME: it could be that hyphen is also in input stream !! */
1587 format += 1;
1588 rd->char_pos++;
1589 break;
1590
1591 #if 0
1592 case 0xA9:
1593 format += 2;
1594 break;
1595 #endif
1596
1597 case 0xC8:
1598 case 0xCC:
1599 WINE_TRACE("macro => %s\n", format + 3);
1600 HLPFILE_AllocLink(rd, hlp_link_macro, (const char*)format + 3,
1601 GET_USHORT(format, 1), 0, !(*format & 4), 0, -1);
1602 format += 3 + GET_USHORT(format, 1);
1603 break;
1604
1605 case 0xE0:
1606 case 0xE1:
1607 WINE_WARN("jump topic 1 => %u\n", GET_UINT(format, 1));
1608 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1609 page->file->lpszPath, -1, GET_UINT(format, 1), 1, 0, -1);
1610
1611
1612 format += 5;
1613 break;
1614
1615 case 0xE2:
1616 case 0xE3:
1617 case 0xE6:
1618 case 0xE7:
1619 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1620 page->file->lpszPath, -1, GET_UINT(format, 1),
1621 !(*format & 4), 0, -1);
1622 format += 5;
1623 break;
1624
1625 case 0xEA:
1626 case 0xEB:
1627 case 0xEE:
1628 case 0xEF:
1629 {
1630 const char* ptr = (const char*) format + 8;
1631 BYTE type = format[3];
1632 int wnd = -1;
1633
1634 switch (type)
1635 {
1636 case 1:
1637 wnd = *ptr;
1638 /* fall through */
1639 case 0:
1640 ptr = page->file->lpszPath;
1641 break;
1642 case 6:
1643 for (wnd = page->file->numWindows - 1; wnd >= 0; wnd--)
1644 {
1645 if (!strcmp(ptr, page->file->windows[wnd].name)) break;
1646 }
1647 if (wnd == -1)
1648 WINE_WARN("Couldn't find window info for %s\n", ptr);
1649 ptr += strlen(ptr) + 1;
1650 /* fall through */
1651 case 4:
1652 break;
1653 default:
1654 WINE_WARN("Unknown link type %d\n", type);
1655 break;
1656 }
1657 HLPFILE_AllocLink(rd, (*format & 1) ? hlp_link_link : hlp_link_popup,
1658 ptr, -1, GET_UINT(format, 4), !(*format & 4), 0, wnd);
1659 }
1660 format += 3 + GET_USHORT(format, 1);
1661 break;
1662
1663 default:
1664 WINE_WARN("format %02x\n", *format);
1665 format++;
1666 }
1667 }
1668 }
1669 if (in_table)
1670 {
1671 if (!HLPFILE_RtfAddControl(rd, "\\row\\par\\pard\\plain")) goto done;
1672 rd->char_pos += 2;
1673 }
1674 ret = TRUE;
1675 done:
1676
1677 HeapFree(GetProcessHeap(), 0, text_base);
1678 return ret;
1679 }
1680
1681 /******************************************************************
1682 * HLPFILE_BrowsePage
1683 *
1684 */
1685 BOOL HLPFILE_BrowsePage(HLPFILE_PAGE* page, struct RtfData* rd,
1686 unsigned font_scale, unsigned relative)
1687 {
1688 HLPFILE *hlpfile = page->file;
1689 BYTE *buf, *end;
1690 DWORD ref = page->reference;
1691 unsigned index, old_index = -1, offset, count = 0, offs = 0;
1692 unsigned cpg, parlen;
1693 char tmp[1024];
1694 const char* ck = NULL;
1695
1696 rd->in_text = TRUE;
1697 rd->data = rd->ptr = HeapAlloc(GetProcessHeap(), 0, rd->allocated = 32768);
1698 rd->char_pos = 0;
1699 rd->first_link = rd->current_link = NULL;
1700 rd->force_color = FALSE;
1701 rd->font_scale = font_scale;
1702 rd->relative = relative;
1703 rd->char_pos_rel = 0;
1704
1705 switch (hlpfile->charset)
1706 {
1707 case DEFAULT_CHARSET:
1708 case ANSI_CHARSET: cpg = 1252; break;
1709 case SHIFTJIS_CHARSET: cpg = 932; break;
1710 case HANGEUL_CHARSET: cpg = 949; break;
1711 case GB2312_CHARSET: cpg = 936; break;
1712 case CHINESEBIG5_CHARSET: cpg = 950; break;
1713 case GREEK_CHARSET: cpg = 1253; break;
1714 case TURKISH_CHARSET: cpg = 1254; break;
1715 case HEBREW_CHARSET: cpg = 1255; break;
1716 case ARABIC_CHARSET: cpg = 1256; break;
1717 case BALTIC_CHARSET: cpg = 1257; break;
1718 case VIETNAMESE_CHARSET: cpg = 1258; break;
1719 case RUSSIAN_CHARSET: cpg = 1251; break;
1720 case EE_CHARSET: cpg = 1250; break;
1721 case THAI_CHARSET: cpg = 874; break;
1722 case JOHAB_CHARSET: cpg = 1361; break;
1723 case MAC_CHARSET: ck = "mac"; break;
1724 default:
1725 WINE_FIXME("Unsupported charset %u\n", hlpfile->charset);
1726 cpg = 1252;
1727 }
1728 if (ck)
1729 {
1730 sprintf(tmp, "{\\rtf1\\%s\\deff0", ck);
1731 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1732 }
1733 else
1734 {
1735 sprintf(tmp, "{\\rtf1\\ansi\\ansicpg%d\\deff0", cpg);
1736 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1737 }
1738
1739 /* generate font table */
1740 if (!HLPFILE_RtfAddControl(rd, "{\\fonttbl")) return FALSE;
1741 for (index = 0; index < hlpfile->numFonts; index++)
1742 {
1743 const char* family;
1744 switch (hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0xF0)
1745 {
1746 case FF_MODERN: family = "modern"; break;
1747 case FF_ROMAN: family = "roman"; break;
1748 case FF_SWISS: family = "swiss"; break;
1749 case FF_SCRIPT: family = "script"; break;
1750 case FF_DECORATIVE: family = "decor"; break;
1751 default: family = "nil"; break;
1752 }
1753 sprintf(tmp, "{\\f%d\\f%s\\fprq%d\\fcharset%d %s;}",
1754 index, family,
1755 hlpfile->fonts[index].LogFont.lfPitchAndFamily & 0x0F,
1756 hlpfile->fonts[index].LogFont.lfCharSet,
1757 hlpfile->fonts[index].LogFont.lfFaceName);
1758 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1759 }
1760 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1761 /* generate color table */
1762 if (!HLPFILE_RtfAddControl(rd, "{\\colortbl ;\\red0\\green128\\blue0;")) return FALSE;
1763 for (index = 0; index < hlpfile->numFonts; index++)
1764 {
1765 sprintf(tmp, "\\red%d\\green%d\\blue%d;",
1766 GetRValue(hlpfile->fonts[index].color),
1767 GetGValue(hlpfile->fonts[index].color),
1768 GetBValue(hlpfile->fonts[index].color));
1769 if (!HLPFILE_RtfAddControl(rd, tmp)) return FALSE;
1770 }
1771 if (!HLPFILE_RtfAddControl(rd, "}")) return FALSE;
1772
1773 do
1774 {
1775 if (hlpfile->version <= 16)
1776 {
1777 index = (ref - 0x0C) / hlpfile->dsize;
1778 offset = (ref - 0x0C) % hlpfile->dsize;
1779 }
1780 else
1781 {
1782 index = (ref - 0x0C) >> 14;
1783 offset = (ref - 0x0C) & 0x3FFF;
1784 }
1785
1786 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
1787 {
1788 /* we jumped to the next block, adjust pointers */
1789 ref -= 12;
1790 offset -= 12;
1791 }
1792
1793 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
1794 buf = hlpfile->topic_map[index] + offset;
1795 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
1796 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
1797 if (index != old_index) {offs = 0; old_index = index;}
1798
1799 switch (buf[0x14])
1800 {
1801 case 0x02:
1802 if (count++) goto done;
1803 break;
1804 case 0x01:
1805 case 0x20:
1806 case 0x23:
1807 if (!HLPFILE_BrowseParagraph(page, rd, buf, end, &parlen)) return FALSE;
1808 if (relative > index * 0x8000 + offs)
1809 rd->char_pos_rel = rd->char_pos;
1810 offs += parlen;
1811 break;
1812 default:
1813 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
1814 }
1815 if (hlpfile->version <= 16)
1816 {
1817 ref += GET_UINT(buf, 0xc);
1818 if (GET_UINT(buf, 0xc) == 0)
1819 break;
1820 }
1821 else
1822 ref = GET_UINT(buf, 0xc);
1823 } while (ref != 0xffffffff);
1824 done:
1825 page->first_link = rd->first_link;
1826 return HLPFILE_RtfAddControl(rd, "}");
1827 }
1828
1829 /******************************************************************
1830 * HLPFILE_ReadFont
1831 *
1832 *
1833 */
1834 static BOOL HLPFILE_ReadFont(HLPFILE* hlpfile)
1835 {
1836 BYTE *ref, *end;
1837 unsigned i, len, idx;
1838 unsigned face_num, dscr_num, face_offset, dscr_offset;
1839 BYTE flag, family;
1840
1841 if (!HLPFILE_FindSubFile(hlpfile, "|FONT", &ref, &end))
1842 {
1843 WINE_WARN("no subfile FONT\n");
1844 hlpfile->numFonts = 0;
1845 hlpfile->fonts = NULL;
1846 return FALSE;
1847 }
1848
1849 ref += 9;
1850
1851 face_num = GET_USHORT(ref, 0);
1852 dscr_num = GET_USHORT(ref, 2);
1853 face_offset = GET_USHORT(ref, 4);
1854 dscr_offset = GET_USHORT(ref, 6);
1855
1856 WINE_TRACE("Got NumFacenames=%u@%u NumDesc=%u@%u\n",
1857 face_num, face_offset, dscr_num, dscr_offset);
1858
1859 hlpfile->numFonts = dscr_num;
1860 hlpfile->fonts = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_FONT) * dscr_num);
1861
1862 len = (dscr_offset - face_offset) / face_num;
1863 /* EPP for (i = face_offset; i < dscr_offset; i += len) */
1864 /* EPP WINE_FIXME("[%d]: %*s\n", i / len, len, ref + i); */
1865 for (i = 0; i < dscr_num; i++)
1866 {
1867 flag = ref[dscr_offset + i * 11 + 0];
1868 family = ref[dscr_offset + i * 11 + 2];
1869
1870 hlpfile->fonts[i].LogFont.lfHeight = ref[dscr_offset + i * 11 + 1];
1871 hlpfile->fonts[i].LogFont.lfWidth = 0;
1872 hlpfile->fonts[i].LogFont.lfEscapement = 0;
1873 hlpfile->fonts[i].LogFont.lfOrientation = 0;
1874 hlpfile->fonts[i].LogFont.lfWeight = (flag & 1) ? 700 : 400;
1875 hlpfile->fonts[i].LogFont.lfItalic = (flag & 2) != 0;
1876 hlpfile->fonts[i].LogFont.lfUnderline = (flag & 4) != 0;
1877 hlpfile->fonts[i].LogFont.lfStrikeOut = (flag & 8) != 0;
1878 hlpfile->fonts[i].LogFont.lfCharSet = hlpfile->charset;
1879 hlpfile->fonts[i].LogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1880 hlpfile->fonts[i].LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1881 hlpfile->fonts[i].LogFont.lfQuality = DEFAULT_QUALITY;
1882 hlpfile->fonts[i].LogFont.lfPitchAndFamily = DEFAULT_PITCH;
1883
1884 switch (family)
1885 {
1886 case 0x01: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_MODERN; break;
1887 case 0x02: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_ROMAN; break;
1888 case 0x03: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SWISS; break;
1889 case 0x04: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_SCRIPT; break;
1890 case 0x05: hlpfile->fonts[i].LogFont.lfPitchAndFamily |= FF_DECORATIVE; break;
1891 default: WINE_FIXME("Unknown family %u\n", family);
1892 }
1893 idx = GET_USHORT(ref, dscr_offset + i * 11 + 3);
1894
1895 if (idx < face_num)
1896 {
1897 memcpy(hlpfile->fonts[i].LogFont.lfFaceName, ref + face_offset + idx * len, min(len, LF_FACESIZE - 1));
1898 hlpfile->fonts[i].LogFont.lfFaceName[min(len, LF_FACESIZE - 1)] = '\0';
1899 }
1900 else
1901 {
1902 WINE_FIXME("Too high face ref (%u/%u)\n", idx, face_num);
1903 strcpy(hlpfile->fonts[i].LogFont.lfFaceName, "Helv");
1904 }
1905 hlpfile->fonts[i].hFont = 0;
1906 hlpfile->fonts[i].color = RGB(ref[dscr_offset + i * 11 + 5],
1907 ref[dscr_offset + i * 11 + 6],
1908 ref[dscr_offset + i * 11 + 7]);
1909 #define X(b,s) ((flag & (1 << b)) ? "-"s: "")
1910 WINE_TRACE("Font[%d]: flags=%02x%s%s%s%s%s%s pSize=%u family=%u face=%s[%u] color=%08x\n",
1911 i, flag,
1912 X(0, "bold"),
1913 X(1, "italic"),
1914 X(2, "underline"),
1915 X(3, "strikeOut"),
1916 X(4, "dblUnderline"),
1917 X(5, "smallCaps"),
1918 ref[dscr_offset + i * 11 + 1],
1919 family,
1920 hlpfile->fonts[i].LogFont.lfFaceName, idx,
1921 GET_UINT(ref, dscr_offset + i * 11 + 5) & 0x00FFFFFF);
1922 }
1923 return TRUE;
1924 }
1925
1926 /***********************************************************************
1927 *
1928 * HLPFILE_ReadFileToBuffer
1929 */
1930 static BOOL HLPFILE_ReadFileToBuffer(HLPFILE* hlpfile, HFILE hFile)
1931 {
1932 BYTE header[16], dummy[1];
1933
1934 if (_hread(hFile, header, 16) != 16) {WINE_WARN("header\n"); return FALSE;};
1935
1936 /* sanity checks */
1937 if (GET_UINT(header, 0) != 0x00035F3F)
1938 {WINE_WARN("wrong header\n"); return FALSE;};
1939
1940 hlpfile->file_buffer_size = GET_UINT(header, 12);
1941 hlpfile->file_buffer = HeapAlloc(GetProcessHeap(), 0, hlpfile->file_buffer_size + 1);
1942 if (!hlpfile->file_buffer) return FALSE;
1943
1944 memcpy(hlpfile->file_buffer, header, 16);
1945 if (_hread(hFile, hlpfile->file_buffer + 16, hlpfile->file_buffer_size - 16) !=hlpfile->file_buffer_size - 16)
1946 {WINE_WARN("filesize1\n"); return FALSE;};
1947
1948 if (_hread(hFile, dummy, 1) != 0) WINE_WARN("filesize2\n");
1949
1950 hlpfile->file_buffer[hlpfile->file_buffer_size] = '\0'; /* FIXME: was '0', sounds backwards to me */
1951
1952 return TRUE;
1953 }
1954
1955 /***********************************************************************
1956 *
1957 * HLPFILE_SystemCommands
1958 */
1959 static BOOL HLPFILE_SystemCommands(HLPFILE* hlpfile)
1960 {
1961 BYTE *buf, *ptr, *end;
1962 HLPFILE_MACRO *macro, **m;
1963 LPSTR p;
1964 unsigned short magic, minor, major, flags;
1965
1966 hlpfile->lpszTitle = NULL;
1967
1968 if (!HLPFILE_FindSubFile(hlpfile, "|SYSTEM", &buf, &end)) return FALSE;
1969
1970 magic = GET_USHORT(buf + 9, 0);
1971 minor = GET_USHORT(buf + 9, 2);
1972 major = GET_USHORT(buf + 9, 4);
1973 /* gen date on 4 bytes */
1974 flags = GET_USHORT(buf + 9, 10);
1975 WINE_TRACE("Got system header: magic=%04x version=%d.%d flags=%04x\n",
1976 magic, major, minor, flags);
1977 if (magic != 0x036C || major != 1)
1978 {WINE_WARN("Wrong system header\n"); return FALSE;}
1979 if (minor <= 16)
1980 {
1981 hlpfile->tbsize = 0x800;
1982 hlpfile->compressed = 0;
1983 }
1984 else if (flags == 0)
1985 {
1986 hlpfile->tbsize = 0x1000;
1987 hlpfile->compressed = 0;
1988 }
1989 else if (flags == 4)
1990 {
1991 hlpfile->tbsize = 0x1000;
1992 hlpfile->compressed = 1;
1993 }
1994 else
1995 {
1996 hlpfile->tbsize = 0x800;
1997 hlpfile->compressed = 1;
1998 }
1999
2000 if (hlpfile->compressed)
2001 hlpfile->dsize = 0x4000;
2002 else
2003 hlpfile->dsize = hlpfile->tbsize - 0x0C;
2004
2005 hlpfile->version = minor;
2006 hlpfile->flags = flags;
2007 hlpfile->charset = DEFAULT_CHARSET;
2008
2009 if (hlpfile->version <= 16)
2010 {
2011 char *str = (char*)buf + 0x15;
2012
2013 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
2014 if (!hlpfile->lpszTitle) return FALSE;
2015 strcpy(hlpfile->lpszTitle, str);
2016 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
2017 /* Nothing more to parse */
2018 return TRUE;
2019 }
2020 for (ptr = buf + 0x15; ptr + 4 <= end; ptr += GET_USHORT(ptr, 2) + 4)
2021 {
2022 char *str = (char*) ptr + 4;
2023 switch (GET_USHORT(ptr, 0))
2024 {
2025 case 1:
2026 if (hlpfile->lpszTitle) {WINE_WARN("title\n"); break;}
2027 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
2028 if (!hlpfile->lpszTitle) return FALSE;
2029 strcpy(hlpfile->lpszTitle, str);
2030 WINE_TRACE("Title: %s\n", hlpfile->lpszTitle);
2031 break;
2032
2033 case 2:
2034 if (hlpfile->lpszCopyright) {WINE_WARN("copyright\n"); break;}
2035 hlpfile->lpszCopyright = HeapAlloc(GetProcessHeap(), 0, strlen(str) + 1);
2036 if (!hlpfile->lpszCopyright) return FALSE;
2037 strcpy(hlpfile->lpszCopyright, str);
2038 WINE_TRACE("Copyright: %s\n", hlpfile->lpszCopyright);
2039 break;
2040
2041 case 3:
2042 if (GET_USHORT(ptr, 2) != 4) {WINE_WARN("system3\n");break;}
2043 hlpfile->contents_start = GET_UINT(ptr, 4);
2044 WINE_TRACE("Setting contents start at %08lx\n", hlpfile->contents_start);
2045 break;
2046
2047 case 4:
2048 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + strlen(str) + 1);
2049 if (!macro) break;
2050 p = (char*)macro + sizeof(HLPFILE_MACRO);
2051 strcpy(p, str);
2052 macro->lpszMacro = p;
2053 macro->next = 0;
2054 for (m = &hlpfile->first_macro; *m; m = &(*m)->next);
2055 *m = macro;
2056 break;
2057
2058 case 5:
2059 if (GET_USHORT(ptr, 4 + 4) != 1)
2060 WINE_FIXME("More than one icon, picking up first\n");
2061 /* 0x16 is sizeof(CURSORICONDIR), see user32/user_private.h */
2062 hlpfile->hIcon = CreateIconFromResourceEx(ptr + 4 + 0x16,
2063 GET_USHORT(ptr, 2) - 0x16, TRUE,
2064 0x30000, 0, 0, 0);
2065 break;
2066
2067 case 6:
2068 if (GET_USHORT(ptr, 2) != 90) {WINE_WARN("system6\n");break;}
2069
2070 if (hlpfile->windows)
2071 hlpfile->windows = HeapReAlloc(GetProcessHeap(), 0, hlpfile->windows,
2072 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
2073 else
2074 hlpfile->windows = HeapAlloc(GetProcessHeap(), 0,
2075 sizeof(HLPFILE_WINDOWINFO) * ++hlpfile->numWindows);
2076
2077 if (hlpfile->windows)
2078 {
2079 HLPFILE_WINDOWINFO* wi = &hlpfile->windows[hlpfile->numWindows - 1];
2080
2081 flags = GET_USHORT(ptr, 4);
2082 if (flags & 0x0001) strcpy(wi->type, &str[2]);
2083 else wi->type[0] = '\0';
2084 if (flags & 0x0002) strcpy(wi->name, &str[12]);
2085 else wi->name[0] = '\0';
2086 if (flags & 0x0004) strcpy(wi->caption, &str[21]);
2087 else lstrcpynA(wi->caption, hlpfile->lpszTitle, sizeof(wi->caption));
2088 wi->origin.x = (flags & 0x0008) ? GET_USHORT(ptr, 76) : CW_USEDEFAULT;
2089 wi->origin.y = (flags & 0x0010) ? GET_USHORT(ptr, 78) : CW_USEDEFAULT;
2090 wi->size.cx = (flags & 0x0020) ? GET_USHORT(ptr, 80) : CW_USEDEFAULT;
2091 wi->size.cy = (flags & 0x0040) ? GET_USHORT(ptr, 82) : CW_USEDEFAULT;
2092 wi->style = (flags & 0x0080) ? GET_USHORT(ptr, 84) : SW_SHOW;
2093 wi->win_style = WS_OVERLAPPEDWINDOW;
2094 wi->sr_color = (flags & 0x0100) ? GET_UINT(ptr, 86) : 0xFFFFFF;
2095 wi->nsr_color = (flags & 0x0200) ? GET_UINT(ptr, 90) : 0xFFFFFF;
2096 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",
2097 flags & 0x0001 ? 'T' : 't',
2098 flags & 0x0002 ? 'N' : 'n',
2099 flags & 0x0004 ? 'C' : 'c',
2100 flags & 0x0008 ? 'X' : 'x',
2101 flags & 0x0010 ? 'Y' : 'y',
2102 flags & 0x0020 ? 'W' : 'w',
2103 flags & 0x0040 ? 'H' : 'h',
2104 flags & 0x0080 ? 'S' : 's',
2105 wi->type, wi->name, wi->caption, wi->origin.x, wi->origin.y,
2106 wi->size.cx, wi->size.cy);
2107 }
2108 break;
2109 case 8:
2110 WINE_WARN("Citation: '%s'\n", ptr + 4);
2111 break;
2112 case 11:
2113 hlpfile->charset = ptr[4];
2114 WINE_TRACE("Charset: %d\n", hlpfile->charset);
2115 break;
2116 default:
2117 WINE_WARN("Unsupported SystemRecord[%d]\n", GET_USHORT(ptr, 0));
2118 }
2119 }
2120 if (!hlpfile->lpszTitle)
2121 hlpfile->lpszTitle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1);
2122 return TRUE;
2123 }
2124
2125 /***********************************************************************
2126 *
2127 * HLPFILE_GetContext
2128 */
2129 static BOOL HLPFILE_GetContext(HLPFILE *hlpfile)
2130 {
2131 BYTE *cbuf, *cend;
2132 unsigned clen;
2133
2134 if (!HLPFILE_FindSubFile(hlpfile, "|CONTEXT", &cbuf, &cend))
2135 {WINE_WARN("context0\n"); return FALSE;}
2136
2137 clen = cend - cbuf;
2138 hlpfile->Context = HeapAlloc(GetProcessHeap(), 0, clen);
2139 if (!hlpfile->Context) return FALSE;
2140 memcpy(hlpfile->Context, cbuf, clen);
2141
2142 return TRUE;
2143 }
2144
2145 /***********************************************************************
2146 *
2147 * HLPFILE_GetKeywords
2148 */
2149 static BOOL HLPFILE_GetKeywords(HLPFILE *hlpfile)
2150 {
2151 BYTE *cbuf, *cend;
2152 unsigned clen;
2153
2154 if (!HLPFILE_FindSubFile(hlpfile, "|KWBTREE", &cbuf, &cend)) return FALSE;
2155 clen = cend - cbuf;
2156 hlpfile->kwbtree = HeapAlloc(GetProcessHeap(), 0, clen);
2157 if (!hlpfile->kwbtree) return FALSE;
2158 memcpy(hlpfile->kwbtree, cbuf, clen);
2159
2160 if (!HLPFILE_FindSubFile(hlpfile, "|KWDATA", &cbuf, &cend))
2161 {
2162 WINE_ERR("corrupted help file: kwbtree present but kwdata absent\n");
2163 HeapFree(GetProcessHeap(), 0, hlpfile->kwbtree);
2164 return FALSE;
2165 }
2166 clen = cend - cbuf;
2167 hlpfile->kwdata = HeapAlloc(GetProcessHeap(), 0, clen);
2168 if (!hlpfile->kwdata)
2169 {
2170 HeapFree(GetProcessHeap(), 0, hlpfile->kwdata);
2171 return FALSE;
2172 }
2173 memcpy(hlpfile->kwdata, cbuf, clen);
2174
2175 return TRUE;
2176 }
2177
2178 /***********************************************************************
2179 *
2180 * HLPFILE_GetMap
2181 */
2182 static BOOL HLPFILE_GetMap(HLPFILE *hlpfile)
2183 {
2184 BYTE *cbuf, *cend;
2185 unsigned entries, i;
2186
2187 if (!HLPFILE_FindSubFile(hlpfile, "|CTXOMAP", &cbuf, &cend))
2188 {WINE_WARN("no map section\n"); return FALSE;}
2189
2190 entries = GET_USHORT(cbuf, 9);
2191 hlpfile->Map = HeapAlloc(GetProcessHeap(), 0, entries * sizeof(HLPFILE_MAP));
2192 if (!hlpfile->Map) return FALSE;
2193 hlpfile->wMapLen = entries;
2194 for (i = 0; i < entries; i++)
2195 {
2196 hlpfile->Map[i].lMap = GET_UINT(cbuf+11,i*8);
2197 hlpfile->Map[i].offset = GET_UINT(cbuf+11,i*8+4);
2198 }
2199 return TRUE;
2200 }
2201
2202 /***********************************************************************
2203 *
2204 * HLPFILE_GetTOMap
2205 */
2206 static BOOL HLPFILE_GetTOMap(HLPFILE *hlpfile)
2207 {
2208 BYTE *cbuf, *cend;
2209 unsigned clen;
2210
2211 if (!HLPFILE_FindSubFile(hlpfile, "|TOMAP", &cbuf, &cend))
2212 {WINE_WARN("no tomap section\n"); return FALSE;}
2213
2214 clen = cend - cbuf - 9;
2215 hlpfile->TOMap = HeapAlloc(GetProcessHeap(), 0, clen);
2216 if (!hlpfile->TOMap) return FALSE;
2217 memcpy(hlpfile->TOMap, cbuf+9, clen);
2218 hlpfile->wTOMapLen = clen/4;
2219 return TRUE;
2220 }
2221
2222 /***********************************************************************
2223 *
2224 * DeleteMacro
2225 */
2226 static void HLPFILE_DeleteMacro(HLPFILE_MACRO* macro)
2227 {
2228 HLPFILE_MACRO* next;
2229
2230 while (macro)
2231 {
2232 next = macro->next;
2233 HeapFree(GetProcessHeap(), 0, macro);
2234 macro = next;
2235 }
2236 }
2237
2238 /***********************************************************************
2239 *
2240 * DeletePage
2241 */
2242 static void HLPFILE_DeletePage(HLPFILE_PAGE* page)
2243 {
2244 HLPFILE_PAGE* next;
2245
2246 while (page)
2247 {
2248 next = page->next;
2249 HLPFILE_DeleteMacro(page->first_macro);
2250 HeapFree(GetProcessHeap(), 0, page);
2251 page = next;
2252 }
2253 }
2254
2255 /***********************************************************************
2256 *
2257 * HLPFILE_FreeHlpFile
2258 */
2259 void HLPFILE_FreeHlpFile(HLPFILE* hlpfile)
2260 {
2261 unsigned i;
2262
2263 if (!hlpfile || --hlpfile->wRefCount > 0) return;
2264
2265 if (hlpfile->next) hlpfile->next->prev = hlpfile->prev;
2266 if (hlpfile->prev) hlpfile->prev->next = hlpfile->next;
2267 else first_hlpfile = hlpfile->next;
2268
2269 if (hlpfile->numFonts)
2270 {
2271 for (i = 0; i < hlpfile->numFonts; i++)
2272 {
2273 DeleteObject(hlpfile->fonts[i].hFont);
2274 }
2275 HeapFree(GetProcessHeap(), 0, hlpfile->fonts);
2276 }
2277
2278 if (hlpfile->numBmps)
2279 {
2280 for (i = 0; i < hlpfile->numBmps; i++)
2281 {
2282 DeleteObject(hlpfile->bmps[i]);
2283 }
2284 HeapFree(GetProcessHeap(), 0, hlpfile->bmps);
2285 }
2286
2287 HLPFILE_DeletePage(hlpfile->first_page);
2288 HLPFILE_DeleteMacro(hlpfile->first_macro);
2289
2290 DestroyIcon(hlpfile->hIcon);
2291 if (hlpfile->numWindows) HeapFree(GetProcessHeap(), 0, hlpfile->windows);
2292 HeapFree(GetProcessHeap(), 0, hlpfile->Context);
2293 HeapFree(GetProcessHeap(), 0, hlpfile->Map);
2294 HeapFree(GetProcessHeap(), 0, hlpfile->lpszTitle);
2295 HeapFree(GetProcessHeap(), 0, hlpfile->lpszCopyright);
2296 HeapFree(GetProcessHeap(), 0, hlpfile->file_buffer);
2297 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2298 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2299 HeapFree(GetProcessHeap(), 0, hlpfile->topic_map);
2300 HeapFree(GetProcessHeap(), 0, hlpfile->help_on_file);
2301 HeapFree(GetProcessHeap(), 0, hlpfile);
2302 }
2303
2304 /***********************************************************************
2305 *
2306 * HLPFILE_UncompressLZ77_Phrases
2307 */
2308 static BOOL HLPFILE_UncompressLZ77_Phrases(HLPFILE* hlpfile)
2309 {
2310 UINT i, num, dec_size, head_size;
2311 BYTE *buf, *end;
2312
2313 if (!HLPFILE_FindSubFile(hlpfile, "|Phrases", &buf, &end)) return FALSE;
2314
2315 if (hlpfile->version <= 16)
2316 head_size = 13;
2317 else
2318 head_size = 17;
2319
2320 num = hlpfile->num_phrases = GET_USHORT(buf, 9);
2321 if (buf + 2 * num + 0x13 >= end) {WINE_WARN("1a\n"); return FALSE;};
2322
2323 if (hlpfile->version <= 16)
2324 dec_size = end - buf - 15 - 2 * num;
2325 else
2326 dec_size = HLPFILE_UncompressedLZ77_Size(buf + 0x13 + 2 * num, end);
2327
2328 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2329 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2330 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2331 {
2332 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2333 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2334 return FALSE;
2335 }
2336
2337 for (i = 0; i <= num; i++)
2338 hlpfile->phrases_offsets[i] = GET_USHORT(buf, head_size + 2 * i) - 2 * num - 2;
2339
2340 if (hlpfile->version <= 16)
2341 memcpy(hlpfile->phrases_buffer, buf + 15 + 2*num, dec_size);
2342 else
2343 HLPFILE_UncompressLZ77(buf + 0x13 + 2 * num, end, (BYTE*)hlpfile->phrases_buffer);
2344
2345 hlpfile->hasPhrases = TRUE;
2346 return TRUE;
2347 }
2348
2349 /***********************************************************************
2350 *
2351 * HLPFILE_Uncompress_Phrases40
2352 */
2353 static BOOL HLPFILE_Uncompress_Phrases40(HLPFILE* hlpfile)
2354 {
2355 UINT num;
2356 INT dec_size, cpr_size;
2357 BYTE *buf_idx, *end_idx;
2358 BYTE *buf_phs, *end_phs;
2359 ULONG* ptr, mask = 0;
2360 unsigned int i;
2361 unsigned short bc, n;
2362
2363 if (!HLPFILE_FindSubFile(hlpfile, "|PhrIndex", &buf_idx, &end_idx) ||
2364 !HLPFILE_FindSubFile(hlpfile, "|PhrImage", &buf_phs, &end_phs)) return FALSE;
2365
2366 ptr = (ULONG*)(buf_idx + 9 + 28);
2367 bc = GET_USHORT(buf_idx, 9 + 24) & 0x0F;
2368 num = hlpfile->num_phrases = GET_USHORT(buf_idx, 9 + 4);
2369
2370 WINE_TRACE("Index: Magic=%08x #entries=%u CpsdSize=%u PhrImgSize=%u\n"
2371 "\tPhrImgCprsdSize=%u 0=%u bc=%x ukn=%x\n",
2372 GET_UINT(buf_idx, 9 + 0),
2373 GET_UINT(buf_idx, 9 + 4),
2374 GET_UINT(buf_idx, 9 + 8),
2375 GET_UINT(buf_idx, 9 + 12),
2376 GET_UINT(buf_idx, 9 + 16),
2377 GET_UINT(buf_idx, 9 + 20),
2378 GET_USHORT(buf_idx, 9 + 24),
2379 GET_USHORT(buf_idx, 9 + 26));
2380
2381 dec_size = GET_UINT(buf_idx, 9 + 12);
2382 cpr_size = GET_UINT(buf_idx, 9 + 16);
2383
2384 if (dec_size != cpr_size &&
2385 dec_size != HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs))
2386 {
2387 WINE_WARN("size mismatch %u %u\n",
2388 dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2389 dec_size = max(dec_size, HLPFILE_UncompressedLZ77_Size(buf_phs + 9, end_phs));
2390 }
2391
2392 hlpfile->phrases_offsets = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned) * (num + 1));
2393 hlpfile->phrases_buffer = HeapAlloc(GetProcessHeap(), 0, dec_size);
2394 if (!hlpfile->phrases_offsets || !hlpfile->phrases_buffer)
2395 {
2396 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_offsets);
2397 HeapFree(GetProcessHeap(), 0, hlpfile->phrases_buffer);
2398 return FALSE;
2399 }
2400
2401 #define getbit() ((mask <<= 1) ? (*ptr & mask) != 0: (*++ptr & (mask=1)) != 0)
2402
2403 hlpfile->phrases_offsets[0] = 0;
2404 ptr--; /* as we'll first increment ptr because mask is 0 on first getbit() call */
2405 for (i = 0; i < num; i++)
2406 {
2407 for (n = 1; getbit(); n += 1 << bc);
2408 if (getbit()) n++;
2409 if (bc > 1 && getbit()) n += 2;
2410 if (bc > 2 && getbit()) n += 4;
2411 if (bc > 3 && getbit()) n += 8;
2412 if (bc > 4 && getbit()) n += 16;
2413 hlpfile->phrases_offsets[i + 1] = hlpfile->phrases_offsets[i] + n;
2414 }
2415 #undef getbit
2416
2417 if (dec_size == cpr_size)
2418 memcpy(hlpfile->phrases_buffer, buf_phs + 9, dec_size);
2419 else
2420 HLPFILE_UncompressLZ77(buf_phs + 9, end_phs, (BYTE*)hlpfile->phrases_buffer);
2421
2422 hlpfile->hasPhrases40 = TRUE;
2423 return TRUE;
2424 }
2425
2426 /***********************************************************************
2427 *
2428 * HLPFILE_Uncompress_Topic
2429 */
2430 static BOOL HLPFILE_Uncompress_Topic(HLPFILE* hlpfile)
2431 {
2432 BYTE *buf, *ptr, *end, *newptr;
2433 unsigned int i, newsize = 0;
2434 unsigned int topic_size;
2435
2436 if (!HLPFILE_FindSubFile(hlpfile, "|TOPIC", &buf, &end))
2437 {WINE_WARN("topic0\n"); return FALSE;}
2438
2439 buf += 9; /* Skip file header */
2440 topic_size = end - buf;
2441 if (hlpfile->compressed)
2442 {
2443 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2444
2445 for (i = 0; i < hlpfile->topic_maplen; i++)
2446 {
2447 ptr = buf + i * hlpfile->tbsize;
2448
2449 /* I don't know why, it's necessary for printman.hlp */
2450 if (ptr + 0x44 > end) ptr = end - 0x44;
2451
2452 newsize += HLPFILE_UncompressedLZ77_Size(ptr + 0xc, min(end, ptr + hlpfile->tbsize));
2453 }
2454
2455 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2456 hlpfile->topic_maplen * sizeof(hlpfile->topic_map[0]) + newsize);
2457 if (!hlpfile->topic_map) return FALSE;
2458 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2459 hlpfile->topic_end = newptr + newsize;
2460
2461 for (i = 0; i < hlpfile->topic_maplen; i++)
2462 {
2463 ptr = buf + i * hlpfile->tbsize;
2464 if (ptr + 0x44 > end) ptr = end - 0x44;
2465
2466 hlpfile->topic_map[i] = newptr;
2467 newptr = HLPFILE_UncompressLZ77(ptr + 0xc, min(end, ptr + hlpfile->tbsize), newptr);
2468 }
2469 }
2470 else
2471 {
2472 /* basically, we need to copy the TopicBlockSize byte pages
2473 * (removing the first 0x0C) in one single area in memory
2474 */
2475 hlpfile->topic_maplen = (topic_size - 1) / hlpfile->tbsize + 1;
2476 hlpfile->topic_map = HeapAlloc(GetProcessHeap(), 0,
2477 hlpfile->topic_maplen * (sizeof(hlpfile->topic_map[0]) + hlpfile->dsize));
2478 if (!hlpfile->topic_map) return FALSE;
2479 newptr = (BYTE*)(hlpfile->topic_map + hlpfile->topic_maplen);
2480 hlpfile->topic_end = newptr + topic_size;
2481
2482 for (i = 0; i < hlpfile->topic_maplen; i++)
2483 {
2484 hlpfile->topic_map[i] = newptr + i * hlpfile->dsize;
2485 memcpy(hlpfile->topic_map[i], buf + i * hlpfile->tbsize + 0x0C, hlpfile->dsize);
2486 }
2487 }
2488 return TRUE;
2489 }
2490
2491 /***********************************************************************
2492 *
2493 * HLPFILE_AddPage
2494 */
2495 static BOOL HLPFILE_AddPage(HLPFILE *hlpfile, const BYTE *buf, const BYTE *end, unsigned ref, unsigned offset)
2496 {
2497 HLPFILE_PAGE* page;
2498 const BYTE* title;
2499 UINT titlesize, blocksize, datalen;
2500 char* ptr;
2501 HLPFILE_MACRO*macro;
2502
2503 blocksize = GET_UINT(buf, 0);
2504 datalen = GET_UINT(buf, 0x10);
2505 title = buf + datalen;
2506 if (title > end) {WINE_WARN("page2\n"); return FALSE;};
2507
2508 titlesize = GET_UINT(buf, 4);
2509 page = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_PAGE) + titlesize + 1);
2510 if (!page) return FALSE;
2511 page->lpszTitle = (char*)page + sizeof(HLPFILE_PAGE);
2512
2513 if (titlesize > blocksize - datalen)
2514 {
2515 /* need to decompress */
2516 if (hlpfile->hasPhrases)
2517 HLPFILE_Uncompress2(hlpfile, title, end, (BYTE*)page->lpszTitle, (BYTE*)page->lpszTitle + titlesize);
2518 else if (hlpfile->hasPhrases40)
2519 HLPFILE_Uncompress3(hlpfile, page->lpszTitle, page->lpszTitle + titlesize, title, end);
2520 else
2521 {
2522 WINE_FIXME("Text size is too long, splitting\n");
2523 titlesize = blocksize - datalen;
2524 memcpy(page->lpszTitle, title, titlesize);
2525 }
2526 }
2527 else
2528 memcpy(page->lpszTitle, title, titlesize);
2529
2530 page->lpszTitle[titlesize] = '\0';
2531
2532 if (hlpfile->first_page)
2533 {
2534 hlpfile->last_page->next = page;
2535 page->prev = hlpfile->last_page;
2536 hlpfile->last_page = page;
2537 }
2538 else
2539 {
2540 hlpfile->first_page = page;
2541 hlpfile->last_page = page;
2542 page->prev = NULL;
2543 }
2544
2545 page->file = hlpfile;
2546 page->next = NULL;
2547 page->first_macro = NULL;
2548 page->first_link = NULL;
2549 page->wNumber = GET_UINT(buf, 0x21);
2550 page->offset = offset;
2551 page->reference = ref;
2552
2553 page->browse_bwd = GET_UINT(buf, 0x19);
2554 page->browse_fwd = GET_UINT(buf, 0x1D);
2555
2556 if (hlpfile->version <= 16)
2557 {
2558 if (page->browse_bwd == 0xFFFF || page->browse_bwd == 0xFFFFFFFF)
2559 page->browse_bwd = 0xFFFFFFFF;
2560 else
2561 page->browse_bwd = hlpfile->TOMap[page->browse_bwd];
2562
2563 if (page->browse_fwd == 0xFFFF || page->browse_fwd == 0xFFFFFFFF)
2564 page->browse_fwd = 0xFFFFFFFF;
2565 else
2566 page->browse_fwd = hlpfile->TOMap[page->browse_fwd];
2567 }
2568
2569 WINE_TRACE("Added page[%d]: title='%s' %08x << %08x >> %08x\n",
2570 page->wNumber, page->lpszTitle,
2571 page->browse_bwd, page->offset, page->browse_fwd);
2572
2573 /* now load macros */
2574 ptr = page->lpszTitle + strlen(page->lpszTitle) + 1;
2575 while (ptr < page->lpszTitle + titlesize)
2576 {
2577 unsigned len = strlen(ptr);
2578 char* macro_str;
2579
2580 WINE_TRACE("macro: %s\n", ptr);
2581 macro = HeapAlloc(GetProcessHeap(), 0, sizeof(HLPFILE_MACRO) + len + 1);
2582 macro->lpszMacro = macro_str = (char*)(macro + 1);
2583 memcpy(macro_str, ptr, len + 1);
2584 /* FIXME: shall we really link macro in reverse order ??
2585 * may produce strange results when played at page opening
2586 */
2587 macro->next = page->first_macro;
2588 page->first_macro = macro;
2589 ptr += len + 1;
2590 }
2591
2592 return TRUE;
2593 }
2594
2595 /***********************************************************************
2596 *
2597 * HLPFILE_SkipParagraph
2598 */
2599 static BOOL HLPFILE_SkipParagraph(HLPFILE *hlpfile, const BYTE *buf, const BYTE *end, unsigned* len)
2600 {
2601 const BYTE *tmp;
2602
2603 if (!hlpfile->first_page) {WINE_WARN("no page\n"); return FALSE;};
2604 if (buf + 0x19 > end) {WINE_WARN("header too small\n"); return FALSE;};
2605
2606 tmp = buf + 0x15;
2607 if (buf[0x14] == 0x20 || buf[0x14] == 0x23)
2608 {
2609 fetch_long(&tmp);
2610 *len = fetch_ushort(&tmp);
2611 }
2612 else *len = end-buf-15;
2613
2614 return TRUE;
2615 }
2616
2617 /***********************************************************************
2618 *
2619 * HLPFILE_DoReadHlpFile
2620 */
2621 static BOOL HLPFILE_DoReadHlpFile(HLPFILE *hlpfile, LPCSTR lpszPath)
2622 {
2623 BOOL ret;
2624 HFILE hFile;
2625 OFSTRUCT ofs;
2626 BYTE* buf;
2627 DWORD ref = 0x0C;
2628 unsigned index, old_index, offset, len, offs, topicoffset;
2629
2630 hFile = OpenFile(lpszPath, &ofs, OF_READ);
2631 if (hFile == HFILE_ERROR) return FALSE;
2632
2633 ret = HLPFILE_ReadFileToBuffer(hlpfile, hFile);
2634 _lclose(hFile);
2635 if (!ret) return FALSE;
2636
2637 if (!HLPFILE_SystemCommands(hlpfile)) return FALSE;
2638
2639 if (hlpfile->version <= 16 && !HLPFILE_GetTOMap(hlpfile)) return FALSE;
2640
2641 /* load phrases support */
2642 if (!HLPFILE_UncompressLZ77_Phrases(hlpfile))
2643 HLPFILE_Uncompress_Phrases40(hlpfile);
2644
2645 if (!HLPFILE_Uncompress_Topic(hlpfile)) return FALSE;
2646 if (!HLPFILE_ReadFont(hlpfile)) return FALSE;
2647
2648 old_index = -1;
2649 offs = 0;
2650 do
2651 {
2652 BYTE* end;
2653
2654 if (hlpfile->version <= 16)
2655 {
2656 index = (ref - 0x0C) / hlpfile->dsize;
2657 offset = (ref - 0x0C) % hlpfile->dsize;
2658 }
2659 else
2660 {
2661 index = (ref - 0x0C) >> 14;
2662 offset = (ref - 0x0C) & 0x3FFF;
2663 }
2664
2665 if (hlpfile->version <= 16 && index != old_index && old_index != -1)
2666 {
2667 /* we jumped to the next block, adjust pointers */
2668 ref -= 12;
2669 offset -= 12;
2670 }
2671
2672 WINE_TRACE("ref=%08x => [%u/%u]\n", ref, index, offset);
2673
2674 if (index >= hlpfile->topic_maplen) {WINE_WARN("maplen\n"); break;}
2675 buf = hlpfile->topic_map[index] + offset;
2676 if (buf + 0x15 >= hlpfile->topic_end) {WINE_WARN("extra\n"); break;}
2677 end = min(buf + GET_UINT(buf, 0), hlpfile->topic_end);
2678 if (index != old_index) {offs = 0; old_index = index;}
2679
2680 switch (buf[0x14])
2681 {
2682 case 0x02:
2683 if (hlpfile->version <= 16)
2684 topicoffset = ref + index * 12;
2685 else
2686 topicoffset = index * 0x8000 + offs;
2687 if (!HLPFILE_AddPage(hlpfile, buf, end, ref, topicoffset)) return FALSE;
2688 break;
2689
2690 case 0x01:
2691 case 0x20:
2692 case 0x23:
2693 if (!HLPFILE_SkipParagraph(hlpfile, buf, end, &len)) return FALSE;
2694 offs += len;
2695 break;
2696
2697 default:
2698 WINE_ERR("buf[0x14] = %x\n", buf[0x14]);
2699 }
2700
2701 if (hlpfile->version <= 16)
2702 {
2703 ref += GET_UINT(buf, 0xc);
2704 if (GET_UINT(buf, 0xc) == 0)
2705 break;
2706 }
2707 else
2708 ref = GET_UINT(buf, 0xc);
2709 } while (ref != 0xffffffff);
2710
2711 HLPFILE_GetKeywords(hlpfile);
2712 HLPFILE_GetMap(hlpfile);
2713 if (hlpfile->version <= 16) return TRUE;
2714 return HLPFILE_GetContext(hlpfile);
2715 }
2716
2717 /***********************************************************************
2718 *
2719 * HLPFILE_ReadHlpFile
2720 */
2721 HLPFILE *HLPFILE_ReadHlpFile(LPCSTR lpszPath)
2722 {
2723 HLPFILE* hlpfile;
2724
2725 for (hlpfile = first_hlpfile; hlpfile; hlpfile = hlpfile->next)
2726 {
2727 if (!strcmp(lpszPath, hlpfile->lpszPath))
2728 {
2729 hlpfile->wRefCount++;
2730 return hlpfile;
2731 }
2732 }
2733
2734 hlpfile = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2735 sizeof(HLPFILE) + strlen(lpszPath) + 1);
2736 if (!hlpfile) return 0;
2737
2738 hlpfile->lpszPath = (char*)hlpfile + sizeof(HLPFILE);
2739 hlpfile->contents_start = 0xFFFFFFFF;
2740 hlpfile->next = first_hlpfile;
2741 hlpfile->wRefCount = 1;
2742
2743 strcpy(hlpfile->lpszPath, lpszPath);
2744
2745 first_hlpfile = hlpfile;
2746 if (hlpfile->next) hlpfile->next->prev = hlpfile;
2747
2748 if (!HLPFILE_DoReadHlpFile(hlpfile, lpszPath))
2749 {
2750 HLPFILE_FreeHlpFile(hlpfile);
2751 hlpfile = 0;
2752 }
2753
2754 return hlpfile;
2755 }