summaryrefslogtreecommitdiff
path: root/src/encode.c
blob: be5b8206ed053522dfaaeead85348cbc8598c2d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Handle so called `shell archives'.

   Copyright (C) 1994, 1995, 2007 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "system.h"

/* Basic one-character encoding function to make a char printing.  */
#define ENCODE_BYTE(Byte) ((((Byte) + 63) & 63) + ' ' + 1)

/* Buffer size for one line of output.  */
#define LINE_BUFFER_SIZE 45

/*------------------------------------------.
| Output one GROUP of three bytes on FILE.  |
`------------------------------------------*/

static void
write_encoded_bytes (group, file)
     char *group;
     FILE *file;
{
  int c1, c2, c3, c4;

  c1 = group[0] >> 2;
  c2 = ((group[0] << 4) & (3 << 4)) | ((group[1] >> 4) & 15);
  c3 = ((group[1] << 2) & (15 << 2)) | ((group[2] >> 6) & 3);
  c4 = group[2] & 63;
  putc (ENCODE_BYTE (c1), file);
  putc (ENCODE_BYTE (c2), file);
  putc (ENCODE_BYTE (c3), file);
  putc (ENCODE_BYTE (c4), file);
}

/*--------------------------------------------------------------------.
| From FILE, refill BUFFER up to BUFFER_SIZE raw bytes, returning the |
| number of bytes read.						      |
`--------------------------------------------------------------------*/

static int
read_raw_bytes (file, buffer, buffer_size)
     FILE *file;
     char *buffer;
     int buffer_size;
{
  int character;
  int counter;

  for (counter = 0; counter < buffer_size; counter++)
    {
      character = getc (file);
      if (character == EOF)
	return counter;
      buffer[counter] = character;
    }
  return buffer_size;
}

/*----------------------------------------------------.
| Copy INPUT file to OUTPUT file, while encoding it.  |
`----------------------------------------------------*/

void
copy_file_encoded (input, output)
     FILE *input;
     FILE *output;
{
  char buffer[LINE_BUFFER_SIZE];
  int counter;
  int number_of_bytes;

  while (1)
    {
      number_of_bytes = read_raw_bytes (input, buffer, LINE_BUFFER_SIZE);
      putc (ENCODE_BYTE (number_of_bytes), output);

      for (counter = 0; counter < number_of_bytes; counter += 3)
	write_encoded_bytes (&buffer[counter], output);
      putc ('\n', output);

      if (number_of_bytes == 0)
	break;
    }
}