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