/*
* Copyright 2013 Garrett D'Amore <garrett@damore.org>
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 2002-2004 Tim J. Robbins
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "lint.h"
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "mblocal.h"
#include "lctype.h"
const char *_RESTRICT_KYWD,
static int _UTF8_mbsinit(const mbstate_t *);
const wchar_t **_RESTRICT_KYWD,
typedef struct {
int want;
} _UTF8State;
void
{
lct->lc_is_ascii = 0;
}
static int
{
}
static size_t
{
return ((size_t)-1);
}
if (s == NULL) {
s = "";
n = 1;
}
if (n == 0)
/* Incomplete multibyte sequence */
return ((size_t)-2);
/*
* Determine the number of octets that make up this character
* from the first octet, and a mask that extracts the
* interesting bits of the first octet. We already know
* the character is at least two bytes long.
*
* We also specify a lower bound for the character code to
* detect redundant, non-"shortest form" encodings. For
* example, the sequence C0 80 is _not_ a legal representation
* of the null character. This enforces a 1-to-1 mapping
* between character codes and their multibyte representations.
*/
ch = (unsigned char)*s;
if ((ch & 0x80) == 0) {
/* Fast path for plain ASCII characters. */
}
mask = 0x1f;
want = 2;
lbound = 0x80;
mask = 0x0f;
want = 3;
lbound = 0x800;
mask = 0x07;
want = 4;
lbound = 0x10000;
#if 0
/* These would be illegal in the UTF-8 space */
mask = 0x03;
want = 5;
lbound = 0x200000;
mask = 0x01;
want = 6;
lbound = 0x4000000;
#endif
} else {
/*
* Malformed input; input is not UTF-8.
*/
return ((size_t)-1);
}
} else {
}
/*
* Decode the octet sequence representing the character in chunks
* of 6 bits, most significant first.
*/
else
if ((*s & 0xc0) != 0x80) {
/*
* Malformed input; bad characters in the middle
* of a character.
*/
return ((size_t)-1);
}
wch <<= 6;
wch |= *s++ & 0x3f;
}
if (i < want) {
/* Incomplete multibyte sequence. */
return ((size_t)-2);
}
/*
* Malformed input; redundant encoding.
*/
return ((size_t)-1);
}
}
static size_t
{
const char *s;
s = *src;
nchr = 0;
/*
* The fast path in the loop below is not safe if an ASCII
* character appears as anything but the first byte of a
* multibyte sequence. Check now to avoid doing it in the loop.
*/
return ((size_t)-1);
}
for (;;) {
if (nms > 0 && (signed char)*s > 0)
/*
* Fast path for plain ASCII characters
* excluding NUL.
*/
nb = 1;
(size_t)-1)
/* Invalid sequence - mbrtowc() sets errno. */
return ((size_t)-1);
return (nchr);
s += nb;
nchr++;
}
/*NOTREACHED*/
}
/*
* The fast path in the loop below is not safe if an ASCII
* character appears as anything but the first byte of a
* multibyte sequence. Check now to avoid doing it in the loop.
*/
return ((size_t)-1);
}
while (len-- > 0) {
if (nms > 0 && (signed char)*s > 0) {
/*
* Fast path for plain ASCII characters
* excluding NUL.
*/
nb = 1;
(size_t)-1) {
*src = s;
return ((size_t)-1);
return (nchr);
} else if (nb == 0) {
return (nchr);
}
s += nb;
nchr++;
dst++;
}
*src = s;
return (nchr);
}
static size_t
{
unsigned char lead;
int i, len;
return ((size_t)-1);
}
if (s == NULL)
/* Reset to initial shift state (no-op) */
return (1);
/*
* Determine the number of octets needed to represent this character.
* We always output the shortest sequence possible. Also specify the
* first few bits of the first octet, which contains the information
* about the sequence length.
*/
if ((wc & ~0x7f) == 0) {
/* Fast path for plain ASCII characters. */
*s = (char)wc;
return (1);
} else if ((wc & ~0x7ff) == 0) {
lead = 0xc0;
len = 2;
} else if ((wc & ~0xffff) == 0) {
lead = 0xe0;
len = 3;
} else if ((wc & ~0x1fffff) == 0) {
lead = 0xf0;
len = 4;
#if 0
/* Again, 5 and 6 byte encodings are simply not permitted */
} else if ((wc & ~0x3ffffff) == 0) {
lead = 0xf8;
len = 5;
} else if ((wc & ~0x7fffffff) == 0) {
lead = 0xfc;
len = 6;
#endif
} else {
return ((size_t)-1);
}
/*
* Output the octets representing the character in chunks
* of 6 bits, least significant last. The first octet is
* a special case because it contains the sequence length
* information.
*/
for (i = len - 1; i > 0; i--) {
wc >>= 6;
}
return (len);
}
static size_t
{
const wchar_t *s;
return ((size_t)-1);
}
s = *src;
nbytes = 0;
while (nwc-- > 0) {
if (0 <= *s && *s < 0x80)
/* Fast path for plain ASCII characters. */
nb = 1;
(size_t)-1)
/* Invalid character - wcrtomb() sets errno. */
return ((size_t)-1);
if (*s == L'\0')
s++;
}
return (nbytes);
}
if (0 <= *s && *s < 0x80) {
/* Fast path for plain ASCII characters. */
nb = 1;
*dst = *s;
/* Enough space to translate in-place. */
*src = s;
return ((size_t)-1);
}
} else {
/*
* May not be enough space; use temp. buffer.
*/
*src = s;
return ((size_t)-1);
}
/* MB sequence for character won't fit. */
break;
}
if (*s == L'\0') {
}
s++;
}
*src = s;
return (nbytes);
}