ppu.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. if change_endian then
  455. getword:=swap(w)
  456. else
  457. getword:=w;
  458. inc(entryidx,2);
  459. end;
  460. function tppufile.getlongint:longint;
  461. type
  462. plongint = ^longint;
  463. var
  464. l : longint;
  465. begin
  466. if entryidx+4>entry.size then
  467. begin
  468. error:=true;
  469. getlongint:=0;
  470. exit;
  471. end;
  472. readdata(l,4);
  473. if change_endian then
  474. getlongint:=swap(l shr 16) or (longint(swap(l and $ffff)) shl 16)
  475. else
  476. getlongint:=l;
  477. inc(entryidx,4);
  478. end;
  479. function tppufile.getreal:ppureal;
  480. type
  481. pppureal = ^ppureal;
  482. var
  483. d : ppureal;
  484. begin
  485. if entryidx+sizeof(ppureal)>entry.size then
  486. begin
  487. error:=true;
  488. getreal:=0;
  489. exit;
  490. end;
  491. readdata(d,sizeof(ppureal));
  492. getreal:=d;
  493. inc(entryidx,sizeof(ppureal));
  494. end;
  495. function tppufile.getstring:string;
  496. var
  497. s : string;
  498. begin
  499. s[0]:=chr(getbyte);
  500. if entryidx+length(s)>entry.size then
  501. begin
  502. error:=true;
  503. exit;
  504. end;
  505. ReadData(s[1],length(s));
  506. getstring:=s;
  507. inc(entryidx,length(s));
  508. end;
  509. function tppufile.skipuntilentry(untilb:byte):boolean;
  510. var
  511. b : byte;
  512. begin
  513. repeat
  514. b:=readentry;
  515. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  516. skipuntilentry:=(b=untilb);
  517. end;
  518. {*****************************************************************************
  519. TPPUFile Writing
  520. *****************************************************************************}
  521. function tppufile.create:boolean;
  522. begin
  523. create:=false;
  524. assign(f,fname);
  525. {$I-}
  526. rewrite(f,1);
  527. {$I+}
  528. if ioresult<>0 then
  529. exit;
  530. Mode:=2;
  531. {write header for sure}
  532. blockwrite(f,header,sizeof(tppuheader));
  533. bufsize:=ppubufsize;
  534. bufstart:=sizeof(tppuheader);
  535. bufidx:=0;
  536. {reset}
  537. crc:=$ffffffff;
  538. Error:=false;
  539. do_crc:=true;
  540. size:=0;
  541. entrytyp:=mainentryid;
  542. {start}
  543. NewEntry;
  544. create:=true;
  545. end;
  546. procedure tppufile.writeheader;
  547. var
  548. opos : longint;
  549. begin
  550. { flush buffer }
  551. writebuf;
  552. { update size (w/o header!) in the header }
  553. header.size:=bufstart-sizeof(tppuheader);
  554. { write header and restore filepos after it }
  555. opos:=filepos(f);
  556. seek(f,0);
  557. blockwrite(f,header,sizeof(tppuheader));
  558. seek(f,opos);
  559. end;
  560. procedure tppufile.writebuf;
  561. begin
  562. blockwrite(f,buf^,bufidx);
  563. inc(bufstart,bufidx);
  564. bufidx:=0;
  565. end;
  566. procedure tppufile.writedata(var b;len:longint);
  567. var
  568. p : pchar;
  569. left,
  570. idx : longint;
  571. begin
  572. p:=pchar(@b);
  573. idx:=0;
  574. while len>0 do
  575. begin
  576. left:=bufsize-bufidx;
  577. if len>left then
  578. begin
  579. move(p[idx],buf[bufidx],left);
  580. dec(len,left);
  581. inc(idx,left);
  582. inc(bufidx,left);
  583. writebuf;
  584. end
  585. else
  586. begin
  587. move(p[idx],buf[bufidx],len);
  588. inc(bufidx,len);
  589. exit;
  590. end;
  591. end;
  592. end;
  593. procedure tppufile.NewEntry;
  594. begin
  595. with entry do
  596. begin
  597. id:=entrytyp;
  598. nr:=ibend;
  599. size:=0;
  600. end;
  601. {Reset Entry State}
  602. entryidx:=0;
  603. entrybufstart:=bufstart;
  604. entrystart:=bufstart+bufidx;
  605. {Alloc in buffer}
  606. writedata(entry,sizeof(tppuentry));
  607. end;
  608. procedure tppufile.writeentry(ibnr:byte);
  609. var
  610. opos : longint;
  611. begin
  612. {create entry}
  613. entry.id:=entrytyp;
  614. entry.nr:=ibnr;
  615. entry.size:=entryidx;
  616. {it's already been sent to disk ?}
  617. if entrybufstart<>bufstart then
  618. begin
  619. {flush to be sure}
  620. WriteBuf;
  621. {write entry}
  622. opos:=filepos(f);
  623. seek(f,entrystart);
  624. blockwrite(f,entry,sizeof(tppuentry));
  625. seek(f,opos);
  626. entrybufstart:=bufstart;
  627. end
  628. else
  629. move(entry,buf[entrystart-bufstart],sizeof(entry));
  630. {Add New Entry, which is ibend by default}
  631. entrystart:=bufstart+bufidx; {next entry position}
  632. NewEntry;
  633. end;
  634. procedure tppufile.putdata(var b;len:longint);
  635. begin
  636. if do_crc then
  637. crc:=UpdateCrc32(crc,b,len);
  638. writedata(b,len);
  639. inc(entryidx,len);
  640. end;
  641. procedure tppufile.putbyte(b:byte);
  642. begin
  643. writedata(b,1);
  644. inc(entryidx);
  645. end;
  646. procedure tppufile.putword(w:word);
  647. begin
  648. if change_endian then
  649. w:=swap(w);
  650. putdata(w,2);
  651. end;
  652. procedure tppufile.putlongint(l:longint);
  653. begin
  654. if change_endian then
  655. l:=swap(l shr 16) or (longint(swap(l and $ffff)) shl 16);
  656. putdata(l,4);
  657. end;
  658. procedure tppufile.putreal(d:ppureal);
  659. begin
  660. putdata(d,sizeof(ppureal));
  661. end;
  662. procedure tppufile.putstring(s:string);
  663. begin
  664. putdata(s,length(s)+1);
  665. end;
  666. end.
  667. {
  668. $Log$
  669. Revision 1.10 1998-08-31 12:26:30 peter
  670. * m68k and palmos updates from surebugfixes
  671. Revision 1.9 1998/08/17 09:17:51 peter
  672. * static/shared linking updates
  673. Revision 1.8 1998/08/11 15:31:40 peter
  674. * write extended to ppu file
  675. * new version 0.99.7
  676. Revision 1.7 1998/06/25 10:51:01 pierre
  677. * removed a remaining ifndef NEWPPU
  678. replaced by ifdef OLDPPU
  679. * added uf_finalize to ppu unit
  680. Revision 1.6 1998/06/16 08:56:26 peter
  681. + targetcpu
  682. * cleaner pmodules for newppu
  683. Revision 1.5 1998/06/13 00:10:12 peter
  684. * working browser and newppu
  685. * some small fixes against crashes which occured in bp7 (but not in
  686. fpc?!)
  687. Revision 1.4 1998/06/09 16:01:48 pierre
  688. + added procedure directive parsing for procvars
  689. (accepted are popstack cdecl and pascal)
  690. + added C vars with the following syntax
  691. var C calias 'true_c_name';(can be followed by external)
  692. reason is that you must add the Cprefix
  693. which is target dependent
  694. Revision 1.3 1998/05/28 14:40:26 peter
  695. * fixes for newppu, remake3 works now with it
  696. Revision 1.2 1998/05/27 19:45:08 peter
  697. * symtable.pas splitted into includefiles
  698. * symtable adapted for $ifdef NEWPPU
  699. Revision 1.1 1998/05/12 10:56:07 peter
  700. + the ppufile object unit
  701. }