UNMANGLE.C 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* UNMANGLE 13/11/94 for WATCOM C
  2. * Assumes Intel data in mangled file (no method set in mangle)
  3. * Converted by P.Hiley.
  4. * Modified by Kevin Dudley.
  5. */
  6. #include <conio.h>
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "unmangle.h"
  12. //************************************************************************************************
  13. //
  14. // UnMangle from Memory to Memory
  15. //
  16. // unsigned char *source - pointer to start of unmangled data (not the header)
  17. // unsigned char *dest - pointer to area of memory for destination (make sure it is big enough)
  18. //
  19. // returns - length of data
  20. //
  21. //************************************************************************************************
  22. extern long int UnMangleMemToMem(unsigned char *source, unsigned char *dest)
  23. {
  24. int c;
  25. int u,v;
  26. unsigned char *pt;
  27. unsigned char *start=dest;
  28. while ((c=*source++)!=0)
  29. {
  30. if (c & 128)
  31. {
  32. // Bit 7 : 1 : BLOCK
  33. if (c & 64)
  34. {
  35. // Bit 6 : 1 Block size
  36. if (c & 32)
  37. {
  38. // Bit 5 : 1 Long
  39. pt= dest-((c & 31)<<8) -*source++ -3; // offset
  40. c= *source++ + 5; // size
  41. while (c--)
  42. *dest++=*pt++;
  43. }
  44. else
  45. {
  46. // Bit 5 : 0 Medium
  47. pt= dest-((c & 3)<<8) -*source++ -3; // offset
  48. c= ((c>>2) & 7) + 4; // size
  49. while (c--)
  50. *dest++=*pt++;
  51. }
  52. }
  53. else
  54. {
  55. // Bit 6 : 0 : Short
  56. pt= dest-(c & 63) -3;
  57. *dest = *pt;
  58. *(dest+1)= *(pt+1);
  59. *(dest+2)= *(pt+2);
  60. dest+=3;
  61. }
  62. }
  63. else
  64. {
  65. // Bit 7 : 0
  66. if (c & 64)
  67. {
  68. // Bit 6 : 1 Seq/Diff or String
  69. if (c & 32)
  70. {
  71. // Bit 5 : 1 : Sequence
  72. if (c & 16)
  73. {
  74. // Bit 4 : 1 : Word sequence
  75. c= (c & 15) + 2; // bits 3-0 = len 2->17
  76. v= *(short *)(dest-2);
  77. while (c--)
  78. {
  79. *(short *)dest=(short)v;
  80. dest+=2;
  81. }
  82. }
  83. else
  84. {
  85. // Bit 4 : 0 : Byte sequence
  86. c= (c & 15) + 3; // bits 3-0 = len 3->18
  87. v= *(dest-1);
  88. while (c--)
  89. *dest++=(signed char) v;
  90. }
  91. }
  92. else
  93. {
  94. // Bit 5 : 0 : Difference
  95. if (c & 16)
  96. {
  97. // Bit 4 : 1 : Word difference
  98. c= (c & 15) + 2; // bits 3-0 = len 2->17
  99. u= *(short *)(dest-2); // start word
  100. v= u -*(short *)(dest-4); // dif
  101. while (c--)
  102. {
  103. u+=v;
  104. *(short *)dest=(short)u;
  105. dest+=2;
  106. }
  107. }
  108. else
  109. {
  110. // Bit 4 : 0 : Byte difference
  111. c= (c & 15) + 3; // bits 3-0 = len 3->18
  112. u= *(dest-1); // start byte
  113. v= u - *(dest-2); // dif
  114. while (c--)
  115. {
  116. u+=v;
  117. *dest++=(char)u;
  118. }
  119. }
  120. }
  121. }
  122. else
  123. {
  124. // Bit 6 : 0 : String
  125. c &= 63; // len
  126. memcpy(dest,source,c);
  127. dest+=c;
  128. source+=c;
  129. }
  130. }
  131. }
  132. return (dest-start);
  133. }
  134. //************************************************************************************************
  135. //
  136. // UnMangle from File to Memory
  137. //
  138. // *srcfile - pointer to the source filename (make sure there is no header)
  139. // *dest - pointer to area of memory for destination (make sure it is big enough)
  140. //
  141. // returns - Negative on error
  142. //
  143. //************************************************************************************************
  144. unsigned char *unmanglebuf;
  145. unsigned char *unmangleinbuf;
  146. unsigned char *unmangledst; // unmangle output address into it's buffer
  147. unsigned long unmangleinpoff; // offset position reading from mangled file
  148. unsigned long unmanglebufpos; // offset position of input buffer in mangled file
  149. FILE *unmanglefile;
  150. extern long int UnMangleFileToMem(char *srcfile,unsigned char *dest)
  151. {
  152. int c;
  153. int u,v;
  154. unsigned char *pt;
  155. // Initialise the unmangle
  156. unmanglebuf=(unsigned char *)malloc(LOOKBACK+BLOCKSIZE+260);
  157. unmangleinbuf=(unsigned char *)malloc(INPUTBLOCK);
  158. unmanglefile=fopen(srcfile,"rb");
  159. unmangledst=dest;
  160. unmangleinpoff=0;
  161. unmanglebufpos=0;
  162. fseek(unmanglefile,unmanglebufpos,SEEK_SET);
  163. fread(unmangleinbuf,1,INPUTBLOCK,unmanglefile);
  164. // Do the unmangle
  165. for(;;)
  166. {
  167. c=*unmangleGet(unmangleinpoff++,1);
  168. if (!c) break; // done
  169. if (c & 128)
  170. {
  171. // Bit 7 : 1 : BLOCK
  172. if (c & 64)
  173. {
  174. // Bit 6 : 1 Block size
  175. if (c & 32)
  176. {
  177. // Bit 5 : 1 Long
  178. pt= unmangledst-((c & 31)<<8) -*unmangleGet(unmangleinpoff++,1) -3; // offset
  179. c= *unmangleGet(unmangleinpoff++,1) + 5; // size
  180. while (c--)
  181. *unmangledst++=*pt++;
  182. }
  183. else
  184. {
  185. // Bit 5 : 0 Medium
  186. pt= unmangledst-((c & 3)<<8) -*unmangleGet(unmangleinpoff++,1) -3; // offset
  187. c= ((c>>2) & 7) + 4; // size
  188. while (c--)
  189. *unmangledst++=*pt++;
  190. }
  191. }
  192. else
  193. {
  194. // Bit 6 : 0 : Short
  195. pt= unmangledst-(c & 63) -3;
  196. *unmangledst = *pt;
  197. *(unmangledst+1)= *(pt+1);
  198. *(unmangledst+2)= *(pt+2);
  199. unmangledst+=3;
  200. }
  201. }
  202. else
  203. {
  204. // Bit 7 : 0
  205. if (c & 64)
  206. {
  207. // Bit 6 : 1 Seq/Diff or String
  208. if (c & 32)
  209. {
  210. // Bit 5 : 1 : Sequence
  211. if (c & 16)
  212. {
  213. // Bit 4 : 1 : Word sequence
  214. c= (c & 15) + 2; // bits 3-0 = len 2->17
  215. v= *(short *)(unmangledst-2);
  216. while (c--)
  217. {
  218. *(short *)unmangledst=(short)v;
  219. unmangledst+=2;
  220. }
  221. }
  222. else
  223. {
  224. // Bit 4 : 0 : Byte sequence
  225. c= (c & 15) + 3; // bits 3-0 = len 3->18
  226. v= *(unmangledst-1);
  227. while (c--)
  228. *unmangledst++=(signed char) v;
  229. }
  230. }
  231. else
  232. {
  233. // Bit 5 : 0 : Difference
  234. if (c & 16)
  235. {
  236. // Bit 4 : 1 : Word difference
  237. c= (c & 15) + 2; // bits 3-0 = len 2->17
  238. u= *(short *)(unmangledst-2); // start word
  239. v= u -*(short *)(unmangledst-4); // dif
  240. while (c--)
  241. {
  242. u+=v;
  243. *(short *)unmangledst=(short)u;
  244. unmangledst+=2;
  245. }
  246. }
  247. else
  248. {
  249. // Bit 4 : 0 : Byte difference
  250. c= (c & 15) + 3; // bits 3-0 = len 3->18
  251. u= *(unmangledst-1); // start byte
  252. v= u - *(unmangledst-2); // dif
  253. while (c--)
  254. {
  255. u+=v;
  256. *unmangledst++=(char)u;
  257. }
  258. }
  259. }
  260. }
  261. else
  262. {
  263. // Bit 6 : 0 : String
  264. c &= 63; // len
  265. memcpy(unmangledst,unmangleGet(unmangleinpoff,c),c);
  266. unmangledst+=c;
  267. unmangleinpoff+=c;
  268. }
  269. }
  270. }
  271. fclose(unmanglefile);
  272. free(unmanglebuf);
  273. free(unmangleinbuf);
  274. return(EZERO);
  275. }
  276. //************************************************************************************************
  277. //
  278. // UnMangle from file to another file
  279. //
  280. // *srcfile - pointer to the source filename (make sure there is no header)
  281. // *destfile - name of destfile
  282. //
  283. // returns - currently always zero
  284. //
  285. //************************************************************************************************
  286. extern long int UnMangleFileToFile(char *srcfile,char *destfile)
  287. {
  288. int c;
  289. int u,v;
  290. int len;
  291. unsigned char *pt;
  292. FILE *outfile;
  293. // Initialise the unmangle
  294. unmanglebuf=(unsigned char *)malloc(LOOKBACK+BLOCKSIZE+260);
  295. unmangleinbuf=(unsigned char *)malloc(INPUTBLOCK);
  296. unmanglefile=fopen(srcfile,"rb");
  297. outfile=fopen(destfile,"wb");
  298. unmangledst=unmanglebuf+LOOKBACK;
  299. unmangleinpoff=0;
  300. unmanglebufpos=0;
  301. fseek(unmanglefile,unmanglebufpos,SEEK_SET);
  302. fread(unmangleinbuf,1,INPUTBLOCK,unmanglefile);
  303. // Do the unmangle
  304. for(;;)
  305. {
  306. c=*unmangleGet(unmangleinpoff++,1);
  307. if (!c) break; // done
  308. len=unmangledst-(unmanglebuf+LOOKBACK);
  309. if (len>BLOCKSIZE)
  310. {
  311. // output this block
  312. fwrite(unmanglebuf+LOOKBACK,1,len,outfile);
  313. // copy back to keep just the LOOKBACK amount
  314. memcpy(unmanglebuf,unmangledst-LOOKBACK,LOOKBACK);
  315. unmangledst=unmanglebuf+LOOKBACK;
  316. }
  317. if (c & 128)
  318. {
  319. // Bit 7 : 1 : BLOCK
  320. if (c & 64)
  321. {
  322. // Bit 6 : 1 Block size
  323. if (c & 32)
  324. {
  325. // Bit 5 : 1 Long
  326. pt= unmangledst-((c & 31)<<8) -*unmangleGet(unmangleinpoff++,1) -3; // offset
  327. c= *unmangleGet(unmangleinpoff++,1) + 5; // size
  328. while (c--)
  329. *unmangledst++=*pt++;
  330. }
  331. else
  332. {
  333. // Bit 5 : 0 Medium
  334. pt= unmangledst-((c & 3)<<8) -*unmangleGet(unmangleinpoff++,1) -3; // offset
  335. c= ((c>>2) & 7) + 4; // size
  336. while (c--)
  337. *unmangledst++=*pt++;
  338. }
  339. }
  340. else
  341. {
  342. // Bit 6 : 0 : Short
  343. pt= unmangledst-(c & 63) -3;
  344. *unmangledst = *pt;
  345. *(unmangledst+1)= *(pt+1);
  346. *(unmangledst+2)= *(pt+2);
  347. unmangledst+=3;
  348. }
  349. }
  350. else
  351. {
  352. // Bit 7 : 0
  353. if (c & 64)
  354. {
  355. // Bit 6 : 1 Seq/Diff or String
  356. if (c & 32)
  357. {
  358. // Bit 5 : 1 : Sequence
  359. if (c & 16)
  360. {
  361. // Bit 4 : 1 : Word sequence
  362. c= (c & 15) + 2; // bits 3-0 = len 2->17
  363. v= *(short *)(unmangledst-2);
  364. while (c--)
  365. {
  366. *(short *)unmangledst=(short)v;
  367. unmangledst+=2;
  368. }
  369. }
  370. else
  371. {
  372. // Bit 4 : 0 : Byte sequence
  373. c= (c & 15) + 3; // bits 3-0 = len 3->18
  374. v= *(unmangledst-1);
  375. while (c--)
  376. *unmangledst++=(signed char) v;
  377. }
  378. }
  379. else
  380. {
  381. // Bit 5 : 0 : Difference
  382. if (c & 16)
  383. {
  384. // Bit 4 : 1 : Word difference
  385. c= (c & 15) + 2; // bits 3-0 = len 2->17
  386. u= *(short *)(unmangledst-2); // start word
  387. v= u -*(short *)(unmangledst-4); // dif
  388. while (c--)
  389. {
  390. u+=v;
  391. *(short *)unmangledst=(short)u;
  392. unmangledst+=2;
  393. }
  394. }
  395. else
  396. {
  397. // Bit 4 : 0 : Byte difference
  398. c= (c & 15) + 3; // bits 3-0 = len 3->18
  399. u= *(unmangledst-1); // start byte
  400. v= u - *(unmangledst-2); // dif
  401. while (c--)
  402. {
  403. u+=v;
  404. *unmangledst++=(char)u;
  405. }
  406. }
  407. }
  408. }
  409. else
  410. {
  411. // Bit 6 : 0 : String
  412. c &= 63; // len
  413. memcpy(unmangledst,unmangleGet(unmangleinpoff,c),c);
  414. unmangledst+=c;
  415. unmangleinpoff+=c;
  416. }
  417. }
  418. }
  419. // output any remaining stuff
  420. len=unmangledst-(unmanglebuf+LOOKBACK);
  421. if (len>0)
  422. fwrite(unmanglebuf+LOOKBACK,1,len,outfile);
  423. fclose(unmanglefile);
  424. free(unmanglebuf);
  425. free(unmangleinbuf);
  426. return(EZERO);
  427. }
  428. //************************************************************************************************
  429. unsigned char *
  430. unmangleGet(unsigned long offset,unsigned long size)
  431. {
  432. // Returns a pointer to the mangled data at 'offset' in the mangled file
  433. // 'size' bytes must be available
  434. if (offset<unmanglebufpos || size+offset>unmanglebufpos+INPUTBLOCK)
  435. {
  436. unmanglebufpos=offset;
  437. fseek(unmanglefile,unmanglebufpos,SEEK_SET);
  438. fread(unmangleinbuf,1,INPUTBLOCK,unmanglefile);
  439. }
  440. return(unmangleinbuf+offset-unmanglebufpos);
  441. }