1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*******************************************************************************
3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Copyright (C) 2003, International Business Machines
5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Corporation and others.  All Rights Reserved.
6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*******************************************************************************
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   file name:  udataswp.c
9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   encoding:   US-ASCII
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   tab size:   8 (not used)
11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   indentation:4
12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   created on: 2003jun05
14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   created by: Markus W. Scherer
15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   Definitions for ICU data transformations for different platforms,
17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   changing between big- and little-endian data and/or between
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*   charset families (ASCII<->EBCDIC).
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/
20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <stdarg.h>
22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h"
23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/udata.h" /* UDataInfo */
24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "ucmndata.h" /* DataHeader */
25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cmemory.h"
26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "udataswp.h"
27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* swapping primitives ------------------------------------------------------ */
29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic int32_t U_CALLCONV
31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_swapArray16(const UDataSwapper *ds,
32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                 const void *inData, int32_t length, void *outData,
33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                 UErrorCode *pErrorCode) {
34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const uint16_t *p;
35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint16_t *q;
36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t count;
37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint16_t x;
38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(ds==NULL || inData==NULL || length<0 || (length&1)!=0 || outData==NULL) {
43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* setup and swapping */
48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    p=(const uint16_t *)inData;
49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    q=(uint16_t *)outData;
50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    count=length/2;
51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    while(count>0) {
52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        x=*p++;
53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *q++=(uint16_t)((x<<8)|(x>>8));
54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        --count;
55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return length;
58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic int32_t U_CALLCONV
61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_copyArray16(const UDataSwapper *ds,
62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                 const void *inData, int32_t length, void *outData,
63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                 UErrorCode *pErrorCode) {
64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(ds==NULL || inData==NULL || length<0 || (length&1)!=0 || outData==NULL) {
68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(length>0 && inData!=outData) {
73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uprv_memcpy(outData, inData, length);
74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return length;
76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic int32_t U_CALLCONV
79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_swapArray32(const UDataSwapper *ds,
80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                 const void *inData, int32_t length, void *outData,
81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                 UErrorCode *pErrorCode) {
82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const uint32_t *p;
83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint32_t *q;
84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t count;
85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint32_t x;
86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(ds==NULL || inData==NULL || length<0 || (length&3)!=0 || outData==NULL) {
91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* setup and swapping */
96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    p=(const uint32_t *)inData;
97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    q=(uint32_t *)outData;
98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    count=length/4;
99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    while(count>0) {
100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        x=*p++;
101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *q++=(uint32_t)((x<<24)|((x<<8)&0xff0000)|((x>>8)&0xff00)|(x>>24));
102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        --count;
103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return length;
106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic int32_t U_CALLCONV
109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_copyArray32(const UDataSwapper *ds,
110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                 const void *inData, int32_t length, void *outData,
111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                 UErrorCode *pErrorCode) {
112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(ds==NULL || inData==NULL || length<0 || (length&3)!=0 || outData==NULL) {
116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(length>0 && inData!=outData) {
121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uprv_memcpy(outData, inData, length);
122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return length;
124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic uint16_t U_CALLCONV
127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_readSwapUInt16(uint16_t x) {
128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return (uint16_t)((x<<8)|(x>>8));
129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic uint16_t U_CALLCONV
132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_readDirectUInt16(uint16_t x) {
133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return x;
134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic uint32_t U_CALLCONV
137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_readSwapUInt32(uint32_t x) {
138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return (uint32_t)((x<<24)|((x<<8)&0xff0000)|((x>>8)&0xff00)|(x>>24));
139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic uint32_t U_CALLCONV
142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_readDirectUInt32(uint32_t x) {
143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return x;
144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void U_CALLCONV
147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_writeSwapUInt16(uint16_t *p, uint16_t x) {
148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    *p=(uint16_t)((x<<8)|(x>>8));
149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void U_CALLCONV
152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_writeDirectUInt16(uint16_t *p, uint16_t x) {
153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    *p=x;
154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void U_CALLCONV
157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_writeSwapUInt32(uint32_t *p, uint32_t x) {
158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    *p=(uint32_t)((x<<24)|((x<<8)&0xff0000)|((x>>8)&0xff00)|(x>>24));
159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void U_CALLCONV
162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuprv_writeDirectUInt32(uint32_t *p, uint32_t x) {
163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    *p=x;
164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int16_t U_EXPORT2
167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruudata_readInt16(const UDataSwapper *ds, int16_t x) {
168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return (int16_t)ds->readUInt16((uint16_t)x);
169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2
172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruudata_readInt32(const UDataSwapper *ds, int32_t x) {
173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return (int32_t)ds->readUInt32((uint32_t)x);
174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/**
177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Swap a block of invariant, NUL-terminated strings, but not padding
178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * bytes after the last string.
179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @internal
180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2
182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruudata_swapInvStringBlock(const UDataSwapper *ds,
183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                         const void *inData, int32_t length, void *outData,
184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                         UErrorCode *pErrorCode) {
185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const char *inChars;
186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int32_t stringsLength;
187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(ds==NULL || inData==NULL || length<0 || (length>0 && outData==NULL)) {
192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* reduce the strings length to not include bytes after the last NUL */
197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    inChars=(const char *)inData;
198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    stringsLength=length;
199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    while(stringsLength>0 && inChars[stringsLength-1]!=0) {
200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        --stringsLength;
201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* swap up to the last NUL */
204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ds->swapInvChars(ds, inData, stringsLength, outData, pErrorCode);
205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* copy the bytes after the last NUL */
207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(inData!=outData && length>stringsLength) {
208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        uprv_memcpy((char *)outData+stringsLength, inChars+stringsLength, length-stringsLength);
209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* return the length including padding bytes */
212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(U_SUCCESS(*pErrorCode)) {
213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return length;
214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI void U_EXPORT2
220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruudata_printError(const UDataSwapper *ds,
221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                 const char *fmt,
222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                 ...) {
223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    va_list args;
224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(ds->printError!=NULL) {
226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        va_start(args, fmt);
227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        ds->printError(ds->printErrorContext, fmt, args);
228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        va_end(args);
229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
231ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* swap a data header ------------------------------------------------------- */
233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2
235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruudata_swapDataHeader(const UDataSwapper *ds,
236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                     const void *inData, int32_t length, void *outData,
237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                     UErrorCode *pErrorCode) {
238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const DataHeader *pHeader;
239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint16_t headerSize, infoSize;
240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* argument checking */
242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(ds==NULL || inData==NULL || length<-1 || (length>0 && outData==NULL)) {
246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* check minimum length and magic bytes */
251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    pHeader=(const DataHeader *)inData;
252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if( (length>=0 && length<sizeof(DataHeader)) ||
253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        pHeader->dataHeader.magic1!=0xda ||
254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        pHeader->dataHeader.magic2!=0x27 ||
255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        pHeader->info.sizeofUChar!=2
256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ) {
257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        udata_printError(ds, "udata_swapDataHeader(): initial bytes do not look like ICU data\n");
258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_UNSUPPORTED_ERROR;
259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    headerSize=ds->readUInt16(pHeader->dataHeader.headerSize);
263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    infoSize=ds->readUInt16(pHeader->info.size);
264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if( headerSize<sizeof(DataHeader) ||
266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        infoSize<sizeof(UDataInfo) ||
267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        headerSize<(sizeof(pHeader->dataHeader)+infoSize) ||
268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        (length>=0 && length<headerSize)
269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ) {
270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        udata_printError(ds, "udata_swapDataHeader(): header size mismatch - headerSize %d infoSize %d length %d\n",
271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                         headerSize, infoSize, length);
272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(length>0) {
277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        DataHeader *outHeader;
278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        const char *s;
279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        int32_t maxLength;
280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* Most of the fields are just bytes and need no swapping. */
282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        if(inData!=outData) {
283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru            uprv_memcpy(outData, inData, headerSize);
284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        }
285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        outHeader=(DataHeader *)outData;
286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        outHeader->info.isBigEndian = ds->outIsBigEndian;
288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        outHeader->info.charsetFamily = ds->outCharset;
289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* swap headerSize */
291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        ds->swapArray16(ds, &pHeader->dataHeader.headerSize, 2, &outHeader->dataHeader.headerSize, pErrorCode);
292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* swap UDataInfo size and reservedWord */
294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        ds->swapArray16(ds, &pHeader->info.size, 4, &outHeader->info.size, pErrorCode);
295ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
296ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* swap copyright statement after the UDataInfo */
297ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        infoSize+=sizeof(pHeader->dataHeader);
298ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        s=(const char *)inData+infoSize;
299ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        maxLength=headerSize-infoSize;
300ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* get the length of the string */
301ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        for(length=0; length<maxLength && s[length]!=0; ++length) {}
302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        /* swap the string contents */
303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        ds->swapInvChars(ds, s, length, (char *)outData+infoSize, pErrorCode);
304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
305ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
306ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return headerSize;
307ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
308ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
309ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* API functions ------------------------------------------------------------ */
310ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI UDataSwapper * U_EXPORT2
312ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruudata_openSwapper(UBool inIsBigEndian, uint8_t inCharset,
313ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                  UBool outIsBigEndian, uint8_t outCharset,
314ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                  UErrorCode *pErrorCode) {
315ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UDataSwapper *swapper;
316ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
317ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
318ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return NULL;
319ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
320ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(inCharset>U_EBCDIC_FAMILY || outCharset>U_EBCDIC_FAMILY) {
321ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
322ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return NULL;
323ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
324ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
325ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* allocate the swapper */
326ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper=uprv_malloc(sizeof(UDataSwapper));
327ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(swapper==NULL) {
328ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
329ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return NULL;
330ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
331ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uprv_memset(swapper, 0, sizeof(UDataSwapper));
332ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
333ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    /* set values and functions pointers according to in/out parameters */
334ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper->inIsBigEndian=inIsBigEndian;
335ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper->inCharset=inCharset;
336ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper->outIsBigEndian=outIsBigEndian;
337ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper->outCharset=outCharset;
338ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
339ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper->readUInt16= inIsBigEndian==U_IS_BIG_ENDIAN ? uprv_readDirectUInt16 : uprv_readSwapUInt16;
340ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper->readUInt32= inIsBigEndian==U_IS_BIG_ENDIAN ? uprv_readDirectUInt32 : uprv_readSwapUInt32;
341ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
342ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper->writeUInt16= outIsBigEndian==U_IS_BIG_ENDIAN ? uprv_writeDirectUInt16 : uprv_writeSwapUInt16;
343ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper->writeUInt32= outIsBigEndian==U_IS_BIG_ENDIAN ? uprv_writeDirectUInt32 : uprv_writeSwapUInt32;
344ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
345ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper->compareInvChars= outCharset==U_ASCII_FAMILY ? uprv_compareInvAscii : uprv_compareInvEbcdic;
346ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
347ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper->swapArray16= inIsBigEndian==outIsBigEndian ? uprv_copyArray16 : uprv_swapArray16;
348ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    swapper->swapArray32= inIsBigEndian==outIsBigEndian ? uprv_copyArray32 : uprv_swapArray32;
349ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
350ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(inCharset==U_ASCII_FAMILY) {
351ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        swapper->swapInvChars= outCharset==U_ASCII_FAMILY ? uprv_copyAscii : uprv_ebcdicFromAscii;
352ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else /* U_EBCDIC_FAMILY */ {
353ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        swapper->swapInvChars= outCharset==U_EBCDIC_FAMILY ? uprv_copyEbcdic : uprv_asciiFromEbcdic;
354ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
355ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
356ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return swapper;
357ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
358ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
359ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI UDataSwapper * U_EXPORT2
360ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruudata_openSwapperForInputData(const void *data, int32_t length,
361ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                              UBool outIsBigEndian, uint8_t outCharset,
362ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru                              UErrorCode *pErrorCode) {
363ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    const DataHeader *pHeader;
364ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uint16_t headerSize, infoSize;
365ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    UBool inIsBigEndian;
366ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    int8_t inCharset;
367ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
368ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
369ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return NULL;
370ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
371ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if( data==NULL ||
372ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        (length>=0 && length<sizeof(DataHeader)) ||
373ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        outCharset>U_EBCDIC_FAMILY
374ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ) {
375ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
376ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return NULL;
377ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
378ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
379ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    pHeader=(const DataHeader *)data;
380ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if( (length>=0 && length<sizeof(DataHeader)) ||
381ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        pHeader->dataHeader.magic1!=0xda ||
382ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        pHeader->dataHeader.magic2!=0x27 ||
383ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        pHeader->info.sizeofUChar!=2
384ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ) {
385ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_UNSUPPORTED_ERROR;
386ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
387ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
388ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
389ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    inIsBigEndian=(UBool)pHeader->info.isBigEndian;
390ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    inCharset=pHeader->info.charsetFamily;
391ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
392ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if(inIsBigEndian==U_IS_BIG_ENDIAN) {
393ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        headerSize=pHeader->dataHeader.headerSize;
394ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        infoSize=pHeader->info.size;
395ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    } else {
396ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        headerSize=uprv_readSwapUInt16(pHeader->dataHeader.headerSize);
397ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        infoSize=uprv_readSwapUInt16(pHeader->info.size);
398ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
399ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
400ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    if( headerSize<sizeof(DataHeader) ||
401ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        infoSize<sizeof(UDataInfo) ||
402ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        headerSize<(sizeof(pHeader->dataHeader)+infoSize) ||
403ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        (length>=0 && length<headerSize)
404ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    ) {
405ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        *pErrorCode=U_UNSUPPORTED_ERROR;
406ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru        return 0;
407ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    }
408ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
409ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    return udata_openSwapper(inIsBigEndian, inCharset, outIsBigEndian, outCharset, pErrorCode);
410ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
411ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
412ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI void U_EXPORT2
413ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruudata_closeSwapper(UDataSwapper *ds) {
414ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru    uprv_free(ds);
415ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}
416