1 /*
2 * "streamable kanji code filter and converter"
3 * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
4 *
5 * LICENSE NOTICES
6 *
7 * This file is part of "streamable kanji code filter and converter",
8 * which is distributed under the terms of GNU Lesser General Public
9 * License (version 2) as published by the Free Software Foundation.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with "streamable kanji code filter and converter";
18 * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 * Suite 330, Boston, MA 02111-1307 USA
20 *
21 * The author of this file:
22 *
23 */
24 /*
25 * The source code included in this files was separated from mbfilter.c
26 * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
27 *
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "mbfilter.h"
35 #include "mbfilter_uuencode.h"
36
37 const mbfl_encoding mbfl_encoding_uuencode = {
38 mbfl_no_encoding_uuencode,
39 "UUENCODE",
40 "x-uuencode",
41 NULL,
42 NULL,
43 MBFL_ENCTYPE_SBCS,
44 NULL,
45 NULL
46 };
47
48 const struct mbfl_convert_vtbl vtbl_uuencode_8bit = {
49 mbfl_no_encoding_uuencode,
50 mbfl_no_encoding_8bit,
51 mbfl_filt_conv_common_ctor,
52 mbfl_filt_conv_common_dtor,
53 mbfl_filt_conv_uudec,
54 mbfl_filt_conv_common_flush
55 };
56
57 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
58
59 /* uuencode => any */
60 #define UUDEC(c) (char)(((c)-' ')&077)
61 static const char * uuenc_begin_text = "begin ";
62 enum { uudec_state_ground=0, uudec_state_inbegin,
63 uudec_state_until_newline,
64 uudec_state_size, uudec_state_a, uudec_state_b, uudec_state_c, uudec_state_d,
65 uudec_state_skip_newline};
66
mbfl_filt_conv_uudec(int c,mbfl_convert_filter * filter)67 int mbfl_filt_conv_uudec(int c, mbfl_convert_filter * filter)
68 {
69 int n;
70
71 switch(filter->status) {
72 case uudec_state_ground:
73 /* looking for "begin 0666 filename\n" line */
74 if (filter->cache == 0 && c == 'b')
75 {
76 filter->status = uudec_state_inbegin;
77 filter->cache = 1; /* move to 'e' */
78 }
79 else if (c == '\n')
80 filter->cache = 0;
81 else
82 filter->cache++;
83 break;
84 case uudec_state_inbegin:
85 if (uuenc_begin_text[filter->cache++] != c) {
86 /* doesn't match pattern */
87 filter->status = uudec_state_ground;
88 break;
89 }
90 if (filter->cache == 5)
91 {
92 /* thats good enough - wait for a newline */
93 filter->status = uudec_state_until_newline;
94 filter->cache = 0;
95 }
96 break;
97 case uudec_state_until_newline:
98 if (c == '\n')
99 filter->status = uudec_state_size;
100 break;
101 case uudec_state_size:
102 /* get "size" byte */
103 n = UUDEC(c);
104 filter->cache = n << 24;
105 filter->status = uudec_state_a;
106 break;
107 case uudec_state_a:
108 /* get "a" byte */
109 n = UUDEC(c);
110 filter->cache |= (n << 16);
111 filter->status = uudec_state_b;
112 break;
113 case uudec_state_b:
114 /* get "b" byte */
115 n = UUDEC(c);
116 filter->cache |= (n << 8);
117 filter->status = uudec_state_c;
118 break;
119 case uudec_state_c:
120 /* get "c" byte */
121 n = UUDEC(c);
122 filter->cache |= n;
123 filter->status = uudec_state_d;
124 break;
125 case uudec_state_d:
126 /* get "d" byte */
127 {
128 int A, B, C, D = UUDEC(c);
129 A = (filter->cache >> 16) & 0xff;
130 B = (filter->cache >> 8) & 0xff;
131 C = (filter->cache) & 0xff;
132 n = (filter->cache >> 24) & 0xff;
133 if (n-- > 0)
134 CK((*filter->output_function)( (A << 2) | (B >> 4), filter->data));
135 if (n-- > 0)
136 CK((*filter->output_function)( (B << 4) | (C >> 2), filter->data));
137 if (n-- > 0)
138 CK((*filter->output_function)( (C << 6) | D, filter->data));
139 filter->cache = n << 24;
140
141 if (n == 0)
142 filter->status = uudec_state_skip_newline; /* skip next byte (newline) */
143 else
144 filter->status = uudec_state_a; /* go back to fetch "A" byte */
145 }
146 break;
147 case uudec_state_skip_newline:
148 /* skip newline */
149 filter->status = uudec_state_size;
150 }
151 return c;
152 }
153