]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/propname.cpp
ICU-8.11.1.tar.gz
[apple/icu.git] / icuSources / common / propname.cpp
1 /*
2 **********************************************************************
3 * Copyright (c) 2002-2005, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Author: Alan Liu
7 * Created: October 30 2002
8 * Since: ICU 2.4
9 **********************************************************************
10 */
11 #include "propname.h"
12 #include "unicode/uchar.h"
13 #include "unicode/udata.h"
14 #include "umutex.h"
15 #include "cmemory.h"
16 #include "cstring.h"
17 #include "ucln_cmn.h"
18 #include "uarrsort.h"
19
20 U_CDECL_BEGIN
21
22 /**
23 * Get the next non-ignorable ASCII character from a property name
24 * and lowercases it.
25 * @return ((advance count for the name)<<8)|character
26 */
27 static inline int32_t
28 getASCIIPropertyNameChar(const char *name) {
29 int32_t i;
30 char c;
31
32 /* Ignore delimiters '-', '_', and ASCII White_Space */
33 for(i=0;
34 (c=name[i++])==0x2d || c==0x5f ||
35 c==0x20 || (0x09<=c && c<=0x0d);
36 ) {}
37
38 if(c!=0) {
39 return (i<<8)|(uint8_t)uprv_asciitolower((char)c);
40 } else {
41 return i<<8;
42 }
43 }
44
45 /**
46 * Get the next non-ignorable EBCDIC character from a property name
47 * and lowercases it.
48 * @return ((advance count for the name)<<8)|character
49 */
50 static inline int32_t
51 getEBCDICPropertyNameChar(const char *name) {
52 int32_t i;
53 char c;
54
55 /* Ignore delimiters '-', '_', and EBCDIC White_Space */
56 for(i=0;
57 (c=name[i++])==0x60 || c==0x6d ||
58 c==0x40 || c==0x05 || c==0x15 || c==0x25 || c==0x0b || c==0x0c || c==0x0d;
59 ) {}
60
61 if(c!=0) {
62 return (i<<8)|(uint8_t)uprv_ebcdictolower((char)c);
63 } else {
64 return i<<8;
65 }
66 }
67
68 /**
69 * Unicode property names and property value names are compared "loosely".
70 *
71 * UCD.html 4.0.1 says:
72 * For all property names, property value names, and for property values for
73 * Enumerated, Binary, or Catalog properties, use the following
74 * loose matching rule:
75 *
76 * LM3. Ignore case, whitespace, underscore ('_'), and hyphens.
77 *
78 * This function does just that, for (char *) name strings.
79 * It is almost identical to ucnv_compareNames() but also ignores
80 * C0 White_Space characters (U+0009..U+000d, and U+0085 on EBCDIC).
81 *
82 * @internal
83 */
84
85 U_CAPI int32_t U_EXPORT2
86 uprv_compareASCIIPropertyNames(const char *name1, const char *name2) {
87 int32_t rc, r1, r2;
88
89 for(;;) {
90 r1=getASCIIPropertyNameChar(name1);
91 r2=getASCIIPropertyNameChar(name2);
92
93 /* If we reach the ends of both strings then they match */
94 if(((r1|r2)&0xff)==0) {
95 return 0;
96 }
97
98 /* Compare the lowercased characters */
99 if(r1!=r2) {
100 rc=(r1&0xff)-(r2&0xff);
101 if(rc!=0) {
102 return rc;
103 }
104 }
105
106 name1+=r1>>8;
107 name2+=r2>>8;
108 }
109 }
110
111 U_CAPI int32_t U_EXPORT2
112 uprv_compareEBCDICPropertyNames(const char *name1, const char *name2) {
113 int32_t rc, r1, r2;
114
115 for(;;) {
116 r1=getEBCDICPropertyNameChar(name1);
117 r2=getEBCDICPropertyNameChar(name2);
118
119 /* If we reach the ends of both strings then they match */
120 if(((r1|r2)&0xff)==0) {
121 return 0;
122 }
123
124 /* Compare the lowercased characters */
125 if(r1!=r2) {
126 rc=(r1&0xff)-(r2&0xff);
127 if(rc!=0) {
128 return rc;
129 }
130 }
131
132 name1+=r1>>8;
133 name2+=r2>>8;
134 }
135 }
136
137 U_CDECL_END
138
139 U_NAMESPACE_BEGIN
140
141 //----------------------------------------------------------------------
142 // PropertyAliases implementation
143
144 const char*
145 PropertyAliases::chooseNameInGroup(Offset offset,
146 UPropertyNameChoice choice) const {
147 int32_t c = choice;
148 if (!offset || c < 0) {
149 return NULL;
150 }
151 const Offset* p = (const Offset*) getPointer(offset);
152 while (c-- > 0) {
153 if (*p++ < 0) return NULL;
154 }
155 Offset a = *p;
156 if (a < 0) a = -a;
157 return (const char*) getPointerNull(a);
158 }
159
160 const ValueMap*
161 PropertyAliases::getValueMap(EnumValue prop) const {
162 NonContiguousEnumToOffset* e2o = (NonContiguousEnumToOffset*) getPointer(enumToValue_offset);
163 Offset a = e2o->getOffset(prop);
164 return (const ValueMap*) (a ? getPointerNull(a) : NULL);
165 }
166
167 inline const char*
168 PropertyAliases::getPropertyName(EnumValue prop,
169 UPropertyNameChoice choice) const {
170 NonContiguousEnumToOffset* e2n = (NonContiguousEnumToOffset*) getPointer(enumToName_offset);
171 return chooseNameInGroup(e2n->getOffset(prop), choice);
172 }
173
174 inline EnumValue
175 PropertyAliases::getPropertyEnum(const char* alias) const {
176 NameToEnum* n2e = (NameToEnum*) getPointer(nameToEnum_offset);
177 return n2e->getEnum(alias, *this);
178 }
179
180 inline const char*
181 PropertyAliases::getPropertyValueName(EnumValue prop,
182 EnumValue value,
183 UPropertyNameChoice choice) const {
184 const ValueMap* vm = getValueMap(prop);
185 if (!vm) return NULL;
186 Offset a;
187 if (vm->enumToName_offset) {
188 a = ((EnumToOffset*) getPointer(vm->enumToName_offset))->
189 getOffset(value);
190 } else {
191 a = ((NonContiguousEnumToOffset*) getPointer(vm->ncEnumToName_offset))->
192 getOffset(value);
193 }
194 return chooseNameInGroup(a, choice);
195 }
196
197 inline EnumValue
198 PropertyAliases::getPropertyValueEnum(EnumValue prop,
199 const char* alias) const {
200 const ValueMap* vm = getValueMap(prop);
201 if (!vm) return UCHAR_INVALID_CODE;
202 NameToEnum* n2e = (NameToEnum*) getPointer(vm->nameToEnum_offset);
203 return n2e->getEnum(alias, *this);
204 }
205
206 U_NAMESPACE_END
207
208 //----------------------------------------------------------------------
209 // UDataMemory structures
210
211 static const PropertyAliases* PNAME = NULL;
212 static UDataMemory* UDATA = NULL;
213
214 //----------------------------------------------------------------------
215 // UDataMemory loading/unloading
216
217 /**
218 * udata callback to verify the zone data.
219 */
220 U_CDECL_BEGIN
221 static UBool U_CALLCONV
222 isPNameAcceptable(void* /*context*/,
223 const char* /*type*/, const char* /*name*/,
224 const UDataInfo* info) {
225 return
226 info->size >= sizeof(UDataInfo) &&
227 info->isBigEndian == U_IS_BIG_ENDIAN &&
228 info->charsetFamily == U_CHARSET_FAMILY &&
229 info->dataFormat[0] == PNAME_SIG_0 &&
230 info->dataFormat[1] == PNAME_SIG_1 &&
231 info->dataFormat[2] == PNAME_SIG_2 &&
232 info->dataFormat[3] == PNAME_SIG_3 &&
233 info->formatVersion[0] == PNAME_FORMAT_VERSION;
234 }
235
236 static UBool U_CALLCONV pname_cleanup(void) {
237 if (UDATA) {
238 udata_close(UDATA);
239 UDATA = NULL;
240 }
241 PNAME = NULL;
242 return TRUE;
243 }
244 U_CDECL_END
245
246 /**
247 * Load the property names data. Caller should check that data is
248 * not loaded BEFORE calling this function. Returns TRUE if the load
249 * succeeds.
250 */
251 static UBool _load() {
252 UErrorCode ec = U_ZERO_ERROR;
253 UDataMemory* data =
254 udata_openChoice(0, PNAME_DATA_TYPE, PNAME_DATA_NAME,
255 isPNameAcceptable, 0, &ec);
256 if (U_SUCCESS(ec)) {
257 umtx_lock(NULL);
258 if (UDATA == NULL) {
259 UDATA = data;
260 PNAME = (const PropertyAliases*) udata_getMemory(UDATA);
261 ucln_common_registerCleanup(UCLN_COMMON_PNAME, pname_cleanup);
262 data = NULL;
263 }
264 umtx_unlock(NULL);
265 }
266 if (data) {
267 udata_close(data);
268 }
269 return PNAME!=NULL;
270 }
271
272 /**
273 * Inline function that expands to code that does a lazy load of the
274 * property names data. If the data is already loaded, avoids an
275 * unnecessary function call. If the data is not loaded, call _load()
276 * to load it, and return TRUE if the load succeeds.
277 */
278 static inline UBool load() {
279 UBool f;
280 UMTX_CHECK(NULL, (PNAME!=NULL), f);
281 return f || _load();
282 }
283
284 //----------------------------------------------------------------------
285 // Public API implementation
286
287 // The C API is just a thin wrapper. Each function obtains a pointer
288 // to the singleton PropertyAliases, and calls the appropriate method
289 // on it. If it cannot obtain a pointer, because valid data is not
290 // available, then it returns NULL or UCHAR_INVALID_CODE.
291
292 U_CAPI const char* U_EXPORT2
293 u_getPropertyName(UProperty property,
294 UPropertyNameChoice nameChoice) {
295 return load() ? PNAME->getPropertyName(property, nameChoice)
296 : NULL;
297 }
298
299 U_CAPI UProperty U_EXPORT2
300 u_getPropertyEnum(const char* alias) {
301 UProperty p = load() ? (UProperty) PNAME->getPropertyEnum(alias)
302 : UCHAR_INVALID_CODE;
303 return p;
304 }
305
306 U_CAPI const char* U_EXPORT2
307 u_getPropertyValueName(UProperty property,
308 int32_t value,
309 UPropertyNameChoice nameChoice) {
310 return load() ? PNAME->getPropertyValueName(property, value, nameChoice)
311 : NULL;
312 }
313
314 U_CAPI int32_t U_EXPORT2
315 u_getPropertyValueEnum(UProperty property,
316 const char* alias) {
317 return load() ? PNAME->getPropertyValueEnum(property, alias)
318 : (int32_t)UCHAR_INVALID_CODE;
319 }
320
321 /* data swapping ------------------------------------------------------------ */
322
323 /*
324 * Sub-structure-swappers use the temp array (which is as large as the
325 * actual data) for intermediate storage,
326 * as well as to indicate if a particular structure has been swapped already.
327 * The temp array is initially reset to all 0.
328 * pos is the byte offset of the sub-structure in the inBytes/outBytes/temp arrays.
329 */
330
331 int32_t
332 EnumToOffset::swap(const UDataSwapper *ds,
333 const uint8_t *inBytes, int32_t length, uint8_t *outBytes,
334 uint8_t *temp, int32_t pos,
335 UErrorCode *pErrorCode) {
336 const EnumToOffset *inMap;
337 EnumToOffset *outMap, *tempMap;
338 int32_t size;
339
340 tempMap=(EnumToOffset *)(temp+pos);
341 if(tempMap->enumStart!=0 || tempMap->enumLimit!=0) {
342 /* this map was swapped already */
343 size=tempMap->getSize();
344 return size;
345 }
346
347 inMap=(const EnumToOffset *)(inBytes+pos);
348 outMap=(EnumToOffset *)(outBytes+pos);
349
350 tempMap->enumStart=udata_readInt32(ds, inMap->enumStart);
351 tempMap->enumLimit=udata_readInt32(ds, inMap->enumLimit);
352 size=tempMap->getSize();
353
354 if(length>=0) {
355 if(length<(pos+size)) {
356 if(length<(int32_t)sizeof(PropertyAliases)) {
357 udata_printError(ds, "upname_swap(EnumToOffset): too few bytes (%d after header)\n"
358 " for pnames.icu EnumToOffset{%d..%d} at %d\n",
359 length, tempMap->enumStart, tempMap->enumLimit, pos);
360 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
361 return 0;
362 }
363 }
364
365 /* swap enumStart and enumLimit */
366 ds->swapArray32(ds, inMap, 2*sizeof(EnumValue), outMap, pErrorCode);
367
368 /* swap _offsetArray[] */
369 ds->swapArray16(ds, inMap->getOffsetArray(), (tempMap->enumLimit-tempMap->enumStart)*sizeof(Offset),
370 outMap->getOffsetArray(), pErrorCode);
371 }
372
373 return size;
374 }
375
376 int32_t
377 NonContiguousEnumToOffset::swap(const UDataSwapper *ds,
378 const uint8_t *inBytes, int32_t length, uint8_t *outBytes,
379 uint8_t *temp, int32_t pos,
380 UErrorCode *pErrorCode) {
381 const NonContiguousEnumToOffset *inMap;
382 NonContiguousEnumToOffset *outMap, *tempMap;
383 int32_t size;
384
385 tempMap=(NonContiguousEnumToOffset *)(temp+pos);
386 if(tempMap->count!=0) {
387 /* this map was swapped already */
388 size=tempMap->getSize();
389 return size;
390 }
391
392 inMap=(const NonContiguousEnumToOffset *)(inBytes+pos);
393 outMap=(NonContiguousEnumToOffset *)(outBytes+pos);
394
395 tempMap->count=udata_readInt32(ds, inMap->count);
396 size=tempMap->getSize();
397
398 if(length>=0) {
399 if(length<(pos+size)) {
400 if(length<(int32_t)sizeof(PropertyAliases)) {
401 udata_printError(ds, "upname_swap(NonContiguousEnumToOffset): too few bytes (%d after header)\n"
402 " for pnames.icu NonContiguousEnumToOffset[%d] at %d\n",
403 length, tempMap->count, pos);
404 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
405 return 0;
406 }
407 }
408
409 /* swap count and _enumArray[] */
410 length=(1+tempMap->count)*sizeof(EnumValue);
411 ds->swapArray32(ds, inMap, length,
412 outMap, pErrorCode);
413
414 /* swap _offsetArray[] */
415 pos+=length;
416 ds->swapArray16(ds, inBytes+pos, tempMap->count*sizeof(Offset),
417 outBytes+pos, pErrorCode);
418 }
419
420 return size;
421 }
422
423 struct NameAndIndex {
424 Offset name, index;
425 };
426
427 U_CDECL_BEGIN
428 typedef int32_t U_CALLCONV PropNameCompareFn(const char *name1, const char *name2);
429
430 struct CompareContext {
431 const char *chars;
432 PropNameCompareFn *propCompare;
433 };
434
435 static int32_t U_CALLCONV
436 upname_compareRows(const void *context, const void *left, const void *right) {
437 CompareContext *cmp=(CompareContext *)context;
438 return cmp->propCompare(cmp->chars+((const NameAndIndex *)left)->name,
439 cmp->chars+((const NameAndIndex *)right)->name);
440 }
441 U_CDECL_END
442
443 int32_t
444 NameToEnum::swap(const UDataSwapper *ds,
445 const uint8_t *inBytes, int32_t length, uint8_t *outBytes,
446 uint8_t *temp, int32_t pos,
447 UErrorCode *pErrorCode) {
448 const NameToEnum *inMap;
449 NameToEnum *outMap, *tempMap;
450
451 const EnumValue *inEnumArray;
452 EnumValue *outEnumArray;
453
454 const Offset *inNameArray;
455 Offset *outNameArray;
456
457 NameAndIndex *sortArray;
458 CompareContext cmp;
459
460 int32_t i, size, oldIndex;
461
462 tempMap=(NameToEnum *)(temp+pos);
463 if(tempMap->count!=0) {
464 /* this map was swapped already */
465 size=tempMap->getSize();
466 return size;
467 }
468
469 inMap=(const NameToEnum *)(inBytes+pos);
470 outMap=(NameToEnum *)(outBytes+pos);
471
472 tempMap->count=udata_readInt32(ds, inMap->count);
473 size=tempMap->getSize();
474
475 if(length>=0) {
476 if(length<(pos+size)) {
477 if(length<(int32_t)sizeof(PropertyAliases)) {
478 udata_printError(ds, "upname_swap(NameToEnum): too few bytes (%d after header)\n"
479 " for pnames.icu NameToEnum[%d] at %d\n",
480 length, tempMap->count, pos);
481 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
482 return 0;
483 }
484 }
485
486 /* swap count */
487 ds->swapArray32(ds, inMap, 4, outMap, pErrorCode);
488
489 inEnumArray=inMap->getEnumArray();
490 outEnumArray=outMap->getEnumArray();
491
492 inNameArray=(const Offset *)(inEnumArray+tempMap->count);
493 outNameArray=(Offset *)(outEnumArray+tempMap->count);
494
495 if(ds->inCharset==ds->outCharset) {
496 /* no need to sort, just swap the enum/name arrays */
497 ds->swapArray32(ds, inEnumArray, tempMap->count*4, outEnumArray, pErrorCode);
498 ds->swapArray16(ds, inNameArray, tempMap->count*2, outNameArray, pErrorCode);
499 return size;
500 }
501
502 /*
503 * The name and enum arrays are sorted by names and must be resorted
504 * if inCharset!=outCharset.
505 * We use the corresponding part of the temp array to sort an array
506 * of pairs of name offsets and sorting indexes.
507 * Then the sorting indexes are used to permutate-swap the name and enum arrays.
508 *
509 * The outBytes must already contain the swapped strings.
510 */
511 sortArray=(NameAndIndex *)tempMap->getEnumArray();
512 for(i=0; i<tempMap->count; ++i) {
513 sortArray[i].name=udata_readInt16(ds, inNameArray[i]);
514 sortArray[i].index=(Offset)i;
515 }
516
517 /*
518 * use a stable sort to avoid shuffling of equal strings,
519 * which makes testing harder
520 */
521 cmp.chars=(const char *)outBytes;
522 if (ds->outCharset==U_ASCII_FAMILY) {
523 cmp.propCompare=uprv_compareASCIIPropertyNames;
524 }
525 else {
526 cmp.propCompare=uprv_compareEBCDICPropertyNames;
527 }
528 uprv_sortArray(sortArray, tempMap->count, sizeof(NameAndIndex),
529 upname_compareRows, &cmp,
530 TRUE, pErrorCode);
531 if(U_FAILURE(*pErrorCode)) {
532 udata_printError(ds, "upname_swap(NameToEnum).uprv_sortArray(%d items) failed\n",
533 tempMap->count);
534 return 0;
535 }
536
537 /* copy/swap/permutate _enumArray[] and _nameArray[] */
538 if(inEnumArray!=outEnumArray) {
539 for(i=0; i<tempMap->count; ++i) {
540 oldIndex=sortArray[i].index;
541 ds->swapArray32(ds, inEnumArray+oldIndex, 4, outEnumArray+i, pErrorCode);
542 ds->swapArray16(ds, inNameArray+oldIndex, 2, outNameArray+i, pErrorCode);
543 }
544 } else {
545 /*
546 * in-place swapping: need to permutate into a temporary array
547 * and then copy back to not destroy the data
548 */
549 EnumValue *tempEnumArray;
550 Offset *oldIndexes;
551
552 /* write name offsets directly from sortArray */
553 for(i=0; i<tempMap->count; ++i) {
554 ds->writeUInt16((uint16_t *)outNameArray+i, (uint16_t)sortArray[i].name);
555 }
556
557 /*
558 * compress the oldIndexes into a separate array to make space for tempEnumArray
559 * the tempMap _nameArray becomes oldIndexes[], getting the index
560 * values from the 2D sortArray[],
561 * while sortArray=tempMap _enumArray[] becomes tempEnumArray[]
562 * this saves us allocating more memory
563 *
564 * it works because sizeof(NameAndIndex)<=sizeof(EnumValue)
565 * and because the nameArray[] can be used for oldIndexes[]
566 */
567 tempEnumArray=(EnumValue *)sortArray;
568 oldIndexes=(Offset *)(sortArray+tempMap->count);
569
570 /* copy sortArray[].index values into oldIndexes[] */
571 for(i=0; i<tempMap->count; ++i) {
572 oldIndexes[i]=sortArray[i].index;
573 }
574
575 /* permutate inEnumArray[] into tempEnumArray[] */
576 for(i=0; i<tempMap->count; ++i) {
577 ds->swapArray32(ds, inEnumArray+oldIndexes[i], 4, tempEnumArray+i, pErrorCode);
578 }
579
580 /* copy tempEnumArray[] to outEnumArray[] */
581 uprv_memcpy(outEnumArray, tempEnumArray, tempMap->count*4);
582 }
583 }
584
585 return size;
586 }
587
588 int32_t
589 PropertyAliases::swap(const UDataSwapper *ds,
590 const uint8_t *inBytes, int32_t length, uint8_t *outBytes,
591 UErrorCode *pErrorCode) {
592 const PropertyAliases *inAliases;
593 PropertyAliases *outAliases;
594 PropertyAliases aliases;
595
596 const ValueMap *inValueMaps;
597 ValueMap *outValueMaps;
598 ValueMap valueMap;
599
600 uint8_t *temp;
601
602 int32_t i;
603
604 inAliases=(const PropertyAliases *)inBytes;
605 outAliases=(PropertyAliases *)outBytes;
606
607 /* read the input PropertyAliases - all 16-bit values */
608 for(i=0; i<(int32_t)sizeof(PropertyAliases)/2; ++i) {
609 ((uint16_t *)&aliases)[i]=ds->readUInt16(((const uint16_t *)inBytes)[i]);
610 }
611
612 if(length>=0) {
613 if(length<aliases.total_size) {
614 udata_printError(ds, "upname_swap(): too few bytes (%d after header) for all of pnames.icu\n",
615 length);
616 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
617 return 0;
618 }
619
620 /* copy the data for inaccessible bytes */
621 if(inBytes!=outBytes) {
622 uprv_memcpy(outBytes, inBytes, aliases.total_size);
623 }
624
625 /* swap the PropertyAliases class fields */
626 ds->swapArray16(ds, inAliases, sizeof(PropertyAliases), outAliases, pErrorCode);
627
628 /* swap the name groups */
629 ds->swapArray16(ds, inBytes+aliases.nameGroupPool_offset,
630 aliases.stringPool_offset-aliases.nameGroupPool_offset,
631 outBytes+aliases.nameGroupPool_offset, pErrorCode);
632
633 /* swap the strings */
634 udata_swapInvStringBlock(ds, inBytes+aliases.stringPool_offset,
635 aliases.total_size-aliases.stringPool_offset,
636 outBytes+aliases.stringPool_offset, pErrorCode);
637
638 /*
639 * alloc uint8_t temp[total_size] and reset it
640 * swap each top-level struct, put at least the count fields into temp
641 * use subclass-specific swap() functions
642 * enumerate value maps, for each
643 * if temp does not have count!=0 yet
644 * read count, put it into temp
645 * swap the array(s)
646 * resort strings in name->enum maps
647 * swap value maps
648 */
649 temp=(uint8_t *)uprv_malloc(aliases.total_size);
650 if(temp==NULL) {
651 udata_printError(ds, "upname_swap(): unable to allocate temp memory (%d bytes)\n",
652 aliases.total_size);
653 *pErrorCode=U_MEMORY_ALLOCATION_ERROR;
654 return 0;
655 }
656 uprv_memset(temp, 0, aliases.total_size);
657
658 /* swap properties->name groups map */
659 NonContiguousEnumToOffset::swap(ds, inBytes, length, outBytes,
660 temp, aliases.enumToName_offset, pErrorCode);
661
662 /* swap name->properties map */
663 NameToEnum::swap(ds, inBytes, length, outBytes,
664 temp, aliases.nameToEnum_offset, pErrorCode);
665
666 /* swap properties->value maps map */
667 NonContiguousEnumToOffset::swap(ds, inBytes, length, outBytes,
668 temp, aliases.enumToValue_offset, pErrorCode);
669
670 /* enumerate all ValueMaps and swap them */
671 inValueMaps=(const ValueMap *)(inBytes+aliases.valueMap_offset);
672 outValueMaps=(ValueMap *)(outBytes+aliases.valueMap_offset);
673
674 for(i=0; i<aliases.valueMap_count; ++i) {
675 valueMap.enumToName_offset=udata_readInt16(ds, inValueMaps[i].enumToName_offset);
676 valueMap.ncEnumToName_offset=udata_readInt16(ds, inValueMaps[i].ncEnumToName_offset);
677 valueMap.nameToEnum_offset=udata_readInt16(ds, inValueMaps[i].nameToEnum_offset);
678
679 if(valueMap.enumToName_offset!=0) {
680 EnumToOffset::swap(ds, inBytes, length, outBytes,
681 temp, valueMap.enumToName_offset,
682 pErrorCode);
683 } else if(valueMap.ncEnumToName_offset!=0) {
684 NonContiguousEnumToOffset::swap(ds, inBytes, length, outBytes,
685 temp, valueMap.ncEnumToName_offset,
686 pErrorCode);
687 }
688 if(valueMap.nameToEnum_offset!=0) {
689 NameToEnum::swap(ds, inBytes, length, outBytes,
690 temp, valueMap.nameToEnum_offset,
691 pErrorCode);
692 }
693 }
694
695 /* swap the ValueMaps array itself */
696 ds->swapArray16(ds, inValueMaps, aliases.valueMap_count*sizeof(ValueMap),
697 outValueMaps, pErrorCode);
698
699 /* name groups and strings were swapped above */
700
701 /* release temp */
702 uprv_free(temp);
703 }
704
705 return aliases.total_size;
706 }
707
708 U_CAPI int32_t U_EXPORT2
709 upname_swap(const UDataSwapper *ds,
710 const void *inData, int32_t length, void *outData,
711 UErrorCode *pErrorCode) {
712 const UDataInfo *pInfo;
713 int32_t headerSize;
714
715 const uint8_t *inBytes;
716 uint8_t *outBytes;
717
718 /* udata_swapDataHeader checks the arguments */
719 headerSize=udata_swapDataHeader(ds, inData, length, outData, pErrorCode);
720 if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
721 return 0;
722 }
723
724 /* check data format and format version */
725 pInfo=(const UDataInfo *)((const char *)inData+4);
726 if(!(
727 pInfo->dataFormat[0]==0x70 && /* dataFormat="pnam" */
728 pInfo->dataFormat[1]==0x6e &&
729 pInfo->dataFormat[2]==0x61 &&
730 pInfo->dataFormat[3]==0x6d &&
731 pInfo->formatVersion[0]==1
732 )) {
733 udata_printError(ds, "upname_swap(): data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as pnames.icu\n",
734 pInfo->dataFormat[0], pInfo->dataFormat[1],
735 pInfo->dataFormat[2], pInfo->dataFormat[3],
736 pInfo->formatVersion[0]);
737 *pErrorCode=U_UNSUPPORTED_ERROR;
738 return 0;
739 }
740
741 inBytes=(const uint8_t *)inData+headerSize;
742 outBytes=(uint8_t *)outData+headerSize;
743
744 if(length>=0) {
745 length-=headerSize;
746 if(length<(int32_t)sizeof(PropertyAliases)) {
747 udata_printError(ds, "upname_swap(): too few bytes (%d after header) for pnames.icu\n",
748 length);
749 *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
750 return 0;
751 }
752 }
753
754 return headerSize+PropertyAliases::swap(ds, inBytes, length, outBytes, pErrorCode);
755 }
756
757 //eof