home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / sharutils-4.1-src.tgz / tar.out / fsf / sharutils / encode.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  91 lines

  1. /* Handle so called `shell archives'.
  2.    Copyright (C) 1994 Free Software Foundation, Inc.
  3.   
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.   
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.   
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #include "system.h"
  20.  
  21. /* Basic one-character encoding function to make a char printing.  */
  22. #define ENCODE_BYTE(Byte) ((((Byte) + 63) & 63) + ' ' + 1)
  23.  
  24. /* Buffer size for one line of output.  */
  25. #define LINE_BUFFER_SIZE 45
  26.  
  27. /*------------------------------------------.
  28. | Output one GROUP of three bytes on FILE.  |
  29. `------------------------------------------*/
  30.  
  31. static void
  32. write_encoded_bytes (char *group, FILE *file)
  33. {
  34.   int c1, c2, c3, c4;
  35.  
  36.   c1 = group[0] >> 2;
  37.   c2 = ((group[0] << 4) & (3 << 4)) | ((group[1] >> 4) & 15);
  38.   c3 = ((group[1] << 2) & (15 << 2)) | ((group[2] >> 6) & 3);
  39.   c4 = group[2] & 63;
  40.   putc (ENCODE_BYTE (c1), file);
  41.   putc (ENCODE_BYTE (c2), file);
  42.   putc (ENCODE_BYTE (c3), file);
  43.   putc (ENCODE_BYTE (c4), file);
  44. }
  45.  
  46. /*--------------------------------------------------------------------.
  47. | From FILE, refill BUFFER up to BUFFER_SIZE raw bytes, returning the |
  48. | number of bytes read.                              |
  49. `--------------------------------------------------------------------*/
  50.  
  51. static int
  52. read_raw_bytes (FILE *file, char *buffer, int buffer_size)
  53. {
  54.   int character;
  55.   int counter;
  56.  
  57.   for (counter = 0; counter < buffer_size; counter++)
  58.     {
  59.       character = getc (file);
  60.       if (character == EOF)
  61.     return counter;
  62.       buffer[counter] = character;
  63.     }
  64.   return buffer_size;
  65. }
  66.  
  67. /*----------------------------------------------------.
  68. | Copy INPUT file to OUTPUT file, while encoding it.  |
  69. `----------------------------------------------------*/
  70.  
  71. void
  72. copy_file_encoded (FILE *input, FILE *output)
  73. {
  74.   char buffer[LINE_BUFFER_SIZE];
  75.   int counter;
  76.   int number_of_bytes;
  77.  
  78.   while (1)
  79.     {
  80.       number_of_bytes = read_raw_bytes (input, buffer, LINE_BUFFER_SIZE);
  81.       putc (ENCODE_BYTE (number_of_bytes), output);
  82.  
  83.       for (counter = 0; counter < number_of_bytes; counter += 3)
  84.     write_encoded_bytes (&buffer[counter], output);
  85.       putc ('\n', output);
  86.  
  87.       if (number_of_bytes == 0)
  88.     break;
  89.     }
  90. }
  91.