ppu.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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. unit ppu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype;
  23. { Also write the ppu if only crc if done, this can be used with ppudump to
  24. see the differences between the intf and implementation }
  25. { define INTFPPU}
  26. {$ifdef Test_Double_checksum}
  27. var
  28. CRCFile : text;
  29. const
  30. CRC_array_Size = 200000;
  31. type
  32. tcrc_array = array[0..crc_array_size] of longint;
  33. pcrc_array = ^tcrc_array;
  34. {$endif Test_Double_checksum}
  35. const
  36. CurrentPPUVersion=48;
  37. { buffer sizes }
  38. maxentrysize = 1024;
  39. ppubufsize = 16384;
  40. {ppu entries}
  41. mainentryid = 1;
  42. subentryid = 2;
  43. {special}
  44. iberror = 0;
  45. ibstartdefs = 248;
  46. ibenddefs = 249;
  47. ibstartsyms = 250;
  48. ibendsyms = 251;
  49. ibendinterface = 252;
  50. ibendimplementation = 253;
  51. ibendbrowser = 254;
  52. ibend = 255;
  53. {general}
  54. ibmodulename = 1;
  55. ibsourcefiles = 2;
  56. ibloadunit = 3;
  57. ibinitunit = 4;
  58. iblinkunitofiles = 5;
  59. iblinkunitstaticlibs = 6;
  60. iblinkunitsharedlibs = 7;
  61. iblinkotherofiles = 8;
  62. iblinkotherstaticlibs = 9;
  63. iblinkothersharedlibs = 10;
  64. ibdbxcount = 11;
  65. ibsymref = 12;
  66. ibdefref = 13;
  67. ibendsymtablebrowser = 14;
  68. ibbeginsymtablebrowser = 15;
  69. {$IFDEF MACRO_DIFF_HINT}
  70. ibusedmacros = 16;
  71. {$ENDIF}
  72. ibderefdata = 17;
  73. ibexportedmacros = 18;
  74. ibderefmap = 19;
  75. {syms}
  76. ibtypesym = 20;
  77. ibprocsym = 21;
  78. ibglobalvarsym = 22;
  79. ibconstsym = 23;
  80. ibenumsym = 24;
  81. ibtypedconstsym = 25;
  82. ibabsolutevarsym = 26;
  83. ibpropertysym = 27;
  84. ibfieldvarsym = 28;
  85. ibunitsym = 29; { needed for browser }
  86. iblabelsym = 30;
  87. ibsyssym = 31;
  88. ibrttisym = 32;
  89. iblocalvarsym = 33;
  90. ibparavarsym = 34;
  91. ibmacrosym = 35;
  92. {definitions}
  93. iborddef = 40;
  94. ibpointerdef = 41;
  95. ibarraydef = 42;
  96. ibprocdef = 43;
  97. ibshortstringdef = 44;
  98. ibrecorddef = 45;
  99. ibfiledef = 46;
  100. ibformaldef = 47;
  101. ibobjectdef = 48;
  102. ibenumdef = 49;
  103. ibsetdef = 50;
  104. ibprocvardef = 51;
  105. ibfloatdef = 52;
  106. ibclassrefdef = 53;
  107. iblongstringdef = 54;
  108. {$ifdef ansistring_bits}
  109. ibansistring16def = 58;
  110. ibansistring32def = 55;
  111. ibansistring64def = 59;
  112. {$else}
  113. ibansistringdef = 55;
  114. {$endif}
  115. ibwidestringdef = 56;
  116. ibvariantdef = 57;
  117. {implementation/objectdata}
  118. ibnodetree = 80;
  119. ibasmsymbols = 81;
  120. { unit flags }
  121. uf_init = $1;
  122. uf_finalize = $2;
  123. uf_big_endian = $4;
  124. uf_has_dbx = $8;
  125. uf_has_browser = $10;
  126. uf_in_library = $20; { is the file in another file than <ppufile>.* ? }
  127. uf_smart_linked = $40; { the ppu can be smartlinked }
  128. uf_static_linked = $80; { the ppu can be linked static }
  129. uf_shared_linked = $100; { the ppu can be linked shared }
  130. uf_local_browser = $200;
  131. uf_no_link = $400; { unit has no .o generated, but can still have
  132. external linking! }
  133. uf_has_resources = $800; { unit has resource section }
  134. uf_little_endian = $1000;
  135. uf_release = $2000; { unit was compiled with -Ur option }
  136. uf_threadvars = $4000; { unit has threadvars }
  137. uf_fpu_emulation = $8000; { this unit was compiled with fpu emulation on }
  138. uf_has_debuginfo = $10000; { this unit has debuginfo generated }
  139. uf_local_symtable = $20000; { this unit has a local symtable stored }
  140. uf_uses_variants = $40000; { this unit uses variants }
  141. type
  142. ppureal=extended;
  143. tppuerror=(ppuentrytoobig,ppuentryerror);
  144. tppuheader=record
  145. id : array[1..3] of char; { = 'PPU' }
  146. ver : array[1..3] of char;
  147. compiler : word;
  148. cpu : word;
  149. target : word;
  150. flags : longint;
  151. size : longint; { size of the ppufile without header }
  152. checksum : cardinal; { checksum for this ppufile }
  153. interface_checksum : cardinal;
  154. future : array[0..2] of longint;
  155. end;
  156. tppuentry=packed record
  157. size : longint;
  158. id : byte;
  159. nr : byte;
  160. end;
  161. tppufile=class
  162. private
  163. f : file;
  164. mode : byte; {0 - Closed, 1 - Reading, 2 - Writing}
  165. fname : string;
  166. fsize : integer;
  167. {$ifdef Test_Double_checksum}
  168. public
  169. crcindex,
  170. crc_index,
  171. crcindex2,
  172. crc_index2 : cardinal;
  173. crc_test,
  174. crc_test2 : pcrc_array;
  175. private
  176. {$endif def Test_Double_checksum}
  177. change_endian : boolean;
  178. buf : pchar;
  179. bufstart,
  180. bufsize,
  181. bufidx : integer;
  182. entrybufstart,
  183. entrystart,
  184. entryidx : integer;
  185. entry : tppuentry;
  186. closed,
  187. tempclosed : boolean;
  188. closepos : integer;
  189. public
  190. entrytyp : byte;
  191. header : tppuheader;
  192. size : integer;
  193. crc,
  194. interface_crc : cardinal;
  195. error,
  196. do_crc,
  197. do_interface_crc : boolean;
  198. crc_only : boolean; { used to calculate interface_crc before implementation }
  199. constructor Create(const fn:string);
  200. destructor Destroy;override;
  201. procedure flush;
  202. procedure closefile;
  203. function CheckPPUId:boolean;
  204. function GetPPUVersion:integer;
  205. procedure NewHeader;
  206. procedure NewEntry;
  207. {read}
  208. function openfile:boolean;
  209. procedure reloadbuf;
  210. procedure readdata(var b;len:integer);
  211. procedure skipdata(len:integer);
  212. function readentry:byte;
  213. function EndOfEntry:boolean;
  214. function entrysize:longint;
  215. procedure getdatabuf(var b;len:integer;var res:integer);
  216. procedure getdata(var b;len:integer);
  217. function getbyte:byte;
  218. function getword:word;
  219. function getlongint:longint;
  220. function getint64:int64;
  221. function getaint:aint;
  222. function getreal:ppureal;
  223. function getstring:string;
  224. procedure getnormalset(var b);
  225. procedure getsmallset(var b);
  226. function skipuntilentry(untilb:byte):boolean;
  227. {write}
  228. function createfile:boolean;
  229. procedure writeheader;
  230. procedure writebuf;
  231. procedure writedata(const b;len:integer);
  232. procedure writeentry(ibnr:byte);
  233. procedure putdata(const b;len:integer);
  234. procedure putbyte(b:byte);
  235. procedure putword(w:word);
  236. procedure putlongint(l:longint);
  237. procedure putint64(i:int64);
  238. procedure putaint(i:aint);
  239. procedure putreal(d:ppureal);
  240. procedure putstring(s:string);
  241. procedure putnormalset(const b);
  242. procedure putsmallset(const b);
  243. procedure tempclose;
  244. function tempopen:boolean;
  245. end;
  246. implementation
  247. uses
  248. {$ifdef Test_Double_checksum}
  249. comphook,
  250. {$endif def Test_Double_checksum}
  251. crc,
  252. cutils;
  253. {*****************************************************************************
  254. Endian Handling
  255. *****************************************************************************}
  256. Function SwapLong(x : longint): longint;
  257. var
  258. y : word;
  259. z : word;
  260. Begin
  261. y := x shr 16;
  262. y := word(longint(y) shl 8) or (y shr 8);
  263. z := x and $FFFF;
  264. z := word(longint(z) shl 8) or (z shr 8);
  265. SwapLong := (longint(z) shl 16) or longint(y);
  266. End;
  267. Function SwapWord(x : word): word;
  268. var
  269. z : byte;
  270. Begin
  271. z := x shr 8;
  272. x := x and $ff;
  273. x := word(x shl 8);
  274. SwapWord := x or z;
  275. End;
  276. {*****************************************************************************
  277. TPPUFile
  278. *****************************************************************************}
  279. constructor tppufile.Create(const fn:string);
  280. begin
  281. fname:=fn;
  282. change_endian:=false;
  283. crc_only:=false;
  284. Mode:=0;
  285. NewHeader;
  286. Error:=false;
  287. closed:=true;
  288. tempclosed:=false;
  289. getmem(buf,ppubufsize);
  290. end;
  291. destructor tppufile.destroy;
  292. begin
  293. closefile;
  294. if assigned(buf) then
  295. freemem(buf,ppubufsize);
  296. end;
  297. procedure tppufile.flush;
  298. begin
  299. if Mode=2 then
  300. writebuf;
  301. end;
  302. procedure tppufile.closefile;
  303. begin
  304. {$ifdef Test_Double_checksum}
  305. if mode=2 then
  306. begin
  307. if assigned(crc_test) then
  308. dispose(crc_test);
  309. if assigned(crc_test2) then
  310. dispose(crc_test2);
  311. end;
  312. {$endif Test_Double_checksum}
  313. if Mode<>0 then
  314. begin
  315. Flush;
  316. {$I-}
  317. system.close(f);
  318. {$I+}
  319. if ioresult<>0 then;
  320. Mode:=0;
  321. closed:=true;
  322. end;
  323. end;
  324. function tppufile.CheckPPUId:boolean;
  325. begin
  326. CheckPPUId:=((Header.Id[1]='P') and (Header.Id[2]='P') and (Header.Id[3]='U'));
  327. end;
  328. function tppufile.GetPPUVersion:integer;
  329. var
  330. l : integer;
  331. code : integer;
  332. begin
  333. Val(header.ver[1]+header.ver[2]+header.ver[3],l,code);
  334. if code=0 then
  335. GetPPUVersion:=l
  336. else
  337. GetPPUVersion:=0;
  338. end;
  339. procedure tppufile.NewHeader;
  340. var
  341. s : string;
  342. begin
  343. fillchar(header,sizeof(tppuheader),0);
  344. str(currentppuversion,s);
  345. while length(s)<3 do
  346. s:='0'+s;
  347. with header do
  348. begin
  349. Id[1]:='P';
  350. Id[2]:='P';
  351. Id[3]:='U';
  352. Ver[1]:=s[1];
  353. Ver[2]:=s[2];
  354. Ver[3]:=s[3];
  355. end;
  356. end;
  357. {*****************************************************************************
  358. TPPUFile Reading
  359. *****************************************************************************}
  360. function tppufile.openfile:boolean;
  361. var
  362. ofmode : byte;
  363. i : integer;
  364. begin
  365. openfile:=false;
  366. assign(f,fname);
  367. ofmode:=filemode;
  368. filemode:=$0;
  369. {$I-}
  370. reset(f,1);
  371. {$I+}
  372. filemode:=ofmode;
  373. if ioresult<>0 then
  374. exit;
  375. closed:=false;
  376. {read ppuheader}
  377. fsize:=filesize(f);
  378. if fsize<sizeof(tppuheader) then
  379. exit;
  380. blockread(f,header,sizeof(tppuheader),i);
  381. { The header is always stored in little endian order }
  382. { therefore swap if on a big endian machine }
  383. {$IFDEF ENDIAN_BIG}
  384. header.compiler := SwapWord(header.compiler);
  385. header.cpu := SwapWord(header.cpu);
  386. header.target := SwapWord(header.target);
  387. header.flags := SwapLong(header.flags);
  388. header.size := SwapLong(header.size);
  389. header.checksum := cardinal(SwapLong(longint(header.checksum)));
  390. header.interface_checksum := cardinal(SwapLong(longint(header.interface_checksum)));
  391. {$ENDIF}
  392. { the PPU DATA is stored in native order }
  393. if (header.flags and uf_big_endian) = uf_big_endian then
  394. Begin
  395. {$IFDEF ENDIAN_LITTLE}
  396. change_endian := TRUE;
  397. {$ELSE}
  398. change_endian := FALSE;
  399. {$ENDIF}
  400. End
  401. else if (header.flags and uf_little_endian) = uf_little_endian then
  402. Begin
  403. {$IFDEF ENDIAN_BIG}
  404. change_endian := TRUE;
  405. {$ELSE}
  406. change_endian := FALSE;
  407. {$ENDIF}
  408. End;
  409. {reset buffer}
  410. bufstart:=i;
  411. bufsize:=0;
  412. bufidx:=0;
  413. Mode:=1;
  414. FillChar(entry,sizeof(tppuentry),0);
  415. entryidx:=0;
  416. entrystart:=0;
  417. entrybufstart:=0;
  418. Error:=false;
  419. openfile:=true;
  420. end;
  421. procedure tppufile.reloadbuf;
  422. begin
  423. inc(bufstart,bufsize);
  424. blockread(f,buf^,ppubufsize,bufsize);
  425. bufidx:=0;
  426. end;
  427. procedure tppufile.readdata(var b;len:integer);
  428. var
  429. p : pchar;
  430. left,
  431. idx : integer;
  432. begin
  433. p:=pchar(@b);
  434. idx:=0;
  435. while len>0 do
  436. begin
  437. left:=bufsize-bufidx;
  438. if len>left then
  439. begin
  440. move(buf[bufidx],p[idx],left);
  441. dec(len,left);
  442. inc(idx,left);
  443. reloadbuf;
  444. if bufsize=0 then
  445. exit;
  446. end
  447. else
  448. begin
  449. move(buf[bufidx],p[idx],len);
  450. inc(bufidx,len);
  451. exit;
  452. end;
  453. end;
  454. end;
  455. procedure tppufile.skipdata(len:integer);
  456. var
  457. left : integer;
  458. begin
  459. while len>0 do
  460. begin
  461. left:=bufsize-bufidx;
  462. if len>left then
  463. begin
  464. dec(len,left);
  465. reloadbuf;
  466. if bufsize=0 then
  467. exit;
  468. end
  469. else
  470. begin
  471. inc(bufidx,len);
  472. exit;
  473. end;
  474. end;
  475. end;
  476. function tppufile.readentry:byte;
  477. begin
  478. if entryidx<entry.size then
  479. skipdata(entry.size-entryidx);
  480. readdata(entry,sizeof(tppuentry));
  481. if change_endian then
  482. entry.size:=swaplong(entry.size);
  483. entrystart:=bufstart+bufidx;
  484. entryidx:=0;
  485. if not(entry.id in [mainentryid,subentryid]) then
  486. begin
  487. readentry:=iberror;
  488. error:=true;
  489. exit;
  490. end;
  491. readentry:=entry.nr;
  492. end;
  493. function tppufile.endofentry:boolean;
  494. begin
  495. endofentry:=(entryidx>=entry.size);
  496. end;
  497. function tppufile.entrysize:longint;
  498. begin
  499. entrysize:=entry.size;
  500. end;
  501. procedure tppufile.getdatabuf(var b;len:integer;var res:integer);
  502. begin
  503. if entryidx+len>entry.size then
  504. res:=entry.size-entryidx
  505. else
  506. res:=len;
  507. readdata(b,res);
  508. inc(entryidx,res);
  509. end;
  510. procedure tppufile.getdata(var b;len:integer);
  511. begin
  512. if entryidx+len>entry.size then
  513. begin
  514. error:=true;
  515. exit;
  516. end;
  517. readdata(b,len);
  518. inc(entryidx,len);
  519. end;
  520. function tppufile.getbyte:byte;
  521. var
  522. b : byte;
  523. begin
  524. if entryidx+1>entry.size then
  525. begin
  526. error:=true;
  527. getbyte:=0;
  528. exit;
  529. end;
  530. readdata(b,1);
  531. getbyte:=b;
  532. inc(entryidx);
  533. end;
  534. function tppufile.getword:word;
  535. var
  536. w : word;
  537. begin
  538. if entryidx+2>entry.size then
  539. begin
  540. error:=true;
  541. getword:=0;
  542. exit;
  543. end;
  544. readdata(w,2);
  545. if change_endian then
  546. getword:=swapword(w)
  547. else
  548. getword:=w;
  549. inc(entryidx,2);
  550. end;
  551. function tppufile.getlongint:longint;
  552. var
  553. l : longint;
  554. begin
  555. if entryidx+4>entry.size then
  556. begin
  557. error:=true;
  558. getlongint:=0;
  559. exit;
  560. end;
  561. readdata(l,4);
  562. if change_endian then
  563. getlongint:=swaplong(l)
  564. else
  565. getlongint:=l;
  566. inc(entryidx,4);
  567. end;
  568. function tppufile.getint64:int64;
  569. var
  570. i : int64;
  571. begin
  572. if entryidx+8>entry.size then
  573. begin
  574. error:=true;
  575. result:=0;
  576. exit;
  577. end;
  578. readdata(i,8);
  579. if change_endian then
  580. result:=swapint64(i)
  581. else
  582. result:=i;
  583. inc(entryidx,8);
  584. end;
  585. function tppufile.getaint:aint;
  586. begin
  587. {$ifdef cpu64bit}
  588. result:=getint64;
  589. {$else cpu64bit}
  590. result:=getlongint;
  591. {$endif cpu64bit}
  592. end;
  593. function tppufile.getreal:ppureal;
  594. var
  595. d : ppureal;
  596. begin
  597. if entryidx+sizeof(ppureal)>entry.size then
  598. begin
  599. error:=true;
  600. getreal:=0;
  601. exit;
  602. end;
  603. readdata(d,sizeof(ppureal));
  604. getreal:=d;
  605. inc(entryidx,sizeof(ppureal));
  606. end;
  607. function tppufile.getstring:string;
  608. var
  609. s : string;
  610. begin
  611. s[0]:=chr(getbyte);
  612. if entryidx+length(s)>entry.size then
  613. begin
  614. error:=true;
  615. exit;
  616. end;
  617. ReadData(s[1],length(s));
  618. getstring:=s;
  619. inc(entryidx,length(s));
  620. end;
  621. procedure tppufile.getsmallset(var b);
  622. var
  623. l : longint;
  624. begin
  625. l:=getlongint;
  626. longint(b):=l;
  627. end;
  628. procedure tppufile.getnormalset(var b);
  629. type
  630. SetLongintArray = Array [0..7] of longint;
  631. var
  632. i : longint;
  633. begin
  634. if change_endian then
  635. begin
  636. for i:=0 to 7 do
  637. SetLongintArray(b)[i]:=getlongint;
  638. end
  639. else
  640. getdata(b,32);
  641. end;
  642. function tppufile.skipuntilentry(untilb:byte):boolean;
  643. var
  644. b : byte;
  645. begin
  646. repeat
  647. b:=readentry;
  648. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  649. skipuntilentry:=(b=untilb);
  650. end;
  651. {*****************************************************************************
  652. TPPUFile Writing
  653. *****************************************************************************}
  654. function tppufile.createfile:boolean;
  655. begin
  656. createfile:=false;
  657. {$ifdef INTFPPU}
  658. if crc_only then
  659. begin
  660. fname:=fname+'.intf';
  661. crc_only:=false;
  662. end;
  663. {$endif}
  664. if not crc_only then
  665. begin
  666. assign(f,fname);
  667. {$ifdef MACOS}
  668. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  669. SetDefaultMacOSCreator('FPas');
  670. SetDefaultMacOSFiletype('FPPU');
  671. {$endif}
  672. {$I-}
  673. rewrite(f,1);
  674. {$I+}
  675. {$ifdef MACOS}
  676. SetDefaultMacOSCreator('MPS ');
  677. SetDefaultMacOSFiletype('TEXT');
  678. {$endif}
  679. if ioresult<>0 then
  680. exit;
  681. Mode:=2;
  682. {write header for sure}
  683. blockwrite(f,header,sizeof(tppuheader));
  684. end;
  685. bufsize:=ppubufsize;
  686. bufstart:=sizeof(tppuheader);
  687. bufidx:=0;
  688. {reset}
  689. crc:=cardinal($ffffffff);
  690. interface_crc:=cardinal($ffffffff);
  691. do_interface_crc:=true;
  692. Error:=false;
  693. do_crc:=true;
  694. size:=0;
  695. entrytyp:=mainentryid;
  696. {start}
  697. NewEntry;
  698. createfile:=true;
  699. end;
  700. procedure tppufile.writeheader;
  701. var
  702. opos : integer;
  703. begin
  704. if crc_only then
  705. exit;
  706. { flush buffer }
  707. writebuf;
  708. { update size (w/o header!) in the header }
  709. header.size:=bufstart-sizeof(tppuheader);
  710. { set the endian flag }
  711. {$ifndef FPC_BIG_ENDIAN}
  712. header.flags := header.flags or uf_little_endian;
  713. {$else not FPC_BIG_ENDIAN}
  714. header.flags := header.flags or uf_big_endian;
  715. { Now swap the header in the correct endian (always little endian) }
  716. header.compiler := SwapWord(header.compiler);
  717. header.cpu := SwapWord(header.cpu);
  718. header.target := SwapWord(header.target);
  719. header.flags := SwapLong(header.flags);
  720. header.size := SwapLong(header.size);
  721. header.checksum := cardinal(SwapLong(longint(header.checksum)));
  722. header.interface_checksum := cardinal(SwapLong(longint(header.interface_checksum)));
  723. {$endif not FPC_BIG_ENDIAN}
  724. { write header and restore filepos after it }
  725. opos:=filepos(f);
  726. seek(f,0);
  727. blockwrite(f,header,sizeof(tppuheader));
  728. seek(f,opos);
  729. end;
  730. procedure tppufile.writebuf;
  731. begin
  732. if not crc_only then
  733. blockwrite(f,buf^,bufidx);
  734. inc(bufstart,bufidx);
  735. bufidx:=0;
  736. end;
  737. procedure tppufile.writedata(const b;len:integer);
  738. var
  739. p : pchar;
  740. left,
  741. idx : integer;
  742. begin
  743. if crc_only then
  744. exit;
  745. p:=pchar(@b);
  746. idx:=0;
  747. while len>0 do
  748. begin
  749. left:=bufsize-bufidx;
  750. if len>left then
  751. begin
  752. move(p[idx],buf[bufidx],left);
  753. dec(len,left);
  754. inc(idx,left);
  755. inc(bufidx,left);
  756. writebuf;
  757. end
  758. else
  759. begin
  760. move(p[idx],buf[bufidx],len);
  761. inc(bufidx,len);
  762. exit;
  763. end;
  764. end;
  765. end;
  766. procedure tppufile.NewEntry;
  767. begin
  768. with entry do
  769. begin
  770. id:=entrytyp;
  771. nr:=ibend;
  772. size:=0;
  773. end;
  774. {Reset Entry State}
  775. entryidx:=0;
  776. entrybufstart:=bufstart;
  777. entrystart:=bufstart+bufidx;
  778. {Alloc in buffer}
  779. writedata(entry,sizeof(tppuentry));
  780. end;
  781. procedure tppufile.writeentry(ibnr:byte);
  782. var
  783. opos : integer;
  784. begin
  785. {create entry}
  786. entry.id:=entrytyp;
  787. entry.nr:=ibnr;
  788. entry.size:=entryidx;
  789. {it's already been sent to disk ?}
  790. if entrybufstart<>bufstart then
  791. begin
  792. if not crc_only then
  793. begin
  794. {flush to be sure}
  795. WriteBuf;
  796. {write entry}
  797. opos:=filepos(f);
  798. seek(f,entrystart);
  799. blockwrite(f,entry,sizeof(tppuentry));
  800. seek(f,opos);
  801. end;
  802. entrybufstart:=bufstart;
  803. end
  804. else
  805. move(entry,buf[entrystart-bufstart],sizeof(entry));
  806. {Add New Entry, which is ibend by default}
  807. entrystart:=bufstart+bufidx; {next entry position}
  808. NewEntry;
  809. end;
  810. procedure tppufile.putdata(const b;len:integer);
  811. begin
  812. if do_crc then
  813. begin
  814. crc:=UpdateCrc32(crc,b,len);
  815. {$ifdef Test_Double_checksum}
  816. if crc_only then
  817. begin
  818. crc_test2^[crc_index2]:=crc;
  819. {$ifdef Test_Double_checksum_write}
  820. Writeln(CRCFile,crc);
  821. {$endif Test_Double_checksum_write}
  822. if crc_index2<crc_array_size then
  823. inc(crc_index2);
  824. end
  825. else
  826. begin
  827. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  828. (crc_test2^[crcindex2]<>crc) then
  829. Do_comment(V_Note,'impl CRC changed');
  830. {$ifdef Test_Double_checksum_write}
  831. Writeln(CRCFile,crc);
  832. {$endif Test_Double_checksum_write}
  833. inc(crcindex2);
  834. end;
  835. {$endif def Test_Double_checksum}
  836. if do_interface_crc then
  837. begin
  838. interface_crc:=UpdateCrc32(interface_crc,b,len);
  839. {$ifdef Test_Double_checksum}
  840. if crc_only then
  841. begin
  842. crc_test^[crc_index]:=interface_crc;
  843. {$ifdef Test_Double_checksum_write}
  844. Writeln(CRCFile,interface_crc);
  845. {$endif Test_Double_checksum_write}
  846. if crc_index<crc_array_size then
  847. inc(crc_index);
  848. end
  849. else
  850. begin
  851. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  852. (crc_test^[crcindex]<>interface_crc) then
  853. Do_comment(V_Warning,'CRC changed');
  854. {$ifdef Test_Double_checksum_write}
  855. Writeln(CRCFile,interface_crc);
  856. {$endif Test_Double_checksum_write}
  857. inc(crcindex);
  858. end;
  859. {$endif def Test_Double_checksum}
  860. end;
  861. end;
  862. if not crc_only then
  863. writedata(b,len);
  864. inc(entryidx,len);
  865. end;
  866. procedure tppufile.putbyte(b:byte);
  867. begin
  868. putdata(b,1);
  869. end;
  870. procedure tppufile.putword(w:word);
  871. begin
  872. putdata(w,2);
  873. end;
  874. procedure tppufile.putlongint(l:longint);
  875. begin
  876. putdata(l,4);
  877. end;
  878. procedure tppufile.putint64(i:int64);
  879. begin
  880. putdata(i,8);
  881. end;
  882. procedure tppufile.putaint(i:aint);
  883. begin
  884. putdata(i,sizeof(aint));
  885. end;
  886. procedure tppufile.putreal(d:ppureal);
  887. begin
  888. putdata(d,sizeof(ppureal));
  889. end;
  890. procedure tppufile.putstring(s:string);
  891. begin
  892. putdata(s,length(s)+1);
  893. end;
  894. procedure tppufile.putsmallset(const b);
  895. var
  896. l : longint;
  897. begin
  898. l:=longint(b);
  899. putlongint(l);
  900. end;
  901. procedure tppufile.putnormalset(const b);
  902. type
  903. SetLongintArray = Array [0..7] of longint;
  904. var
  905. i : longint;
  906. tempb : setlongintarray;
  907. begin
  908. if change_endian then
  909. begin
  910. for i:=0 to 7 do
  911. tempb[i]:=SwapLong(SetLongintArray(b)[i]);
  912. putdata(tempb,32);
  913. end
  914. else
  915. putdata(b,32);
  916. end;
  917. procedure tppufile.tempclose;
  918. begin
  919. if not closed then
  920. begin
  921. closepos:=filepos(f);
  922. {$I-}
  923. system.close(f);
  924. {$I+}
  925. if ioresult<>0 then;
  926. closed:=true;
  927. tempclosed:=true;
  928. end;
  929. end;
  930. function tppufile.tempopen:boolean;
  931. var
  932. ofm : byte;
  933. begin
  934. tempopen:=false;
  935. if not closed or not tempclosed then
  936. exit;
  937. ofm:=filemode;
  938. filemode:=0;
  939. {$I-}
  940. reset(f,1);
  941. {$I+}
  942. filemode:=ofm;
  943. if ioresult<>0 then
  944. exit;
  945. closed:=false;
  946. tempclosed:=false;
  947. { restore state }
  948. seek(f,closepos);
  949. tempopen:=true;
  950. end;
  951. end.
  952. {
  953. $Log$
  954. Revision 1.64 2005-02-14 17:13:07 peter
  955. * truncate log
  956. Revision 1.63 2005/01/24 17:46:18 olle
  957. + ppu files now has FPas as creator code on MacOS
  958. Revision 1.62 2005/01/19 22:19:41 peter
  959. * unit mapping rewrite
  960. * new derefmap added
  961. Revision 1.61 2005/01/09 20:24:43 olle
  962. * rework of macro subsystem
  963. + exportable macros for mode macpas
  964. }