owar.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. interface
  20. uses
  21. cobjects,owbase;
  22. type
  23. tarhdr=packed record
  24. name : array[0..15] of char;
  25. date : array[0..11] of char;
  26. uid : array[0..5] of char;
  27. gid : array[0..5] of char;
  28. mode : array[0..7] of char;
  29. size : array[0..9] of char;
  30. fmag : array[0..1] of char;
  31. end;
  32. parobjectwriter=^tarobjectwriter;
  33. tarobjectwriter=object(tobjectwriter)
  34. constructor Init(const Aarfn:string);
  35. destructor Done;virtual;
  36. procedure create(const fn:string);virtual;
  37. procedure close;virtual;
  38. procedure writesym(sym:string);virtual;
  39. procedure write(var b;len:longint);virtual;
  40. private
  41. arfn : string;
  42. arhdr : tarhdr;
  43. symreloc,
  44. symstr,
  45. lfnstr,
  46. ardata,
  47. objdata : PDynamicArray;
  48. objfixup : 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. verbose,
  57. {$ifdef Delphi}
  58. dmisc;
  59. {$else Delphi}
  60. dos;
  61. {$endif Delphi}
  62. const
  63. {$ifdef TP}
  64. symrelocbufsize = 32;
  65. symstrbufsize = 256;
  66. lfnstrbufsize = 256;
  67. arbufsize = 256;
  68. objbufsize = 256;
  69. {$else}
  70. symrelocbufsize = 1024;
  71. symstrbufsize = 8192;
  72. lfnstrbufsize = 4096;
  73. arbufsize = 65536;
  74. objbufsize = 16384;
  75. {$endif}
  76. {*****************************************************************************
  77. Helpers
  78. *****************************************************************************}
  79. const
  80. C1970=2440588;
  81. D0=1461;
  82. D1=146097;
  83. D2=1721119;
  84. Function Gregorian2Julian(DT:DateTime):LongInt;
  85. Var
  86. Century,XYear,Month : LongInt;
  87. Begin
  88. Month:=DT.Month;
  89. If Month<=2 Then
  90. Begin
  91. Dec(DT.Year);
  92. Inc(Month,12);
  93. End;
  94. Dec(Month,3);
  95. Century:=(longint(DT.Year Div 100)*D1) shr 2;
  96. XYear:=(longint(DT.Year Mod 100)*D0) shr 2;
  97. Gregorian2Julian:=((((Month*153)+2) div 5)+DT.Day)+D2+XYear+Century;
  98. End;
  99. function DT2Unix(DT:DateTime):LongInt;
  100. Begin
  101. DT2Unix:=(Gregorian2Julian(DT)-C1970)*86400+(LongInt(DT.Hour)*3600)+(DT.Min*60)+DT.Sec;
  102. end;
  103. {*****************************************************************************
  104. TArObjectWriter
  105. *****************************************************************************}
  106. constructor tarobjectwriter.init(const Aarfn:string);
  107. var
  108. time : datetime;
  109. dummy : word;
  110. begin
  111. arfn:=Aarfn;
  112. new(arData,init(1,arbufsize));
  113. new(symreloc,init(4,symrelocbufsize));
  114. new(symstr,init(1,symstrbufsize));
  115. new(lfnstr,init(1,lfnstrbufsize));
  116. { create timestamp }
  117. getdate(time.year,time.month,time.day,dummy);
  118. gettime(time.hour,time.min,time.sec,dummy);
  119. Str(DT2Unix(time),timestamp);
  120. end;
  121. destructor tarobjectwriter.done;
  122. begin
  123. if Errorcount=0 then
  124. writear;
  125. dispose(arData,done);
  126. dispose(symreloc,done);
  127. dispose(symstr,done);
  128. dispose(lfnstr,done);
  129. end;
  130. procedure tarobjectwriter.createarhdr(fn:string;size:longint;const gid,uid,mode:string);
  131. var
  132. tmp : string[9];
  133. begin
  134. fillchar(arhdr,sizeof(tarhdr),' ');
  135. { create ar header }
  136. fn:=fn+'/';
  137. if length(fn)>16 then
  138. begin
  139. arhdr.name[0]:='/';
  140. str(lfnstr^.usedsize,tmp);
  141. move(tmp[1],arhdr.name[1],length(tmp));
  142. fn:=fn+#10;
  143. lfnstr^.write(fn[1],length(fn));
  144. end
  145. else
  146. move(fn[1],arhdr.name,length(fn));
  147. { don't write a date if also no gid/uid/mode is specified }
  148. if gid<>'' then
  149. move(timestamp[1],arhdr.date,sizeof(timestamp));
  150. str(size,tmp);
  151. move(tmp[1],arhdr.size,length(tmp));
  152. move(gid[1],arhdr.gid,length(gid));
  153. move(uid[1],arhdr.uid,length(uid));
  154. move(mode[1],arhdr.mode,length(mode));
  155. arhdr.fmag:='`'#10;
  156. end;
  157. procedure tarobjectwriter.create(const fn:string);
  158. begin
  159. objfn:=fn;
  160. objfixup:=ardata^.usedsize;
  161. { reset size }
  162. new(objdata,init(1,objbufsize));
  163. end;
  164. procedure tarobjectwriter.close;
  165. begin
  166. objdata^.align(2);
  167. { fix the size in the header }
  168. createarhdr(objfn,objdata^.usedsize,'42','42','644');
  169. { write the header }
  170. ardata^.write(arhdr,sizeof(tarhdr));
  171. { write the data of this objfile }
  172. ardata^.write(objdata^.data^,objdata^.usedsize);
  173. { free this object }
  174. dispose(objdata,done);
  175. end;
  176. procedure tarobjectwriter.writesym(sym:string);
  177. begin
  178. sym:=sym+#0;
  179. symreloc^.write(objfixup,1);
  180. symstr^.write(sym[1],length(sym));
  181. end;
  182. procedure tarobjectwriter.write(var b;len:longint);
  183. begin
  184. objdata^.write(b,len);
  185. end;
  186. procedure tarobjectwriter.writear;
  187. function lsb2msb(l:longint):longint;
  188. type
  189. bytearr=array[0..3] of byte;
  190. var
  191. l1 : longint;
  192. begin
  193. bytearr(l1)[0]:=bytearr(l)[3];
  194. bytearr(l1)[1]:=bytearr(l)[2];
  195. bytearr(l1)[2]:=bytearr(l)[1];
  196. bytearr(l1)[3]:=bytearr(l)[0];
  197. lsb2msb:=l1;
  198. end;
  199. const
  200. armagic:array[1..8] of char='!<arch>'#10;
  201. type
  202. plongint=^longint;
  203. var
  204. arf : file;
  205. fixup,
  206. relocs,i : longint;
  207. begin
  208. assign(arf,arfn);
  209. {$I-}
  210. rewrite(arf,1);
  211. {$I+}
  212. if ioresult<>0 then
  213. exit;
  214. blockwrite(arf,armagic,sizeof(armagic));
  215. { align first, because we need the size for the fixups of the symbol reloc }
  216. if lfnstr^.usedsize>0 then
  217. lfnstr^.align(2);
  218. if symreloc^.usedsize>0 then
  219. begin
  220. symstr^.align(2);
  221. fixup:=12+sizeof(tarhdr)+symreloc^.usedsize+symstr^.usedsize;
  222. if lfnstr^.usedsize>0 then
  223. inc(fixup,lfnstr^.usedsize+sizeof(tarhdr));
  224. relocs:=symreloc^.count;
  225. for i:=0to relocs-1 do
  226. plongint(@symreloc^.data[i*4])^:=lsb2msb(plongint(@symreloc^.data[i*4])^+fixup);
  227. createarhdr('',4+symreloc^.usedsize+symstr^.usedsize,'0','0','0');
  228. blockwrite(arf,arhdr,sizeof(tarhdr));
  229. relocs:=lsb2msb(relocs);
  230. blockwrite(arf,relocs,4);
  231. blockwrite(arf,symreloc^.data^,symreloc^.usedsize);
  232. blockwrite(arf,symstr^.data^,symstr^.usedsize);
  233. end;
  234. if lfnstr^.usedsize>0 then
  235. begin
  236. createarhdr('/',lfnstr^.usedsize,'','','');
  237. blockwrite(arf,arhdr,sizeof(tarhdr));
  238. blockwrite(arf,lfnstr^.data^,lfnstr^.usedsize);
  239. end;
  240. blockwrite(arf,ardata^.data^,ardata^.usedsize);
  241. system.close(arf);
  242. end;
  243. end.
  244. {
  245. $Log$
  246. Revision 1.6 2000-02-09 13:22:55 peter
  247. * log truncated
  248. Revision 1.5 2000/01/07 01:14:28 peter
  249. * updated copyright to 2000
  250. }