ppu.pas 23 KB

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