1 /* $FreeBSD$ */
2 /*	$NetBSD: citrus_iconv_none.c,v 1.3 2011/05/23 14:45:44 joerg Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause
6  *
7  * Copyright (c)2003 Citrus Project,
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #include <sys/queue.h>
34 
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdbool.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include "citrus_types.h"
42 #include "citrus_module.h"
43 #include "citrus_hash.h"
44 #include "citrus_iconv.h"
45 #include "citrus_iconv_none.h"
46 
47 /* ---------------------------------------------------------------------- */
48 
49 _CITRUS_ICONV_DECLS(iconv_none);
50 _CITRUS_ICONV_DEF_OPS(iconv_none);
51 
52 
53 /* ---------------------------------------------------------------------- */
54 
55 int
56 _citrus_iconv_none_iconv_getops(struct _citrus_iconv_ops *ops)
57 {
58 
59 	memcpy(ops, &_citrus_iconv_none_iconv_ops,
60 	       sizeof(_citrus_iconv_none_iconv_ops));
61 
62 	return (0);
63 }
64 
65 static int
66 /*ARGSUSED*/
67 _citrus_iconv_none_iconv_init_shared(
68     struct _citrus_iconv_shared * __restrict ci,
69     const char * __restrict in __unused, const char * __restrict out __unused)
70 {
71 
72 	ci->ci_closure = NULL;
73 	return (0);
74 }
75 
76 static void
77 /*ARGSUSED*/
78 _citrus_iconv_none_iconv_uninit_shared(struct _citrus_iconv_shared *ci __unused)
79 {
80 
81 }
82 
83 static int
84 /*ARGSUSED*/
85 _citrus_iconv_none_iconv_init_context(struct _citrus_iconv *cv)
86 {
87 
88 	cv->cv_closure = NULL;
89 	return (0);
90 }
91 
92 static void
93 /*ARGSUSED*/
94 _citrus_iconv_none_iconv_uninit_context(struct _citrus_iconv *cv __unused)
95 {
96 
97 }
98 
99 static int
100 /*ARGSUSED*/
101 _citrus_iconv_none_iconv_convert(struct _citrus_iconv * __restrict ci __unused,
102     char * __restrict * __restrict in, size_t * __restrict inbytes,
103     char * __restrict * __restrict out, size_t * __restrict outbytes,
104     uint32_t flags __unused, size_t * __restrict invalids)
105 {
106 	size_t len;
107 	int e2big;
108 
109 	if ((in == NULL) || (out == NULL) || (inbytes == NULL))
110 		return (0);
111 	if ((*in == NULL) || (*out == NULL) || (*inbytes == 0) || (*outbytes == 0))
112 		return (0);
113 	len = *inbytes;
114 	e2big = 0;
115 	if (*outbytes<len) {
116 		e2big = 1;
117 		len = *outbytes;
118 	}
119 	memcpy(*out, *in, len);
120 	in += len;
121 	*inbytes -= len;
122 	out += len;
123 	*outbytes -= len;
124 	*invalids = 0;
125 	if (e2big)
126 		return (E2BIG);
127 
128 	return (0);
129 }
130