Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/locale/gb2312.c
/*- | /*- | ||||
* Copyright 2013 Garrett D'Amore <garrett@damore.org> | |||||
* Copyright 2010 Nexenta Systems, Inc. All rights reserved. | |||||
* Copyright (c) 2004 Tim J. Robbins. All rights reserved. | * Copyright (c) 2004 Tim J. Robbins. All rights reserved. | ||||
* Copyright (c) 2003 David Xu <davidxu@freebsd.org> | * Copyright (c) 2003 David Xu <davidxu@freebsd.org> | ||||
* All rights reserved. | * All rights reserved. | ||||
* | * | ||||
* Copyright (c) 2011 The FreeBSD Foundation | * Copyright (c) 2011 The FreeBSD Foundation | ||||
* All rights reserved. | * All rights reserved. | ||||
* Portions of this software were developed by David Chisnall | * Portions of this software were developed by David Chisnall | ||||
* under sponsorship from the FreeBSD Foundation. | * under sponsorship from the FreeBSD Foundation. | ||||
Show All 30 Lines | |||||
#include <wchar.h> | #include <wchar.h> | ||||
#include "mblocal.h" | #include "mblocal.h" | ||||
static size_t _GB2312_mbrtowc(wchar_t * __restrict, const char * __restrict, | static size_t _GB2312_mbrtowc(wchar_t * __restrict, const char * __restrict, | ||||
size_t, mbstate_t * __restrict); | size_t, mbstate_t * __restrict); | ||||
static int _GB2312_mbsinit(const mbstate_t *); | static int _GB2312_mbsinit(const mbstate_t *); | ||||
static size_t _GB2312_wcrtomb(char * __restrict, wchar_t, | static size_t _GB2312_wcrtomb(char * __restrict, wchar_t, | ||||
mbstate_t * __restrict); | mbstate_t * __restrict); | ||||
static size_t _GB2312_mbsnrtowcs(wchar_t * __restrict, | |||||
const char ** __restrict, size_t, size_t, | |||||
mbstate_t * __restrict); | |||||
static size_t _GB2312_wcsnrtombs(char * __restrict, | |||||
const wchar_t ** __restrict, size_t, size_t, | |||||
mbstate_t * __restrict); | |||||
typedef struct { | typedef struct { | ||||
int count; | int count; | ||||
u_char bytes[2]; | u_char bytes[2]; | ||||
} _GB2312State; | } _GB2312State; | ||||
int | int | ||||
_GB2312_init(struct xlocale_ctype *l, _RuneLocale *rl) | _GB2312_init(struct xlocale_ctype *l, _RuneLocale *rl) | ||||
{ | { | ||||
l->runes = rl; | l->runes = rl; | ||||
l->__mbrtowc = _GB2312_mbrtowc; | l->__mbrtowc = _GB2312_mbrtowc; | ||||
l->__wcrtomb = _GB2312_wcrtomb; | l->__wcrtomb = _GB2312_wcrtomb; | ||||
l->__mbsinit = _GB2312_mbsinit; | l->__mbsinit = _GB2312_mbsinit; | ||||
l->__mbsnrtowcs = _GB2312_mbsnrtowcs; | |||||
l->__wcsnrtombs = _GB2312_wcsnrtombs; | |||||
l->__mb_cur_max = 2; | l->__mb_cur_max = 2; | ||||
l->__mb_sb_limit = 128; | l->__mb_sb_limit = 128; | ||||
return (0); | return (0); | ||||
} | } | ||||
static int | static int | ||||
_GB2312_mbsinit(const mbstate_t *ps) | _GB2312_mbsinit(const mbstate_t *ps) | ||||
{ | { | ||||
return (ps == NULL || ((const _GB2312State *)ps)->count == 0); | return (ps == NULL || ((const _GB2312State *)ps)->count == 0); | ||||
} | } | ||||
static __inline int | static int | ||||
_GB2312_check(const char *str, size_t n) | _GB2312_check(const char *str, size_t n) | ||||
{ | { | ||||
const u_char *s = (const u_char *)str; | const u_char *s = (const u_char *)str; | ||||
if (n == 0) | if (n == 0) | ||||
/* Incomplete multibyte sequence */ | /* Incomplete multibyte sequence */ | ||||
return (-2); | return (-2); | ||||
if (s[0] >= 0xa1 && s[0] <= 0xfe) { | if (s[0] >= 0xa1 && s[0] <= 0xfe) { | ||||
if (n < 2) | if (n < 2) | ||||
/* Incomplete multibyte sequence */ | /* Incomplete multibyte sequence */ | ||||
return (-2); | return (-2); | ||||
if (s[1] < 0xa1 || s[1] > 0xfe) | if (s[1] < 0xa1 || s[1] > 0xfe) | ||||
/* Invalid multibyte sequence */ | /* Invalid multibyte sequence */ | ||||
return (-1); | return (-1); | ||||
return (2); | return (2); | ||||
} else if (s[0] & 0x80) { | } else if (s[0] & 0x80) { | ||||
/* Invalid multibyte sequence */ | /* Invalid multibyte sequence */ | ||||
return (-1); | return (-1); | ||||
} | } | ||||
return (1); | return (1); | ||||
} | } | ||||
static size_t | static size_t | ||||
_GB2312_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n, | _GB2312_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n, | ||||
mbstate_t * __restrict ps) | mbstate_t * __restrict ps) | ||||
{ | { | ||||
_GB2312State *gs; | _GB2312State *gs; | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | if (s == NULL) | ||||
return (1); | return (1); | ||||
if (wc & 0x8000) { | if (wc & 0x8000) { | ||||
*s++ = (wc >> 8) & 0xff; | *s++ = (wc >> 8) & 0xff; | ||||
*s = wc & 0xff; | *s = wc & 0xff; | ||||
return (2); | return (2); | ||||
} | } | ||||
*s = wc & 0xff; | *s = wc & 0xff; | ||||
return (1); | return (1); | ||||
} | |||||
static size_t | |||||
_GB2312_mbsnrtowcs(wchar_t * __restrict dst, | |||||
const char ** __restrict src, size_t nms, size_t len, | |||||
mbstate_t * __restrict ps) | |||||
{ | |||||
return (__mbsnrtowcs_std(dst, src, nms, len, ps, _GB2312_mbrtowc)); | |||||
} | |||||
static size_t | |||||
_GB2312_wcsnrtombs(char * __restrict dst, | |||||
const wchar_t ** __restrict src, size_t nwc, size_t len, | |||||
mbstate_t * __restrict ps) | |||||
{ | |||||
return (__wcsnrtombs_std(dst, src, nwc, len, ps, _GB2312_wcrtomb)); | |||||
} | } |