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
|
/* ****************************************************************************
* iconv.c -- Main conversion function.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of the 'all/iconv' module,
* in libcarrot, an experimental modular libc project.
*
* This file is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Lesser Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this file. If not, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
#include "iconv.h"
/**
* iconv:
* Main conversion function.
*
* @arg cd the conversion descriptor.
* @arg inbuf the input buffer pointer reference.
* @arg inleft the input buffer left bytes counter reference.
* @arg outbuf the output buffer pointer reference.
* @arg outleft the output buffer left bytes counter reference.
* @return the number of characters converted in a nonreversible
* way during this call.
*/
size_t iconv(iconv_t cd, char **inbuf, size_t *inleft,
char **outbuf, size_t *outleft)
{
const struct iconv_route *r = (const void*)cd;
struct __iconv_step step;
size_t ret = 0; int err;
/* Check the route. */
if (!r || r->magic != I_MAGIC) {
__set_errno(I_EBADF);
return ((size_t)-1);
}
/* Prepare the step. */
step.next = NULL;
step.next_func = NULL;
step.in = *inbuf;
step.out = *outbuf;
while (*inleft) {
int in0, out0;
int indiff, outdiff;
/* Set the I/O. */
in0 = (int)( *inleft > 0x7FFF ? 0x7FFF : *inleft);
out0 = (int)(*outleft > 0x7FFF ? 0x7FFF : *outleft);
step.in = *inbuf;
step.inleft = in0;
step.out = *outbuf;
step.outleft = out0;
/* Decode the sequence. */
err = (*r->func)(&step);
if (err) {
__set_errno(err);
return ((size_t)-1);
}
/* Check the difference. */
indiff = in0 - step.inleft;
outdiff = out0 - step.outleft;
if (indiff == outdiff && !memcmp(*inbuf, *outbuf, indiff))
ret++;
/* Update the positions. */
*inbuf += indiff;
*inleft -= indiff;
*outbuf += outdiff;
*outleft -= outdiff;
}
/* Everything seem to have gone well. */
return (ret);
}
|