ppu.pas 25 KB

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