ppu.pas 30 KB

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