owar.pas 6.8 KB

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