Coverage Report

Created: 2024-04-24 06:23

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