Initialize the gmime for upstream
[platform/upstream/gmime.git] / gmime / gen-table.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 2000-2012 Jeffrey Stedfast
4  *
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.
9  *
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.
14  *
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
18  *  02110-1301, USA.
19  */
20
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25
26 enum {
27         FALSE,
28         TRUE
29 };
30
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 */
39
40 static unsigned short gmime_special_table[256];
41
42 enum {
43         IS_CTRL         = (1 << 0),
44         IS_LWSP         = (1 << 1),
45         IS_TSPECIAL     = (1 << 2),
46         IS_SPECIAL      = (1 << 3),
47         IS_SPACE        = (1 << 4),
48         IS_DSPECIAL     = (1 << 5),
49         IS_QPSAFE       = (1 << 6),
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 */
53         
54         /* ctype replacements */
55         IS_ASCII        = (1 << 10), /* ascii */
56         IS_BLANK        = (1 << 11), /* space or tab */
57 };
58
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)
71
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)
75
76 /* code to rebuild the gmime_special_table */
77 static void
78 header_remove_bits (unsigned short bit, char *vals)
79 {
80         register unsigned char *inptr = (unsigned char *) vals;
81         
82         while (*inptr != '\0')
83                 gmime_special_table[*inptr++] &= ~bit;
84 }
85
86 static void
87 header_init_bits (unsigned short bit, unsigned short bitcopy, int remove, char *vals)
88 {
89         register unsigned char *inptr = (unsigned char *) vals;
90         int i;
91         
92         if (!remove) {
93                 while (*inptr != '\0')
94                         gmime_special_table[*inptr++] |= bit;
95                 if (bitcopy) {
96                         for (i = 0; i < 256; i++) {
97                                 if (gmime_special_table[i] & bitcopy)
98                                         gmime_special_table[i] |= bit;
99                         }
100                 }
101         } else {
102                 for (i = 0; i < 256; i++)
103                         gmime_special_table[i] |= bit;
104                 while (*inptr != '\0')
105                         gmime_special_table[*inptr++] &= ~bit;
106                 if (bitcopy) {
107                         for (i = 0; i < 256; i++) {
108                                 if (gmime_special_table[i] & bitcopy)
109                                         gmime_special_table[i] &= ~bit;
110                         }
111                 }
112         }
113 }
114
115 static void
116 header_decode_init (void)
117 {
118         int i;
119         
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;
130                 if (isascii (i))
131                         gmime_special_table[i] |= IS_ASCII;
132         }
133         
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);
143 }
144
145 int main (int argc, char **argv)
146 {
147         int i;
148         
149         header_decode_init ();
150         
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");
154         
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");
160         }
161         printf ("};\n\n");
162         
163         /* print out the enum */
164         printf ("enum {\n");
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");
175         printf ("\t\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");
179         printf ("};\n\n");
180         
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");
193         printf ("\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");
197         printf ("\n");
198         
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");
207         printf ("\n");
208         
209         printf ("#define GMIME_FOLD_LEN 76\n");
210         
211         return 0;
212 }