-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqp.c
More file actions
140 lines (126 loc) · 2.98 KB
/
qp.c
File metadata and controls
140 lines (126 loc) · 2.98 KB
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
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "utils.h"
#include "wmail.h"
static char hex2int(int c)
{
if (isdigit(c)) {
return c - '0';
}
else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
else {
return -1;
}
}
/*
*
* Decoding Quoted-printable string.
*
*/
/* {{{ proto string quoted_printable_decode(string str)
Convert a quoted-printable string to an 8 bit string */
char *quoted_printable_decode(char *str_in,char *str_out,int header) {
int i = 0, j = 0, k;
str_out = malloc(strlen(str_in) + 1);
while (str_in[i]) {
switch (str_in[i]) {
case '=':
if (str_in[i + 1] && str_in[i + 2] &&
isxdigit((int) str_in[i + 1]) &&
isxdigit((int) str_in[i + 2]))
{
str_out[j++] = (hex2int((int) str_in[i + 1]) << 4)
+ hex2int((int) str_in[i + 2]);
i += 3;
} else /* check for soft line break according to RFC 2045*/ {
k = 1;
while (str_in[i + k] && ((str_in[i + k] == 32) || (str_in[i + k] == 9))) {
/* Possibly, skip spaces/tabs at the end of line */
k++;
}
if (!str_in[i + k]) {
/* End of line reached */
i += k;
}
else if ((str_in[i + k] == 13) && (str_in[i + k + 1] == 10)) {
/* CRLF */
i += k + 2;
}
else if ((str_in[i + k] == 13) || (str_in[i + k] == 10)) {
/* CR or LF */
i += k + 1;
}
else {
str_out[j++] = str_in[i++];
}
}
break;
case '_':
if (header) {
str_out[j++] = 0x20;
++i;
} else {
str_out[j++] = str_in[i++];
}
break;
default:
str_out[j++] = str_in[i++];
}
}
str_out[j] = '\0';
return str_out;
}
char *quoted_printable_encode(char *str, int str_len, int binary_mode) {
long line_len = 76;
char *str_in, *str_out;
int i = 0, j = 0, l = 0;
int buf_len;
str_in = str;
buf_len = (str_len * 3) + (((str_len * 3 / line_len) + 1) * 3) + 1;
str_out = (char *) malloc(buf_len);
if (!str_out) return NULL;
while (i < str_len) {
if (j >= buf_len) {
SETWARN(E_PARSE,"QENCODE: invalid memory pointer");
}
if ((binary_mode && ((str_in[i] == 0x20) || (str_in[i] == 0x09) ||
(str_in[i] == 0x0A) || (str_in[i] == 0x0D))) ||
(((str_in[i] < 0x20) && (str_in[i] != 0x09) &&
(str_in[i] != 0x0A) && (str_in[i] != 0x0D)) ||
(str_in[i] == 0x3D) || (str_in[i] > 0x7E))) {
if ((line_len > 0) && (l + 3 > line_len - 1)) {
str_out[j++] = '=';
str_out[j++] = 0x0D;
str_out[j++] = 0x0A;
l = 0;
}
j += sprintf(str_out + j, "=%02X", (unsigned char)(str_in[i++]));
l += 3;
} else {
if ((line_len > 0) && (l + 1 > line_len - 1)
&& (str_in[i+1] != 0x0A) && (str_in[i+1] != 0x0D)) {
str_out[j++] = '=';
str_out[j++] = 0x0D;
str_out[j++] = 0x0A;
l = 0;
}
if (!binary_mode && ((str_in[i] == 0x0A) || (str_in[i] == 0x0D))) {
l = 0;
} else {
l++;
}
str_out[j++] = str_in[i++];
}
}
str_out[j] = '\0';
return str_out;
}