hlpfile.c 89 KB

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