ppu.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Routines to read/write ppu files
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef TP}
  19. {$N+,E+}
  20. {$endif}
  21. unit ppu;
  22. interface
  23. const
  24. { buffer sizes }
  25. maxentrysize = 1024;
  26. {$ifdef TP}
  27. ppubufsize = 1024;
  28. {$else}
  29. ppubufsize = 16384;
  30. {$endif}
  31. {ppu entries}
  32. mainentryid = 1;
  33. subentryid = 2;
  34. {special}
  35. iberror = 0;
  36. ibenddefs = 250;
  37. ibendsyms = 251;
  38. ibendinterface = 252;
  39. ibendimplementation = 253;
  40. ibendbrowser = 254;
  41. ibend = 255;
  42. {general}
  43. ibmodulename = 1;
  44. ibsourcefiles = 2;
  45. ibloadunit_int = 3;
  46. ibloadunit_imp = 4;
  47. ibinitunit = 5;
  48. iblinkofiles = 6;
  49. iblinksharedlibs = 7;
  50. iblinkstaticlibs = 8;
  51. ibdbxcount = 9;
  52. ibsymref = 10;
  53. ibdefref = 11;
  54. ibendsymtablebrowser = 12;
  55. ibbeginsymtablebrowser = 13;
  56. {syms}
  57. ibtypesym = 20;
  58. ibprocsym = 21;
  59. ibvarsym = 22;
  60. ibconstsym = 23;
  61. ibenumsym = 24;
  62. ibtypedconstsym = 25;
  63. ibabsolutesym = 26;
  64. ibpropertysym = 27;
  65. ibvarsym_C = 28;
  66. {defenitions}
  67. iborddef = 40;
  68. ibpointerdef = 41;
  69. ibarraydef = 42;
  70. ibprocdef = 43;
  71. ibstringdef = 44;
  72. ibrecorddef = 45;
  73. ibfiledef = 46;
  74. ibformaldef = 47;
  75. ibobjectdef = 48;
  76. ibenumdef = 49;
  77. ibsetdef = 50;
  78. ibprocvardef = 51;
  79. ibfloatdef = 52;
  80. ibclassrefdef = 53;
  81. iblongstringdef = 54;
  82. ibansistringdef = 55;
  83. ibwidestringdef = 56;
  84. { unit flags }
  85. uf_init = $1;
  86. uf_finalize = $2;
  87. uf_big_endian = $4;
  88. uf_has_dbx = $8;
  89. uf_has_browser = $10;
  90. uf_smartlink = $20;
  91. uf_in_library = $40; { is the file in another file than <ppufile>.* ? }
  92. uf_static_linked = $80;
  93. uf_shared_linked = $100;
  94. type
  95. {$ifdef m68k}
  96. ppureal=single;
  97. {$else}
  98. ppureal=extended;
  99. {$endif}
  100. type
  101. tppuerror=(ppuentrytoobig,ppuentryerror);
  102. tppuheader=packed record
  103. id : array[1..3] of char; { = 'PPU' }
  104. ver : array[1..3] of char;
  105. compiler : word;
  106. cpu : word;
  107. target : word;
  108. flags : longint;
  109. size : longint; { size of the ppufile without header }
  110. checksum : longint; { checksum for this ppufile }
  111. end;
  112. tppuentry=packed record
  113. id : byte;
  114. nr : byte;
  115. size : longint;
  116. end;
  117. pppufile=^tppufile;
  118. tppufile=object
  119. f : file;
  120. mode : byte; {0 - Closed, 1 - Reading, 2 - Writing}
  121. error : boolean;
  122. fname : string;
  123. fsize : longint;
  124. header : tppuheader;
  125. size,crc : longint;
  126. do_crc,
  127. change_endian : boolean;
  128. buf : pchar;
  129. bufstart,
  130. bufsize,
  131. bufidx : longint;
  132. entrybufstart,
  133. entrystart,
  134. entryidx : longint;
  135. entry : tppuentry;
  136. entrytyp : byte;
  137. constructor init(fn:string);
  138. destructor done;
  139. procedure flush;
  140. procedure close;
  141. function CheckPPUId:boolean;
  142. function GetPPUVersion:longint;
  143. procedure NewHeader;
  144. procedure NewEntry;
  145. {read}
  146. function open:boolean;
  147. procedure reloadbuf;
  148. procedure readdata(var b;len:longint);
  149. procedure skipdata(len:longint);
  150. function readentry:byte;
  151. function EndOfEntry:boolean;
  152. procedure getdatabuf(var b;len:longint;var result:longint);
  153. procedure getdata(var b;len:longint);
  154. function getbyte:byte;
  155. function getword:word;
  156. function getlongint:longint;
  157. function getreal:ppureal;
  158. function getstring:string;
  159. function skipuntilentry(untilb:byte):boolean;
  160. {write}
  161. function create:boolean;
  162. procedure writeheader;
  163. procedure writebuf;
  164. procedure writedata(var b;len:longint);
  165. procedure writeentry(ibnr:byte);
  166. procedure putdata(var b;len:longint);
  167. procedure putbyte(b:byte);
  168. procedure putword(w:word);
  169. procedure putlongint(l:longint);
  170. procedure putreal(d:ppureal);
  171. procedure putstring(s:string);
  172. end;
  173. implementation
  174. {*****************************************************************************
  175. Crc 32
  176. *****************************************************************************}
  177. var
  178. Crc32Tbl : array[0..255] of longint;
  179. procedure MakeCRC32Tbl;
  180. var
  181. crc : longint;
  182. i,n : byte;
  183. begin
  184. for i:=0 to 255 do
  185. begin
  186. crc:=i;
  187. for n:=1 to 8 do
  188. if odd(crc) then
  189. crc:=(crc shr 1) xor $edb88320
  190. else
  191. crc:=crc shr 1;
  192. Crc32Tbl[i]:=crc;
  193. end;
  194. end;
  195. {CRC 32}
  196. Function Crc32(Const HStr:String):longint;
  197. var
  198. i,InitCrc : longint;
  199. begin
  200. if Crc32Tbl[1]=0 then
  201. MakeCrc32Tbl;
  202. InitCrc:=$ffffffff;
  203. for i:=1to Length(Hstr) do
  204. InitCrc:=Crc32Tbl[byte(InitCrc) xor ord(Hstr[i])] xor (InitCrc shr 8);
  205. Crc32:=InitCrc;
  206. end;
  207. Function UpdateCrc32(InitCrc:longint;var InBuf;InLen:Longint):longint;
  208. var
  209. i : word;
  210. p : pchar;
  211. begin
  212. if Crc32Tbl[1]=0 then
  213. MakeCrc32Tbl;
  214. p:=@InBuf;
  215. for i:=1to InLen do
  216. begin
  217. InitCrc:=Crc32Tbl[byte(InitCrc) xor byte(p^)] xor (InitCrc shr 8);
  218. inc(longint(p));
  219. end;
  220. UpdateCrc32:=InitCrc;
  221. end;
  222. Function UpdCrc32(InitCrc:longint;b:byte):longint;
  223. begin
  224. if Crc32Tbl[1]=0 then
  225. MakeCrc32Tbl;
  226. UpdCrc32:=Crc32Tbl[byte(InitCrc) xor b] xor (InitCrc shr 8);
  227. end;
  228. {*****************************************************************************
  229. TPPUFile
  230. *****************************************************************************}
  231. constructor tppufile.init(fn:string);
  232. begin
  233. fname:=fn;
  234. change_endian:=false;
  235. Mode:=0;
  236. NewHeader;
  237. Error:=false;
  238. getmem(buf,ppubufsize);
  239. end;
  240. destructor tppufile.done;
  241. begin
  242. close;
  243. freemem(buf,ppubufsize);
  244. end;
  245. procedure tppufile.flush;
  246. begin
  247. if Mode=2 then
  248. writebuf;
  249. end;
  250. procedure tppufile.close;
  251. var
  252. i : word;
  253. begin
  254. if Mode<>0 then
  255. begin
  256. Flush;
  257. {$I-}
  258. system.close(f);
  259. {$I+}
  260. i:=ioresult;
  261. Mode:=0;
  262. end;
  263. end;
  264. function tppufile.CheckPPUId:boolean;
  265. begin
  266. CheckPPUId:=((Header.Id[1]='P') and (Header.Id[2]='P') and (Header.Id[3]='U'));
  267. end;
  268. function tppufile.GetPPUVersion:longint;
  269. var
  270. l : longint;
  271. code : word;
  272. begin
  273. Val(header.ver[1]+header.ver[2]+header.ver[3],l,code);
  274. if code=0 then
  275. GetPPUVersion:=l
  276. else
  277. GetPPUVersion:=0;
  278. end;
  279. procedure tppufile.NewHeader;
  280. begin
  281. fillchar(header,sizeof(tppuheader),0);
  282. with header do
  283. begin
  284. Id[1]:='P';
  285. Id[2]:='P';
  286. Id[3]:='U';
  287. Ver[1]:='0';
  288. Ver[2]:='1';
  289. Ver[3]:='5';
  290. end;
  291. end;
  292. {*****************************************************************************
  293. TPPUFile Reading
  294. *****************************************************************************}
  295. function tppufile.open:boolean;
  296. var
  297. ofmode : byte;
  298. i : word;
  299. begin
  300. open:=false;
  301. assign(f,fname);
  302. ofmode:=filemode;
  303. filemode:=$0;
  304. {$I-}
  305. reset(f,1);
  306. {$I+}
  307. filemode:=ofmode;
  308. if ioresult<>0 then
  309. exit;
  310. {read ppuheader}
  311. fsize:=filesize(f);
  312. if fsize<sizeof(tppuheader) then
  313. exit;
  314. blockread(f,header,sizeof(tppuheader),i);
  315. {reset buffer}
  316. bufstart:=i;
  317. bufsize:=0;
  318. bufidx:=0;
  319. Mode:=1;
  320. FillChar(entry,sizeof(tppuentry),0);
  321. entryidx:=0;
  322. entrystart:=0;
  323. entrybufstart:=0;
  324. Error:=false;
  325. open:=true;
  326. end;
  327. procedure tppufile.reloadbuf;
  328. {$ifdef TP}
  329. var
  330. i : word;
  331. {$endif}
  332. begin
  333. inc(bufstart,bufsize);
  334. {$ifdef TP}
  335. blockread(f,buf^,ppubufsize,i);
  336. bufsize:=i;
  337. {$else}
  338. blockread(f,buf^,ppubufsize,bufsize);
  339. {$endif}
  340. bufidx:=0;
  341. end;
  342. procedure tppufile.readdata(var b;len:longint);
  343. var
  344. p : pchar;
  345. left,
  346. idx : longint;
  347. begin
  348. p:=pchar(@b);
  349. idx:=0;
  350. while len>0 do
  351. begin
  352. left:=bufsize-bufidx;
  353. if len>left then
  354. begin
  355. move(buf[bufidx],p[idx],left);
  356. dec(len,left);
  357. inc(idx,left);
  358. reloadbuf;
  359. if bufsize=0 then
  360. exit;
  361. end
  362. else
  363. begin
  364. move(buf[bufidx],p[idx],len);
  365. inc(bufidx,len);
  366. exit;
  367. end;
  368. end;
  369. end;
  370. procedure tppufile.skipdata(len:longint);
  371. var
  372. left : longint;
  373. begin
  374. while len>0 do
  375. begin
  376. left:=bufsize-bufidx;
  377. if len>left then
  378. begin
  379. dec(len,left);
  380. reloadbuf;
  381. if bufsize=0 then
  382. exit;
  383. end
  384. else
  385. begin
  386. inc(bufidx,len);
  387. exit;
  388. end;
  389. end;
  390. end;
  391. function tppufile.readentry:byte;
  392. begin
  393. if entryidx<entry.size then
  394. skipdata(entry.size-entryidx);
  395. readdata(entry,sizeof(tppuentry));
  396. entrystart:=bufstart+bufidx;
  397. entryidx:=0;
  398. if not(entry.id in [mainentryid,subentryid]) then
  399. begin
  400. readentry:=iberror;
  401. error:=true;
  402. exit;
  403. end;
  404. readentry:=entry.nr;
  405. end;
  406. function tppufile.endofentry:boolean;
  407. begin
  408. endofentry:=(entryidx>=entry.size);
  409. end;
  410. procedure tppufile.getdatabuf(var b;len:longint;var result:longint);
  411. begin
  412. if entryidx+len>entry.size then
  413. result:=entry.size-entryidx
  414. else
  415. result:=len;
  416. readdata(b,result);
  417. inc(entryidx,result);
  418. end;
  419. procedure tppufile.getdata(var b;len:longint);
  420. begin
  421. if entryidx+len>entry.size then
  422. begin
  423. error:=true;
  424. exit;
  425. end;
  426. readdata(b,len);
  427. inc(entryidx,len);
  428. end;
  429. function tppufile.getbyte:byte;
  430. var
  431. b : byte;
  432. begin
  433. if entryidx+1>entry.size then
  434. begin
  435. error:=true;
  436. getbyte:=0;
  437. exit;
  438. end;
  439. readdata(b,1);
  440. getbyte:=b;
  441. inc(entryidx);
  442. end;
  443. function tppufile.getword:word;
  444. type
  445. pword = ^word;
  446. var
  447. w : word;
  448. begin
  449. if entryidx+2>entry.size then
  450. begin
  451. error:=true;
  452. getword:=0;
  453. exit;
  454. end;
  455. readdata(w,2);
  456. if change_endian then
  457. getword:=swap(w)
  458. else
  459. getword:=w;
  460. inc(entryidx,2);
  461. end;
  462. function tppufile.getlongint:longint;
  463. type
  464. plongint = ^longint;
  465. var
  466. l : longint;
  467. begin
  468. if entryidx+4>entry.size then
  469. begin
  470. error:=true;
  471. getlongint:=0;
  472. exit;
  473. end;
  474. readdata(l,4);
  475. if change_endian then
  476. getlongint:=swap(l shr 16) or (longint(swap(l and $ffff)) shl 16)
  477. else
  478. getlongint:=l;
  479. inc(entryidx,4);
  480. end;
  481. function tppufile.getreal:ppureal;
  482. type
  483. pppureal = ^ppureal;
  484. var
  485. d : ppureal;
  486. begin
  487. if entryidx+sizeof(ppureal)>entry.size then
  488. begin
  489. error:=true;
  490. getreal:=0;
  491. exit;
  492. end;
  493. readdata(d,sizeof(ppureal));
  494. getreal:=d;
  495. inc(entryidx,sizeof(ppureal));
  496. end;
  497. function tppufile.getstring:string;
  498. var
  499. s : string;
  500. begin
  501. s[0]:=chr(getbyte);
  502. if entryidx+length(s)>entry.size then
  503. begin
  504. error:=true;
  505. exit;
  506. end;
  507. ReadData(s[1],length(s));
  508. getstring:=s;
  509. inc(entryidx,length(s));
  510. end;
  511. function tppufile.skipuntilentry(untilb:byte):boolean;
  512. var
  513. b : byte;
  514. begin
  515. repeat
  516. b:=readentry;
  517. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  518. skipuntilentry:=(b=untilb);
  519. end;
  520. {*****************************************************************************
  521. TPPUFile Writing
  522. *****************************************************************************}
  523. function tppufile.create:boolean;
  524. begin
  525. create:=false;
  526. assign(f,fname);
  527. {$I-}
  528. rewrite(f,1);
  529. {$I+}
  530. if ioresult<>0 then
  531. exit;
  532. Mode:=2;
  533. {write header for sure}
  534. blockwrite(f,header,sizeof(tppuheader));
  535. bufsize:=ppubufsize;
  536. bufstart:=sizeof(tppuheader);
  537. bufidx:=0;
  538. {reset}
  539. crc:=$ffffffff;
  540. Error:=false;
  541. do_crc:=true;
  542. size:=0;
  543. entrytyp:=mainentryid;
  544. {start}
  545. NewEntry;
  546. create:=true;
  547. end;
  548. procedure tppufile.writeheader;
  549. var
  550. opos : longint;
  551. begin
  552. { flush buffer }
  553. writebuf;
  554. { update size (w/o header!) in the header }
  555. header.size:=bufstart-sizeof(tppuheader);
  556. { write header and restore filepos after it }
  557. opos:=filepos(f);
  558. seek(f,0);
  559. blockwrite(f,header,sizeof(tppuheader));
  560. seek(f,opos);
  561. end;
  562. procedure tppufile.writebuf;
  563. begin
  564. blockwrite(f,buf^,bufidx);
  565. inc(bufstart,bufidx);
  566. bufidx:=0;
  567. end;
  568. procedure tppufile.writedata(var b;len:longint);
  569. var
  570. p : pchar;
  571. left,
  572. idx : longint;
  573. begin
  574. p:=pchar(@b);
  575. idx:=0;
  576. while len>0 do
  577. begin
  578. left:=bufsize-bufidx;
  579. if len>left then
  580. begin
  581. move(p[idx],buf[bufidx],left);
  582. dec(len,left);
  583. inc(idx,left);
  584. inc(bufidx,left);
  585. writebuf;
  586. end
  587. else
  588. begin
  589. move(p[idx],buf[bufidx],len);
  590. inc(bufidx,len);
  591. exit;
  592. end;
  593. end;
  594. end;
  595. procedure tppufile.NewEntry;
  596. begin
  597. with entry do
  598. begin
  599. id:=entrytyp;
  600. nr:=ibend;
  601. size:=0;
  602. end;
  603. {Reset Entry State}
  604. entryidx:=0;
  605. entrybufstart:=bufstart;
  606. entrystart:=bufstart+bufidx;
  607. {Alloc in buffer}
  608. writedata(entry,sizeof(tppuentry));
  609. end;
  610. procedure tppufile.writeentry(ibnr:byte);
  611. var
  612. opos : longint;
  613. begin
  614. {create entry}
  615. entry.id:=entrytyp;
  616. entry.nr:=ibnr;
  617. entry.size:=entryidx;
  618. {it's already been sent to disk ?}
  619. if entrybufstart<>bufstart then
  620. begin
  621. {flush to be sure}
  622. WriteBuf;
  623. {write entry}
  624. opos:=filepos(f);
  625. seek(f,entrystart);
  626. blockwrite(f,entry,sizeof(tppuentry));
  627. seek(f,opos);
  628. entrybufstart:=bufstart;
  629. end
  630. else
  631. move(entry,buf[entrystart-bufstart],sizeof(entry));
  632. {Add New Entry, which is ibend by default}
  633. entrystart:=bufstart+bufidx; {next entry position}
  634. NewEntry;
  635. end;
  636. procedure tppufile.putdata(var b;len:longint);
  637. begin
  638. if do_crc then
  639. crc:=UpdateCrc32(crc,b,len);
  640. writedata(b,len);
  641. inc(entryidx,len);
  642. end;
  643. procedure tppufile.putbyte(b:byte);
  644. begin
  645. writedata(b,1);
  646. inc(entryidx);
  647. end;
  648. procedure tppufile.putword(w:word);
  649. begin
  650. if change_endian then
  651. w:=swap(w);
  652. putdata(w,2);
  653. end;
  654. procedure tppufile.putlongint(l:longint);
  655. begin
  656. if change_endian then
  657. l:=swap(l shr 16) or (longint(swap(l and $ffff)) shl 16);
  658. putdata(l,4);
  659. end;
  660. procedure tppufile.putreal(d:ppureal);
  661. begin
  662. putdata(d,sizeof(ppureal));
  663. end;
  664. procedure tppufile.putstring(s:string);
  665. begin
  666. putdata(s,length(s)+1);
  667. end;
  668. end.
  669. {
  670. $Log$
  671. Revision 1.12 1998-09-18 08:01:37 pierre
  672. + improvement on the usebrowser part
  673. (does not work correctly for now)
  674. Revision 1.11 1998/09/11 15:16:47 peter
  675. * merge fixes
  676. Revision 1.10.2.1 1998/09/11 15:15:04 peter
  677. * fixed not in [] bug
  678. Revision 1.10 1998/08/31 12:26:30 peter
  679. * m68k and palmos updates from surebugfixes
  680. Revision 1.9 1998/08/17 09:17:51 peter
  681. * static/shared linking updates
  682. Revision 1.8 1998/08/11 15:31:40 peter
  683. * write extended to ppu file
  684. * new version 0.99.7
  685. Revision 1.7 1998/06/25 10:51:01 pierre
  686. * removed a remaining ifndef NEWPPU
  687. replaced by ifdef OLDPPU
  688. * added uf_finalize to ppu unit
  689. Revision 1.6 1998/06/16 08:56:26 peter
  690. + targetcpu
  691. * cleaner pmodules for newppu
  692. Revision 1.5 1998/06/13 00:10:12 peter
  693. * working browser and newppu
  694. * some small fixes against crashes which occured in bp7 (but not in
  695. fpc?!)
  696. Revision 1.4 1998/06/09 16:01:48 pierre
  697. + added procedure directive parsing for procvars
  698. (accepted are popstack cdecl and pascal)
  699. + added C vars with the following syntax
  700. var C calias 'true_c_name';(can be followed by external)
  701. reason is that you must add the Cprefix
  702. which is target dependent
  703. Revision 1.3 1998/05/28 14:40:26 peter
  704. * fixes for newppu, remake3 works now with it
  705. Revision 1.2 1998/05/27 19:45:08 peter
  706. * symtable.pas splitted into includefiles
  707. * symtable adapted for $ifdef NEWPPU
  708. Revision 1.1 1998/05/12 10:56:07 peter
  709. + the ppufile object unit
  710. }