]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ****************************************************************************** | |
3 | * | |
374ca955 | 4 | * Copyright (C) 1999-2004, International Business Machines |
b75a7d8f A |
5 | * Corporation and others. All Rights Reserved. |
6 | * | |
7 | ****************************************************************************** | |
8 | * | |
9 | * uconv_cnv.c: | |
10 | * Implements all the low level conversion functions | |
11 | * T_UnicodeConverter_{to,from}Unicode_$ConversionType | |
12 | * | |
13 | * Change history: | |
14 | * | |
15 | * 06/29/2000 helena Major rewrite of the callback APIs. | |
16 | */ | |
17 | ||
18 | #include "unicode/utypes.h" | |
374ca955 A |
19 | |
20 | #if !UCONFIG_NO_CONVERSION | |
21 | ||
b75a7d8f A |
22 | #include "unicode/ucnv_err.h" |
23 | #include "unicode/ucnv.h" | |
24 | #include "unicode/uset.h" | |
25 | #include "ucnv_cnv.h" | |
374ca955 | 26 | #include "ucnv_bld.h" |
b75a7d8f A |
27 | #include "cmemory.h" |
28 | ||
374ca955 A |
29 | U_CFUNC void |
30 | ucnv_getCompleteUnicodeSet(const UConverter *cnv, | |
73c04bcf | 31 | const USetAdder *sa, |
374ca955 A |
32 | UConverterUnicodeSet which, |
33 | UErrorCode *pErrorCode) { | |
34 | sa->addRange(sa->set, 0, 0x10ffff); | |
35 | } | |
36 | ||
37 | U_CFUNC void | |
38 | ucnv_getNonSurrogateUnicodeSet(const UConverter *cnv, | |
73c04bcf | 39 | const USetAdder *sa, |
374ca955 A |
40 | UConverterUnicodeSet which, |
41 | UErrorCode *pErrorCode) { | |
42 | sa->addRange(sa->set, 0, 0xd7ff); | |
43 | sa->addRange(sa->set, 0xe000, 0x10ffff); | |
b75a7d8f A |
44 | } |
45 | ||
374ca955 A |
46 | U_CFUNC void |
47 | ucnv_fromUWriteBytes(UConverter *cnv, | |
48 | const char *bytes, int32_t length, | |
49 | char **target, const char *targetLimit, | |
50 | int32_t **offsets, | |
51 | int32_t sourceIndex, | |
52 | UErrorCode *pErrorCode) { | |
53 | char *t=*target; | |
54 | int32_t *o; | |
55 | ||
56 | /* write bytes */ | |
57 | if(offsets==NULL || (o=*offsets)==NULL) { | |
58 | while(length>0 && t<targetLimit) { | |
59 | *t++=*bytes++; | |
60 | --length; | |
b75a7d8f | 61 | } |
374ca955 A |
62 | } else { |
63 | /* output with offsets */ | |
64 | while(length>0 && t<targetLimit) { | |
65 | *t++=*bytes++; | |
66 | *o++=sourceIndex; | |
67 | --length; | |
68 | } | |
69 | *offsets=o; | |
b75a7d8f | 70 | } |
374ca955 A |
71 | *target=t; |
72 | ||
73 | /* write overflow */ | |
74 | if(length>0) { | |
75 | if(cnv!=NULL) { | |
76 | t=(char *)cnv->charErrorBuffer; | |
77 | cnv->charErrorBufferLength=(int8_t)length; | |
78 | do { | |
79 | *t++=(uint8_t)*bytes++; | |
80 | } while(--length>0); | |
b75a7d8f | 81 | } |
374ca955 | 82 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
b75a7d8f A |
83 | } |
84 | } | |
85 | ||
374ca955 A |
86 | U_CFUNC void |
87 | ucnv_toUWriteUChars(UConverter *cnv, | |
88 | const UChar *uchars, int32_t length, | |
89 | UChar **target, const UChar *targetLimit, | |
90 | int32_t **offsets, | |
91 | int32_t sourceIndex, | |
92 | UErrorCode *pErrorCode) { | |
93 | UChar *t=*target; | |
94 | int32_t *o; | |
95 | ||
96 | /* write UChars */ | |
97 | if(offsets==NULL || (o=*offsets)==NULL) { | |
98 | while(length>0 && t<targetLimit) { | |
99 | *t++=*uchars++; | |
100 | --length; | |
101 | } | |
102 | } else { | |
103 | /* output with offsets */ | |
104 | while(length>0 && t<targetLimit) { | |
105 | *t++=*uchars++; | |
106 | *o++=sourceIndex; | |
107 | --length; | |
108 | } | |
109 | *offsets=o; | |
b75a7d8f | 110 | } |
374ca955 | 111 | *target=t; |
b75a7d8f | 112 | |
374ca955 A |
113 | /* write overflow */ |
114 | if(length>0) { | |
115 | if(cnv!=NULL) { | |
116 | t=cnv->UCharErrorBuffer; | |
117 | cnv->UCharErrorBufferLength=(int8_t)length; | |
b75a7d8f | 118 | do { |
374ca955 A |
119 | *t++=*uchars++; |
120 | } while(--length>0); | |
b75a7d8f | 121 | } |
374ca955 | 122 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
b75a7d8f | 123 | } |
b75a7d8f A |
124 | } |
125 | ||
374ca955 A |
126 | U_CFUNC void |
127 | ucnv_toUWriteCodePoint(UConverter *cnv, | |
128 | UChar32 c, | |
129 | UChar **target, const UChar *targetLimit, | |
130 | int32_t **offsets, | |
131 | int32_t sourceIndex, | |
132 | UErrorCode *pErrorCode) { | |
133 | UChar *t; | |
134 | int32_t *o; | |
135 | ||
136 | t=*target; | |
137 | ||
138 | if(t<targetLimit) { | |
139 | if(c<=0xffff) { | |
140 | *t++=(UChar)c; | |
141 | c=U_SENTINEL; | |
142 | } else /* c is a supplementary code point */ { | |
143 | *t++=U16_LEAD(c); | |
144 | c=U16_TRAIL(c); | |
145 | if(t<targetLimit) { | |
146 | *t++=(UChar)c; | |
147 | c=U_SENTINEL; | |
b75a7d8f A |
148 | } |
149 | } | |
b75a7d8f | 150 | |
374ca955 A |
151 | /* write offsets */ |
152 | if(offsets!=NULL && (o=*offsets)!=NULL) { | |
153 | *o++=sourceIndex; | |
154 | if((*target+1)<t) { | |
155 | *o++=sourceIndex; | |
b75a7d8f | 156 | } |
374ca955 | 157 | *offsets=o; |
b75a7d8f A |
158 | } |
159 | } | |
160 | ||
374ca955 | 161 | *target=t; |
b75a7d8f | 162 | |
374ca955 A |
163 | /* write overflow from c */ |
164 | if(c>=0) { | |
165 | if(cnv!=NULL) { | |
166 | int8_t i=0; | |
167 | U16_APPEND_UNSAFE(cnv->UCharErrorBuffer, i, c); | |
168 | cnv->UCharErrorBufferLength=i; | |
169 | } | |
170 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; | |
171 | } | |
b75a7d8f A |
172 | } |
173 | ||
374ca955 | 174 | #endif |