ppu.pas 15 KB

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