ppu.pas 26 KB

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