ppu.pas 31 KB

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