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-2004, International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 **********************************************************************
8  *  ucnv_cb.h:
9  *  External APIs for the ICU's codeset conversion library
10  *  Helena Shih
11  *
12  * Modification History:
13  *
14  *   Date        Name        Description
15  */
16 
17 /**
18  * \file
19  * \brief C UConverter functions to aid the writers of callbacks
20  *
21  * <h2> Callback API for UConverter </h2>
22  *
23  * These functions are provided here for the convenience of the callback
24  * writer. If you are just looking for callback functions to use, please
25  * see ucnv_err.h.  DO NOT call these functions directly when you are
26  * working with converters, unless your code has been called as a callback
27  * via ucnv_setFromUCallback or ucnv_setToUCallback !!
28  *
29  * A note about error codes and overflow.  Unlike other ICU functions,
30  * these functions do not expect the error status to be U_ZERO_ERROR.
31  * Callbacks must be much more careful about their error codes.
32  * The error codes used here are in/out parameters, which should be passed
33  * back in the callback's error parameter.
34  *
35  * For example, if you call ucnv_cbfromUWriteBytes to write data out
36  * to the output codepage, it may return U_BUFFER_OVERFLOW_ERROR if
37  * the data did not fit in the target. But this isn't a failing error,
38  * in fact, ucnv_cbfromUWriteBytes may be called AGAIN with the error
39  * status still U_BUFFER_OVERFLOW_ERROR to attempt to write further bytes,
40  * which will also go into the internal overflow buffers.
41  *
42  * Concerning offsets, the 'offset' parameters here are relative to the start
43  * of SOURCE.  For example, Suppose the string "ABCD" was being converted
44  * from Unicode into a codepage which doesn't have a mapping for 'B'.
45  * 'A' will be written out correctly, but
46  * The FromU Callback will be called on an unassigned character for 'B'.
47  * At this point, this is the state of the world:
48  *    Target:    A [..]     [points after A]
49  *    Source:  A B [C] D    [points to C - B has been consumed]
50  *             0 1  2  3
51  *    codePoint = "B"       [the unassigned codepoint]
52  *
53  * Now, suppose a callback wants to write the substitution character '?' to
54  * the target. It calls ucnv_cbFromUWriteBytes() to write the ?.
55  * It should pass ZERO as the offset, because the offset as far as the
56  * callback is concerned is relative to the SOURCE pointer [which points
57  * before 'C'.]  If the callback goes into the args and consumes 'C' also,
58  * it would call FromUWriteBytes with an offset of 1 (and advance the source
59  * pointer).
60  *
61  */
62 
63 #ifndef UCNV_CB_H
64 #define UCNV_CB_H
65 
66 #include "unicode/utypes.h"
67 
68 #if !UCONFIG_NO_CONVERSION
69 
70 #include "unicode/ucnv.h"
71 #include "unicode/ucnv_err.h"
72 
73 /**
74  * ONLY used by FromU callback functions.
75  * Writes out the specified byte output bytes to the target byte buffer or to converter internal buffers.
76  *
77  * @param args callback fromUnicode arguments
78  * @param source source bytes to write
79  * @param length length of bytes to write
80  * @param offsetIndex the relative offset index from callback.
81  * @param err error status. If <TT>U_BUFFER_OVERFLOW</TT> is returned, then U_BUFFER_OVERFLOW <STRONG>must</STRONG>
82  * be returned to the user, because it means that not all data could be written into the target buffer, and some is
83  * in the converter error buffer.
84  * @see ucnv_cbFromUWriteSub
85  * @stable ICU 2.0
86  */
87 U_CAPI void U_EXPORT2
88 ucnv_cbFromUWriteBytes (UConverterFromUnicodeArgs *args,
89                         const char* source,
90                         int32_t length,
91                         int32_t offsetIndex,
92                         UErrorCode * err);
93 
94 /**
95  * ONLY used by FromU callback functions.
96  * This function will write out the correct substitution character sequence
97  * to the target.
98  *
99  * @param args callback fromUnicode arguments
100  * @param offsetIndex the relative offset index from the current source pointer to be used
101  * @param err error status. If <TT>U_BUFFER_OVERFLOW</TT> is returned, then U_BUFFER_OVERFLOW <STRONG>must</STRONG>
102  * be returned to the user, because it means that not all data could be written into the target buffer, and some is
103  * in the converter error buffer.
104  * @see ucnv_cbFromUWriteBytes
105  * @stable ICU 2.0
106  */
107 U_CAPI void U_EXPORT2
108 ucnv_cbFromUWriteSub (UConverterFromUnicodeArgs *args,
109                       int32_t offsetIndex,
110                       UErrorCode * err);
111 
112 /**
113  * ONLY used by fromU callback functions.
114  * This function will write out the error character(s) to the target UChar buffer.
115  *
116  * @param args callback fromUnicode arguments
117  * @param source pointer to pointer to first UChar to write [on exit: 1 after last UChar processed]
118  * @param sourceLimit pointer after last UChar to write
119  * @param offsetIndex the relative offset index from callback which will be set
120  * @param err error status <TT>U_BUFFER_OVERFLOW</TT>
121  * @see ucnv_cbToUWriteSub
122  * @stable ICU 2.0
123  */
124 U_CAPI void U_EXPORT2 ucnv_cbFromUWriteUChars(UConverterFromUnicodeArgs *args,
125                              const UChar** source,
126                              const UChar*  sourceLimit,
127                              int32_t offsetIndex,
128                              UErrorCode * err);
129 
130 /**
131  * ONLY used by ToU callback functions.
132  *  This function will write out the specified characters to the target
133  * UChar buffer.
134  *
135  * @param args callback toUnicode arguments
136  * @param source source string to write
137  * @param length the length of source string
138  * @param offsetIndex the relative offset index which will be written.
139  * @param err error status <TT>U_BUFFER_OVERFLOW</TT>
140  * @see ucnv_cbToUWriteSub
141  * @stable ICU 2.0
142  */
143 U_CAPI void U_EXPORT2 ucnv_cbToUWriteUChars (UConverterToUnicodeArgs *args,
144                                              const UChar* source,
145                                              int32_t length,
146                                              int32_t offsetIndex,
147                                              UErrorCode * err);
148 
149 /**
150  * ONLY used by ToU  callback functions.
151  * This function will write out the Unicode substitution character (U+FFFD).
152  *
153  * @param args callback fromUnicode arguments
154  * @param offsetIndex the relative offset index from callback.
155  * @param err error status <TT>U_BUFFER_OVERFLOW</TT>
156  * @see ucnv_cbToUWriteUChars
157  * @stable ICU 2.0
158  */
159 U_CAPI void U_EXPORT2 ucnv_cbToUWriteSub (UConverterToUnicodeArgs *args,
160                        int32_t offsetIndex,
161                        UErrorCode * err);
162 #endif
163 
164 #endif
165