Deleted Added
full compact
gbk.c (123665) gbk.c (128004)
1/*-
1/*-
2 * Copyright (c) 2002, 2003 Tim J. Robbins. All rights reserved.
2 * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
3 * Copyright (c) 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Paul Borman at Krystal Technologies.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions

--- 19 unchanged lines hidden (view full) ---

30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
3 * Copyright (c) 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Paul Borman at Krystal Technologies.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions

--- 19 unchanged lines hidden (view full) ---

30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/lib/libc/locale/gbk.c 123665 2003-12-19 12:54:42Z ache $");
38#include <sys/param.h>
39__FBSDID("$FreeBSD: head/lib/libc/locale/gbk.c 128004 2004-04-07 10:48:19Z tjr $");
40
40
41#include <sys/types.h>
42#include <runetype.h>
43#include <stdlib.h>
41#include <runetype.h>
42#include <stdlib.h>
43#include <string.h>
44#include <wchar.h>
45
46extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
47 size_t, mbstate_t * __restrict);
44#include <wchar.h>
45
46extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
47 size_t, mbstate_t * __restrict);
48extern int (*__mbsinit)(const mbstate_t *);
48extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict);
49
50int _GBK_init(_RuneLocale *);
51size_t _GBK_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
52 mbstate_t * __restrict);
49extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict);
50
51int _GBK_init(_RuneLocale *);
52size_t _GBK_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
53 mbstate_t * __restrict);
54int _GBK_mbsinit(const mbstate_t *);
53size_t _GBK_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
54
55size_t _GBK_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
56
57typedef struct {
58 int count;
59 u_char bytes[2];
60} _GBKState;
61
55int
56_GBK_init(_RuneLocale *rl)
57{
58
59 __mbrtowc = _GBK_mbrtowc;
60 __wcrtomb = _GBK_wcrtomb;
62int
63_GBK_init(_RuneLocale *rl)
64{
65
66 __mbrtowc = _GBK_mbrtowc;
67 __wcrtomb = _GBK_wcrtomb;
68 __mbsinit = _GBK_mbsinit;
61 _CurrentRuneLocale = rl;
62 __mb_cur_max = 2;
63 return (0);
64}
65
69 _CurrentRuneLocale = rl;
70 __mb_cur_max = 2;
71 return (0);
72}
73
74int
75_GBK_mbsinit(const mbstate_t *ps)
76{
77
78 return (ps == NULL || ((_GBKState *)ps)->count == 0);
79}
80
66static __inline int
67_gbk_check(u_int c)
68{
69
70 c &= 0xff;
71 return ((c >= 0x81 && c <= 0xfe) ? 2 : 1);
72}
73
74size_t
75_GBK_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
81static __inline int
82_gbk_check(u_int c)
83{
84
85 c &= 0xff;
86 return ((c >= 0x81 && c <= 0xfe) ? 2 : 1);
87}
88
89size_t
90_GBK_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
76 mbstate_t * __restrict ps __unused)
91 mbstate_t * __restrict ps)
77{
92{
93 _GBKState *gs;
78 wchar_t wc;
94 wchar_t wc;
79 int i, len;
95 int i, len, ocount;
96 size_t ncopy;
80
97
81 if (s == NULL)
82 /* Reset to initial shift state (no-op) */
83 return (0);
98 gs = (_GBKState *)ps;
99
100 if (s == NULL) {
101 s = "";
102 n = 1;
103 pwc = NULL;
104 }
105
106 ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(gs->bytes) - gs->count);
107 memcpy(gs->bytes + gs->count, s, ncopy);
108 ocount = gs->count;
109 gs->count += ncopy;
110 s = (char *)gs->bytes;
111 n = gs->count;
112
84 if (n == 0 || (size_t)(len = _gbk_check(*s)) > n)
85 /* Incomplete multibyte sequence */
86 return ((size_t)-2);
87 wc = 0;
88 i = len;
89 while (i-- > 0)
90 wc = (wc << 8) | (unsigned char)*s++;
91 if (pwc != NULL)
92 *pwc = wc;
113 if (n == 0 || (size_t)(len = _gbk_check(*s)) > n)
114 /* Incomplete multibyte sequence */
115 return ((size_t)-2);
116 wc = 0;
117 i = len;
118 while (i-- > 0)
119 wc = (wc << 8) | (unsigned char)*s++;
120 if (pwc != NULL)
121 *pwc = wc;
93 return (wc == L'\0' ? 0 : len);
122 gs->count = 0;
123 return (wc == L'\0' ? 0 : len - ocount);
94}
95
96size_t
97_GBK_wcrtomb(char * __restrict s, wchar_t wc,
98 mbstate_t * __restrict ps __unused)
99{
100
101 if (s == NULL)
102 /* Reset to initial shift state (no-op) */
103 return (1);
104 if (wc & 0x8000) {
105 *s++ = (wc >> 8) & 0xff;
106 *s = wc & 0xff;
107 return (2);
108 }
109 *s = wc & 0xff;
110 return (1);
111}
124}
125
126size_t
127_GBK_wcrtomb(char * __restrict s, wchar_t wc,
128 mbstate_t * __restrict ps __unused)
129{
130
131 if (s == NULL)
132 /* Reset to initial shift state (no-op) */
133 return (1);
134 if (wc & 0x8000) {
135 *s++ = (wc >> 8) & 0xff;
136 *s = wc & 0xff;
137 return (2);
138 }
139 *s = wc & 0xff;
140 return (1);
141}