ppu.pas 23 KB

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