owar.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. Contains the stuff for writing .a files directly
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit owar;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cclasses,
  23. owbase;
  24. type
  25. tarhdr=packed record
  26. name : array[0..15] of char;
  27. date : array[0..11] of char;
  28. uid : array[0..5] of char;
  29. gid : array[0..5] of char;
  30. mode : array[0..7] of char;
  31. size : array[0..9] of char;
  32. fmag : array[0..1] of char;
  33. end;
  34. tarobjectwriter=class(tobjectwriter)
  35. constructor create(const Aarfn:string);
  36. destructor destroy;override;
  37. procedure createfile(const fn:string);override;
  38. procedure closefile;override;
  39. procedure writesym(const sym:string);override;
  40. procedure write(const b;len:longint);override;
  41. private
  42. arfn : string;
  43. arhdr : tarhdr;
  44. symreloc,
  45. symstr,
  46. lfnstr,
  47. ardata : TDynamicArray;
  48. objpos : longint;
  49. objfn : string;
  50. timestamp : string[12];
  51. procedure createarhdr(fn:string;size:longint;const gid,uid,mode:string);
  52. procedure writear;
  53. end;
  54. implementation
  55. uses
  56. cstreams,
  57. verbose,
  58. {$ifdef Delphi}
  59. dmisc;
  60. {$else Delphi}
  61. dos;
  62. {$endif Delphi}
  63. const
  64. symrelocbufsize = 4096;
  65. symstrbufsize = 8192;
  66. lfnstrbufsize = 4096;
  67. arbufsize = 65536;
  68. objbufsize = 16384;
  69. {*****************************************************************************
  70. Helpers
  71. *****************************************************************************}
  72. const
  73. C1970=2440588;
  74. D0=1461;
  75. D1=146097;
  76. D2=1721119;
  77. Function Gregorian2Julian(DT:DateTime):LongInt;
  78. Var
  79. Century,XYear,Month : LongInt;
  80. Begin
  81. Month:=DT.Month;
  82. If Month<=2 Then
  83. Begin
  84. Dec(DT.Year);
  85. Inc(Month,12);
  86. End;
  87. Dec(Month,3);
  88. Century:=(longint(DT.Year Div 100)*D1) shr 2;
  89. XYear:=(longint(DT.Year Mod 100)*D0) shr 2;
  90. Gregorian2Julian:=((((Month*153)+2) div 5)+DT.Day)+D2+XYear+Century;
  91. End;
  92. function DT2Unix(DT:DateTime):LongInt;
  93. Begin
  94. DT2Unix:=(Gregorian2Julian(DT)-C1970)*86400+(LongInt(DT.Hour)*3600)+(DT.Min*60)+DT.Sec;
  95. end;
  96. {*****************************************************************************
  97. TArObjectWriter
  98. *****************************************************************************}
  99. constructor tarobjectwriter.create(const Aarfn:string);
  100. var
  101. time : datetime;
  102. dummy : word;
  103. begin
  104. arfn:=Aarfn;
  105. ardata:=TDynamicArray.Create(arbufsize);
  106. symreloc:=TDynamicArray.Create(symrelocbufsize);
  107. symstr:=TDynamicArray.Create(symstrbufsize);
  108. lfnstr:=TDynamicArray.Create(lfnstrbufsize);
  109. { create timestamp }
  110. getdate(time.year,time.month,time.day,dummy);
  111. gettime(time.hour,time.min,time.sec,dummy);
  112. Str(DT2Unix(time),timestamp);
  113. end;
  114. destructor tarobjectwriter.destroy;
  115. begin
  116. if Errorcount=0 then
  117. writear;
  118. arData.Free;
  119. symreloc.Free;
  120. symstr.Free;
  121. lfnstr.Free;
  122. end;
  123. procedure tarobjectwriter.createarhdr(fn:string;size:longint;const gid,uid,mode:string);
  124. var
  125. tmp : string[9];
  126. begin
  127. fillchar(arhdr,sizeof(tarhdr),' ');
  128. { create ar header }
  129. fn:=fn+'/';
  130. if length(fn)>16 then
  131. begin
  132. arhdr.name[0]:='/';
  133. str(lfnstr.size,tmp);
  134. move(tmp[1],arhdr.name[1],length(tmp));
  135. fn:=fn+#10;
  136. lfnstr.write(fn[1],length(fn));
  137. end
  138. else
  139. move(fn[1],arhdr.name,length(fn));
  140. { don't write a date if also no gid/uid/mode is specified }
  141. if gid<>'' then
  142. move(timestamp[1],arhdr.date,sizeof(timestamp));
  143. str(size,tmp);
  144. move(tmp[1],arhdr.size,length(tmp));
  145. move(gid[1],arhdr.gid,length(gid));
  146. move(uid[1],arhdr.uid,length(uid));
  147. move(mode[1],arhdr.mode,length(mode));
  148. arhdr.fmag:='`'#10;
  149. end;
  150. procedure tarobjectwriter.createfile(const fn:string);
  151. begin
  152. objfn:=fn;
  153. objpos:=ardata.size;
  154. ardata.seek(objpos + sizeof(tarhdr));
  155. end;
  156. procedure tarobjectwriter.closefile;
  157. begin
  158. ardata.align(2);
  159. { fix the size in the header }
  160. createarhdr(objfn,ardata.size-objpos-sizeof(tarhdr),'42','42','644');
  161. { write the header }
  162. ardata.seek(objpos);
  163. ardata.write(arhdr,sizeof(tarhdr));
  164. end;
  165. procedure tarobjectwriter.writesym(const sym:string);
  166. var
  167. c : char;
  168. begin
  169. c:=#0;
  170. symreloc.write(objpos,4);
  171. symstr.write(sym[1],length(sym));
  172. symstr.write(c,1);
  173. end;
  174. procedure tarobjectwriter.write(const b;len:longint);
  175. begin
  176. ardata.write(b,len);
  177. end;
  178. procedure tarobjectwriter.writear;
  179. function lsb2msb(l:longint):longint;
  180. type
  181. bytearr=array[0..3] of byte;
  182. var
  183. l1 : longint;
  184. begin
  185. bytearr(l1)[0]:=bytearr(l)[3];
  186. bytearr(l1)[1]:=bytearr(l)[2];
  187. bytearr(l1)[2]:=bytearr(l)[1];
  188. bytearr(l1)[3]:=bytearr(l)[0];
  189. lsb2msb:=l1;
  190. end;
  191. const
  192. armagic:array[1..8] of char='!<arch>'#10;
  193. type
  194. plongint=^longint;
  195. var
  196. arf : TCFileStream;
  197. fixup,l,
  198. relocs,i : longint;
  199. begin
  200. arf:=TCFileStream.Create(arfn,fmCreate);
  201. if CStreamError<>0 then
  202. begin
  203. Message1(exec_e_cant_create_archivefile,arfn);
  204. exit;
  205. end;
  206. arf.Write(armagic,sizeof(armagic));
  207. { align first, because we need the size for the fixups of the symbol reloc }
  208. if lfnstr.size>0 then
  209. lfnstr.align(2);
  210. if symreloc.size>0 then
  211. begin
  212. symstr.align(2);
  213. fixup:=12+sizeof(tarhdr)+symreloc.size+symstr.size;
  214. if lfnstr.size>0 then
  215. inc(fixup,lfnstr.size+sizeof(tarhdr));
  216. relocs:=symreloc.size div 4;
  217. { fixup relocs }
  218. for i:=0to relocs-1 do
  219. begin
  220. symreloc.seek(i*4);
  221. symreloc.read(l,4);
  222. symreloc.seek(i*4);
  223. l:=lsb2msb(l+fixup);
  224. symreloc.write(l,4);
  225. end;
  226. createarhdr('',4+symreloc.size+symstr.size,'0','0','0');
  227. arf.Write(arhdr,sizeof(tarhdr));
  228. relocs:=lsb2msb(relocs);
  229. arf.Write(relocs,4);
  230. symreloc.WriteStream(arf);
  231. symstr.WriteStream(arf);
  232. end;
  233. if lfnstr.size>0 then
  234. begin
  235. createarhdr('/',lfnstr.size,'','','');
  236. arf.Write(arhdr,sizeof(tarhdr));
  237. lfnstr.WriteStream(arf);
  238. end;
  239. ardata.WriteStream(arf);
  240. Arf.Free;
  241. end;
  242. end.
  243. {
  244. $Log$
  245. Revision 1.7 2000-12-24 12:25:32 peter
  246. + cstreams unit
  247. * dynamicarray object to class
  248. Revision 1.5 2000/09/24 15:06:20 peter
  249. * use defines.inc
  250. Revision 1.4 2000/08/19 18:44:27 peter
  251. * new tdynamicarray implementation using blocks instead of
  252. reallocmem (merged)
  253. Revision 1.3 2000/08/08 19:28:57 peter
  254. * memdebug/memory patches (merged)
  255. * only once illegal directive (merged)
  256. Revision 1.2 2000/07/13 11:32:44 michael
  257. + removed logs
  258. }