home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / W / DEVBBS.ZIP / XFERTMP.C < prev    next >
C/C++ Source or Header  |  1992-07-28  |  4KB  |  167 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1991 by Wayne Bell
  5.  
  6. *****************************************************************************/
  7. #include "vars.h"
  8. #pragma hdrstop
  9. #include <dir.h>
  10. #define SETREC(i)  lseek(dlf,((long) (i))*((long)sizeof(uploadsrec)),SEEK_SET);
  11.  
  12. /* the archive type to use */
  13. #define ARC_NUMBER 0
  14.  
  15. /* .ZIP structures and defines */
  16. #define ZIP_LOCAL_SIG 0x04034b50
  17. #define ZIP_CENT_START_SIG 0x02014b50
  18. #define ZIP_CENT_END_SIG 0x06054b50
  19.  
  20. typedef struct {
  21.   unsigned long         signature;    /* 0x04034b50 */
  22.   unsigned short        extract_ver;
  23.   unsigned short        flags;
  24.   unsigned short        comp_meth;
  25.   unsigned short        mod_time;
  26.   unsigned short        mod_date;
  27.   unsigned long         crc_32;
  28.   unsigned long         comp_size;
  29.   unsigned long         uncomp_size;
  30.   unsigned short        filename_len;
  31.   unsigned short        extra_length;
  32. } zip_local_header;
  33.  
  34. typedef struct {
  35.   unsigned long         signature;    /* 0x02014b50 */
  36.   unsigned short        made_ver;
  37.   unsigned short        extract_ver;
  38.   unsigned short        flags;
  39.   unsigned short        comp_meth;
  40.   unsigned short        mod_time;
  41.   unsigned short        mod_date;
  42.   unsigned long         crc_32;
  43.   unsigned long         comp_size;
  44.   unsigned long         uncomp_size;
  45.   unsigned short        filename_len;
  46.   unsigned short        extra_len;
  47.   unsigned short        comment_len;
  48.   unsigned short        disk_start;
  49.   unsigned short        int_attr;
  50.   unsigned long         ext_attr;
  51.   unsigned long         rel_ofs_header;
  52. } zip_central_dir;
  53.  
  54. typedef struct {
  55.   unsigned long         signature;    /* 0x06054b50 */
  56.   unsigned short        disk_num;
  57.   unsigned short        cent_dir_disk_num;
  58.   unsigned short        total_entries_this_disk;
  59.   unsigned short        total_entries_total;
  60.   unsigned long         central_dir_size;
  61.   unsigned long         ofs_cent_dir;
  62.   unsigned short        comment_len;
  63. } zip_end_dir;
  64.  
  65. /* strings not to allow in a .zip file to extract from */
  66. char *bad_words[] = {
  67.   "COMMAND",
  68.   "..",
  69.   NULL,
  70. };
  71.  
  72. long bad_filename(char *fn)
  73. {
  74.   int i;
  75.  
  76.   strupr(fn);
  77.   for (i=0; bad_words[i]; i++) {
  78.     if (strstr(fn,bad_words[i])) {
  79.       npr("Can't extract from that because it has '%s'.\r\n",fn);
  80.       return(1);
  81.     }
  82.   }
  83.   if (!okfn(fn)) {
  84.     npr("Can't extract from that because it has '%s'.\r\n",fn);
  85.     return(1);
  86.   }
  87.   return(0);
  88. }
  89.  
  90. long check_for_files_zip(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.           npr("Error examining that; can't extract from it.\r\n");
  137.           return(1);
  138.       }
  139.     }
  140.     close(f);
  141.     return(0);
  142.   }
  143.   npr("file not found: %s\r\n",fn);
  144.   return(1);
  145. }
  146.  
  147. int check_for_files(char *fn)
  148. {
  149.   char *ss;
  150.  
  151.   ss=strrchr(fn,'.');
  152.   if (ss) {
  153.     if (stricmp(ss+1,"ZIP")==NULL) {
  154.       return(check_for_files_zip(fn));
  155.     } else if (stricmp(ss+1,"ARC")==NULL) {
  156.       /* no processing for .ARC files yet */
  157.     } else {
  158.       /* unknown archive type; let it through. */
  159.     }
  160.   } else {
  161.     /* no extension? */
  162.     npr("No extension.\r\n");
  163.     return(1);
  164.   }
  165.   return(0);
  166. }
  167.