ppu.pas 15 KB

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