summaryrefslogtreecommitdiff
path: root/src/charset/codec.c
blob: 727d6006b42bd0ac3c0d61b7fb03283b55127d4e (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
/*
 * This file is part of Hubbub.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
 */

#include <string.h>

#include "charset/aliases.h"

#include "codec_impl.h"

extern hubbub_charsethandler hubbub_iconv_codec_handler;
extern hubbub_charsethandler hubbub_utf8_codec_handler;
extern hubbub_charsethandler hubbub_utf16_codec_handler;

static hubbub_charsethandler *handler_table[] = {
	&hubbub_utf8_codec_handler,
	&hubbub_utf16_codec_handler,
	&hubbub_iconv_codec_handler,
	NULL,
};

/**
 * Create a charset codec
 *
 * \param charset  Target charset
 * \param alloc    Memory (de)allocation function
 * \param pw       Pointer to client-specific private data (may be NULL)
 * \return Pointer to codec instance, or NULL on failure
 */
hubbub_charsetcodec *hubbub_charsetcodec_create(const char *charset,
		hubbub_alloc alloc, void *pw)
{
	hubbub_charsetcodec *codec;
	hubbub_charsethandler **handler;
	const hubbub_aliases_canon * canon;

	if (charset == NULL || alloc == NULL)
		return NULL;

	/* Canonicalise charset name. */
	canon = hubbub_alias_canonicalise(charset, strlen(charset));
	if (canon == NULL)
		return NULL;

	/* Search for handler class */
	for (handler = handler_table; *handler != NULL; handler++) {
		if ((*handler)->handles_charset(canon->name))
			break;
	}

	/* None found */
	if ((*handler) == NULL)
		return NULL;

	/* Instantiate class */
	codec = (*handler)->create(canon->name, alloc, pw);
	if (codec == NULL)
		return NULL;

	/* and initialise it */
	codec->mibenum = canon->mib_enum;

	codec->filter = NULL;
	codec->filter_pw = NULL;

	codec->errormode = HUBBUB_CHARSETCODEC_ERROR_LOOSE;

	codec->alloc = alloc;
	codec->alloc_pw = pw;

	return codec;
}

/**
 * Destroy a charset codec
 *
 * \param codec  The codec to destroy
 */
void hubbub_charsetcodec_destroy(hubbub_charsetcodec *codec)
{
	if (codec == NULL)
		return;

	codec->handler.destroy(codec);

	codec->alloc(codec, 0, codec->alloc_pw);
}

/**
 * Configure a charset codec
 *
 * \param codec   The codec to configure
 * \parem type    The codec option type to configure
 * \param params  Option-specific parameters
 * \return HUBBUB_OK on success, appropriate error otherwise
 */
hubbub_error hubbub_charsetcodec_setopt(hubbub_charsetcodec *codec,
		hubbub_charsetcodec_opttype type,
		hubbub_charsetcodec_optparams *params)
{
	if (codec == NULL || params == NULL)
		return HUBBUB_BADPARM;

	switch (type) {
	case HUBBUB_CHARSETCODEC_FILTER_FUNC:
		codec->filter = params->filter_func.filter;
		codec->filter_pw = params->filter_func.pw;
		break;

	case HUBBUB_CHARSETCODEC_ERROR_MODE:
		codec->errormode = params->error_mode.mode;
		break;
	}

	return HUBBUB_OK;
}

/**
 * Encode a chunk of UCS4 data into a codec's charset
 *
 * \param codec      The codec to use
 * \param source     Pointer to pointer to source data
 * \param sourcelen  Pointer to length (in bytes) of source data
 * \param dest       Pointer to pointer to output buffer
 * \param destlen    Pointer to length (in bytes) of output buffer
 * \return HUBBUB_OK on success, appropriate error otherwise.
 *
 * source, sourcelen, dest and destlen will be updated appropriately on exit
 */
hubbub_error hubbub_charsetcodec_encode(hubbub_charsetcodec *codec,
		const uint8_t **source, size_t *sourcelen,
		uint8_t **dest, size_t *destlen)
{
	if (codec == NULL || source == NULL || *source == NULL ||
			sourcelen == NULL || dest == NULL || *dest == NULL ||
			destlen == NULL)
		return HUBBUB_BADPARM;

	return codec->handler.encode(codec, source, sourcelen, dest, destlen);
}

/**
 * Decode a chunk of data in a codec's charset into UCS4
 *
 * \param codec      The codec to use
 * \param source     Pointer to pointer to source data
 * \param sourcelen  Pointer to length (in bytes) of source data
 * \param dest       Pointer to pointer to output buffer
 * \param destlen    Pointer to length (in bytes) of output buffer
 * \return HUBBUB_OK on success, appropriate error otherwise.
 *
 * source, sourcelen, dest and destlen will be updated appropriately on exit
 *
 * Call this with a source length of 0 to flush any buffers.
 */
hubbub_error hubbub_charsetcodec_decode(hubbub_charsetcodec *codec,
		const uint8_t **source, size_t *sourcelen,
		uint8_t **dest, size_t *destlen)
{
	if (codec == NULL || source == NULL || *source == NULL ||
			sourcelen == NULL || dest == NULL || *dest == NULL ||
			destlen == NULL)
		return HUBBUB_BADPARM;

	return codec->handler.decode(codec, source, sourcelen, dest, destlen);
}

/**
 * Clear a charset codec's encoding state
 *
 * \param codec  The codec to reset
 * \return HUBBUB_OK on success, appropriate error otherwise
 */
hubbub_error hubbub_charsetcodec_reset(hubbub_charsetcodec *codec)
{
	if (codec == NULL)
		return HUBBUB_BADPARM;

	/* Reset filter */
	if (codec->filter)
		codec->filter(HUBBUB_CHARSETCODEC_NULL, NULL, NULL, NULL);

	return codec->handler.reset(codec);
}