4 * Copyright (c) 2010 project bchan
6 * This software is provided 'as-is', without any express or implied
7 * warranty. In no event will the authors be held liable for any damages
8 * arising from the use of this software.
10 * Permission is granted to anyone to use this software for any purpose,
11 * including commercial applications, and to alter it and redistribute it
12 * freely, subject to the following restrictions:
14 * 1. The origin of this software must not be misrepresented; you must not
15 * claim that you wrote the original software. If you use this software
16 * in a product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
22 * 3. This notice may not be removed or altered from any source
33 /* like internet message format. rfc5322. */
35 EXPORT VOID timfparser_clear(timfparser_t *timf)
37 //taditerator_initialize(&timf->iterator);
38 tctokenchecker_clear(&timf->headerchecker);
39 timf->state = TIMFPARSER_STATE_SEARCH_HEADER;
40 timf->ishankaku = False;
43 LOCAL Bool is_chratio_fusen(LTADSEG *seg, RATIO *w_ratio)
48 if ((seg->id & 0xFF) != TS_TFONT) {
54 data = ((UB*)seg) + 4;
55 subid = *(UH*)data >> 8;
59 *w_ratio = *(RATIO*)(data + 4);
63 EXPORT W timfparser_next(timfparser_t *timf, W *headername, UB **bin, W *len)
65 W segsize, ret, val, ratio_a, ratio_b;
69 TADITERATOR_RESULT_T result;
74 result = taditerator_next(&timf->iterator, NULL, &ch, &seg, &segsize, &data);
75 if (result == TADITERATOR_RESULT_END) {
76 return TIMFPARSER_RESULT_END;
78 if (result == TADITERATOR_RESULT_CHARCTOR) {
79 switch (timf->state) {
80 case TIMFPARSER_STATE_SEARCH_HEADER:
82 timf->state = TIMFPARSER_STATE_READ_VALUE_MESSAGE;
83 if (timf->ishankaku == True) {
84 *bin = (UB*)timf->chratio;
86 return TIMFPARSER_RESULT_BODY;
90 tctokenchecker_clear(&timf->headerchecker);
91 timf->state = TIMFPARSER_STATE_READ_HEADER_FIELDNAME;
92 ret = tctokenchecker_inputchar(&timf->headerchecker, ch, &val);
93 if (ret == TCTOKENCHECKER_DETERMINE) {
94 timf->state = TIMFPARSER_STATE_READ_HEADER_FIELDVALUE;
95 timf->headertype = val;
96 } else if (ret != TCTOKENCHECKER_CONTINUE) {
97 timf->state = TIMFPARSER_STATE_SKIP_HEADER;
99 timf->state = TIMFPARSER_STATE_SEARCH_HEADER_FIELDVALUE;
102 case TIMFPARSER_STATE_SKIP_HEADER:
104 timf->state = TIMFPARSER_STATE_SEARCH_HEADER;
107 case TIMFPARSER_STATE_READ_HEADER_FIELDNAME:
108 ret = tctokenchecker_inputchar(&timf->headerchecker, ch, &val);
109 if (ret == TCTOKENCHECKER_DETERMINE) {
110 timf->state = TIMFPARSER_STATE_READ_HEADER_FIELDVALUE;
111 timf->headertype = val;
112 } else if (ret != TCTOKENCHECKER_CONTINUE) {
113 timf->state = TIMFPARSER_STATE_SKIP_HEADER;
115 timf->state = TIMFPARSER_STATE_SEARCH_HEADER_FIELDVALUE;
118 case TIMFPARSER_STATE_SEARCH_HEADER_FIELDVALUE:
122 timf->state = TIMFPARSER_STATE_READ_HEADER_FIELDNAME;
123 *headername = timf->headertype;
124 if (timf->ishankaku == True) {
125 memcpy(timf->buf, timf->chratio, 10);
127 *bin = (UB*)timf->buf;
131 *bin = (UB*)timf->buf;
134 return TIMFPARSER_RESULT_HEADERVALUE;
135 case TIMFPARSER_STATE_READ_HEADER_FIELDVALUE:
137 timf->state = TIMFPARSER_STATE_SEARCH_HEADER;
141 *headername = timf->headertype;
142 *bin = (UB*)timf->buf;
144 return TIMFPARSER_RESULT_HEADERVALUE;
145 case TIMFPARSER_STATE_READ_VALUE_MESSAGE:
147 *bin = (UB*)timf->buf;
149 return TIMFPARSER_RESULT_BODY;
151 } else if (result == TADITERATOR_RESULT_SEGMENT) {
152 is_chratio = is_chratio_fusen(seg, &w_ratio);
153 if (is_chratio == False) {
157 switch (timf->state) {
158 case TIMFPARSER_STATE_SEARCH_HEADER:
159 case TIMFPARSER_STATE_SKIP_HEADER:
160 case TIMFPARSER_STATE_READ_HEADER_FIELDNAME:
161 case TIMFPARSER_STATE_SEARCH_HEADER_FIELDVALUE:
162 memcpy(timf->chratio, seg, 10);
163 ratio_a = w_ratio >> 8;
164 ratio_b = w_ratio & 0xFF;
165 if ((ratio_a * 2 > ratio_b)||(ratio_b == 0)) {
166 timf->ishankaku = False;
168 timf->ishankaku = True;
171 case TIMFPARSER_STATE_READ_HEADER_FIELDVALUE:
172 memcpy(timf->chratio, seg, 10);
173 ratio_a = w_ratio >> 8;
174 ratio_b = w_ratio & 0xFF;
175 if ((ratio_a * 2 > ratio_b)||(ratio_b == 0)) {
176 timf->ishankaku = False;
178 timf->ishankaku = True;
180 *headername = timf->headertype;
181 *bin = (UB*)timf->chratio;
183 return TIMFPARSER_RESULT_HEADERVALUE;
184 case TIMFPARSER_STATE_READ_VALUE_MESSAGE:
185 *bin = (UB*)timf->chratio;
187 return TIMFPARSER_RESULT_BODY;
192 return TIMFPARSER_RESULT_END;
195 LOCAL TC timfparser_headerend[] = {TK_COLN, TNULL};
197 EXPORT VOID timfparser_initialize(timfparser_t *timf, tctokenchecker_valuetuple_t *val, W val_len, TC *bin, W strlen)
199 taditerator_initialize(&timf->iterator, bin, strlen);
200 tctokenchecker_initialize(&timf->headerchecker, val, val_len, timfparser_headerend);
201 timf->state = TIMFPARSER_STATE_SEARCH_HEADER;
202 timf->ishankaku = False;
205 EXPORT VOID timfparser_finalize(timfparser_t *timf)
207 tctokenchecker_finalize(&timf->headerchecker);
208 taditerator_finalize(&timf->iterator);