1 /* ------------------------------------------------------------------------ */
2 /* LHa for UNIX    Archiver Driver                                          */
3 /*                                                                          */
4 /*      Modified                Nobutaka Watazaki                           */
5 /*                                                                          */
6 /*  Ver. 1.14   Soruce All chagned              1995.01.14  N.Watazaki      */
7 /*  Ver. 1.14i  Modified and bug fixed          2000.10.06  t.okamoto       */
8 /* ------------------------------------------------------------------------ */
9 /*
10     Included...
11         lharc.h     interface.h     slidehuf.h
12 */
13 
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <ctype.h>
22 #include <sys/types.h>
23 #include <sys/file.h>
24 #include <sys/stat.h>
25 #include <signal.h>
26 
27 #if HAVE_INTTYPES_H
28 # include <inttypes.h>
29 #else
30 # if HAVE_STDINT_H
31 #  include <stdint.h>
32 # endif
33 #endif
34 
35 #if STDC_HEADERS
36 # include <string.h>
37 #else
38 # if !HAVE_STRCHR
39 #  define strchr index
40 #  define strrchr rindex
41 # endif
42 char *strchr (), *strrchr ();
43 # if !HAVE_MEMCPY
44 #  define memcmp(s1, s2, n) bcmp ((s1), (s2), (n))
45 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
46 #  define memmove(d, s, n) bcopy ((s), (d), (n))
47 # endif
48 #endif
49 
50 #if STDC_HEADERS
51 # include <stdlib.h>
52 # include <stddef.h>
53 #else
54 # if HAVE_STDLIB_H
55 #  include <stdlib.h>
56 # endif
57 #endif
58 
59 #ifndef NULL
60 #define NULL ((char *)0)
61 #endif
62 
63 #if HAVE_UNISTD_H
64 #include <unistd.h>
65 #endif
66 
67 #if STDC_HEADERS
68 # include <stdarg.h>
69 # define va_init(a,b) va_start(a,b)
70 #else
71 # include <varargs.h>
72 # define va_init(a,b) va_start(a)
73 #endif
74 
75 #if HAVE_PWD_H
76 # include <pwd.h>
77 #endif
78 #if HAVE_GRP_H
79 # include <grp.h>
80 #endif
81 
82 #if !HAVE_UID_T
83 typedef int uid_t;
84 #endif
85 #if !HAVE_GID_T
86 typedef int gid_t;
87 #endif
88 
89 #if !HAVE_UINT64_T
90 # define HAVE_UINT64_T 1
91 # if SIZEOF_LONG == 8
92     typedef unsigned long uint64_t;
93 # elif HAVE_LONG_LONG
94     typedef unsigned long long uint64_t;
95 # else
96 #  undef HAVE_UINT64_T
97 # endif
98 #endif
99 
100 #if !HAVE_SSIZE_T
101 typedef long ssize_t;
102 #endif
103 
104 #if TIME_WITH_SYS_TIME
105 # include <sys/time.h>
106 # include <time.h>
107 #else
108 # if HAVE_SYS_TIME_H
109 #  include <sys/time.h>
110 # else
111 #  include <time.h>
112 # endif
113 #endif
114 
115 #if HAVE_UTIME_H
116 #include <utime.h>
117 #else
118 struct utimbuf {
119     time_t actime;
120     time_t modtime;
121 };
122 int utime(const char *, struct utimbuf *);
123 #endif
124 
125 #if HAVE_DIRENT_H
126 # include <dirent.h>
127 # define NAMLEN(dirent) strlen((dirent)->d_name)
128 #else
129 # define dirent direct
130 # define NAMLEN(dirent) (dirent)->d_namlen
131 # if HAVE_SYS_NDIR_H
132 #  include <sys/ndir.h>
133 # endif
134 # if HAVE_SYS_DIR_H
135 #  include <sys/dir.h>
136 # endif
137 # if HAVE_NDIR_H
138 #  include <ndir.h>
139 # endif
140 # ifdef NONSYSTEM_DIR_LIBRARY           /* no use ?? */
141 #  include "lhdir.h"
142 # endif
143 #endif
144 
145 #if HAVE_FNMATCH_H
146 # include <fnmatch.h>
147 #else
148 int fnmatch(const char *pattern, const char *string, int flags);
149 # define FNM_PATHNAME 1
150 # define FNM_NOESCAPE 2
151 # define FNM_PERIOD   4
152 #endif
153 
154 #ifndef SEEK_SET
155 #define SEEK_SET        0
156 #define SEEK_CUR        1
157 #define SEEK_END        2
158 #endif  /* SEEK_SET */
159 
160 #if HAVE_LIMITS_H
161 #include <limits.h>
162 #else
163 
164 #ifndef CHAR_BIT
165 #define CHAR_BIT 8
166 #endif
167 
168 #ifndef UCHAR_MAX
169 #define UCHAR_MAX ((1<<(sizeof(unsigned char)*8))-1)
170 #endif
171 
172 #ifndef USHRT_MAX
173 #define USHRT_MAX ((1<<(sizeof(unsigned short)*8))-1)
174 #endif
175 
176 #ifndef SHRT_MAX
177 #define SHRT_MAX ((1<<(sizeof(short)*8-1))-1)
178 #endif
179 
180 #ifndef SHRT_MIN
181 #define SHRT_MIN (SHRT_MAX-USHRT_MAX)
182 #endif
183 
184 #ifndef ULONG_MAX
185 #define ULONG_MAX ((1<<(sizeof(unsigned long)*8))-1)
186 #endif
187 
188 #ifndef LONG_MAX
189 #define LONG_MAX ((1<<(sizeof(long)*8-1))-1)
190 #endif
191 
192 #ifndef LONG_MIN
193 #define LONG_MIN (LONG_MAX-ULONG_MAX)
194 #endif
195 
196 #endif /* HAVE_LIMITS_H */
197 
198 #if !HAVE_FSEEKO
199 # define fseeko  fseek
200 #endif
201 #if !HAVE_FTELLO
202 # define ftello  ftell
203 #endif
204 
205 #include "lha_macro.h"
206 
207 #define exit(n) lha_exit(n)
208 
209 struct encode_option {
210 #if defined(__STDC__) || defined(AIX)
211     void            (*output) ();
212     void            (*encode_start) ();
213     void            (*encode_end) ();
214 #else
215     int             (*output) ();
216     int             (*encode_start) ();
217     int             (*encode_end) ();
218 #endif
219 };
220 
221 struct decode_option {
222     unsigned short  (*decode_c) ();
223     unsigned short  (*decode_p) ();
224 #if defined(__STDC__) || defined(AIX)
225     void            (*decode_start) ();
226 #else
227     int             (*decode_start) ();
228 #endif
229 };
230 
231 /* ------------------------------------------------------------------------ */
232 /*  LHa File Type Definition                                                */
233 /* ------------------------------------------------------------------------ */
234 typedef int boolean;            /* TRUE or FALSE */
235 
236 struct string_pool {
237     int             used;
238     int             size;
239     int             n;
240     char           *buffer;
241 };
242 
243 typedef struct LzHeader {
244     size_t          header_size;
245     int             size_field_length;
246     char            method[METHOD_TYPE_STORAGE];
247     size_t          packed_size;
248     size_t          original_size;
249     unsigned char   attribute;
250     unsigned char   header_level;
251     char            name[FILENAME_LENGTH];
252     char            realname[FILENAME_LENGTH];/* real name for symbolic link */
253     unsigned int    crc;      /* file CRC */
254     boolean         has_crc;  /* file CRC */
255     unsigned int    header_crc; /* header CRC */
256     unsigned char   extend_type;
257     unsigned char   minor_version;
258 
259     /* extend_type == EXTEND_UNIX  and convert from other type. */
260     time_t          unix_last_modified_stamp;
261     unsigned short  unix_mode;
262     unsigned short  unix_uid;
263     unsigned short  unix_gid;
264     char            user[256];
265     char            group[256];
266 }  LzHeader;
267 
268 struct interfacing {
269     FILE            *infile;
270     FILE            *outfile;
271     size_t          original;
272     size_t          packed;
273     size_t          read_size;
274     int             dicbit;
275     int             method;
276 };
277 
278 
279 /* ------------------------------------------------------------------------ */
280 /*  Option switch variable                                                  */
281 /* ------------------------------------------------------------------------ */
282 #ifdef LHA_MAIN_SRC
283 #define EXTERN
284 #else
285 #define EXTERN extern
286 #endif
287 
288 /* command line options (common options) */
289 EXTERN boolean  quiet;
290 EXTERN boolean  text_mode;
291 EXTERN boolean  verbose;
292 EXTERN boolean  noexec;     /* debugging option */
293 EXTERN boolean  force;
294 EXTERN boolean  delete_after_append;
295 EXTERN int      compress_method;
296 EXTERN int      header_level;
297 /* EXTERN int       quiet_mode; */   /* 1996.8.13 t.okamoto */
298 #ifdef EUC
299 EXTERN boolean  euc_mode;
300 #endif
301 
302 /* list command flags */
303 EXTERN boolean  verbose_listing;
304 
305 /* extract/print command flags */
306 EXTERN boolean  output_to_stdout;
307 
308 /* add/update/delete command flags */
309 EXTERN boolean  new_archive;
310 EXTERN boolean  update_if_newer;
311 EXTERN boolean  generic_format;
312 
313 EXTERN boolean  remove_temporary_at_error;
314 EXTERN boolean  recover_archive_when_interrupt;
315 EXTERN boolean  remove_extracting_file_when_interrupt;
316 EXTERN boolean  get_filename_from_stdin;
317 EXTERN boolean  ignore_directory;
318 EXTERN char   **exclude_files;
319 EXTERN boolean  verify_mode;
320 
321 /* Indicator flag */
322 EXTERN int      quiet_mode;
323 
324 EXTERN boolean backup_old_archive;
325 EXTERN boolean  extract_broken_archive;
326 
327 /* ------------------------------------------------------------------------ */
328 /*  Globale Variable                                                        */
329 /* ------------------------------------------------------------------------ */
330 EXTERN char     **cmd_filev;
331 EXTERN int      cmd_filec;
332 
333 EXTERN char     *archive_name;
334 EXTERN char     temporary_name[FILENAME_LENGTH];
335 EXTERN char     backup_archive_name[FILENAME_LENGTH];
336 
337 EXTERN char     *extract_directory;
338 EXTERN char     *reading_filename, *writing_filename;
339 
340 EXTERN int      archive_file_mode;
341 EXTERN int      archive_file_gid;
342 
343 EXTERN int      noconvertcase; /* 2000.10.6 */
344 
345 /* slide.c */
346 EXTERN int      unpackable;
347 EXTERN size_t origsize, compsize;
348 EXTERN unsigned short dicbit;
349 EXTERN unsigned short maxmatch;
350 EXTERN size_t decode_count;
351 EXTERN unsigned long loc;           /* short -> long .. Changed N.Watazaki */
352 EXTERN unsigned char *text;
353 
354 /* huf.c */
355 #ifndef LHA_MAIN_SRC  /* t.okamoto 96/2/20 */
356 EXTERN unsigned short left[], right[];
357 EXTERN unsigned char c_len[], pt_len[];
358 EXTERN unsigned short c_freq[], c_table[], c_code[];
359 EXTERN unsigned short p_freq[], pt_table[], pt_code[], t_freq[];
360 #endif
361 
362 /* bitio.c */
363 EXTERN FILE     *infile, *outfile;
364 EXTERN unsigned short bitbuf;
365 
366 /* crcio.c */
367 EXTERN unsigned int crctable[UCHAR_MAX + 1];
368 EXTERN int      dispflg;
369 
370 /* from dhuf.c */
371 EXTERN unsigned int n_max;
372 
373 /* lhadd.c */
374 EXTERN int temporary_fd;
375 
376 /* ------------------------------------------------------------------------ */
377 /*  Functions                                                               */
378 /* ------------------------------------------------------------------------ */
379 #include "prototypes.h"
380