home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / UTILITY / UNZIP.ARJ / UNZIP.C < prev    next >
C/C++ Source or Header  |  1991-02-10  |  4KB  |  166 lines

  1. #include <io.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <sys\stat.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7.  
  8.  
  9. #define ZIP_LOCAL_SIG 0x04034b50
  10. #define ZIP_CENT_START_SIG 0x02014b50
  11. #define ZIP_CENT_END_SIG 0x06054b50
  12.  
  13. typedef struct {
  14.   unsigned long         signature;    /* 0x04034b50 */
  15.   unsigned short        extract_ver;
  16.   unsigned short        flags;
  17.   unsigned short        comp_meth;
  18.   unsigned short        mod_time;
  19.   unsigned short        mod_date;
  20.   unsigned long         crc_32;
  21.   unsigned long         comp_size;
  22.   unsigned long         uncomp_size;
  23.   unsigned short        filename_len;
  24.   unsigned short        extra_length;
  25. } zip_local_header;
  26.  
  27. typedef struct {
  28.   unsigned long         signature;    /* 0x02014b50 */
  29.   unsigned short        made_ver;
  30.   unsigned short        extract_ver;
  31.   unsigned short        flags;
  32.   unsigned short        comp_meth;
  33.   unsigned short        mod_time;
  34.   unsigned short        mod_date;
  35.   unsigned long         crc_32;
  36.   unsigned long         comp_size;
  37.   unsigned long         uncomp_size;
  38.   unsigned short        filename_len;
  39.   unsigned short        extra_len;
  40.   unsigned short        comment_len;
  41.   unsigned short        disk_start;
  42.   unsigned short        int_attr;
  43.   unsigned long         ext_attr;
  44.   unsigned long         rel_ofs_header;
  45. } zip_central_dir;
  46.  
  47.  
  48. typedef struct {
  49.   unsigned long         signature;    /* 0x06054b50 */
  50.   unsigned short        disk_num;
  51.   unsigned short        cent_dir_disk_num;
  52.   unsigned short        total_entries_this_disk;
  53.   unsigned short        total_entries_total;
  54.   unsigned long         central_dir_size;
  55.   unsigned long         ofs_cent_dir;
  56.   unsigned short        comment_len;
  57. } zip_end_dir;
  58.  
  59.  
  60. char *bad_words[] = {
  61.   "PKZIP",
  62.   "PKUNZIP",
  63.   "COMMAND",
  64.   "DSZ",
  65.   "UNZIP",
  66.   "\\",
  67.   "/",
  68.   ":",
  69.   ">",
  70.   "<",
  71.   "|",
  72.   "..",
  73.   NULL,
  74. };
  75.  
  76. long bad_filename(char *fn)
  77. {
  78.   int i;
  79.  
  80.   strupr(fn);
  81.   for (i=0; bad_words[i]; i++) {
  82.     if (strstr(fn,bad_words[i])) {
  83.       printf("Questionable file '%s' in that .ZIP file.\n",fn);
  84.       return(1);
  85.     }
  86.   }
  87.   return(0);
  88. }
  89.  
  90. long check_for_files(char *fn)
  91. {
  92.   int f;
  93.   long l,sig,len;
  94.   zip_local_header zl;
  95.   zip_central_dir zc;
  96.   zip_end_dir ze;
  97.   char s[161];
  98.  
  99. #define READ_FN(ln) {read(f,s,ln); s[ln]=0;}
  100.  
  101.   f=open(fn,O_RDWR | O_BINARY);
  102.   if (f>0) {
  103.     l=0;
  104.     len=filelength(f);
  105.     while (l<len) {
  106.       lseek(f,l,SEEK_SET);
  107.       read(f,&sig, 4);
  108.       lseek(f,l,SEEK_SET);
  109.       switch(sig) {
  110.         case ZIP_LOCAL_SIG:
  111.           read(f,&zl, sizeof(zl));
  112.           READ_FN(zl.filename_len);
  113.           if (bad_filename(s)) {
  114.             close(f);
  115.             return(1);
  116.           }
  117.           l += sizeof(zl);
  118.           l += zl.comp_size + zl.filename_len + zl.extra_length;
  119.           break;
  120.         case ZIP_CENT_START_SIG:
  121.           read(f,&zc, sizeof(zc));
  122.           READ_FN(zc.filename_len);
  123.           if (bad_filename(s)) {
  124.             close(f);
  125.             return(1);
  126.           }
  127.           l += sizeof(zc);
  128.           l += zc.filename_len + zc.extra_len;
  129.           break;
  130.         case ZIP_CENT_END_SIG:
  131.           read(f,&ze, sizeof(ze));
  132.           close(f);
  133.           return(0);
  134.         default:
  135.           close(f);
  136.           printf("Error in zip file.\n");
  137.           return(1);
  138.       }
  139.     }
  140.     close(f);
  141.     return(0);
  142.   }
  143.  
  144.   printf("file not found: %s\n",fn);
  145.   return(1);
  146. }
  147.  
  148.  
  149.  
  150. void main(int argc, char *argv[])
  151. {
  152.   char s[161];
  153.  
  154.   if (argc!=3) {
  155.     printf("Put 'UNZIP %1 %2' in INIT.\n");
  156.   } else {
  157.     if (!check_for_files(argv[1])) {
  158.       sprintf(s,"PKUNZIP %s %s",argv[1], argv[2]);
  159.       system(s);
  160.     } else {
  161.       printf("Can't unzip from that .ZIP file.\n\n");
  162.     }
  163.   }
  164. }
  165.  
  166.