xref: /freebsd/lib/libc/locale/wcsrtombs.c (revision 74f90def09fb8e53503cdf5d6548619653ed3580)
1e92a3d83STim J. Robbins /*-
274f90defSTim J. Robbins  * Copyright (c) 2002-2004 Tim J. Robbins.
3e92a3d83STim J. Robbins  * All rights reserved.
4e92a3d83STim J. Robbins  *
5e92a3d83STim J. Robbins  * Redistribution and use in source and binary forms, with or without
6e92a3d83STim J. Robbins  * modification, are permitted provided that the following conditions
7e92a3d83STim J. Robbins  * are met:
8e92a3d83STim J. Robbins  * 1. Redistributions of source code must retain the above copyright
9e92a3d83STim J. Robbins  *    notice, this list of conditions and the following disclaimer.
10e92a3d83STim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
11e92a3d83STim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
12e92a3d83STim J. Robbins  *    documentation and/or other materials provided with the distribution.
13e92a3d83STim J. Robbins  *
14e92a3d83STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15e92a3d83STim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16e92a3d83STim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17e92a3d83STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18e92a3d83STim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19e92a3d83STim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20e92a3d83STim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21e92a3d83STim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22e92a3d83STim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23e92a3d83STim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24e92a3d83STim J. Robbins  * SUCH DAMAGE.
25e92a3d83STim J. Robbins  */
26e92a3d83STim J. Robbins 
27e92a3d83STim J. Robbins #include <sys/cdefs.h>
28e92a3d83STim J. Robbins __FBSDID("$FreeBSD$");
29e92a3d83STim J. Robbins 
30e92a3d83STim J. Robbins #include <limits.h>
31e92a3d83STim J. Robbins #include <stdlib.h>
32e92a3d83STim J. Robbins #include <string.h>
33e92a3d83STim J. Robbins #include <wchar.h>
34e92a3d83STim J. Robbins 
35e92a3d83STim J. Robbins size_t
36e92a3d83STim J. Robbins wcsrtombs(char * __restrict dst, const wchar_t ** __restrict src, size_t len,
3774f90defSTim J. Robbins     mbstate_t * __restrict ps)
38e92a3d83STim J. Robbins {
3974f90defSTim J. Robbins 	static mbstate_t mbs;
4074f90defSTim J. Robbins 	mbstate_t mbsbak;
41e92a3d83STim J. Robbins 	char buf[MB_LEN_MAX];
42e92a3d83STim J. Robbins 	const wchar_t *s;
43e92a3d83STim J. Robbins 	size_t nbytes;
44e92a3d83STim J. Robbins 	int nb;
45e92a3d83STim J. Robbins 
46e92a3d83STim J. Robbins 	s = *src;
47e92a3d83STim J. Robbins 	nbytes = 0;
48e92a3d83STim J. Robbins 
4974f90defSTim J. Robbins 	if (ps == NULL)
5074f90defSTim J. Robbins 		ps = &mbs;
51e92a3d83STim J. Robbins 	if (dst == NULL) {
52e92a3d83STim J. Robbins 		for (;;) {
5374f90defSTim J. Robbins 			if ((nb = (int)wcrtomb(buf, *s, ps)) < 0)
54e92a3d83STim J. Robbins 				/* Invalid character - wcrtomb() sets errno. */
55e92a3d83STim J. Robbins 				return ((size_t)-1);
56e92a3d83STim J. Robbins 			else if (*s == L'\0')
57e92a3d83STim J. Robbins 				return (nbytes + nb - 1);
58e92a3d83STim J. Robbins 			s++;
59e92a3d83STim J. Robbins 			nbytes += nb;
60e92a3d83STim J. Robbins 		}
61e92a3d83STim J. Robbins 		/*NOTREACHED*/
62e92a3d83STim J. Robbins 	}
63e92a3d83STim J. Robbins 
64e92a3d83STim J. Robbins 	while (len > 0) {
65e92a3d83STim J. Robbins 		if (len > (size_t)MB_CUR_MAX) {
66e92a3d83STim J. Robbins 			/* Enough space to translate in-place. */
6774f90defSTim J. Robbins 			if ((nb = (int)wcrtomb(dst, *s, ps)) < 0) {
68e92a3d83STim J. Robbins 				*src = s;
69e92a3d83STim J. Robbins 				return ((size_t)-1);
70e92a3d83STim J. Robbins 			}
71e92a3d83STim J. Robbins 		} else {
7274f90defSTim J. Robbins 			/*
7374f90defSTim J. Robbins 			 * May not be enough space; use temp. buffer.
7474f90defSTim J. Robbins 			 *
7574f90defSTim J. Robbins 			 * We need to save a copy of the conversion state
7674f90defSTim J. Robbins 			 * here so we can restore it if the multibyte
7774f90defSTim J. Robbins 			 * character is too long for the buffer.
7874f90defSTim J. Robbins 			 */
7974f90defSTim J. Robbins 			mbsbak = *ps;
8074f90defSTim J. Robbins 			if ((nb = (int)wcrtomb(buf, *s, ps)) < 0) {
81e92a3d83STim J. Robbins 				*src = s;
82e92a3d83STim J. Robbins 				return ((size_t)-1);
83e92a3d83STim J. Robbins 			}
8474f90defSTim J. Robbins 			if (nb > (int)len) {
85e92a3d83STim J. Robbins 				/* MB sequence for character won't fit. */
8674f90defSTim J. Robbins 				*ps = mbsbak;
87e92a3d83STim J. Robbins 				break;
8874f90defSTim J. Robbins 			}
89e92a3d83STim J. Robbins 			memcpy(dst, buf, nb);
90e92a3d83STim J. Robbins 		}
91e92a3d83STim J. Robbins 		if (*s == L'\0') {
92e92a3d83STim J. Robbins 			*src = NULL;
93e92a3d83STim J. Robbins 			return (nbytes + nb - 1);
94e92a3d83STim J. Robbins 		}
95e92a3d83STim J. Robbins 		s++;
96e92a3d83STim J. Robbins 		dst += nb;
97e92a3d83STim J. Robbins 		len -= nb;
98e92a3d83STim J. Robbins 		nbytes += nb;
99e92a3d83STim J. Robbins 	}
100e92a3d83STim J. Robbins 	*src = s;
101e92a3d83STim J. Robbins 	return (nbytes);
102e92a3d83STim J. Robbins }
103