Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 2001-2012, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * file name: ustr_wcs.cpp |
michael@0 | 9 | * encoding: US-ASCII |
michael@0 | 10 | * tab size: 8 (not used) |
michael@0 | 11 | * indentation:4 |
michael@0 | 12 | * |
michael@0 | 13 | * created on: 2004sep07 |
michael@0 | 14 | * created by: Markus W. Scherer |
michael@0 | 15 | * |
michael@0 | 16 | * u_strToWCS() and u_strFromWCS() functions |
michael@0 | 17 | * moved here from ustrtrns.c for better modularization. |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | #include "unicode/utypes.h" |
michael@0 | 21 | #include "unicode/ustring.h" |
michael@0 | 22 | #include "cstring.h" |
michael@0 | 23 | #include "cwchar.h" |
michael@0 | 24 | #include "cmemory.h" |
michael@0 | 25 | #include "ustr_imp.h" |
michael@0 | 26 | #include "ustr_cnv.h" |
michael@0 | 27 | |
michael@0 | 28 | #if defined(U_WCHAR_IS_UTF16) || defined(U_WCHAR_IS_UTF32) || !UCONFIG_NO_CONVERSION |
michael@0 | 29 | |
michael@0 | 30 | #define _STACK_BUFFER_CAPACITY 1000 |
michael@0 | 31 | #define _BUFFER_CAPACITY_MULTIPLIER 2 |
michael@0 | 32 | |
michael@0 | 33 | #if !defined(U_WCHAR_IS_UTF16) && !defined(U_WCHAR_IS_UTF32) |
michael@0 | 34 | // TODO: We should use CharString for char buffers and UnicodeString for UChar buffers. |
michael@0 | 35 | // Then we could change this to work only with wchar_t buffers. |
michael@0 | 36 | static inline UBool |
michael@0 | 37 | u_growAnyBufferFromStatic(void *context, |
michael@0 | 38 | void **pBuffer, int32_t *pCapacity, int32_t reqCapacity, |
michael@0 | 39 | int32_t length, int32_t size) { |
michael@0 | 40 | // Use char* not void* to avoid the compiler's strict-aliasing assumptions |
michael@0 | 41 | // and related warnings. |
michael@0 | 42 | char *newBuffer=(char *)uprv_malloc(reqCapacity*size); |
michael@0 | 43 | if(newBuffer!=NULL) { |
michael@0 | 44 | if(length>0) { |
michael@0 | 45 | uprv_memcpy(newBuffer, *pBuffer, length*size); |
michael@0 | 46 | } |
michael@0 | 47 | *pCapacity=reqCapacity; |
michael@0 | 48 | } else { |
michael@0 | 49 | *pCapacity=0; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | /* release the old pBuffer if it was not statically allocated */ |
michael@0 | 53 | if(*pBuffer!=(char *)context) { |
michael@0 | 54 | uprv_free(*pBuffer); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | *pBuffer=newBuffer; |
michael@0 | 58 | return (UBool)(newBuffer!=NULL); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | /* helper function */ |
michael@0 | 62 | static wchar_t* |
michael@0 | 63 | _strToWCS(wchar_t *dest, |
michael@0 | 64 | int32_t destCapacity, |
michael@0 | 65 | int32_t *pDestLength, |
michael@0 | 66 | const UChar *src, |
michael@0 | 67 | int32_t srcLength, |
michael@0 | 68 | UErrorCode *pErrorCode){ |
michael@0 | 69 | |
michael@0 | 70 | char stackBuffer [_STACK_BUFFER_CAPACITY]; |
michael@0 | 71 | char* tempBuf = stackBuffer; |
michael@0 | 72 | int32_t tempBufCapacity = _STACK_BUFFER_CAPACITY; |
michael@0 | 73 | char* tempBufLimit = stackBuffer + tempBufCapacity; |
michael@0 | 74 | UConverter* conv = NULL; |
michael@0 | 75 | char* saveBuf = tempBuf; |
michael@0 | 76 | wchar_t* intTarget=NULL; |
michael@0 | 77 | int32_t intTargetCapacity=0; |
michael@0 | 78 | int count=0,retVal=0; |
michael@0 | 79 | |
michael@0 | 80 | const UChar *pSrcLimit =NULL; |
michael@0 | 81 | const UChar *pSrc = src; |
michael@0 | 82 | |
michael@0 | 83 | conv = u_getDefaultConverter(pErrorCode); |
michael@0 | 84 | |
michael@0 | 85 | if(U_FAILURE(*pErrorCode)){ |
michael@0 | 86 | return NULL; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | if(srcLength == -1){ |
michael@0 | 90 | srcLength = u_strlen(pSrc); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | pSrcLimit = pSrc + srcLength; |
michael@0 | 94 | |
michael@0 | 95 | for(;;) { |
michael@0 | 96 | /* reset the error state */ |
michael@0 | 97 | *pErrorCode = U_ZERO_ERROR; |
michael@0 | 98 | |
michael@0 | 99 | /* convert to chars using default converter */ |
michael@0 | 100 | ucnv_fromUnicode(conv,&tempBuf,tempBufLimit,&pSrc,pSrcLimit,NULL,(UBool)(pSrc==pSrcLimit),pErrorCode); |
michael@0 | 101 | count =(tempBuf - saveBuf); |
michael@0 | 102 | |
michael@0 | 103 | /* This should rarely occur */ |
michael@0 | 104 | if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR){ |
michael@0 | 105 | tempBuf = saveBuf; |
michael@0 | 106 | |
michael@0 | 107 | /* we dont have enough room on the stack grow the buffer */ |
michael@0 | 108 | int32_t newCapacity = 2 * srcLength; |
michael@0 | 109 | if(newCapacity <= tempBufCapacity) { |
michael@0 | 110 | newCapacity = _BUFFER_CAPACITY_MULTIPLIER * tempBufCapacity; |
michael@0 | 111 | } |
michael@0 | 112 | if(!u_growAnyBufferFromStatic(stackBuffer,(void**) &tempBuf, &tempBufCapacity, |
michael@0 | 113 | newCapacity, count, 1)) { |
michael@0 | 114 | goto cleanup; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | saveBuf = tempBuf; |
michael@0 | 118 | tempBufLimit = tempBuf + tempBufCapacity; |
michael@0 | 119 | tempBuf = tempBuf + count; |
michael@0 | 120 | |
michael@0 | 121 | } else { |
michael@0 | 122 | break; |
michael@0 | 123 | } |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | if(U_FAILURE(*pErrorCode)){ |
michael@0 | 127 | goto cleanup; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | /* done with conversion null terminate the char buffer */ |
michael@0 | 131 | if(count>=tempBufCapacity){ |
michael@0 | 132 | tempBuf = saveBuf; |
michael@0 | 133 | /* we dont have enough room on the stack grow the buffer */ |
michael@0 | 134 | if(!u_growAnyBufferFromStatic(stackBuffer,(void**) &tempBuf, &tempBufCapacity, |
michael@0 | 135 | count+1, count, 1)) { |
michael@0 | 136 | goto cleanup; |
michael@0 | 137 | } |
michael@0 | 138 | saveBuf = tempBuf; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | saveBuf[count]=0; |
michael@0 | 142 | |
michael@0 | 143 | |
michael@0 | 144 | /* allocate more space than required |
michael@0 | 145 | * here we assume that every char requires |
michael@0 | 146 | * no more than 2 wchar_ts |
michael@0 | 147 | */ |
michael@0 | 148 | intTargetCapacity = (count * _BUFFER_CAPACITY_MULTIPLIER + 1) /*for null termination */; |
michael@0 | 149 | intTarget = (wchar_t*)uprv_malloc( intTargetCapacity * sizeof(wchar_t) ); |
michael@0 | 150 | |
michael@0 | 151 | if(intTarget){ |
michael@0 | 152 | |
michael@0 | 153 | int32_t nulLen = 0; |
michael@0 | 154 | int32_t remaining = intTargetCapacity; |
michael@0 | 155 | wchar_t* pIntTarget=intTarget; |
michael@0 | 156 | tempBuf = saveBuf; |
michael@0 | 157 | |
michael@0 | 158 | /* now convert the mbs to wcs */ |
michael@0 | 159 | for(;;){ |
michael@0 | 160 | |
michael@0 | 161 | /* we can call the system API since we are sure that |
michael@0 | 162 | * there is atleast 1 null in the input |
michael@0 | 163 | */ |
michael@0 | 164 | retVal = uprv_mbstowcs(pIntTarget,(tempBuf+nulLen),remaining); |
michael@0 | 165 | |
michael@0 | 166 | if(retVal==-1){ |
michael@0 | 167 | *pErrorCode = U_INVALID_CHAR_FOUND; |
michael@0 | 168 | break; |
michael@0 | 169 | }else if(retVal== remaining){/* should never occur */ |
michael@0 | 170 | int numWritten = (pIntTarget-intTarget); |
michael@0 | 171 | u_growAnyBufferFromStatic(NULL,(void**) &intTarget, |
michael@0 | 172 | &intTargetCapacity, |
michael@0 | 173 | intTargetCapacity * _BUFFER_CAPACITY_MULTIPLIER, |
michael@0 | 174 | numWritten, |
michael@0 | 175 | sizeof(wchar_t)); |
michael@0 | 176 | pIntTarget = intTarget; |
michael@0 | 177 | remaining=intTargetCapacity; |
michael@0 | 178 | |
michael@0 | 179 | if(nulLen!=count){ /*there are embedded nulls*/ |
michael@0 | 180 | pIntTarget+=numWritten; |
michael@0 | 181 | remaining-=numWritten; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | }else{ |
michael@0 | 185 | int32_t nulVal; |
michael@0 | 186 | /*scan for nulls */ |
michael@0 | 187 | /* we donot check for limit since tempBuf is null terminated */ |
michael@0 | 188 | while(tempBuf[nulLen++] != 0){ |
michael@0 | 189 | } |
michael@0 | 190 | nulVal = (nulLen < srcLength) ? 1 : 0; |
michael@0 | 191 | pIntTarget = pIntTarget + retVal+nulVal; |
michael@0 | 192 | remaining -=(retVal+nulVal); |
michael@0 | 193 | |
michael@0 | 194 | /* check if we have reached the source limit*/ |
michael@0 | 195 | if(nulLen>=(count)){ |
michael@0 | 196 | break; |
michael@0 | 197 | } |
michael@0 | 198 | } |
michael@0 | 199 | } |
michael@0 | 200 | count = (int32_t)(pIntTarget-intTarget); |
michael@0 | 201 | |
michael@0 | 202 | if(0 < count && count <= destCapacity){ |
michael@0 | 203 | uprv_memcpy(dest,intTarget,count*sizeof(wchar_t)); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | if(pDestLength){ |
michael@0 | 207 | *pDestLength = count; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | /* free the allocated memory */ |
michael@0 | 211 | uprv_free(intTarget); |
michael@0 | 212 | |
michael@0 | 213 | }else{ |
michael@0 | 214 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 215 | } |
michael@0 | 216 | cleanup: |
michael@0 | 217 | /* are we still using stack buffer */ |
michael@0 | 218 | if(stackBuffer != saveBuf){ |
michael@0 | 219 | uprv_free(saveBuf); |
michael@0 | 220 | } |
michael@0 | 221 | u_terminateWChars(dest,destCapacity,count,pErrorCode); |
michael@0 | 222 | |
michael@0 | 223 | u_releaseDefaultConverter(conv); |
michael@0 | 224 | |
michael@0 | 225 | return dest; |
michael@0 | 226 | } |
michael@0 | 227 | #endif |
michael@0 | 228 | |
michael@0 | 229 | U_CAPI wchar_t* U_EXPORT2 |
michael@0 | 230 | u_strToWCS(wchar_t *dest, |
michael@0 | 231 | int32_t destCapacity, |
michael@0 | 232 | int32_t *pDestLength, |
michael@0 | 233 | const UChar *src, |
michael@0 | 234 | int32_t srcLength, |
michael@0 | 235 | UErrorCode *pErrorCode){ |
michael@0 | 236 | |
michael@0 | 237 | /* args check */ |
michael@0 | 238 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)){ |
michael@0 | 239 | return NULL; |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | if( (src==NULL && srcLength!=0) || srcLength < -1 || |
michael@0 | 243 | (destCapacity<0) || (dest == NULL && destCapacity > 0) |
michael@0 | 244 | ) { |
michael@0 | 245 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 246 | return NULL; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | #ifdef U_WCHAR_IS_UTF16 |
michael@0 | 250 | /* wchar_t is UTF-16 just do a memcpy */ |
michael@0 | 251 | if(srcLength == -1){ |
michael@0 | 252 | srcLength = u_strlen(src); |
michael@0 | 253 | } |
michael@0 | 254 | if(0 < srcLength && srcLength <= destCapacity){ |
michael@0 | 255 | uprv_memcpy(dest,src,srcLength*U_SIZEOF_UCHAR); |
michael@0 | 256 | } |
michael@0 | 257 | if(pDestLength){ |
michael@0 | 258 | *pDestLength = srcLength; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | u_terminateUChars(dest,destCapacity,srcLength,pErrorCode); |
michael@0 | 262 | |
michael@0 | 263 | return dest; |
michael@0 | 264 | |
michael@0 | 265 | #elif defined U_WCHAR_IS_UTF32 |
michael@0 | 266 | |
michael@0 | 267 | return (wchar_t*)u_strToUTF32((UChar32*)dest, destCapacity, pDestLength, |
michael@0 | 268 | src, srcLength, pErrorCode); |
michael@0 | 269 | |
michael@0 | 270 | #else |
michael@0 | 271 | |
michael@0 | 272 | return _strToWCS(dest,destCapacity,pDestLength,src,srcLength, pErrorCode); |
michael@0 | 273 | |
michael@0 | 274 | #endif |
michael@0 | 275 | |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | #if !defined(U_WCHAR_IS_UTF16) && !defined(U_WCHAR_IS_UTF32) |
michael@0 | 279 | /* helper function */ |
michael@0 | 280 | static UChar* |
michael@0 | 281 | _strFromWCS( UChar *dest, |
michael@0 | 282 | int32_t destCapacity, |
michael@0 | 283 | int32_t *pDestLength, |
michael@0 | 284 | const wchar_t *src, |
michael@0 | 285 | int32_t srcLength, |
michael@0 | 286 | UErrorCode *pErrorCode) |
michael@0 | 287 | { |
michael@0 | 288 | int32_t retVal =0, count =0 ; |
michael@0 | 289 | UConverter* conv = NULL; |
michael@0 | 290 | UChar* pTarget = NULL; |
michael@0 | 291 | UChar* pTargetLimit = NULL; |
michael@0 | 292 | UChar* target = NULL; |
michael@0 | 293 | |
michael@0 | 294 | UChar uStack [_STACK_BUFFER_CAPACITY]; |
michael@0 | 295 | |
michael@0 | 296 | wchar_t wStack[_STACK_BUFFER_CAPACITY]; |
michael@0 | 297 | wchar_t* pWStack = wStack; |
michael@0 | 298 | |
michael@0 | 299 | |
michael@0 | 300 | char cStack[_STACK_BUFFER_CAPACITY]; |
michael@0 | 301 | int32_t cStackCap = _STACK_BUFFER_CAPACITY; |
michael@0 | 302 | char* pCSrc=cStack; |
michael@0 | 303 | char* pCSave=pCSrc; |
michael@0 | 304 | char* pCSrcLimit=NULL; |
michael@0 | 305 | |
michael@0 | 306 | const wchar_t* pSrc = src; |
michael@0 | 307 | const wchar_t* pSrcLimit = NULL; |
michael@0 | 308 | |
michael@0 | 309 | if(srcLength ==-1){ |
michael@0 | 310 | /* if the wchar_t source is null terminated we can safely |
michael@0 | 311 | * assume that there are no embedded nulls, this is a fast |
michael@0 | 312 | * path for null terminated strings. |
michael@0 | 313 | */ |
michael@0 | 314 | for(;;){ |
michael@0 | 315 | /* convert wchars to chars */ |
michael@0 | 316 | retVal = uprv_wcstombs(pCSrc,src, cStackCap); |
michael@0 | 317 | |
michael@0 | 318 | if(retVal == -1){ |
michael@0 | 319 | *pErrorCode = U_ILLEGAL_CHAR_FOUND; |
michael@0 | 320 | goto cleanup; |
michael@0 | 321 | }else if(retVal >= (cStackCap-1)){ |
michael@0 | 322 | /* Should rarely occur */ |
michael@0 | 323 | u_growAnyBufferFromStatic(cStack,(void**)&pCSrc,&cStackCap, |
michael@0 | 324 | cStackCap * _BUFFER_CAPACITY_MULTIPLIER, 0, sizeof(char)); |
michael@0 | 325 | pCSave = pCSrc; |
michael@0 | 326 | }else{ |
michael@0 | 327 | /* converted every thing */ |
michael@0 | 328 | pCSrc = pCSrc+retVal; |
michael@0 | 329 | break; |
michael@0 | 330 | } |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | }else{ |
michael@0 | 334 | /* here the source is not null terminated |
michael@0 | 335 | * so it may have nulls embeded and we need to |
michael@0 | 336 | * do some extra processing |
michael@0 | 337 | */ |
michael@0 | 338 | int32_t remaining =cStackCap; |
michael@0 | 339 | |
michael@0 | 340 | pSrcLimit = src + srcLength; |
michael@0 | 341 | |
michael@0 | 342 | for(;;){ |
michael@0 | 343 | register int32_t nulLen = 0; |
michael@0 | 344 | |
michael@0 | 345 | /* find nulls in the string */ |
michael@0 | 346 | while(nulLen<srcLength && pSrc[nulLen++]!=0){ |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | if((pSrc+nulLen) < pSrcLimit){ |
michael@0 | 350 | /* check if we have enough room in pCSrc */ |
michael@0 | 351 | if(remaining < (nulLen * MB_CUR_MAX)){ |
michael@0 | 352 | /* should rarely occur */ |
michael@0 | 353 | int32_t len = (pCSrc-pCSave); |
michael@0 | 354 | pCSrc = pCSave; |
michael@0 | 355 | /* we do not have enough room so grow the buffer*/ |
michael@0 | 356 | u_growAnyBufferFromStatic(cStack,(void**)&pCSrc,&cStackCap, |
michael@0 | 357 | _BUFFER_CAPACITY_MULTIPLIER*cStackCap+(nulLen*MB_CUR_MAX),len,sizeof(char)); |
michael@0 | 358 | |
michael@0 | 359 | pCSave = pCSrc; |
michael@0 | 360 | pCSrc = pCSave+len; |
michael@0 | 361 | remaining = cStackCap-(pCSrc - pCSave); |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | /* we have found a null so convert the |
michael@0 | 365 | * chunk from begining of non-null char to null |
michael@0 | 366 | */ |
michael@0 | 367 | retVal = uprv_wcstombs(pCSrc,pSrc,remaining); |
michael@0 | 368 | |
michael@0 | 369 | if(retVal==-1){ |
michael@0 | 370 | /* an error occurred bail out */ |
michael@0 | 371 | *pErrorCode = U_ILLEGAL_CHAR_FOUND; |
michael@0 | 372 | goto cleanup; |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | pCSrc += retVal+1 /* already null terminated */; |
michael@0 | 376 | |
michael@0 | 377 | pSrc += nulLen; /* skip past the null */ |
michael@0 | 378 | srcLength-=nulLen; /* decrement the srcLength */ |
michael@0 | 379 | remaining -= (pCSrc-pCSave); |
michael@0 | 380 | |
michael@0 | 381 | |
michael@0 | 382 | }else{ |
michael@0 | 383 | /* the source is not null terminated and we are |
michael@0 | 384 | * end of source so we copy the source to a temp buffer |
michael@0 | 385 | * null terminate it and convert wchar_ts to chars |
michael@0 | 386 | */ |
michael@0 | 387 | if(nulLen >= _STACK_BUFFER_CAPACITY){ |
michael@0 | 388 | /* Should rarely occcur */ |
michael@0 | 389 | /* allocate new buffer buffer */ |
michael@0 | 390 | pWStack =(wchar_t*) uprv_malloc(sizeof(wchar_t) * (nulLen + 1)); |
michael@0 | 391 | if(pWStack==NULL){ |
michael@0 | 392 | *pErrorCode = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 393 | goto cleanup; |
michael@0 | 394 | } |
michael@0 | 395 | } |
michael@0 | 396 | if(nulLen>0){ |
michael@0 | 397 | /* copy the contents to tempStack */ |
michael@0 | 398 | uprv_memcpy(pWStack,pSrc,nulLen*sizeof(wchar_t)); |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | /* null terminate the tempBuffer */ |
michael@0 | 402 | pWStack[nulLen] =0 ; |
michael@0 | 403 | |
michael@0 | 404 | if(remaining < (nulLen * MB_CUR_MAX)){ |
michael@0 | 405 | /* Should rarely occur */ |
michael@0 | 406 | int32_t len = (pCSrc-pCSave); |
michael@0 | 407 | pCSrc = pCSave; |
michael@0 | 408 | /* we do not have enough room so grow the buffer*/ |
michael@0 | 409 | u_growAnyBufferFromStatic(cStack,(void**)&pCSrc,&cStackCap, |
michael@0 | 410 | cStackCap+(nulLen*MB_CUR_MAX),len,sizeof(char)); |
michael@0 | 411 | |
michael@0 | 412 | pCSave = pCSrc; |
michael@0 | 413 | pCSrc = pCSave+len; |
michael@0 | 414 | remaining = cStackCap-(pCSrc - pCSave); |
michael@0 | 415 | } |
michael@0 | 416 | /* convert to chars */ |
michael@0 | 417 | retVal = uprv_wcstombs(pCSrc,pWStack,remaining); |
michael@0 | 418 | |
michael@0 | 419 | pCSrc += retVal; |
michael@0 | 420 | pSrc += nulLen; |
michael@0 | 421 | srcLength-=nulLen; /* decrement the srcLength */ |
michael@0 | 422 | break; |
michael@0 | 423 | } |
michael@0 | 424 | } |
michael@0 | 425 | } |
michael@0 | 426 | |
michael@0 | 427 | /* OK..now we have converted from wchar_ts to chars now |
michael@0 | 428 | * convert chars to UChars |
michael@0 | 429 | */ |
michael@0 | 430 | pCSrcLimit = pCSrc; |
michael@0 | 431 | pCSrc = pCSave; |
michael@0 | 432 | pTarget = target= dest; |
michael@0 | 433 | pTargetLimit = dest + destCapacity; |
michael@0 | 434 | |
michael@0 | 435 | conv= u_getDefaultConverter(pErrorCode); |
michael@0 | 436 | |
michael@0 | 437 | if(U_FAILURE(*pErrorCode)|| conv==NULL){ |
michael@0 | 438 | goto cleanup; |
michael@0 | 439 | } |
michael@0 | 440 | |
michael@0 | 441 | for(;;) { |
michael@0 | 442 | |
michael@0 | 443 | *pErrorCode = U_ZERO_ERROR; |
michael@0 | 444 | |
michael@0 | 445 | /* convert to stack buffer*/ |
michael@0 | 446 | ucnv_toUnicode(conv,&pTarget,pTargetLimit,(const char**)&pCSrc,pCSrcLimit,NULL,(UBool)(pCSrc==pCSrcLimit),pErrorCode); |
michael@0 | 447 | |
michael@0 | 448 | /* increment count to number written to stack */ |
michael@0 | 449 | count+= pTarget - target; |
michael@0 | 450 | |
michael@0 | 451 | if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR){ |
michael@0 | 452 | target = uStack; |
michael@0 | 453 | pTarget = uStack; |
michael@0 | 454 | pTargetLimit = uStack + _STACK_BUFFER_CAPACITY; |
michael@0 | 455 | } else { |
michael@0 | 456 | break; |
michael@0 | 457 | } |
michael@0 | 458 | |
michael@0 | 459 | } |
michael@0 | 460 | |
michael@0 | 461 | if(pDestLength){ |
michael@0 | 462 | *pDestLength =count; |
michael@0 | 463 | } |
michael@0 | 464 | |
michael@0 | 465 | u_terminateUChars(dest,destCapacity,count,pErrorCode); |
michael@0 | 466 | |
michael@0 | 467 | cleanup: |
michael@0 | 468 | |
michael@0 | 469 | if(cStack != pCSave){ |
michael@0 | 470 | uprv_free(pCSave); |
michael@0 | 471 | } |
michael@0 | 472 | |
michael@0 | 473 | if(wStack != pWStack){ |
michael@0 | 474 | uprv_free(pWStack); |
michael@0 | 475 | } |
michael@0 | 476 | |
michael@0 | 477 | u_releaseDefaultConverter(conv); |
michael@0 | 478 | |
michael@0 | 479 | return dest; |
michael@0 | 480 | } |
michael@0 | 481 | #endif |
michael@0 | 482 | |
michael@0 | 483 | U_CAPI UChar* U_EXPORT2 |
michael@0 | 484 | u_strFromWCS(UChar *dest, |
michael@0 | 485 | int32_t destCapacity, |
michael@0 | 486 | int32_t *pDestLength, |
michael@0 | 487 | const wchar_t *src, |
michael@0 | 488 | int32_t srcLength, |
michael@0 | 489 | UErrorCode *pErrorCode) |
michael@0 | 490 | { |
michael@0 | 491 | |
michael@0 | 492 | /* args check */ |
michael@0 | 493 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)){ |
michael@0 | 494 | return NULL; |
michael@0 | 495 | } |
michael@0 | 496 | |
michael@0 | 497 | if( (src==NULL && srcLength!=0) || srcLength < -1 || |
michael@0 | 498 | (destCapacity<0) || (dest == NULL && destCapacity > 0) |
michael@0 | 499 | ) { |
michael@0 | 500 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 501 | return NULL; |
michael@0 | 502 | } |
michael@0 | 503 | |
michael@0 | 504 | #ifdef U_WCHAR_IS_UTF16 |
michael@0 | 505 | /* wchar_t is UTF-16 just do a memcpy */ |
michael@0 | 506 | if(srcLength == -1){ |
michael@0 | 507 | srcLength = u_strlen(src); |
michael@0 | 508 | } |
michael@0 | 509 | if(0 < srcLength && srcLength <= destCapacity){ |
michael@0 | 510 | uprv_memcpy(dest,src,srcLength*U_SIZEOF_UCHAR); |
michael@0 | 511 | } |
michael@0 | 512 | if(pDestLength){ |
michael@0 | 513 | *pDestLength = srcLength; |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | u_terminateUChars(dest,destCapacity,srcLength,pErrorCode); |
michael@0 | 517 | |
michael@0 | 518 | return dest; |
michael@0 | 519 | |
michael@0 | 520 | #elif defined U_WCHAR_IS_UTF32 |
michael@0 | 521 | |
michael@0 | 522 | return u_strFromUTF32(dest, destCapacity, pDestLength, |
michael@0 | 523 | (UChar32*)src, srcLength, pErrorCode); |
michael@0 | 524 | |
michael@0 | 525 | #else |
michael@0 | 526 | |
michael@0 | 527 | return _strFromWCS(dest,destCapacity,pDestLength,src,srcLength,pErrorCode); |
michael@0 | 528 | |
michael@0 | 529 | #endif |
michael@0 | 530 | |
michael@0 | 531 | } |
michael@0 | 532 | |
michael@0 | 533 | #endif /* #if !defined(U_WCHAR_IS_UTF16) && !defined(U_WCHAR_IS_UTF32) && !UCONFIG_NO_CONVERSION */ |