13d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes/*	$OpenBSD: mbstowcs.c,v 1.2 2012/12/05 23:20:00 deraadt Exp $ */
23d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes
33d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes/*-
43d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * Copyright (c) 2002-2004 Tim J. Robbins.
53d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * All rights reserved.
63d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes *
73d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * Redistribution and use in source and binary forms, with or without
83d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * modification, are permitted provided that the following conditions
93d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * are met:
103d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * 1. Redistributions of source code must retain the above copyright
113d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes *    notice, this list of conditions and the following disclaimer.
123d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
133d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes *    notice, this list of conditions and the following disclaimer in the
143d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes *    documentation and/or other materials provided with the distribution.
153d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes *
163d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
173d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
203d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
213d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
223d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
233d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
243d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * SUCH DAMAGE.
273d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes */
283d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes
293d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes
303d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes#include <limits.h>
313d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes#include <stdlib.h>
323d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes#include <string.h>
333d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes#include <wchar.h>
343d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes
353d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughessize_t
363d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughesmbstowcs(wchar_t * __restrict pwcs, const char * __restrict s, size_t n)
373d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes{
383d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes	mbstate_t mbs;
393d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes	const char *sp;
403d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes
413d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes	memset(&mbs, 0, sizeof(mbs));
423d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes	sp = s;
433d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes	return (mbsrtowcs(pwcs, &sp, n, &mbs));
443d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes}
45