| 1 |
#include <stdlib.h> |
| 2 |
#include <string.h> |
| 3 |
#include "src/cjkcodecs/multibytecodec.h" |
| 4 |
#include "src/cjkcodecs/fixnames.h" |
| 5 |
|
| 6 |
|
| 7 |
struct pypy_cjk_dec_s *pypy_cjk_dec_new(const MultibyteCodec *codec) |
| 8 |
{ |
| 9 |
struct pypy_cjk_dec_s *d = malloc(sizeof(struct pypy_cjk_dec_s)); |
| 10 |
if (!d) |
| 11 |
return NULL; |
| 12 |
if (codec->decinit != NULL && codec->decinit(&d->state, codec->config) != 0) |
| 13 |
{ |
| 14 |
free(d); |
| 15 |
return NULL; |
| 16 |
} |
| 17 |
d->codec = codec; |
| 18 |
d->outbuf_start = NULL; |
| 19 |
return d; |
| 20 |
} |
| 21 |
|
| 22 |
Py_ssize_t pypy_cjk_dec_init(struct pypy_cjk_dec_s *d, |
| 23 |
char *inbuf, Py_ssize_t inlen) |
| 24 |
{ |
| 25 |
d->inbuf_start = (unsigned char *)inbuf; |
| 26 |
d->inbuf = (unsigned char *)inbuf; |
| 27 |
d->inbuf_end = (unsigned char *)inbuf + inlen; |
| 28 |
if (d->outbuf_start == NULL) |
| 29 |
{ |
| 30 |
d->outbuf_start = (inlen <= (PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) ? |
| 31 |
malloc(inlen * sizeof(Py_UNICODE)) : |
| 32 |
NULL); |
| 33 |
if (d->outbuf_start == NULL) |
| 34 |
return -1; |
| 35 |
d->outbuf_end = d->outbuf_start + inlen; |
| 36 |
} |
| 37 |
d->outbuf = d->outbuf_start; |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
void pypy_cjk_dec_free(struct pypy_cjk_dec_s *d) |
| 42 |
{ |
| 43 |
free(d->outbuf_start); |
| 44 |
free(d); |
| 45 |
} |
| 46 |
|
| 47 |
static int expand_decodebuffer(struct pypy_cjk_dec_s *d, Py_ssize_t esize) |
| 48 |
{ |
| 49 |
Py_ssize_t orgpos, orgsize; |
| 50 |
Py_UNICODE *newbuf; |
| 51 |
|
| 52 |
orgpos = d->outbuf - d->outbuf_start; |
| 53 |
orgsize = d->outbuf_end - d->outbuf_start; |
| 54 |
esize = (esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize); |
| 55 |
newbuf = (esize <= (PY_SSIZE_T_MAX / sizeof(Py_UNICODE) - orgsize) ? |
| 56 |
realloc(d->outbuf_start, (orgsize + esize) * sizeof(Py_UNICODE)) : |
| 57 |
NULL); |
| 58 |
if (!newbuf) |
| 59 |
return -1; |
| 60 |
d->outbuf_start = newbuf; |
| 61 |
d->outbuf = newbuf + orgpos; |
| 62 |
d->outbuf_end = newbuf + orgsize + esize; |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
Py_ssize_t pypy_cjk_dec_chunk(struct pypy_cjk_dec_s *d) |
| 67 |
{ |
| 68 |
while (1) |
| 69 |
{ |
| 70 |
Py_ssize_t r; |
| 71 |
Py_ssize_t inleft = (Py_ssize_t)(d->inbuf_end - d->inbuf); |
| 72 |
Py_ssize_t outleft = (Py_ssize_t)(d->outbuf_end - d->outbuf); |
| 73 |
if (inleft == 0) |
| 74 |
return 0; |
| 75 |
r = d->codec->decode(&d->state, d->codec->config, |
| 76 |
&d->inbuf, inleft, &d->outbuf, outleft); |
| 77 |
if (r != MBERR_TOOSMALL) |
| 78 |
return r; |
| 79 |
|
| 80 |
if (expand_decodebuffer(d, -1) == -1) |
| 81 |
return MBERR_NOMEMORY; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
Py_UNICODE *pypy_cjk_dec_outbuf(struct pypy_cjk_dec_s *d) |
| 86 |
{ |
| 87 |
return d->outbuf_start; |
| 88 |
} |
| 89 |
|
| 90 |
Py_ssize_t pypy_cjk_dec_outlen(struct pypy_cjk_dec_s *d) |
| 91 |
{ |
| 92 |
return d->outbuf - d->outbuf_start; |
| 93 |
} |
| 94 |
|
| 95 |
Py_ssize_t pypy_cjk_dec_inbuf_remaining(struct pypy_cjk_dec_s *d) |
| 96 |
{ |
| 97 |
return d->inbuf_end - d->inbuf; |
| 98 |
} |
| 99 |
|
| 100 |
Py_ssize_t pypy_cjk_dec_inbuf_consumed(struct pypy_cjk_dec_s* d) |
| 101 |
{ |
| 102 |
return d->inbuf - d->inbuf_start; |
| 103 |
} |
| 104 |
|
| 105 |
Py_ssize_t pypy_cjk_dec_replace_on_error(struct pypy_cjk_dec_s* d, |
| 106 |
Py_UNICODE *newbuf, Py_ssize_t newlen, |
| 107 |
Py_ssize_t in_offset) |
| 108 |
{ |
| 109 |
if (newlen > 0) |
| 110 |
{ |
| 111 |
if (d->outbuf + newlen > d->outbuf_end) |
| 112 |
if (expand_decodebuffer(d, newlen) == -1) |
| 113 |
return MBERR_NOMEMORY; |
| 114 |
memcpy(d->outbuf, newbuf, newlen * sizeof(Py_UNICODE)); |
| 115 |
d->outbuf += newlen; |
| 116 |
} |
| 117 |
d->inbuf = d->inbuf_start + in_offset; |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
struct pypy_cjk_enc_s *pypy_cjk_enc_new(const MultibyteCodec *codec) |
| 124 |
{ |
| 125 |
struct pypy_cjk_enc_s *d = calloc(1, sizeof(struct pypy_cjk_enc_s)); |
| 126 |
if (!d) |
| 127 |
return NULL; |
| 128 |
if (codec->encinit != NULL && codec->encinit(&d->state, codec->config) != 0) |
| 129 |
{ |
| 130 |
free(d); |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
d->codec = codec; |
| 134 |
d->outbuf_start = NULL; |
| 135 |
return d; |
| 136 |
} |
| 137 |
|
| 138 |
void pypy_cjk_enc_copystate(struct pypy_cjk_enc_s *dst, struct pypy_cjk_enc_s *src) |
| 139 |
{ |
| 140 |
dst->state = src->state; |
| 141 |
} |
| 142 |
|
| 143 |
void pypy_cjk_enc_getstate(struct pypy_cjk_enc_s *d, unsigned char *buf) |
| 144 |
{ |
| 145 |
memcpy(buf, d->state.c, sizeof(d->state.c)); |
| 146 |
} |
| 147 |
|
| 148 |
void pypy_cjk_enc_setstate(struct pypy_cjk_enc_s *d, const unsigned char *buf) |
| 149 |
{ |
| 150 |
memcpy(d->state.c, buf, sizeof(d->state.c)); |
| 151 |
} |
| 152 |
|
| 153 |
Py_ssize_t pypy_cjk_enc_init(struct pypy_cjk_enc_s *d, |
| 154 |
Py_UNICODE *inbuf, Py_ssize_t inlen) |
| 155 |
{ |
| 156 |
Py_ssize_t outlen; |
| 157 |
d->inbuf_start = inbuf; |
| 158 |
d->inbuf = inbuf; |
| 159 |
d->inbuf_end = inbuf + inlen; |
| 160 |
if (d->outbuf_start == NULL) |
| 161 |
{ |
| 162 |
if (inlen > (PY_SSIZE_T_MAX - 16) / 2) |
| 163 |
return -1; |
| 164 |
outlen = inlen * 2 + 16; |
| 165 |
d->outbuf_start = malloc(outlen); |
| 166 |
if (d->outbuf_start == NULL) |
| 167 |
return -1; |
| 168 |
d->outbuf_end = d->outbuf_start + outlen; |
| 169 |
} |
| 170 |
d->outbuf = d->outbuf_start; |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
void pypy_cjk_enc_free(struct pypy_cjk_enc_s *d) |
| 175 |
{ |
| 176 |
free(d->outbuf_start); |
| 177 |
free(d); |
| 178 |
} |
| 179 |
|
| 180 |
static int expand_encodebuffer(struct pypy_cjk_enc_s *d, Py_ssize_t esize) |
| 181 |
{ |
| 182 |
Py_ssize_t orgpos, orgsize; |
| 183 |
unsigned char *newbuf; |
| 184 |
|
| 185 |
orgpos = d->outbuf - d->outbuf_start; |
| 186 |
orgsize = d->outbuf_end - d->outbuf_start; |
| 187 |
esize = (esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize); |
| 188 |
newbuf = (esize <= PY_SSIZE_T_MAX - orgsize ? |
| 189 |
realloc(d->outbuf_start, orgsize + esize) : |
| 190 |
NULL); |
| 191 |
if (!newbuf) |
| 192 |
return -1; |
| 193 |
d->outbuf_start = newbuf; |
| 194 |
d->outbuf = newbuf + orgpos; |
| 195 |
d->outbuf_end = newbuf + orgsize + esize; |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
Py_ssize_t pypy_cjk_enc_chunk(struct pypy_cjk_enc_s *d, Py_ssize_t flags) |
| 200 |
{ |
| 201 |
while (1) |
| 202 |
{ |
| 203 |
Py_ssize_t r; |
| 204 |
Py_ssize_t inleft = (Py_ssize_t)(d->inbuf_end - d->inbuf); |
| 205 |
Py_ssize_t outleft = (Py_ssize_t)(d->outbuf_end - d->outbuf); |
| 206 |
if (inleft == 0 && !(flags & MBENC_RESET)) |
| 207 |
return 0; |
| 208 |
r = d->codec->encode(&d->state, d->codec->config, |
| 209 |
&d->inbuf, inleft, &d->outbuf, outleft, flags); |
| 210 |
if (r != MBERR_TOOSMALL) |
| 211 |
return r; |
| 212 |
|
| 213 |
if (expand_encodebuffer(d, -1) == -1) |
| 214 |
return MBERR_NOMEMORY; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
Py_ssize_t pypy_cjk_enc_reset(struct pypy_cjk_enc_s *d) |
| 219 |
{ |
| 220 |
if (d->codec->encreset == NULL) |
| 221 |
return 0; |
| 222 |
|
| 223 |
while (1) |
| 224 |
{ |
| 225 |
Py_ssize_t r; |
| 226 |
Py_ssize_t outleft = (Py_ssize_t)(d->outbuf_end - d->outbuf); |
| 227 |
r = d->codec->encreset(&d->state, d->codec->config, &d->outbuf, outleft); |
| 228 |
if (r != MBERR_TOOSMALL) |
| 229 |
return r; |
| 230 |
|
| 231 |
if (expand_encodebuffer(d, -1) == -1) |
| 232 |
return MBERR_NOMEMORY; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
char *pypy_cjk_enc_outbuf(struct pypy_cjk_enc_s *d) |
| 237 |
{ |
| 238 |
return (char *)d->outbuf_start; |
| 239 |
} |
| 240 |
|
| 241 |
Py_ssize_t pypy_cjk_enc_outlen(struct pypy_cjk_enc_s *d) |
| 242 |
{ |
| 243 |
return d->outbuf - d->outbuf_start; |
| 244 |
} |
| 245 |
|
| 246 |
Py_ssize_t pypy_cjk_enc_inbuf_remaining(struct pypy_cjk_enc_s *d) |
| 247 |
{ |
| 248 |
return d->inbuf_end - d->inbuf; |
| 249 |
} |
| 250 |
|
| 251 |
Py_ssize_t pypy_cjk_enc_inbuf_consumed(struct pypy_cjk_enc_s* d) |
| 252 |
{ |
| 253 |
return d->inbuf - d->inbuf_start; |
| 254 |
} |
| 255 |
|
| 256 |
Py_ssize_t pypy_cjk_enc_replace_on_error(struct pypy_cjk_enc_s* d, |
| 257 |
char *newbuf, Py_ssize_t newlen, |
| 258 |
Py_ssize_t in_offset) |
| 259 |
{ |
| 260 |
if (newlen > 0) |
| 261 |
{ |
| 262 |
if (d->outbuf + newlen > d->outbuf_end) |
| 263 |
if (expand_encodebuffer(d, newlen) == -1) |
| 264 |
return MBERR_NOMEMORY; |
| 265 |
memcpy(d->outbuf, newbuf, newlen); |
| 266 |
d->outbuf += newlen; |
| 267 |
} |
| 268 |
d->inbuf = d->inbuf_start + in_offset; |
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
const MultibyteCodec *pypy_cjk_enc_getcodec(struct pypy_cjk_enc_s *d) |
| 273 |
{ |
| 274 |
return d->codec; |
| 275 |
} |