30
30
#include "mbfilter.h"
31
31
#include "mbfilter_utf7.h"
32
32
33
+ static int mbfl_filt_conv_utf7_wchar_flush (mbfl_convert_filter * filter );
34
+
33
35
static const unsigned char mbfl_base64_table [] = {
34
36
/* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
35
37
0x41 ,0x42 ,0x43 ,0x44 ,0x45 ,0x46 ,0x47 ,0x48 ,0x49 ,0x4a ,0x4b ,0x4c ,0x4d ,
@@ -62,7 +64,7 @@ const struct mbfl_convert_vtbl vtbl_utf7_wchar = {
62
64
mbfl_filt_conv_common_ctor ,
63
65
NULL ,
64
66
mbfl_filt_conv_utf7_wchar ,
65
- mbfl_filt_conv_common_flush ,
67
+ mbfl_filt_conv_utf7_wchar_flush ,
66
68
NULL ,
67
69
};
68
70
@@ -102,6 +104,11 @@ int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
102
104
if (filter -> status ) { /* Modified Base64 */
103
105
n = decode_base64_char (c );
104
106
if (n < 0 ) {
107
+ if (filter -> cache ) {
108
+ /* Either we were expecting the 2nd half of a surrogate pair which
109
+ * never came, or else the last Base64 data was not padded with zeroes */
110
+ (* filter -> output_function )(filter -> cache | MBFL_WCSGROUP_THROUGH , filter -> data );
111
+ }
105
112
if (c == '-' ) {
106
113
if (filter -> status == 1 ) { /* "+-" -> "+" */
107
114
CK ((* filter -> output_function )('+' , filter -> data ));
@@ -143,6 +150,10 @@ int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
143
150
n = (n & 0x3 ) << 14 ;
144
151
filter -> status = 5 ;
145
152
if (s >= 0xd800 && s < 0xdc00 ) {
153
+ if (filter -> cache & 0xfff0000 ) {
154
+ /* We were waiting for the 2nd part of a surrogate pair */
155
+ (* filter -> output_function )(((filter -> cache & 0xfff0000 ) >> 6 ) | MBFL_WCSGROUP_THROUGH , filter -> data );
156
+ }
146
157
s = (((s & 0x3ff ) << 16 ) + 0x400000 ) | n ;
147
158
filter -> cache = s ;
148
159
} else if (s >= 0xdc00 && s < 0xe000 ) {
@@ -155,6 +166,10 @@ int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
155
166
CK ((* filter -> output_function )(s | MBFL_WCSGROUP_THROUGH , filter -> data ));
156
167
}
157
168
} else {
169
+ if (filter -> cache & 0xfff0000 ) {
170
+ /* We were waiting for the 2nd part of a surrogate pair */
171
+ (* filter -> output_function )(((filter -> cache & 0xfff0000 ) >> 6 ) | MBFL_WCSGROUP_THROUGH , filter -> data );
172
+ }
158
173
filter -> cache = n ;
159
174
CK ((* filter -> output_function )(s , filter -> data ));
160
175
}
@@ -173,6 +188,10 @@ int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
173
188
n = (n & 0xf ) << 12 ;
174
189
filter -> status = 8 ;
175
190
if (s >= 0xd800 && s < 0xdc00 ) {
191
+ if (filter -> cache & 0xfff0000 ) {
192
+ /* We were waiting for the 2nd part of a surrogate pair */
193
+ (* filter -> output_function )(((filter -> cache & 0xfff0000 ) >> 6 ) | MBFL_WCSGROUP_THROUGH , filter -> data );
194
+ }
176
195
s = (((s & 0x3ff ) << 16 ) + 0x400000 ) | n ;
177
196
filter -> cache = s ;
178
197
} else if (s >= 0xdc00 && s < 0xe000 ) {
@@ -185,6 +204,10 @@ int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
185
204
CK ((* filter -> output_function )(s | MBFL_WCSGROUP_THROUGH , filter -> data ));
186
205
}
187
206
} else {
207
+ if (filter -> cache & 0xfff0000 ) {
208
+ /* We were waiting for the 2nd part of a surrogate pair */
209
+ (* filter -> output_function )(((filter -> cache & 0xfff0000 ) >> 6 ) | MBFL_WCSGROUP_THROUGH , filter -> data );
210
+ }
188
211
filter -> cache = n ;
189
212
CK ((* filter -> output_function )(s , filter -> data ));
190
213
}
@@ -198,6 +221,10 @@ int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
198
221
s = n | (filter -> cache & 0xffff );
199
222
filter -> status = 2 ;
200
223
if (s >= 0xd800 && s < 0xdc00 ) {
224
+ if (filter -> cache & 0xfff0000 ) {
225
+ /* We were waiting for the 2nd part of a surrogate pair */
226
+ (* filter -> output_function )(((filter -> cache & 0xfff0000 ) >> 6 ) | MBFL_WCSGROUP_THROUGH , filter -> data );
227
+ }
201
228
s = (((s & 0x3ff ) << 16 ) + 0x400000 );
202
229
filter -> cache = s ;
203
230
} else if (s >= 0xdc00 && s < 0xe000 ) {
@@ -212,6 +239,10 @@ int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
212
239
CK ((* filter -> output_function )(s , filter -> data ));
213
240
}
214
241
} else {
242
+ if (filter -> cache & 0xfff0000 ) {
243
+ /* We were waiting for the 2nd part of a surrogate pair */
244
+ (* filter -> output_function )(((filter -> cache & 0xfff0000 ) >> 6 ) | MBFL_WCSGROUP_THROUGH , filter -> data );
245
+ }
215
246
filter -> cache = 0 ;
216
247
CK ((* filter -> output_function )(s , filter -> data ));
217
248
}
@@ -225,6 +256,21 @@ int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
225
256
return c ;
226
257
}
227
258
259
+ static int mbfl_filt_conv_utf7_wchar_flush (mbfl_convert_filter * filter )
260
+ {
261
+ if (filter -> cache ) {
262
+ /* Either we were expecting the 2nd half of a surrogate pair which
263
+ * never came, or else the last Base64 data was not padded with zeroes */
264
+ (* filter -> output_function )(filter -> cache | MBFL_WCSGROUP_THROUGH , filter -> data );
265
+ }
266
+
267
+ if (filter -> flush_function ) {
268
+ (* filter -> flush_function )(filter -> data );
269
+ }
270
+
271
+ return 0 ;
272
+ }
273
+
228
274
int mbfl_filt_conv_wchar_utf7 (int c , mbfl_convert_filter * filter )
229
275
{
230
276
int s ;
0 commit comments