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