home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 5 / QRZ Ham Radio Callsign Database - Volume 5.iso / files / amiga / csrc720j.lzh / yapp.c < prev   
Encoding:
C/C++ Source or Header  |  1993-04-22  |  7.0 KB  |  317 lines

  1. /* Yapp.c   YAPP protocol for FBB forwarding in CBBS.
  2.  
  3.    Copyright Peter Hardie VE5VA  1993
  4.  
  5. */
  6.  
  7. #include "mb.h"
  8.  
  9.  
  10.  
  11. #define NUL 0
  12. #define SOH 1
  13. #define STX 2
  14. #define EOT 4
  15. extern unsigned char rdchar();
  16. extern short debug;
  17. /* rflag is in ser.c. It indicates, when on that rdchar should not start
  18.    the next I/O
  19. */
  20. extern short rflag;
  21. /* rdblock reads a block of the specified number of bytes into the buffer.
  22.    If the length is specified as -1 then a null terminated string is read.
  23. */
  24. long tempcount;   /* Actually only used in debugging */
  25. extern unsigned long textsize;
  26. extern char fbb_flag,tmpstr[];
  27. short buf_index;
  28. short buf_num;
  29. short huf_error;
  30. short yapp_eot;
  31. short block_cancel = 0; /* Set to one by i/o routine if SYSOP hits ^F */
  32. short checksum;
  33. unsigned char *yapp_buf;
  34.  
  35. char *out_buf_adr;        /* output buffer */
  36. char *in_buf_adr;         /* input buffer */
  37. short out_index;
  38. extern FILE *infl;
  39. FILE *tfl,*ofl;
  40. snd_yapp()
  41. {
  42.    register char *q;
  43.    register PORTS *p;
  44.    register FILE *fl;
  45.    register int i;
  46.  
  47.    block_cancel = 0;
  48.    p = port;
  49.    sprintf(p->line, "%s%u", msgdir, port->mmhs->number);
  50.    if((fl = fopen(p->line, "rb")) is NULL) {
  51.       printf("Can't find input file %s\n",p->line);
  52.       return(1);
  53.    }
  54.    if((infl = fopen("t:fbb.tmp","w+b")) == NULL) {
  55.       printf("Can't create temporary  file t:fbb.tmp\n");
  56.       fclose(fl);
  57.       return(2);
  58.    }
  59.    /* Initialize output buffer */
  60.    out_index = 0;
  61. tempcount = 0;
  62.    /* send the title */
  63.    puthuf(SOH);
  64.    puthuf(0);     /* Filled in later as length of data */
  65.    q = p->mmhs->title;
  66.    ttputs("Sending Compressed Msg - title:\n");
  67.    ttputs(q);
  68.    ttputs("\n");
  69.    while(*q && (*q != '\n'))puthuf(*q++);
  70.    puthuf(0);
  71.    puthuf('0');   /* Offset length is always zero */
  72.    puthuf(0);
  73.    closehuf();    /* Force transmission of the header */
  74.    /* Now, while that is being sent, we have a bit of time to create a
  75.       temporary file containing the processed message so that we can find
  76.       its true length with LF replaced by CR/LF and my R: header.
  77.    */
  78.    curtim();
  79.    fbb_flag = 1;
  80.    prtx(mm[4]);
  81.    fbb_flag = 0;
  82.    q = tmpstr;
  83.    fputs(tmpstr,infl);
  84.    fseek(fl, (long)RECSIZE, 0);
  85. if(debug)tfl = fopen("t:fbb.xmt","wb");
  86.    while(fgets(tmp->scr, scrmax, fl) isnt NULL) {
  87.       if (chkdis()) {
  88. if(debug)fclose(tfl);
  89.          fclose(fl);
  90.          fclose(infl);
  91.          return(3);
  92.       }
  93.       q = tmp->scr;
  94.       while(*q && (*q != '\n'))q++;
  95.       *q++ = '\r';
  96.       *q++ = '\n';
  97.       *q = 0;
  98.       fputs(tmp->scr,infl);
  99.    }
  100.    fclose(fl);
  101.    rewind(infl);
  102.    checksum = 0;
  103.    if(s_mart & cmpok) {
  104.       i = Encode();
  105.    }
  106. #ifdef MCH_ZOO
  107.    else {
  108.       i = lzc();
  109.    }
  110. #endif
  111.    if(i) {
  112.       ttputs("Problem in encode or console abort\n");
  113.       fclose(infl);
  114. if(debug)fclose(tfl);
  115.       return(4);
  116.    }
  117.    /* Send EOT and checksum */
  118.    out_index = 0;
  119.    i = checksum;
  120.    puthuf(EOT);
  121.    puthuf((-i)&0xff);
  122.    sendblock(out_buf_adr,2);
  123.    fclose(infl);
  124. /* unlink("t:fbb.tmp"); */
  125.    ttputs("File Sent\n");
  126. if(debug)fclose(tfl);
  127.    return(0);
  128. }
  129.  
  130. rcv_yapp(infile)
  131. char *infile;
  132. {
  133.    register int i;
  134.    register PORTS *p;
  135.    register char *q;
  136.    char *r;
  137.    unsigned char offset[10],c;
  138.    unsigned char intitle[100];
  139.  
  140.    block_cancel = 0;
  141.    yapp_eot = 0;
  142.    p = port;
  143.    out_index = 0;
  144.    checksum = 0;
  145.    if((p->fl = fopen(infile, "wb")) is NULL) {
  146.       p->msg = mcant;
  147.       return(1);
  148.    }
  149.    fill(tmp->scr, '.', RECSIZE);
  150.    fwrite(tmp->scr,256,1, p->fl);
  151.    /* First, read the header information */
  152.    rflag = 1;
  153.    if((i = rdchar()) != SOH) {
  154.       printf("No SOH at start of compressed forward - %02x\n",i);
  155.       fclose(p->fl);
  156.       unlink(infile);
  157.       restart();
  158.       return(1);
  159.    }
  160.    /* Get the message header length */
  161.    if(rdblock(&c,1))return(1);
  162. if(debug) {
  163.    sprintf(tmpstr,"Length = %d\n",c);
  164.    ttputs(tmpstr);
  165. }
  166. /*   if(rdblock(p->mmhs->title,-80))return(1);*/
  167. /* FBB can blow up and send a title longer than 80 characters
  168.    So this code reads a long title into a temporary string and then
  169.    copies only, at most, 79 characters into the title string
  170. */
  171.    if(rdblock(intitle,-95))return(1);
  172.    strncpy(p->mmhs->title,(char *)intitle,79);
  173.    ttputs("Receiving compressed message - title:\n");
  174.    ttputs(p->mmhs->title);
  175.    ttputs("\n");
  176. /* At the moment this is ignored even by FBB */
  177.    if(rdblock(&offset[0],-8))return(1);
  178. if(debug)ofl = fopen("t:fbb.rcv","wb");
  179.    huf_error = 0;
  180.    buf_index = 257;
  181.    buf_num = 0;
  182.    filesize = 0;
  183. tempcount = 0;
  184.    if(s_mart & cmpok) {
  185.       i = Decode();
  186.    }
  187. #ifdef MCH_ZOO
  188.    else {
  189.       i = lzd();
  190.    }
  191. #endif
  192.    if(i) {
  193.       fclose(port->fl);
  194.       unlink(infile);
  195.       ttputs("Decoding error or console abort\n");
  196.       restart();
  197.       return(1);
  198.    }
  199.    fclose(p->fl);
  200.    if(!yapp_eot) {
  201. if(debug)fclose(ofl);
  202.       if(rdblock(&c,1))return(1);
  203.       restart();
  204.       if(c != EOT) {
  205.          printf("EOT not found - %02x\n",c);
  206. /*         unlink(infile);*/
  207.          return(1);
  208.       }
  209. if(debug)printf("OTHER_EOT\n");
  210.    }
  211.  
  212.    checksum += rdchar();
  213.    if(checksum & 0xFF) {
  214.       printf("Checksum error in FBB compressed forward\n");
  215.       unlink(infile);
  216.       return(1);
  217.    }
  218.    ttputs("Done\n");
  219.    return(0);
  220. }
  221. unsigned char gethuf()
  222. {
  223.    register int i;
  224.    register unsigned char *q;
  225.    unsigned char c;
  226.  
  227.    if(yapp_eot)return(0);
  228.    if(buf_index >= buf_num) {
  229.       rdblock(&c,1);
  230.       if(c == EOT) {
  231.          restart();
  232. if(debug) {
  233.   printf("YAPP-EOT\n");
  234.   fclose(ofl);
  235. }
  236.          yapp_eot = 1;
  237.          return(0);
  238.       }
  239.       if(c != STX) {
  240.          huf_error = 1;
  241.          printf("Missing STX in compressed forward - %02x\n",c);
  242.          return(0);
  243.       }
  244.       buf_index = 0;
  245.       rdblock(&c,1);
  246.       buf_num = c;
  247.       if(buf_num == 0)buf_num = 256;
  248.       rdblock(yapp_buf,buf_num);
  249. if(debug) {
  250.   fwrite(yapp_buf,buf_num,1,ofl);
  251.   tempcount++;
  252.   printf("Got %3ld length = %d\n",tempcount,buf_num);
  253. }
  254.       q = yapp_buf;
  255.       i = buf_num;
  256.       do {
  257.          checksum += *q++;
  258.       } while(--i);
  259.    }
  260. if(s_mart & zoook)putc(*(yapp_buf+buf_index),stdout);
  261.    return(*(yapp_buf+buf_index++));
  262. }
  263. putdec(cc)
  264. int cc;
  265. {
  266.    register unsigned char c;
  267.    c = cc&0xff;
  268.    if(c != '\r') {
  269.       *(out_buf_adr + out_index++) = c;
  270.       filesize++;
  271.       if(out_index == 256) {
  272.          fwrite(out_buf_adr,256,1,port->fl);
  273.          out_index = 0;
  274.       }
  275.    }
  276. }
  277. closedec()
  278. {
  279.    if(out_index) {
  280.       fwrite(out_buf_adr,out_index,1,port->fl);
  281.       out_index = 0;
  282.    }
  283. }
  284. puthuf(cc)
  285. int cc;
  286. {
  287.    register unsigned char c;
  288.    c = cc & 0xff;
  289.    checksum += c;
  290.    *(out_buf_adr + out_index++) = c;
  291.    if(out_index == 251) {
  292.       *(out_buf_adr + 1) = out_index - 2;
  293.       out_index = 2;
  294. if(debug) {
  295.   fwrite(out_buf_adr+2,249,1,tfl);
  296.   printf("Put %3ld length = 249\n",tempcount++);
  297. }
  298.       return(sendblock(out_buf_adr,251));
  299.    }
  300.    return(0);
  301. }
  302. closehuf()
  303. {
  304.    register int i;
  305.  
  306.    if(out_index > 2) {
  307.       *(out_buf_adr + 1) = out_index - 2;
  308.       sendblock(out_buf_adr,out_index);
  309. if(debug) {
  310.   fwrite(out_buf_adr+2,out_index-2,1,tfl);
  311.   printf("Put %3ld length = %d\n",tempcount++,out_index-2);
  312. }
  313.       out_index = 2;
  314.       *out_buf_adr = STX;
  315.    }
  316. }
  317.