ppu.pas 30 KB

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