summaryrefslogtreecommitdiff
path: root/multibytecodec.c
blob: 545392798e1be6321e48e8f0edb3cf56e3481322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * multibytecodec.c: Common Multibyte Codec Implementation
 *
 * Written by Hye-Shik Chang <perky@FreeBSD.org>
 */

#include "multibytecodec.h"
#include "cjkcodecs.h"
#include "codecs_cn.h"
#include "codecs_hk.h"
#include "codecs_jp.h"
#include "codecs_kr.h"
#include "codecs_tw.h"
#include "codecs_iso2022.h"

#include <stddef.h>
#include <string.h>

#define MBCM_MAX 7
struct MultibyteCodecModule {
    const char *name;
    const struct dbcs_map *mappings;
    const MultibyteCodec *codecs;
};

static struct MultibyteCodecModule *
mbcms_init(struct MultibyteCodecModule mbcs[])
{
    size_t i = 0;

#define MBCM_SET(mod)				\
    do {					\
	mbcs[i].name = #mod;			\
	mbcs[i].mappings = mod##_mapping_list;	\
	mbcs[i].codecs = mod##_codec_list;	\
	i++;					\
    } while(0);
#define MBCM_END()				\
    do {					\
	mbcs[i].name = NULL;			\
	mbcs[i].mappings = NULL;		\
	mbcs[i].codecs = NULL;			\
	i++;					\
    } while(0);

    MBCM_SET(cn);
    MBCM_SET(hk);
    MBCM_SET(kr);
    MBCM_SET(jp);
    MBCM_SET(tw);
    MBCM_SET(iso2022);
    MBCM_END()

    return mbcs;
}

struct MultibyteCodecModule*
mbc_find(struct MultibyteCodecModule *mbcms,
	 const char *modname)
{
    int i;

    for (i = 0; mbcms[i].name; i++)
	if (!strcmp (mbcms[i].name, modname))
	    return &mbcms[i];

    return NULL;
}

static const MultibyteCodec*
mbc_module_find_codec(struct MultibyteCodecModule* module,
		      const char *encoding)
{
    int i;

    for (i = 0; module->codecs[i].encoding && module->codecs[i].encoding[0]; i++)
	if (!strcmp (module->codecs[i].encoding, encoding))
	    return &module->codecs[i];

    return NULL;
}

int
mbc_importmap(const char *modname, const char *symbol,
	      const void **encmap, const void **decmap)
{
    struct MultibyteCodecModule mbcms[MBCM_MAX];
    struct MultibyteCodecModule* module;
    const struct dbcs_map *map;

    module = mbc_find(mbcms_init(mbcms), modname);
    if (!module)
	return -1;

    map = module->mappings;
    for (; map->charset; map++)
	if (!strcmp (map->charset, symbol))
	    break;
    if (!map->charset)
	return -1;

    if (encmap != NULL)
	*encmap = map->encmap;
    if (decmap != NULL)
	*decmap = map->decmap;
    return 0;
}

int
mbcs_init(MultibyteCodecState *state,
	  const char *modname,
	  const char* encoding)
{
    struct MultibyteCodecModule mbcms[MBCM_MAX];
    struct MultibyteCodecModule* module;

    memset(state, 0, sizeof(MultibyteCodecState));

    module = mbc_find(mbcms_init(mbcms), modname);
    if (!module)
	return -1;

    state->codec = mbc_module_find_codec(module, encoding);
    if (!state->codec)
	return -1;

    if (state->codec->codecinit && state->codec->codecinit(state->codec->config))
	return -1;

    mbcs_decode_init(state);
    return 0;
}

int
mbcs_decode(MultibyteCodecState *state,
	    const char** inbuf, size_t inlen,
	    ucs4_t** outbuf, size_t outlen)
{
    return state->codec->decode(&state->state, state->codec->config,
				(const unsigned char**)inbuf, inlen,
				outbuf, outlen);
}

void
mbcs_decode_init(MultibyteCodecState *state)
{
    if (state->codec->decinit)
	state->codec->decinit(&state->state, state->codec->config);
}

int
mbcs_decode_reset(MultibyteCodecState *state)
{
    if (state->codec->decreset)
	return state->codec->decreset(&state->state,
				      state->codec->config);
    return 0;
}

int
mbcs_encode(MultibyteCodecState *state,
	    const ucs4_t** inbuf, size_t inlen,
	    char** outbuf, size_t outlen,
	    int flags)
{
    return state->codec->encode(&state->state,
				state->codec->config,
				inbuf, inlen,
				(unsigned char **)outbuf, outlen,
				flags);
}

void
mbcs_encode_init(MultibyteCodecState *state)
{
  if (state->codec->encinit)
      state->codec->encinit(&state->state, state->codec->config);
}

int
mbcs_encode_reset(MultibyteCodecState *state,
		  char **outbuf, int outleft)
{
    if (state->codec->encreset)
	return state->codec->encreset(&state->state, state->codec->config,
				      (unsigned char **)outbuf, outleft);
    return 0;
}