ppu.pas 15 KB

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