home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / W / WWIVSOR.ZIP / SRRCV.C < prev    next >
C/C++ Source or Header  |  1995-04-25  |  8KB  |  366 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1995 by Wayne Bell
  5.  
  6. Distribution of the source code for WWIV, in any form, modified or unmodified,
  7. without PRIOR, WRITTEN APPROVAL by the author, is expressly prohibited.
  8. Distribution of compiled versions of WWIV is limited to copies compiled BY
  9. THE AUTHOR.  Distribution of any copies of WWIV not compiled by the author
  10. is expressly prohibited.
  11.  
  12.  
  13. *****************************************************************************/
  14.  
  15.  
  16.  
  17. #include "vars.h"
  18.  
  19. #pragma hdrstop
  20.  
  21. #include <math.h>
  22.  
  23.  
  24. char modemkey(int *tout)
  25. {
  26.   double d1;
  27.   char ch;
  28.  
  29.   if (comhit()) {
  30.     ch=get1c();
  31.     calc_CRC(ch);
  32.     return(ch);
  33.   }
  34.   if (*tout)
  35.     return(0);
  36.   d1=timer();
  37.   while ((fabs(timer()-d1)<0.5) && (!comhit()) && (!hangup)) {
  38.     checkhangup();
  39.   }
  40.   if (comhit()) {
  41.     ch=get1c();
  42.     calc_CRC(ch);
  43.     return(ch);
  44.   }
  45.   *tout=1;
  46.   return(0);
  47. }
  48.  
  49.  
  50.  
  51. int receive_block(char *b, unsigned char *bln, int ucrc)
  52. {
  53.   int i,abort,err,tout,cc1;
  54.   unsigned char ch,bn,bn1,cs1;
  55.  
  56.   abort=0;
  57.   ch=gettimeout(5.0,&abort);
  58.   err=0;
  59.   if (abort)
  60.     return(6);
  61.   tout=0;
  62.   if (ch==0x81) {
  63.     bn=modemkey(&tout);
  64.     bn1=modemkey(&tout);
  65.     if ((bn ^ bn1)==0xff) {
  66.       b[0]=bn;
  67.       *bln=bn;
  68.       return(8);
  69.     } else
  70.       return(3);
  71.   } else
  72.     if (ch==1) {
  73.       bn=modemkey(&tout);
  74.       bn1=modemkey(&tout);
  75.       if ((bn ^ bn1)!=0xff)
  76.         err=3;
  77.       *bln=bn;
  78.       crc=0;
  79.       checksum=0;
  80.       for (i=0; (i<128) && (!hangup); i++)
  81.         b[i]=modemkey(&tout);
  82.       if ((!ucrc) && (!hangup)) {
  83.         cs1=checksum;
  84.         bn1=modemkey(&tout);
  85.         if (bn1!=cs1) {
  86.           err=2;
  87.         }
  88.       } else if (!hangup) {
  89.         cc1=crc;
  90.         bn=modemkey(&tout);
  91.         bn1=modemkey(&tout);
  92.         if ((bn!=(unsigned char)(cc1 >> 8)) || (bn1!=(unsigned char)(cc1 & 0x00ff)))
  93.           err=2;
  94.       }
  95.       if (tout)
  96.         return(7);
  97.       return(err);
  98.     } else
  99.       if (ch==2) {
  100.         bn=modemkey(&tout);
  101.         bn1=modemkey(&tout);
  102.         crc=0;
  103.         checksum=0;
  104.         if ((bn ^ bn1)!=0xff)
  105.           err=3;
  106.         *bln=bn;
  107.         for (i=0; (i<1024) && (!hangup); i++)
  108.           b[i]=modemkey(&tout);
  109.         if ((!ucrc) && (!hangup)){
  110.           cs1=checksum;
  111.           bn1=modemkey(&tout);
  112.           if (bn1!=cs1)
  113.             err=2;
  114.         } else if (!hangup) {
  115.           cc1=crc;
  116.           bn=modemkey(&tout);
  117.           bn1=modemkey(&tout);
  118.           if ((bn!=(unsigned char)(cc1 >> 8)) || (bn1!=(unsigned char)(cc1 & 0x00ff)))
  119.             err=2;
  120.         }
  121.         if (tout)
  122.           return(7);
  123.         if (err==0)
  124.           return(1);
  125.         else
  126.           return(err);
  127.       } else
  128.         if (ch==24) {
  129.           return(4);
  130.         } else
  131.           if (ch==4) {
  132.             return(5);
  133.           } else
  134.             if (ch==0)
  135.               return(7);
  136.              else
  137.               return(9);
  138. }
  139.  
  140.  
  141. void xymodem_receive(char *fn, char *ft, int *received, int ucrc)
  142. {
  143.   char b[1025],x[81],ch;
  144.   unsigned char bln;
  145.   unsigned int bn;
  146.   int done,ok,lastcan,lasteot,terr,cerr;
  147.   long pos,reallen,filedatetime,lx;
  148.   int f,i,i1,i2,i3,ox,oy;
  149.   double tpb;
  150.   struct ftime ff;
  151.   struct date d;
  152.   struct time t;
  153.   double d1;
  154.  
  155.   unlink(fn);
  156.   f=sh_open(fn,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  157.   ok=1;
  158.   lastcan=0;
  159.   lasteot=0;
  160.   terr=0;
  161.   cerr=0;
  162.   if (f<0) {
  163.     nln(2);
  164.     pl(get_string(908));
  165.     nl();
  166.     *received=0;
  167.     return;
  168.   }
  169.   pos=0L;
  170.   reallen=0L;
  171.   filedatetime=0L;
  172.   bn=1;
  173.   done=0;
  174.   tpb=(12.656) / ((double) (modem_speed));
  175.   nl();
  176.   pl(get_string(887));
  177.   ox=WhereX();
  178.   oy=WhereY();
  179.   movecsr(52,0);
  180.   outs(get_stringx(1,67));
  181.   movecsr(52,1);
  182.   outs(get_stringx(1,68));
  183.   movecsr(52,2);
  184.   outs(get_stringx(1,69));
  185.   movecsr(52,3);
  186.   outs(get_stringx(1,70));
  187.   movecsr(52,4);
  188.   outs(get_stringx(1,71));
  189.   movecsr(52,5);
  190.   outs(get_stringx(1,72));
  191.   movecsr(52,6);
  192.   outs(get_stringx(1,73));
  193.   movecsr(65,0);
  194.   outs(stripfn(fn));
  195.   i=0;
  196.   do {
  197.     if (i++>9) {
  198.       *received=0;
  199.       sh_close(f);
  200.       unlink(fn);
  201.       return;
  202.     }
  203.     if (ucrc)
  204.       outcomch('C');
  205.     else
  206.       outcomch(21);
  207.  
  208.     d1=timer();
  209.     while ((fabs(timer()-d1)<10.0) && (!comhit()) && (!hangup)) {
  210.       checkhangup();
  211.       if (kbhitb()) {
  212.         ch=getchd();
  213.         if (ch==0)
  214.           getchd();
  215.         else
  216.           if (ch==27) {
  217.             done=1;
  218.             ok=0;
  219.           }
  220.       }
  221.     }
  222.   } while(!comhit() && !hangup);
  223.  
  224.   do {
  225.     bln=255;
  226.     itoa(cerr,x,10);
  227.     strcat(x,"  ");
  228.     movecsr(69,4);
  229.     outs(x);
  230.     itoa(terr,x,10);
  231.     movecsr(69,5);
  232.     outs(x);
  233.     sprintf(x,"%ld - %ldk",
  234.       pos/128+1,
  235.       pos/1024+1);
  236.     movecsr(65,3);
  237.     outs(x);
  238.     if (reallen) {
  239.       movecsr(65,1);
  240.       outs(ctim(((double)(reallen-pos))*tpb));
  241.     }
  242.  
  243.     i=receive_block(b,&bln,ucrc);
  244.     if ((i==0) || (i==1)) {
  245.       if ((bln==0) && (pos==0L)) {
  246.         i1=strlen(b)+1;
  247.         i3=i1;
  248.         while ((b[i3]>='0') && (b[i3]<='9') && ((i3-i1)<15))
  249.           x[i3-i1]=b[i3++];
  250.         x[i3-i1]=0;
  251.         reallen=atol(x);
  252.         sprintf(x,"%ld - %ldk",
  253.           (reallen+127)/128,
  254.           bytes_to_k(reallen));
  255.         movecsr(65,2);
  256.         outs(x);
  257.         while ((b[i1]!=32) && (i1<64))
  258.           ++i1;
  259.         if (b[i1]==32) {
  260.           ++i1;
  261.           while ((b[i1]>='0') && (b[i1]<='8')) {
  262.             filedatetime=(filedatetime*8) + ((long) (b[i1]-'0'));
  263.             ++i1;
  264.           }
  265.           i1+=timezone+5*60*60;
  266.         }
  267.         outcomch(6);
  268.       } else
  269.         if ((bn & 0x00ff)==(unsigned int)bln) {
  270.           sh_lseek(f,pos, SEEK_SET);
  271.           lx=reallen-pos;
  272.           if (i==0)
  273.             i2=128;
  274.           else
  275.             i2=1024;
  276.           if ((((long) i2)>lx) && (reallen))
  277.             i2=(int) lx;
  278.           sh_write(f,(void *)b,i2);
  279.           pos += (long)i2;
  280.           ++bn;
  281.           outcomch(6);
  282.         } else
  283.           if (((bn-1) & 0x00ff)==(unsigned int)bln) {
  284.             outcomch(6);
  285.           } else {
  286.             outcomch(24);
  287.             ok=0;
  288.             done=1;
  289.           }
  290.       cerr=0;
  291.     } else
  292.       if ((i==2) || (i==7) || (i==3)) {
  293.         if ((pos==0L) && (reallen==0L) && (ucrc))
  294.           outcomch('C');
  295.         else
  296.           outcomch(21);
  297.         ++cerr;
  298.         ++terr;
  299.         if (cerr>9) {
  300.           outcomch(24);
  301.           ok=0;
  302.           done=1;
  303.         }
  304.       } else
  305.         if (i==6) {
  306.           ok=0;
  307.           done=1;
  308.           outcomch(24);
  309.         } else
  310.           if (i==4) {
  311.             if (lastcan) {
  312.               ok=0;
  313.               done=1;
  314.               outcomch(6);
  315.             } else {
  316.               lastcan=1;
  317.               outcomch(21);
  318.             }
  319.           } else
  320.             if (i==5) {
  321.               lasteot=1;
  322.               if (lasteot) {
  323.                 done=1;
  324.                 outcomch(6);
  325.               } else {
  326.                 lasteot=1;
  327.                 outcomch(21);
  328.               }
  329.             } else
  330.               if (i==8) {
  331.                 *ft=bln;
  332.                 outcomch(6);
  333.                 cerr=0;
  334.               } else
  335.                 if (i==9)
  336.                   dump();
  337.     if (i!=4)
  338.       lastcan=0;
  339.     if (i!=5)
  340.       lasteot=0;
  341.   } while ((!hangup) && (!done));
  342.   movecsr(ox,oy);
  343.   if (ok) {
  344.     if (filedatetime) {
  345.       unixtodos(filedatetime,&d,&t);
  346.       if (d.da_year>=1980) {
  347.         ff.ft_min=t.ti_min;
  348.         ff.ft_hour=t.ti_hour;
  349.         ff.ft_tsec=t.ti_sec/2;
  350.         ff.ft_year=d.da_year-1980;
  351.         ff.ft_day=d.da_day;
  352.         ff.ft_month=d.da_mon;
  353.         setftime(f,&ff);
  354.       }
  355.     }
  356.     sh_close(f);
  357.     *received=1;
  358.   } else {
  359.     sh_close(f);
  360.     unlink(fn);
  361.     *received=0;
  362.   }
  363. }
  364.  
  365.  
  366.