1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000-2012 Jeffrey Stedfast
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1
8 * of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
31 #define CHARS_LWSP " \t\n\r" /* linear whitespace chars */
32 #define CHARS_TSPECIAL "()<>@,;:\\\"/[]?="
33 #define CHARS_SPECIAL "()<>@,;:\\\".[]"
34 #define CHARS_CSPECIAL "()\\\r" /* not in comments */
35 #define CHARS_DSPECIAL "[]\\\r \t" /* not in domains */
36 #define CHARS_ESPECIAL "()<>@,;:\"/[]?.=_" /* encoded word specials (rfc2047 5.1) */
37 #define CHARS_PSPECIAL "!*+-/=_" /* encoded phrase specials (rfc2047 5.3) */
38 #define CHARS_ATTRCHAR "*'% " /* attribute-char from rfc2184 */
40 static unsigned short gmime_special_table[256];
45 IS_TSPECIAL = (1 << 2),
46 IS_SPECIAL = (1 << 3),
48 IS_DSPECIAL = (1 << 5),
50 IS_ESAFE = (1 << 7), /* encoded word safe */
51 IS_PSAFE = (1 << 8), /* encoded word in phrase safe */
52 IS_ATTRCHAR = (1 << 9), /* attribute-char from rfc2184 */
54 /* ctype replacements */
55 IS_ASCII = (1 << 10), /* ascii */
56 IS_BLANK = (1 << 11), /* space or tab */
59 #define is_ctrl(x) ((gmime_special_table[(unsigned char)(x)] & IS_CTRL) != 0)
60 #define is_lwsp(x) ((gmime_special_table[(unsigned char)(x)] & IS_LWSP) != 0)
61 #define is_tspecial(x) ((gmime_special_table[(unsigned char)(x)] & IS_TSPECIAL) != 0)
62 #define is_type(x, t) ((gmime_special_table[(unsigned char)(x)] & (t)) != 0)
63 #define is_ttoken(x) ((gmime_special_table[(unsigned char)(x)] & (IS_TSPECIAL|IS_LWSP|IS_CTRL)) == 0)
64 #define is_atom(x) ((gmime_special_table[(unsigned char)(x)] & (IS_SPECIAL|IS_SPACE|IS_CTRL)) == 0)
65 #define is_dtext(x) ((gmime_special_table[(unsigned char)(x)] & IS_DSPECIAL) == 0)
66 #define is_fieldname(x) ((gmime_special_table[(unsigned char)(x)] & (IS_CTRL|IS_SPACE)) == 0)
67 #define is_qpsafe(x) ((gmime_special_table[(unsigned char)(x)] & IS_QPSAFE) != 0)
68 #define is_especial(x) ((gmime_special_table[(unsigned char)(x)] & IS_ESAFE) != 0)
69 #define is_psafe(x) ((gmime_special_table[(unsigned char)(x)] & IS_PSAFE) != 0)
70 #define is_attrchar(x) ((gmime_special_table[(unsigned char)(x)] & IS_ATTRCHAR) != 0)
72 /* ctype replacements */
73 #define is_ascii(x) ((gmime_special_table[(unsigned char)(x)] & IS_ASCII) != 0)
74 #define is_blank(x) ((gmime_special_table[(unsigned char)(x)] & IS_BLANK) != 0)
76 /* code to rebuild the gmime_special_table */
78 header_remove_bits (unsigned short bit, char *vals)
80 register unsigned char *inptr = (unsigned char *) vals;
82 while (*inptr != '\0')
83 gmime_special_table[*inptr++] &= ~bit;
87 header_init_bits (unsigned short bit, unsigned short bitcopy, int remove, char *vals)
89 register unsigned char *inptr = (unsigned char *) vals;
93 while (*inptr != '\0')
94 gmime_special_table[*inptr++] |= bit;
96 for (i = 0; i < 256; i++) {
97 if (gmime_special_table[i] & bitcopy)
98 gmime_special_table[i] |= bit;
102 for (i = 0; i < 256; i++)
103 gmime_special_table[i] |= bit;
104 while (*inptr != '\0')
105 gmime_special_table[*inptr++] &= ~bit;
107 for (i = 0; i < 256; i++) {
108 if (gmime_special_table[i] & bitcopy)
109 gmime_special_table[i] &= ~bit;
116 header_decode_init (void)
120 for (i = 0; i < 256; i++) {
121 gmime_special_table[i] = 0;
122 if (i < 32 || i == 127)
123 gmime_special_table[i] |= IS_CTRL;
124 if (i > 32 && i < 127)
125 gmime_special_table[i] |= IS_ATTRCHAR;
126 if ((i >= 33 && i <= 60) || (i >= 62 && i <= 126) || i == 32)
127 gmime_special_table[i] |= (IS_QPSAFE | IS_ESAFE);
128 if ((i >= '0' && i <= '9') || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z'))
129 gmime_special_table[i] |= IS_PSAFE;
131 gmime_special_table[i] |= IS_ASCII;
134 gmime_special_table[' '] |= IS_SPACE | IS_BLANK;
135 gmime_special_table['\t'] |= IS_QPSAFE | IS_BLANK;
136 header_init_bits (IS_LWSP, 0, FALSE, CHARS_LWSP);
137 header_init_bits (IS_TSPECIAL, IS_CTRL, FALSE, CHARS_TSPECIAL);
138 header_init_bits (IS_SPECIAL, 0, FALSE, CHARS_SPECIAL);
139 header_init_bits (IS_DSPECIAL, 0, FALSE, CHARS_DSPECIAL);
140 header_remove_bits (IS_ESAFE, CHARS_ESPECIAL);
141 header_remove_bits (IS_ATTRCHAR, CHARS_TSPECIAL CHARS_ATTRCHAR);
142 header_init_bits (IS_PSAFE, 0, FALSE, CHARS_PSPECIAL);
145 int main (int argc, char **argv)
149 header_decode_init ();
151 printf ("/* THIS FILE IS AUTOGENERATED: DO NOT EDIT! */\n\n");
152 printf ("/**\n * To regenerate:\n * make gen-table\n");
153 printf (" * ./gen-table > gmime-table-private.h\n **/\n\n");
155 /* print out the table */
156 printf ("static unsigned short gmime_special_table[256] = {");
157 for (i = 0; i < 256; i++) {
158 printf ("%s%4d%s", (i % 16) ? "" : "\n\t",
159 gmime_special_table[i], i != 255 ? "," : "\n");
163 /* print out the enum */
165 printf ("\tIS_CTRL = (1 << 0),\n");
166 printf ("\tIS_LWSP = (1 << 1),\n");
167 printf ("\tIS_TSPECIAL = (1 << 2),\n");
168 printf ("\tIS_SPECIAL = (1 << 3),\n");
169 printf ("\tIS_SPACE = (1 << 4),\n");
170 printf ("\tIS_DSPECIAL = (1 << 5),\n");
171 printf ("\tIS_QPSAFE = (1 << 6),\n");
172 printf ("\tIS_ESAFE = (1 << 7), /* encoded word safe */\n");
173 printf ("\tIS_PSAFE = (1 << 8), /* encode word in phrase safe */\n");
174 printf ("\tIS_ATTRCHAR = (1 << 9), /* attribute-char from rfc2184 */\n");
176 printf ("\t/* ctype replacements */\n");
177 printf ("\tIS_ASCII = (1 << 10), /* ascii */\n");
178 printf ("\tIS_BLANK = (1 << 11), /* space or tab */\n");
181 printf ("#define is_ctrl(x) ((gmime_special_table[(unsigned char)(x)] & IS_CTRL) != 0)\n");
182 printf ("#define is_lwsp(x) ((gmime_special_table[(unsigned char)(x)] & IS_LWSP) != 0)\n");
183 printf ("#define is_tspecial(x) ((gmime_special_table[(unsigned char)(x)] & IS_TSPECIAL) != 0)\n");
184 printf ("#define is_type(x, t) ((gmime_special_table[(unsigned char)(x)] & (t)) != 0)\n");
185 printf ("#define is_ttoken(x) ((gmime_special_table[(unsigned char)(x)] & (IS_TSPECIAL|IS_LWSP|IS_CTRL)) == 0)\n");
186 printf ("#define is_atom(x) ((gmime_special_table[(unsigned char)(x)] & (IS_SPECIAL|IS_SPACE|IS_CTRL)) == 0)\n");
187 printf ("#define is_dtext(x) ((gmime_special_table[(unsigned char)(x)] & IS_DSPECIAL) == 0)\n");
188 printf ("#define is_fieldname(x) ((gmime_special_table[(unsigned char)(x)] & (IS_CTRL|IS_SPACE)) == 0)\n");
189 printf ("#define is_qpsafe(x) ((gmime_special_table[(unsigned char)(x)] & IS_QPSAFE) != 0)\n");
190 printf ("#define is_especial(x) ((gmime_special_table[(unsigned char)(x)] & IS_ESAFE) != 0)\n");
191 printf ("#define is_psafe(x) ((gmime_special_table[(unsigned char)(x)] & IS_PSAFE) != 0)\n");
192 printf ("#define is_attrchar(x) ((gmime_special_table[(unsigned char)(x)] & IS_ATTRCHAR) != 0)\n");
194 printf ("/* ctype replacements */\n");
195 printf ("#define is_ascii(x) ((gmime_special_table[(unsigned char)(x)] & IS_ASCII) != 0)\n");
196 printf ("#define is_blank(x) ((gmime_special_table[(unsigned char)(x)] & IS_BLANK) != 0)\n");
199 printf ("#define CHARS_LWSP \" \\t\\n\\r\" /* linear whitespace chars */\n");
200 printf ("#define CHARS_TSPECIAL \"()<>@,;:\\\\\\\"/[]?=\"\n");
201 printf ("#define CHARS_SPECIAL \"()<>@,;:\\\\\\\".[]\"\n");
202 printf ("#define CHARS_CSPECIAL \"()\\\\\\r\" /* not in comments */\n");
203 printf ("#define CHARS_DSPECIAL \"[]\\\\\\r \\t\" /* not in domains */\n");
204 printf ("#define CHARS_ESPECIAL \"()<>@,;:\\\"/[]?.=_\" /* encoded word specials (rfc2047 5.1) */\n");
205 printf ("#define CHARS_PSPECIAL \"!*+-/=_\" /* encoded phrase specials (rfc2047 5.3) */\n");
206 printf ("#define CHARS_ATTRCHAR \"*'%% \" /* attribute-char from rfc2184 */\n");
209 printf ("#define GMIME_FOLD_LEN 76\n");