ucnv_cb.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (C) 2000-2006, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * ucnv_cb.c:
  9. * External APIs for the ICU's codeset conversion library
  10. * Helena Shih
  11. *
  12. * Modification History:
  13. *
  14. * Date Name Description
  15. * 7/28/2000 srl Implementation
  16. */
  17. /**
  18. * @name Character Conversion C API
  19. *
  20. */
  21. #include "unicode/utypes.h"
  22. #if !UCONFIG_NO_CONVERSION
  23. #include "unicode/ucnv_cb.h"
  24. #include "ucnv_bld.h"
  25. #include "ucnv_cnv.h"
  26. #include "cmemory.h"
  27. /* need to update the offsets when the target moves. */
  28. /* Note: Recursion may occur in the cb functions, be sure to update the offsets correctly
  29. if you don't use ucnv_cbXXX functions. Make sure you don't use the same callback within
  30. the same call stack if the complexity arises. */
  31. U_CAPI void U_EXPORT2
  32. ucnv_cbFromUWriteBytes (UConverterFromUnicodeArgs *args,
  33. const char* source,
  34. int32_t length,
  35. int32_t offsetIndex,
  36. UErrorCode * err)
  37. {
  38. if(U_FAILURE(*err)) {
  39. return;
  40. }
  41. ucnv_fromUWriteBytes(
  42. args->converter,
  43. source, length,
  44. &args->target, args->targetLimit,
  45. &args->offsets, offsetIndex,
  46. err);
  47. }
  48. U_CAPI void U_EXPORT2
  49. ucnv_cbFromUWriteUChars(UConverterFromUnicodeArgs *args,
  50. const char16_t** source,
  51. const char16_t* sourceLimit,
  52. int32_t offsetIndex,
  53. UErrorCode * err)
  54. {
  55. /*
  56. This is a fun one. Recursion can occur - we're basically going to
  57. just retry shoving data through the same converter. Note, if you got
  58. here through some kind of invalid sequence, you maybe should emit a
  59. reset sequence of some kind and/or call ucnv_reset(). Since this
  60. IS an actual conversion, take care that you've changed the callback
  61. or the data, or you'll get an infinite loop.
  62. Please set the err value to something reasonable before calling
  63. into this.
  64. */
  65. char *oldTarget;
  66. if(U_FAILURE(*err))
  67. {
  68. return;
  69. }
  70. oldTarget = args->target;
  71. ucnv_fromUnicode(args->converter,
  72. &args->target,
  73. args->targetLimit,
  74. source,
  75. sourceLimit,
  76. nullptr, /* no offsets */
  77. false, /* no flush */
  78. err);
  79. if(args->offsets)
  80. {
  81. while (args->target != oldTarget) /* if it moved at all.. */
  82. {
  83. *(args->offsets)++ = offsetIndex;
  84. oldTarget++;
  85. }
  86. }
  87. /*
  88. Note, if you did something like used a Stop subcallback, things would get interesting.
  89. In fact, here's where we want to return the partially consumed in-source!
  90. */
  91. if(*err == U_BUFFER_OVERFLOW_ERROR)
  92. /* && (*source < sourceLimit && args->target >= args->targetLimit)
  93. -- S. Hrcek */
  94. {
  95. /* Overflowed the target. Now, we'll write into the charErrorBuffer.
  96. It's a fixed size. If we overflow it... Hmm */
  97. char *newTarget;
  98. const char *newTargetLimit;
  99. UErrorCode err2 = U_ZERO_ERROR;
  100. int8_t errBuffLen;
  101. errBuffLen = args->converter->charErrorBufferLength;
  102. /* start the new target at the first free slot in the errbuff.. */
  103. newTarget = (char *)(args->converter->charErrorBuffer + errBuffLen);
  104. newTargetLimit = (char *)(args->converter->charErrorBuffer +
  105. sizeof(args->converter->charErrorBuffer));
  106. if(newTarget >= newTargetLimit)
  107. {
  108. *err = U_INTERNAL_PROGRAM_ERROR;
  109. return;
  110. }
  111. /* We're going to tell the converter that the errbuff len is empty.
  112. This prevents the existing errbuff from being 'flushed' out onto
  113. itself. If the errbuff is needed by the converter this time,
  114. we're hosed - we're out of space! */
  115. args->converter->charErrorBufferLength = 0;
  116. ucnv_fromUnicode(args->converter,
  117. &newTarget,
  118. newTargetLimit,
  119. source,
  120. sourceLimit,
  121. nullptr,
  122. false,
  123. &err2);
  124. /* We can go ahead and overwrite the length here. We know just how
  125. to recalculate it. */
  126. args->converter->charErrorBufferLength = (int8_t)(
  127. newTarget - (char*)args->converter->charErrorBuffer);
  128. if((newTarget >= newTargetLimit) || (err2 == U_BUFFER_OVERFLOW_ERROR))
  129. {
  130. /* now we're REALLY in trouble.
  131. Internal program error - callback shouldn't have written this much
  132. data!
  133. */
  134. *err = U_INTERNAL_PROGRAM_ERROR;
  135. return;
  136. }
  137. /*else {*/
  138. /* sub errs could be invalid/truncated/illegal chars or w/e.
  139. These might want to be passed on up.. But the problem is, we already
  140. need to pass U_BUFFER_OVERFLOW_ERROR. That has to override these
  141. other errs.. */
  142. /*
  143. if(U_FAILURE(err2))
  144. ??
  145. */
  146. /*}*/
  147. }
  148. }
  149. U_CAPI void U_EXPORT2
  150. ucnv_cbFromUWriteSub (UConverterFromUnicodeArgs *args,
  151. int32_t offsetIndex,
  152. UErrorCode * err)
  153. {
  154. UConverter *converter;
  155. int32_t length;
  156. if(U_FAILURE(*err)) {
  157. return;
  158. }
  159. converter = args->converter;
  160. length = converter->subCharLen;
  161. if(length == 0) {
  162. return;
  163. }
  164. if(length < 0) {
  165. /*
  166. * Write/convert the substitution string. Its real length is -length.
  167. * Unlike the escape callback, we need not change the converter's
  168. * callback function because ucnv_setSubstString() verified that
  169. * the string can be converted, so we will not get a conversion error
  170. * and will not recurse.
  171. * At worst we should get a U_BUFFER_OVERFLOW_ERROR.
  172. */
  173. const char16_t *source = (const char16_t *)converter->subChars;
  174. ucnv_cbFromUWriteUChars(args, &source, source - length, offsetIndex, err);
  175. return;
  176. }
  177. if(converter->sharedData->impl->writeSub!=nullptr) {
  178. converter->sharedData->impl->writeSub(args, offsetIndex, err);
  179. }
  180. else if(converter->subChar1!=0 && (uint16_t)converter->invalidUCharBuffer[0]<=(uint16_t)0xffu) {
  181. /*
  182. TODO: Is this untestable because the MBCS converter has a writeSub function to call
  183. and the other converters don't use subChar1?
  184. */
  185. ucnv_cbFromUWriteBytes(args,
  186. (const char *)&converter->subChar1, 1,
  187. offsetIndex, err);
  188. }
  189. else {
  190. ucnv_cbFromUWriteBytes(args,
  191. (const char *)converter->subChars, length,
  192. offsetIndex, err);
  193. }
  194. }
  195. U_CAPI void U_EXPORT2
  196. ucnv_cbToUWriteUChars (UConverterToUnicodeArgs *args,
  197. const char16_t* source,
  198. int32_t length,
  199. int32_t offsetIndex,
  200. UErrorCode * err)
  201. {
  202. if(U_FAILURE(*err)) {
  203. return;
  204. }
  205. ucnv_toUWriteUChars(
  206. args->converter,
  207. source, length,
  208. &args->target, args->targetLimit,
  209. &args->offsets, offsetIndex,
  210. err);
  211. }
  212. U_CAPI void U_EXPORT2
  213. ucnv_cbToUWriteSub (UConverterToUnicodeArgs *args,
  214. int32_t offsetIndex,
  215. UErrorCode * err)
  216. {
  217. static const char16_t kSubstituteChar1 = 0x1A, kSubstituteChar = 0xFFFD;
  218. /* could optimize this case, just one uchar */
  219. if(args->converter->invalidCharLength == 1 && args->converter->subChar1 != 0) {
  220. ucnv_cbToUWriteUChars(args, &kSubstituteChar1, 1, offsetIndex, err);
  221. } else {
  222. ucnv_cbToUWriteUChars(args, &kSubstituteChar, 1, offsetIndex, err);
  223. }
  224. }
  225. #endif