home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / t3dlib_src_r43.lha / encode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-02  |  2.0 KB  |  91 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. /* modified from uuencode.c V 5.3 from Berkeley 1/22/85 for use with */
  19. /* T3DLIB by Rob Hounsell */
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include "t3dlib.h"
  25.  
  26. /* ENC is the basic 1 character encoding function to make a char printing */
  27. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  28.  
  29. encode_data (buffer, file, size)
  30. register char *buffer;
  31. FILE *file;
  32. ULONG size;
  33. {
  34.     char buf[80];
  35.     int i, n;
  36.  
  37.     for (;;) {
  38.         /* 1 (up to) 45 character line */
  39.         if (size <= 45)
  40.         {
  41.             n = fr(file, buf, size);
  42.         } 
  43.         else {
  44.             n = fr(file, buf, 45);
  45.         }
  46.         size -= n;
  47.         *buffer++ = ENC(n);
  48.  
  49.         for (i=0; i<n; i += 3)
  50.         {
  51.             int c1, c2, c3, c4;
  52.  
  53.             c1 = buf[i] >> 2;
  54.             c2 = (buf[i] << 4) & 060 | (buf[i+1] >> 4) & 017;
  55.             c3 = (buf[i+1] << 2) & 074 | (buf[i+2] >> 6) & 03;
  56.             c4 = buf[i+2] & 077;
  57.             *buffer++ = ENC(c1);
  58.             *buffer++ = ENC(c2);
  59.             *buffer++ = ENC(c3);
  60.             *buffer++ = ENC(c4);
  61.         }
  62.  
  63.         /* *buffer++ = '\n';  */
  64.         if (size <= 0)
  65.             break;
  66.     }
  67.     *buffer = '\0';
  68. }
  69.  
  70. /*
  71.  * output one group of 3 bytes, pointed at by p, on file f.
  72.  */
  73.  
  74. /* fr: like read but stdio */
  75. int
  76. fr(fd, buf, cnt)
  77. FILE *fd;
  78. char *buf;
  79. int cnt;
  80. {
  81.     int c, i;
  82.  
  83.     for (i=0; i<cnt; i++) {
  84.         c = getc(fd);
  85.         if (c == EOF)
  86.             return(i);
  87.         buf[i] = c;
  88.     }
  89.     return (cnt);
  90. }
  91.