ppu.pas 25 KB

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