ppu.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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=81;
  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. l : longint;
  637. begin
  638. l:=getlongint;
  639. longint(b):=l;
  640. end;
  641. procedure tppufile.getnormalset(var b);
  642. type
  643. SetLongintArray = Array [0..7] of longint;
  644. var
  645. i : longint;
  646. begin
  647. if change_endian then
  648. for i:=0 to 7 do
  649. SetLongintArray(b)[i]:=getlongint
  650. else
  651. getdata(b,32);
  652. end;
  653. function tppufile.skipuntilentry(untilb:byte):boolean;
  654. var
  655. b : byte;
  656. begin
  657. repeat
  658. b:=readentry;
  659. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  660. skipuntilentry:=(b=untilb);
  661. end;
  662. {*****************************************************************************
  663. TPPUFile Writing
  664. *****************************************************************************}
  665. function tppufile.createfile:boolean;
  666. begin
  667. createfile:=false;
  668. {$ifdef INTFPPU}
  669. if crc_only then
  670. begin
  671. fname:=fname+'.intf';
  672. crc_only:=false;
  673. end;
  674. {$endif}
  675. if not crc_only then
  676. begin
  677. assign(f,fname);
  678. {$ifdef MACOS}
  679. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  680. SetDefaultMacOSCreator('FPas');
  681. SetDefaultMacOSFiletype('FPPU');
  682. {$endif}
  683. {$I-}
  684. rewrite(f,1);
  685. {$I+}
  686. {$ifdef MACOS}
  687. SetDefaultMacOSCreator('MPS ');
  688. SetDefaultMacOSFiletype('TEXT');
  689. {$endif}
  690. if ioresult<>0 then
  691. exit;
  692. Mode:=2;
  693. {write header for sure}
  694. blockwrite(f,header,sizeof(tppuheader));
  695. end;
  696. bufsize:=ppubufsize;
  697. bufstart:=sizeof(tppuheader);
  698. bufidx:=0;
  699. {reset}
  700. crc:=cardinal($ffffffff);
  701. interface_crc:=cardinal($ffffffff);
  702. do_interface_crc:=true;
  703. Error:=false;
  704. do_crc:=true;
  705. size:=0;
  706. entrytyp:=mainentryid;
  707. {start}
  708. NewEntry;
  709. createfile:=true;
  710. end;
  711. procedure tppufile.writeheader;
  712. var
  713. opos : integer;
  714. begin
  715. if crc_only then
  716. exit;
  717. { flush buffer }
  718. writebuf;
  719. { update size (w/o header!) in the header }
  720. header.size:=bufstart-sizeof(tppuheader);
  721. { set the endian flag }
  722. {$ifndef FPC_BIG_ENDIAN}
  723. header.flags := header.flags or uf_little_endian;
  724. {$else not FPC_BIG_ENDIAN}
  725. header.flags := header.flags or uf_big_endian;
  726. { Now swap the header in the correct endian (always little endian) }
  727. header.compiler := swapendian(header.compiler);
  728. header.cpu := swapendian(header.cpu);
  729. header.target := swapendian(header.target);
  730. header.flags := swapendian(header.flags);
  731. header.size := swapendian(header.size);
  732. header.checksum := swapendian(header.checksum);
  733. header.interface_checksum := swapendian(header.interface_checksum);
  734. {$endif not FPC_BIG_ENDIAN}
  735. { write header and restore filepos after it }
  736. opos:=filepos(f);
  737. seek(f,0);
  738. blockwrite(f,header,sizeof(tppuheader));
  739. seek(f,opos);
  740. end;
  741. procedure tppufile.writebuf;
  742. begin
  743. if not crc_only and
  744. (bufidx <> 0) then
  745. blockwrite(f,buf^,bufidx);
  746. inc(bufstart,bufidx);
  747. bufidx:=0;
  748. end;
  749. procedure tppufile.writedata(const b;len:integer);
  750. var
  751. p : pchar;
  752. left,
  753. idx : integer;
  754. begin
  755. if crc_only then
  756. exit;
  757. p:=pchar(@b);
  758. idx:=0;
  759. while len>0 do
  760. begin
  761. left:=bufsize-bufidx;
  762. if len>left then
  763. begin
  764. move(p[idx],buf[bufidx],left);
  765. dec(len,left);
  766. inc(idx,left);
  767. inc(bufidx,left);
  768. writebuf;
  769. end
  770. else
  771. begin
  772. move(p[idx],buf[bufidx],len);
  773. inc(bufidx,len);
  774. exit;
  775. end;
  776. end;
  777. end;
  778. procedure tppufile.NewEntry;
  779. begin
  780. with entry do
  781. begin
  782. id:=entrytyp;
  783. nr:=ibend;
  784. size:=0;
  785. end;
  786. {Reset Entry State}
  787. entryidx:=0;
  788. entrybufstart:=bufstart;
  789. entrystart:=bufstart+bufidx;
  790. {Alloc in buffer}
  791. writedata(entry,sizeof(tppuentry));
  792. end;
  793. procedure tppufile.writeentry(ibnr:byte);
  794. var
  795. opos : integer;
  796. begin
  797. {create entry}
  798. entry.id:=entrytyp;
  799. entry.nr:=ibnr;
  800. entry.size:=entryidx;
  801. {it's already been sent to disk ?}
  802. if entrybufstart<>bufstart then
  803. begin
  804. if not crc_only then
  805. begin
  806. {flush to be sure}
  807. WriteBuf;
  808. {write entry}
  809. opos:=filepos(f);
  810. seek(f,entrystart);
  811. blockwrite(f,entry,sizeof(tppuentry));
  812. seek(f,opos);
  813. end;
  814. entrybufstart:=bufstart;
  815. end
  816. else
  817. move(entry,buf[entrystart-bufstart],sizeof(entry));
  818. {Add New Entry, which is ibend by default}
  819. entrystart:=bufstart+bufidx; {next entry position}
  820. NewEntry;
  821. end;
  822. procedure tppufile.putdata(const b;len:integer);
  823. begin
  824. if do_crc then
  825. begin
  826. crc:=UpdateCrc32(crc,b,len);
  827. {$ifdef Test_Double_checksum}
  828. if crc_only then
  829. begin
  830. crc_test2^[crc_index2]:=crc;
  831. {$ifdef Test_Double_checksum_write}
  832. Writeln(CRCFile,crc);
  833. {$endif Test_Double_checksum_write}
  834. if crc_index2<crc_array_size then
  835. inc(crc_index2);
  836. end
  837. else
  838. begin
  839. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  840. (crc_test2^[crcindex2]<>crc) then
  841. Do_comment(V_Note,'impl CRC changed');
  842. {$ifdef Test_Double_checksum_write}
  843. Writeln(CRCFile,crc);
  844. {$endif Test_Double_checksum_write}
  845. inc(crcindex2);
  846. end;
  847. {$endif def Test_Double_checksum}
  848. if do_interface_crc then
  849. begin
  850. interface_crc:=UpdateCrc32(interface_crc,b,len);
  851. {$ifdef Test_Double_checksum}
  852. if crc_only then
  853. begin
  854. crc_test^[crc_index]:=interface_crc;
  855. {$ifdef Test_Double_checksum_write}
  856. Writeln(CRCFile,interface_crc);
  857. {$endif Test_Double_checksum_write}
  858. if crc_index<crc_array_size then
  859. inc(crc_index);
  860. end
  861. else
  862. begin
  863. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  864. (crc_test^[crcindex]<>interface_crc) then
  865. Do_comment(V_Warning,'CRC changed');
  866. {$ifdef Test_Double_checksum_write}
  867. Writeln(CRCFile,interface_crc);
  868. {$endif Test_Double_checksum_write}
  869. inc(crcindex);
  870. end;
  871. {$endif def Test_Double_checksum}
  872. end;
  873. end;
  874. if not crc_only then
  875. writedata(b,len);
  876. inc(entryidx,len);
  877. end;
  878. procedure tppufile.putbyte(b:byte);
  879. begin
  880. putdata(b,1);
  881. end;
  882. procedure tppufile.putword(w:word);
  883. begin
  884. putdata(w,2);
  885. end;
  886. procedure tppufile.putlongint(l:longint);
  887. begin
  888. putdata(l,4);
  889. end;
  890. procedure tppufile.putint64(i:int64);
  891. begin
  892. putdata(i,8);
  893. end;
  894. procedure tppufile.putaint(i:aint);
  895. begin
  896. putdata(i,sizeof(aint));
  897. end;
  898. procedure tppufile.putreal(d:ppureal);
  899. var
  900. hd : double;
  901. begin
  902. if target_info.system=system_x86_64_win64 then
  903. begin
  904. hd:=d;
  905. putdata(hd,sizeof(hd));
  906. end
  907. else
  908. putdata(d,sizeof(ppureal));
  909. end;
  910. procedure tppufile.putstring(s:string);
  911. begin
  912. putdata(s,length(s)+1);
  913. end;
  914. procedure tppufile.putsmallset(const b);
  915. var
  916. l : longint;
  917. begin
  918. l:=longint(b);
  919. putlongint(l);
  920. end;
  921. procedure tppufile.putnormalset(const b);
  922. type
  923. SetLongintArray = Array [0..7] of longint;
  924. var
  925. i : longint;
  926. tempb : setlongintarray;
  927. begin
  928. putdata(b,32);
  929. end;
  930. procedure tppufile.tempclose;
  931. begin
  932. if not closed then
  933. begin
  934. closepos:=filepos(f);
  935. {$I-}
  936. system.close(f);
  937. {$I+}
  938. if ioresult<>0 then;
  939. closed:=true;
  940. tempclosed:=true;
  941. end;
  942. end;
  943. function tppufile.tempopen:boolean;
  944. var
  945. ofm : byte;
  946. begin
  947. tempopen:=false;
  948. if not closed or not tempclosed then
  949. exit;
  950. ofm:=filemode;
  951. filemode:=0;
  952. {$I-}
  953. reset(f,1);
  954. {$I+}
  955. filemode:=ofm;
  956. if ioresult<>0 then
  957. exit;
  958. closed:=false;
  959. tempclosed:=false;
  960. { restore state }
  961. seek(f,closepos);
  962. tempopen:=true;
  963. end;
  964. end.