owar.pas 7.3 KB

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