aboutsummaryrefslogtreecommitdiff
path: root/lib/qp.c
blob: c2be1bbc716a7baaba0def1d992ebf27334837d6 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* This file is part of GNU Dico
   Copyright (C) 1999-2019 Free Software Foundation, Inc.
  
   GNU Dico 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.

   GNU Dico 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 GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */

#include <config.h>
#include <dico.h>
#include <string.h>

#define ISWS(c) ((c)==' ' || (c)=='\t')

static int
dico_qp_decode(const char *iptr, size_t isize, char *optr, size_t osize,
	       size_t *pnbytes)
{
    char c;
    size_t consumed = 0;
    size_t wscount = 0;
    size_t nbytes = 0;
    
    while (consumed < isize && nbytes < osize) {
	c = *iptr++;

	if (ISWS(c)) {
	    wscount++;
	    consumed++;
	}
	else {
	    /* Octets with values of 9 and 32 MAY be
	       represented as US-ASCII TAB (HT) and SPACE characters,
	       respectively, but MUST NOT be so represented at the end
	       of an encoded line.  Any TAB (HT) or SPACE characters
	       on an encoded line MUST thus be followed on that line
	       by a printable character. */
	  
	    if (wscount) {
		if (c != '\r' && c != '\n') {
		    size_t sz;
		  
		    if (consumed >= isize)
			break;

		    if (nbytes + wscount > osize)
			sz = osize - nbytes;
		    else
			sz = wscount;
		    memcpy(optr, iptr - wscount - 1, sz);
		    optr += sz;
		    nbytes += sz;
		    if (wscount > sz) {
			wscount -= sz;
			break;
		    }
		}
		wscount = 0;
		if (nbytes == osize)
		    break;
	    }
		
	    if (c == '=') {
		/* There must be 2 more characters before I consume this.  */
		if (consumed + 2 >= isize)
		    break;
		else {
		    /* you get =XX where XX are hex characters.  */
		    char chr[3];
		    int new_c;

		    chr[2] = 0;
		    chr[0] = *iptr++;
		    /* Ignore LF.  */
		    if (chr[0] != '\n') {
			chr[1] = *iptr++;
			new_c = strtoul (chr, NULL, 16);
			*optr++ = new_c;
			nbytes++;
			consumed += 3;
		    } else
			consumed += 2;
		}
	    }
	    /* CR character.  */
	    else if (c == '\r') {
		/* There must be at least 1 more character before
		   I consume this.  */
		if (consumed + 1 >= isize)
		    break;
		else {
		    iptr++; /* Skip the CR character.  */
		    *optr++ = '\n';
		    nbytes++;
		    consumed += 2;
		}
	    } else {
		*optr++ = c;
		nbytes++;
		consumed++;
	    }
	}	  
    }
    *pnbytes = nbytes;
    return consumed - wscount;
}

static int
dico_qp_encode(const char *iptr, size_t isize, char *optr, size_t osize,
	       size_t *pnbytes)
{
    unsigned int c;
    size_t consumed = 0;
    size_t nbytes = 0;
    static const char _hexdigits[] = "0123456789ABCDEF";

    nbytes = 0;

    /* Strategy: check if we have enough room in the output buffer only
       once the required size has been computed. If there is not enough,
       return and hope that the caller will free up the output buffer a
       bit. */

    while (consumed < isize) {
	int simple_char;
      
	/* candidate byte to convert */
	c = *(unsigned char*) iptr;
	simple_char = (c >= 32 && c <= 60)
	               || (c >= 62 && c <= 126)
	               || c == '\t'
	               || c == '\n';

	if (simple_char) {
	    /* a non-quoted character uses up one byte */
	    if (nbytes + 1 > osize) 
		break;

	    *optr++ = c;
	    nbytes++;
	    
	    iptr++;
	    consumed++;

	} else {
	    /* a quoted character uses up three bytes */
	    if (nbytes + 3 > osize) 
		break;

	    *optr++ = '=';
	    *optr++ = _hexdigits[(c >> 4) & 0xf];
	    *optr++ = _hexdigits[c & 0xf];

	    nbytes += 3;

	    /* we've actuall used up one byte of input */
	    iptr++;
	    consumed++;
	}
    }
    *pnbytes = nbytes;
    return consumed;
}


dico_stream_t
dico_qp_stream_create(dico_stream_t str, int mode)
{
    return filter_stream_create(str, 4, 0,
				mode == FILTER_ENCODE ?
				  dico_qp_encode : dico_qp_decode,
				mode);
}

Return to:

Send suggestions and report system problems to the System administrator.