ppu.pas 30 KB

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