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