1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /* Copyright (c) 1988 AT&T */
22 /* All Rights Reserved */
23 /*
24 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 */
27 /*
28 * This file contains modifications Copyright 2006-2009 J. Schilling
29 *
30 * @(#)encode.c 1.5 09/11/08 J. Schilling
31 */
32 #if defined(sun)
33 #pragma ident "@(#)encode.c 1.5 09/11/08 J. Schilling"
34 #endif
35 /*
36 * @(#)encode.c 1.4 06/12/12
37 */
38
39 #if defined(sun)
40 #pragma ident "@(#)encode.c"
41 #pragma ident "@(#)sccs:lib/comobj/encode.c"
42 #endif
43 # include <defines.h>
44
45 /* ENC is the basic 1 character encoding function to make a char printing */
46 #define ENC(c) (((c) & 077) + ' ')
47
48 /* single character decode */
49 #define DEC(c) (((c) - ' ') & 077)
50
51 static int fr __PR((FILE *, char *, int));
52 static void e_outdec __PR((char *, FILE *));
53 static void d_outdec __PR((char *, FILE *, int));
54
55 void
encode(infile,outfile)56 encode(infile,outfile)
57 FILE *infile;
58 FILE *outfile;
59 {
60 char buf[80];
61 int i,n;
62
63 for (;;)
64 {
65 /* 1 (up to) 45 character line */
66 n = fr(infile, buf, 45);
67 putc(ENC(n), outfile);
68 for (i=0; i<n; i += 3)
69 e_outdec(&buf[i], outfile);
70
71 putc('\n', outfile);
72 if (n <= 0)
73 break;
74 }
75 }
76
77 /*
78 * output one group of 3 bytes, pointed at by p, on file f.
79 */
80 static void
e_outdec(p,f)81 e_outdec(p, f)
82 char *p;
83 FILE *f;
84 {
85 int c1, c2, c3, c4;
86
87
88 c1 = *p >> 2;
89 c2 = ((*p << 4) & 060) | ((p[1] >> 4) & 017);
90 c3 = ((p[1] << 2) & 074) | ((p[2] >> 6) & 03);
91 c4 = p[2] & 077;
92 putc(ENC(c1), f);
93 putc(ENC(c2), f);
94 putc(ENC(c3), f);
95 putc(ENC(c4), f);
96 }
97
98 void
decode(istr,outfile)99 decode(istr,outfile)
100 char *istr;
101 FILE *outfile;
102 {
103 char *bp;
104 int n;
105
106 n = DEC(istr[0]);
107 if (n <= 0)
108 return;
109
110 bp = &istr[1];
111 while (n > 0) {
112 d_outdec(bp, outfile, n);
113 bp += 4;
114 n -= 3;
115 }
116 }
117
118 /*
119 * output a group of 3 bytes (4 input characters).
120 * the input chars are pointed to by p, they are to
121 * be output to file f. n is used to tell us not to
122 * output all of them at the end of the file.
123 */
124 static void
d_outdec(p,f,n)125 d_outdec(p, f, n)
126 char *p;
127 FILE *f;
128 int n;
129 {
130 int c1, c2, c3;
131
132 c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
133 c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
134 c3 = DEC(p[2]) << 6 | DEC(p[3]);
135 if (n >= 1)
136 putc(c1, f);
137 if (n >= 2)
138 putc(c2, f);
139 if (n >= 3)
140 putc(c3, f);
141 }
142
143 /* fr: like read but stdio */
144 static int
fr(fd,buf,cnt)145 fr(fd, buf, cnt)
146 FILE *fd;
147 char *buf;
148 int cnt;
149 {
150 int c, i;
151
152 for (i=0; i<cnt; i++) {
153 c = getc(fd);
154 if (c == EOF)
155 return(i);
156 buf[i] = c;
157 }
158 return (cnt);
159 }
160