Skip to content

Commit 319a340

Browse files
committed
Simplify code for working with halfwidth/fullwidth kana conversion filter
There's no need to dynamically allocate a struct to hold the 'mode' parameter; just store it directly in `filt->opaque`. Some other things were also being done in an unnecessarily roundabout way. Also, the 'copy' function for CP50220 conversion filters was *both* broken and unnecessary. Broken, because it malloc'd memory which was never freed by anything. Unnecessary, because the point of the copy is so that various algorithms can try running bytes through a conversion filter and see how many output bytes or characters result, and then back out by restoring the filters to their previous state. But here's the thing; CP50220 conversion filters don't hold cached bytes, which is the main thing which would need to be restored to a previous state.
1 parent a900ec3 commit 319a340

File tree

4 files changed

+18
-53
lines changed

4 files changed

+18
-53
lines changed

ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,8 @@
3131
#include "unicode_table_jis.h"
3232
#include "cp932_table.h"
3333

34-
typedef struct _mbfl_filt_conv_wchar_cp50220_ctx {
35-
mbfl_filt_tl_jisx0201_jisx0208_param tl_param;
36-
mbfl_convert_filter last;
37-
} mbfl_filt_conv_wchar_cp50220_ctx;
38-
3934
static void mbfl_filt_conv_wchar_cp50220_ctor(mbfl_convert_filter *filt);
4035
static void mbfl_filt_conv_wchar_cp50220_dtor(mbfl_convert_filter *filt);
41-
static void mbfl_filt_conv_wchar_cp50220_copy(mbfl_convert_filter *src, mbfl_convert_filter *dest);
4236
static int mbfl_filt_conv_cp5022x_wchar_flush(mbfl_convert_filter *filter);
4337

4438
/* Previously, a dubious 'encoding' called 'cp50220raw' was supported
@@ -102,7 +96,7 @@ const struct mbfl_convert_vtbl vtbl_wchar_cp50220 = {
10296
mbfl_filt_conv_wchar_cp50220_dtor,
10397
mbfl_filt_conv_wchar_cp50221,
10498
mbfl_filt_conv_any_jis_flush,
105-
mbfl_filt_conv_wchar_cp50220_copy
99+
NULL,
106100
};
107101

108102
const struct mbfl_convert_vtbl vtbl_cp50221_wchar = {
@@ -327,44 +321,27 @@ static int mbfl_filt_conv_cp5022x_wchar_flush(mbfl_convert_filter *filter)
327321
/*
328322
* wchar => CP50220
329323
*/
330-
static void
331-
mbfl_filt_conv_wchar_cp50220_ctor(mbfl_convert_filter *filt)
324+
static void mbfl_filt_conv_wchar_cp50220_ctor(mbfl_convert_filter *filt)
332325
{
333-
mbfl_filt_conv_wchar_cp50220_ctx *ctx;
326+
/* Insert a new convert filter into the chain, after this one, which will
327+
* actually perform the CP50220 conversion. Alter this filter so that it
328+
* converts halfwidth katakana instead */
329+
mbfl_convert_filter *cp50220_filt = emalloc(sizeof(mbfl_convert_filter));
330+
*cp50220_filt = *filt;
334331

332+
/* Reinitialize */
335333
mbfl_filt_conv_common_ctor(filt);
336-
337-
ctx = emalloc(sizeof(mbfl_filt_conv_wchar_cp50220_ctx));
338-
ctx->tl_param.mode = MBFL_FILT_TL_HAN2ZEN_KATAKANA | MBFL_FILT_TL_HAN2ZEN_GLUE;
339-
340-
ctx->last = *filt;
341-
ctx->last.opaque = ctx;
342-
ctx->last.data = filt->data;
343334
filt->filter_function = vtbl_tl_jisx0201_jisx0208.filter_function;
344335
filt->filter_flush = (filter_flush_t)vtbl_tl_jisx0201_jisx0208.filter_flush;
345-
filt->output_function = (output_function_t)ctx->last.filter_function;
346-
filt->flush_function = (flush_function_t)ctx->last.filter_flush;
347-
filt->data = &ctx->last;
348-
filt->opaque = ctx;
336+
filt->output_function = (output_function_t)cp50220_filt->filter_function;
337+
filt->flush_function = (flush_function_t)cp50220_filt->filter_flush;
338+
filt->data = cp50220_filt;
339+
filt->opaque = (void*)(MBFL_FILT_TL_HAN2ZEN_KATAKANA | MBFL_FILT_TL_HAN2ZEN_GLUE);
349340
}
350341

351-
static void
352-
mbfl_filt_conv_wchar_cp50220_copy(mbfl_convert_filter *src, mbfl_convert_filter *dest)
342+
static void mbfl_filt_conv_wchar_cp50220_dtor(mbfl_convert_filter *filt)
353343
{
354-
mbfl_filt_conv_wchar_cp50220_ctx *ctx;
355-
356-
*dest = *src;
357-
ctx = emalloc(sizeof(mbfl_filt_conv_wchar_cp50220_ctx));
358-
dest->opaque = ctx;
359-
dest->data = &ctx->last;
360-
}
361-
362-
static void
363-
mbfl_filt_conv_wchar_cp50220_dtor(mbfl_convert_filter *filt)
364-
{
365-
if (filt->opaque != NULL) {
366-
efree(filt->opaque);
367-
}
344+
efree(filt->data);
368345
}
369346

370347
/*

ext/mbstring/libmbfl/filters/mbfilter_tl_jisx0201_jisx0208.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
*
2323
*/
2424

25+
#include <stdint.h>
2526
#include "mbfilter_tl_jisx0201_jisx0208.h"
2627
#include "translit_kana_jisx0201_jisx0208.h"
2728

2829
int
2930
mbfl_filt_tl_jisx0201_jisx0208(int c, mbfl_convert_filter *filt)
3031
{
3132
int s, n;
32-
int mode = ((mbfl_filt_tl_jisx0201_jisx0208_param *)filt->opaque)->mode;
33+
int mode = (intptr_t)filt->opaque;
3334

3435
s = c;
3536

@@ -262,7 +263,7 @@ int
262263
mbfl_filt_tl_jisx0201_jisx0208_flush(mbfl_convert_filter *filt)
263264
{
264265
int ret, n;
265-
int mode = ((mbfl_filt_tl_jisx0201_jisx0208_param *)filt->opaque)->mode;
266+
int mode = (intptr_t)filt->opaque;
266267

267268
ret = 0;
268269
if (filt->status) {

ext/mbstring/libmbfl/filters/mbfilter_tl_jisx0201_jisx0208.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@
7070
MBFL_FILT_TL_ZEN2HAN_COMPAT1 | \
7171
MBFL_FILT_TL_ZEN2HAN_COMPAT2)
7272

73-
74-
typedef struct _mbfl_filt_tl_jisx0201_jisx0208_param {
75-
int mode;
76-
} mbfl_filt_tl_jisx0201_jisx0208_param;
77-
7873
extern const struct mbfl_convert_vtbl vtbl_tl_jisx0201_jisx0208;
7974

8075
#endif /* MBFILTER_TL_KANA_JISX0201_JISX0208_H */

ext/mbstring/libmbfl/mbfl/mbfilter.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,6 @@ mbfl_ja_jp_hantozen(
14841484
mbfl_convert_filter *encoder = NULL;
14851485
mbfl_convert_filter *tl_filter = NULL;
14861486
mbfl_convert_filter *next_filter = NULL;
1487-
mbfl_filt_tl_jisx0201_jisx0208_param *param = NULL;
14881487

14891488
mbfl_memory_device_init(&device, string->len, 0);
14901489
mbfl_string_init(result);
@@ -1500,20 +1499,16 @@ mbfl_ja_jp_hantozen(
15001499
}
15011500
next_filter = decoder;
15021501

1503-
param = emalloc(sizeof(mbfl_filt_tl_jisx0201_jisx0208_param));
1504-
param->mode = mode;
1505-
15061502
tl_filter = mbfl_convert_filter_new2(
15071503
&vtbl_tl_jisx0201_jisx0208,
15081504
(int(*)(int, void*))next_filter->filter_function,
15091505
(flush_function_t)next_filter->filter_flush,
15101506
next_filter);
15111507
if (tl_filter == NULL) {
1512-
efree(param);
15131508
goto out;
15141509
}
15151510

1516-
tl_filter->opaque = param;
1511+
tl_filter->opaque = (void*)((intptr_t)mode);
15171512
next_filter = tl_filter;
15181513

15191514
encoder = mbfl_convert_filter_new(
@@ -1542,9 +1537,6 @@ mbfl_ja_jp_hantozen(
15421537
result = mbfl_memory_device_result(&device, result);
15431538
out:
15441539
if (tl_filter != NULL) {
1545-
if (tl_filter->opaque != NULL) {
1546-
efree(tl_filter->opaque);
1547-
}
15481540
mbfl_convert_filter_delete(tl_filter);
15491541
}
15501542

0 commit comments

Comments
 (0)