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. 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 = 137;
  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. if sizeof(aint)=8 then
  714. result:=getint64
  715. else
  716. result:=getlongint;
  717. {$endif not generic_cpu}
  718. end;
  719. function tppufile.getasizeint:asizeint;
  720. begin
  721. {$ifdef generic_cpu}
  722. if CpuAddrBitSize[tsystemcpu(header.cpu)]=64 then
  723. result:=getint64
  724. else if CpuAddrBitSize[tsystemcpu(header.cpu)]=32 then
  725. result:=getlongint
  726. else if CpuAddrBitSize[tsystemcpu(header.cpu)]=16 then
  727. result:=smallint(getword)
  728. else
  729. begin
  730. error:=true;
  731. result:=0;
  732. end;
  733. {$else not generic_cpu}
  734. if sizeof(asizeint)=8 then
  735. result:=getint64
  736. else
  737. result:=getlongint;
  738. {$endif not generic_cpu}
  739. end;
  740. function tppufile.getaword:aword;
  741. begin
  742. {$ifdef generic_cpu}
  743. if CpuAluBitSize[tsystemcpu(header.cpu)]=64 then
  744. result:=getqword
  745. else if CpuAluBitSize[tsystemcpu(header.cpu)]=32 then
  746. result:=getdword
  747. else if CpuAluBitSize[tsystemcpu(header.cpu)]=16 then
  748. result:=getword
  749. else if CpuAluBitSize[tsystemcpu(header.cpu)]=8 then
  750. result:=getbyte
  751. else
  752. begin
  753. error:=true;
  754. result:=0;
  755. end;
  756. {$else not generic_cpu}
  757. if sizeof(aword)=8 then
  758. result:=getqword
  759. else
  760. result:=getdword;
  761. {$endif not generic_cpu}
  762. end;
  763. function tppufile.getrealsize(sizeofreal : longint):ppureal;
  764. var
  765. e : ppureal;
  766. d : double;
  767. s : single;
  768. begin
  769. if sizeofreal=sizeof(e) then
  770. begin
  771. if entryidx+sizeof(e)>entry.size then
  772. begin
  773. error:=true;
  774. result:=0;
  775. exit;
  776. end;
  777. readdata(e,sizeof(e));
  778. if change_endian then
  779. result:=swapendian_ppureal(e)
  780. else
  781. result:=e;
  782. inc(entryidx,sizeof(e));
  783. result:=e;
  784. exit;
  785. end;
  786. if sizeofreal=sizeof(d) then
  787. begin
  788. if entryidx+sizeof(d)>entry.size then
  789. begin
  790. error:=true;
  791. result:=0;
  792. exit;
  793. end;
  794. readdata(d,sizeof(d));
  795. if change_endian then
  796. result:=swapendian(pqword(@d)^)
  797. else
  798. result:=d;
  799. inc(entryidx,sizeof(d));
  800. result:=d;
  801. exit;
  802. end;
  803. if sizeofreal=sizeof(s) then
  804. begin
  805. if entryidx+sizeof(s)>entry.size then
  806. begin
  807. error:=true;
  808. result:=0;
  809. exit;
  810. end;
  811. readdata(s,sizeof(s));
  812. if change_endian then
  813. result:=swapendian(pdword(@s)^)
  814. else
  815. result:=s;
  816. inc(entryidx,sizeof(s));
  817. result:=s;
  818. exit;
  819. end;
  820. error:=true;
  821. result:=0.0;
  822. end;
  823. function tppufile.getreal:ppureal;
  824. var
  825. d : ppureal;
  826. hd : double;
  827. begin
  828. if target_info.system=system_x86_64_win64 then
  829. begin
  830. hd:=getrealsize(sizeof(hd));
  831. getreal:=hd;
  832. end
  833. else
  834. begin
  835. d:=getrealsize(sizeof(d));
  836. getreal:=d;
  837. end;
  838. end;
  839. function tppufile.getstring:string;
  840. begin
  841. result[0]:=chr(getbyte);
  842. if entryidx+length(result)>entry.size then
  843. begin
  844. error:=true;
  845. exit;
  846. end;
  847. ReadData(result[1],length(result));
  848. inc(entryidx,length(result));
  849. end;
  850. function tppufile.getansistring:ansistring;
  851. var
  852. len: longint;
  853. begin
  854. len:=getlongint;
  855. if entryidx+len>entry.size then
  856. begin
  857. error:=true;
  858. exit;
  859. end;
  860. setlength(result,len);
  861. if len>0 then
  862. getdata(result[1],len);
  863. end;
  864. procedure tppufile.getsmallset(var b);
  865. var
  866. i : longint;
  867. begin
  868. getdata(b,4);
  869. if change_endian then
  870. for i:=0 to 3 do
  871. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  872. end;
  873. procedure tppufile.getnormalset(var b);
  874. var
  875. i : longint;
  876. begin
  877. getdata(b,32);
  878. if change_endian then
  879. for i:=0 to 31 do
  880. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  881. end;
  882. function tppufile.skipuntilentry(untilb:byte):boolean;
  883. var
  884. b : byte;
  885. begin
  886. repeat
  887. b:=readentry;
  888. until (b in [ibend,iberror]) or ((b=untilb) and (entry.id=mainentryid));
  889. skipuntilentry:=(b=untilb);
  890. end;
  891. {*****************************************************************************
  892. TPPUFile Writing
  893. *****************************************************************************}
  894. function tppufile.createfile:boolean;
  895. var
  896. ok: boolean;
  897. begin
  898. createfile:=false;
  899. {$ifdef INTFPPU}
  900. if crc_only then
  901. begin
  902. fname:=fname+'.intf';
  903. crc_only:=false;
  904. end;
  905. {$endif}
  906. if not crc_only then
  907. begin
  908. {$ifdef MACOS}
  909. {FPas is FreePascal's creator code on MacOS. See systems/mac_crea.txt}
  910. SetDefaultMacOSCreator('FPas');
  911. SetDefaultMacOSFiletype('FPPU');
  912. {$endif}
  913. ok:=false;
  914. try
  915. f:=CFileStreamClass.Create(fname,fmCreate);
  916. ok:=true;
  917. except
  918. end;
  919. {$ifdef MACOS}
  920. SetDefaultMacOSCreator('MPS ');
  921. SetDefaultMacOSFiletype('TEXT');
  922. {$endif}
  923. if not ok then
  924. exit;
  925. Mode:=2;
  926. {write header for sure}
  927. f.Write(header,sizeof(tppuheader));
  928. end;
  929. bufsize:=ppubufsize;
  930. bufstart:=sizeof(tppuheader);
  931. bufidx:=0;
  932. {reset}
  933. crc:=0;
  934. interface_crc:=0;
  935. indirect_crc:=0;
  936. do_interface_crc:=true;
  937. do_indirect_crc:=false;
  938. Error:=false;
  939. do_crc:=true;
  940. size:=0;
  941. entrytyp:=mainentryid;
  942. {start}
  943. NewEntry;
  944. createfile:=true;
  945. end;
  946. procedure tppufile.writeheader;
  947. var
  948. opos : integer;
  949. begin
  950. if crc_only then
  951. exit;
  952. { flush buffer }
  953. writebuf;
  954. { update size (w/o header!) in the header }
  955. header.size:=bufstart-sizeof(tppuheader);
  956. { set the endian flag }
  957. {$ifndef FPC_BIG_ENDIAN}
  958. header.flags := header.flags or uf_little_endian;
  959. {$else not FPC_BIG_ENDIAN}
  960. header.flags := header.flags or uf_big_endian;
  961. { Now swap the header in the correct endian (always little endian) }
  962. header.compiler := swapendian(header.compiler);
  963. header.cpu := swapendian(header.cpu);
  964. header.target := swapendian(header.target);
  965. header.flags := swapendian(header.flags);
  966. header.size := swapendian(header.size);
  967. header.checksum := swapendian(header.checksum);
  968. header.interface_checksum := swapendian(header.interface_checksum);
  969. header.indirect_checksum := swapendian(header.indirect_checksum);
  970. header.deflistsize:=swapendian(header.deflistsize);
  971. header.symlistsize:=swapendian(header.symlistsize);
  972. {$endif not FPC_BIG_ENDIAN}
  973. { write header and restore filepos after it }
  974. opos:=f.Position;
  975. f.Position:=0;
  976. f.Write(header,sizeof(tppuheader));
  977. f.Position:=opos;
  978. end;
  979. procedure tppufile.writebuf;
  980. begin
  981. if not crc_only and
  982. (bufidx <> 0) then
  983. f.Write(buf^,bufidx);
  984. inc(bufstart,bufidx);
  985. bufidx:=0;
  986. end;
  987. procedure tppufile.writedata(const b;len:integer);
  988. var
  989. p : pchar;
  990. left,
  991. idx : integer;
  992. begin
  993. if crc_only then
  994. exit;
  995. p:=pchar(@b);
  996. idx:=0;
  997. while len>0 do
  998. begin
  999. left:=bufsize-bufidx;
  1000. if len>left then
  1001. begin
  1002. move(p[idx],buf[bufidx],left);
  1003. dec(len,left);
  1004. inc(idx,left);
  1005. inc(bufidx,left);
  1006. writebuf;
  1007. end
  1008. else
  1009. begin
  1010. move(p[idx],buf[bufidx],len);
  1011. inc(bufidx,len);
  1012. exit;
  1013. end;
  1014. end;
  1015. end;
  1016. procedure tppufile.NewEntry;
  1017. begin
  1018. with entry do
  1019. begin
  1020. id:=entrytyp;
  1021. nr:=ibend;
  1022. size:=0;
  1023. end;
  1024. {Reset Entry State}
  1025. entryidx:=0;
  1026. entrybufstart:=bufstart;
  1027. entrystart:=bufstart+bufidx;
  1028. {Alloc in buffer}
  1029. writedata(entry,sizeof(tppuentry));
  1030. end;
  1031. procedure tppufile.writeentry(ibnr:byte);
  1032. var
  1033. opos : integer;
  1034. begin
  1035. {create entry}
  1036. entry.id:=entrytyp;
  1037. entry.nr:=ibnr;
  1038. entry.size:=entryidx;
  1039. {it's already been sent to disk ?}
  1040. if entrybufstart<>bufstart then
  1041. begin
  1042. if not crc_only then
  1043. begin
  1044. {flush to be sure}
  1045. WriteBuf;
  1046. {write entry}
  1047. opos:=f.Position;
  1048. f.Position:=entrystart;
  1049. f.write(entry,sizeof(tppuentry));
  1050. f.Position:=opos;
  1051. end;
  1052. entrybufstart:=bufstart;
  1053. end
  1054. else
  1055. move(entry,buf[entrystart-bufstart],sizeof(entry));
  1056. {Add New Entry, which is ibend by default}
  1057. entrystart:=bufstart+bufidx; {next entry position}
  1058. NewEntry;
  1059. end;
  1060. procedure tppufile.putdata(const b;len:integer);
  1061. begin
  1062. if do_crc then
  1063. begin
  1064. crc:=UpdateCrc32(crc,b,len);
  1065. {$ifdef Test_Double_checksum}
  1066. if crc_only then
  1067. begin
  1068. crc_test2^[crc_index2]:=crc;
  1069. {$ifdef Test_Double_checksum_write}
  1070. Writeln(CRCFile,crc);
  1071. {$endif Test_Double_checksum_write}
  1072. if crc_index2<crc_array_size then
  1073. inc(crc_index2);
  1074. end
  1075. else
  1076. begin
  1077. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  1078. (crc_test2^[crcindex2]<>crc) then
  1079. Do_comment(V_Note,'impl CRC changed');
  1080. {$ifdef Test_Double_checksum_write}
  1081. Writeln(CRCFile,crc);
  1082. {$endif Test_Double_checksum_write}
  1083. inc(crcindex2);
  1084. end;
  1085. {$endif def Test_Double_checksum}
  1086. if do_interface_crc then
  1087. begin
  1088. interface_crc:=UpdateCrc32(interface_crc,b,len);
  1089. {$ifdef Test_Double_checksum}
  1090. if crc_only then
  1091. begin
  1092. crc_test^[crc_index]:=interface_crc;
  1093. {$ifdef Test_Double_checksum_write}
  1094. Writeln(CRCFile,interface_crc);
  1095. {$endif Test_Double_checksum_write}
  1096. if crc_index<crc_array_size then
  1097. inc(crc_index);
  1098. end
  1099. else
  1100. begin
  1101. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  1102. (crc_test^[crcindex]<>interface_crc) then
  1103. Do_comment(V_Warning,'CRC changed');
  1104. {$ifdef Test_Double_checksum_write}
  1105. Writeln(CRCFile,interface_crc);
  1106. {$endif Test_Double_checksum_write}
  1107. inc(crcindex);
  1108. end;
  1109. {$endif def Test_Double_checksum}
  1110. { indirect crc must only be calculated for the interface; changes
  1111. to a class in the implementation cannot require another unit to
  1112. be recompiled }
  1113. if do_indirect_crc then
  1114. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  1115. end;
  1116. end;
  1117. if not crc_only then
  1118. writedata(b,len);
  1119. inc(entryidx,len);
  1120. end;
  1121. procedure tppufile.putbyte(b:byte);
  1122. begin
  1123. putdata(b,1);
  1124. end;
  1125. procedure tppufile.putword(w:word);
  1126. begin
  1127. putdata(w,2);
  1128. end;
  1129. procedure tppufile.putdword(w:dword);
  1130. begin
  1131. putdata(w,4);
  1132. end;
  1133. procedure tppufile.putlongint(l:longint);
  1134. begin
  1135. putdata(l,4);
  1136. end;
  1137. procedure tppufile.putint64(i:int64);
  1138. begin
  1139. putdata(i,8);
  1140. end;
  1141. procedure tppufile.putqword(q:qword);
  1142. begin
  1143. putdata(q,sizeof(qword));
  1144. end;
  1145. procedure tppufile.putaint(i:aint);
  1146. begin
  1147. putdata(i,sizeof(aint));
  1148. end;
  1149. procedure tppufile.putasizeint(i: asizeint);
  1150. begin
  1151. putdata(i,sizeof(asizeint));
  1152. end;
  1153. procedure tppufile.putaword(i:aword);
  1154. begin
  1155. putdata(i,sizeof(aword));
  1156. end;
  1157. procedure tppufile.putreal(d:ppureal);
  1158. var
  1159. hd : double;
  1160. begin
  1161. if target_info.system=system_x86_64_win64 then
  1162. begin
  1163. hd:=d;
  1164. putdata(hd,sizeof(hd));
  1165. end
  1166. else
  1167. putdata(d,sizeof(ppureal));
  1168. end;
  1169. procedure tppufile.putstring(const s:string);
  1170. begin
  1171. putdata(s,length(s)+1);
  1172. end;
  1173. procedure tppufile.putansistring(const s:ansistring);
  1174. var
  1175. len: longint;
  1176. begin
  1177. len:=length(s);
  1178. putlongint(len);
  1179. if len>0 then
  1180. putdata(s[1],len);
  1181. end;
  1182. procedure tppufile.putsmallset(const b);
  1183. var
  1184. l : longint;
  1185. begin
  1186. l:=longint(b);
  1187. putlongint(l);
  1188. end;
  1189. procedure tppufile.putnormalset(const b);
  1190. type
  1191. SetLongintArray = Array [0..7] of longint;
  1192. begin
  1193. putdata(b,32);
  1194. end;
  1195. procedure tppufile.tempclose;
  1196. begin
  1197. if not closed then
  1198. begin
  1199. closepos:=f.Position;
  1200. f.Free;
  1201. f:=nil;
  1202. closed:=true;
  1203. tempclosed:=true;
  1204. end;
  1205. end;
  1206. function tppufile.tempopen:boolean;
  1207. begin
  1208. tempopen:=false;
  1209. if not closed or not tempclosed then
  1210. exit;
  1211. { MG: not sure, if this is correct
  1212. f.position:=0;
  1213. No, f was freed in tempclose above, we need to
  1214. recreate it. PM 2011/06/06 }
  1215. try
  1216. f:=CFileStreamClass.Create(fname,fmOpenRead);
  1217. except
  1218. exit;
  1219. end;
  1220. closed:=false;
  1221. tempclosed:=false;
  1222. { restore state }
  1223. f.Position:=closepos;
  1224. tempopen:=true;
  1225. end;
  1226. end.