2 ******************************************************************************
4 * Copyright (C) 2002-2015, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: bocu1tst.c
10 * tab size: 8 (not used)
13 * created on: 2002may27
14 * created by: Markus W. Scherer
16 * This is the reference implementation of BOCU-1,
17 * the MIME-friendly form of the Binary Ordered Compression for Unicode,
18 * taken directly from ### http://source.icu-project.org/repos/icu/icuhtml/trunk/design/conversion/bocu1/
19 * The files bocu1.h and bocu1.c from the design folder are taken
20 * verbatim (minus copyright and #include) and copied together into this file.
21 * The reference code and some of the reference bocu1tst.c
22 * is modified to run as part of the ICU cintltst
23 * test framework (minus main(), log_ln() etc. instead of printf()).
25 * This reference implementation is used here to verify
26 * the ICU BOCU-1 implementation, which is
27 * adapted for ICU conversion APIs and optimized.
28 * ### links in design doc to here and to ucnvbocu.c
31 #include "unicode/utypes.h"
32 #include "unicode/ustring.h"
33 #include "unicode/ucnv.h"
34 #include "unicode/utf16.h"
38 /* icuhtml/design/conversion/bocu1/bocu1.h ---------------------------------- */
40 /* BOCU-1 constants and macros ---------------------------------------------- */
43 * BOCU-1 encodes the code points of a Unicode string as
44 * a sequence of byte-encoded differences (slope detection),
45 * preserving lexical order.
47 * Optimize the difference-taking for runs of Unicode text within
50 * Most small scripts are allocated within aligned 128-blocks of Unicode
51 * code points. Lexical order is preserved if the "previous code point" state
52 * is always moved into the middle of such a block.
54 * Additionally, "prev" is moved from anywhere in the Unihan and Hangul
55 * areas into the middle of those areas.
57 * C0 control codes and space are encoded with their US-ASCII bytes.
58 * "prev" is reset for C0 controls but not for space.
61 /* initial value for "prev": middle of the ASCII range */
62 #define BOCU1_ASCII_PREV 0x40
64 /* bounding byte values for differences */
65 #define BOCU1_MIN 0x21
66 #define BOCU1_MIDDLE 0x90
67 #define BOCU1_MAX_LEAD 0xfe
69 /* add the L suffix to make computations with BOCU1_MAX_TRAIL work on 16-bit compilers */
70 #define BOCU1_MAX_TRAIL 0xffL
71 #define BOCU1_RESET 0xff
73 /* number of lead bytes */
74 #define BOCU1_COUNT (BOCU1_MAX_LEAD-BOCU1_MIN+1)
76 /* adjust trail byte counts for the use of some C0 control byte values */
77 #define BOCU1_TRAIL_CONTROLS_COUNT 20
78 #define BOCU1_TRAIL_BYTE_OFFSET (BOCU1_MIN-BOCU1_TRAIL_CONTROLS_COUNT)
80 /* number of trail bytes */
81 #define BOCU1_TRAIL_COUNT ((BOCU1_MAX_TRAIL-BOCU1_MIN+1)+BOCU1_TRAIL_CONTROLS_COUNT)
84 * number of positive and negative single-byte codes
85 * (counting 0==BOCU1_MIDDLE among the positive ones)
87 #define BOCU1_SINGLE 64
89 /* number of lead bytes for positive and negative 2/3/4-byte sequences */
90 #define BOCU1_LEAD_2 43
91 #define BOCU1_LEAD_3 3
92 #define BOCU1_LEAD_4 1
94 /* The difference value range for single-byters. */
95 #define BOCU1_REACH_POS_1 (BOCU1_SINGLE-1)
96 #define BOCU1_REACH_NEG_1 (-BOCU1_SINGLE)
98 /* The difference value range for double-byters. */
99 #define BOCU1_REACH_POS_2 (BOCU1_REACH_POS_1+BOCU1_LEAD_2*BOCU1_TRAIL_COUNT)
100 #define BOCU1_REACH_NEG_2 (BOCU1_REACH_NEG_1-BOCU1_LEAD_2*BOCU1_TRAIL_COUNT)
102 /* The difference value range for 3-byters. */
103 #define BOCU1_REACH_POS_3 \
104 (BOCU1_REACH_POS_2+BOCU1_LEAD_3*BOCU1_TRAIL_COUNT*BOCU1_TRAIL_COUNT)
106 #define BOCU1_REACH_NEG_3 (BOCU1_REACH_NEG_2-BOCU1_LEAD_3*BOCU1_TRAIL_COUNT*BOCU1_TRAIL_COUNT)
108 /* The lead byte start values. */
109 #define BOCU1_START_POS_2 (BOCU1_MIDDLE+BOCU1_REACH_POS_1+1)
110 #define BOCU1_START_POS_3 (BOCU1_START_POS_2+BOCU1_LEAD_2)
111 #define BOCU1_START_POS_4 (BOCU1_START_POS_3+BOCU1_LEAD_3)
112 /* ==BOCU1_MAX_LEAD */
114 #define BOCU1_START_NEG_2 (BOCU1_MIDDLE+BOCU1_REACH_NEG_1)
115 #define BOCU1_START_NEG_3 (BOCU1_START_NEG_2-BOCU1_LEAD_2)
116 #define BOCU1_START_NEG_4 (BOCU1_START_NEG_3-BOCU1_LEAD_3)
119 /* The length of a byte sequence, according to the lead byte (!=BOCU1_RESET). */
120 #define BOCU1_LENGTH_FROM_LEAD(lead) \
121 ((BOCU1_START_NEG_2<=(lead) && (lead)<BOCU1_START_POS_2) ? 1 : \
122 (BOCU1_START_NEG_3<=(lead) && (lead)<BOCU1_START_POS_3) ? 2 : \
123 (BOCU1_START_NEG_4<=(lead) && (lead)<BOCU1_START_POS_4) ? 3 : 4)
125 /* The length of a byte sequence, according to its packed form. */
126 #define BOCU1_LENGTH_FROM_PACKED(packed) \
127 ((uint32_t)(packed)<0x04000000 ? (packed)>>24 : 4)
130 * 12 commonly used C0 control codes (and space) are only used to encode
131 * themselves directly,
132 * which makes BOCU-1 MIME-usable and reasonably safe for
133 * ASCII-oriented software.
153 * The other 20 C0 controls are also encoded directly (to preserve order)
154 * but are also used as trail bytes in difference encoding
155 * (for better compression).
157 #define BOCU1_TRAIL_TO_BYTE(t) ((t)>=BOCU1_TRAIL_CONTROLS_COUNT ? (t)+BOCU1_TRAIL_BYTE_OFFSET : bocu1TrailToByte[t])
160 * Byte value map for control codes,
161 * from external byte values 0x00..0x20
162 * to trail byte values 0..19 (0..0x13) as used in the difference calculation.
163 * External byte values that are illegal as trail bytes are mapped to -1.
166 bocu1ByteToTrail
[BOCU1_MIN
]={
167 /* 0 1 2 3 4 5 6 7 */
168 -1, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, -1,
170 /* 8 9 a b c d e f */
171 -1, -1, -1, -1, -1, -1, -1, -1,
173 /* 10 11 12 13 14 15 16 17 */
174 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
176 /* 18 19 1a 1b 1c 1d 1e 1f */
177 0x0e, 0x0f, -1, -1, 0x10, 0x11, 0x12, 0x13,
184 * Byte value map for control codes,
185 * from trail byte values 0..19 (0..0x13) as used in the difference calculation
186 * to external byte values 0x00..0x20.
189 bocu1TrailToByte
[BOCU1_TRAIL_CONTROLS_COUNT
]={
190 /* 0 1 2 3 4 5 6 7 */
191 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x10, 0x11,
193 /* 8 9 a b c d e f */
194 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
197 0x1c, 0x1d, 0x1e, 0x1f
201 * Integer division and modulo with negative numerators
202 * yields negative modulo results and quotients that are one more than
204 * This macro adjust the results so that the modulo-value m is always >=0.
206 * For positive n, the if() condition is always FALSE.
208 * @param n Number to be split into quotient and rest.
209 * Will be modified to contain the quotient.
211 * @param m Output variable for the rest (modulo result).
213 #define NEGDIVMOD(n, d, m) { \
222 /* State for BOCU-1 decoder function. */
224 int32_t prev
, count
, diff
;
227 typedef struct Bocu1Rx Bocu1Rx
;
229 /* Function prototypes ------------------------------------------------------ */
233 packDiff(int32_t diff
);
236 encodeBocu1(int32_t *pPrev
, int32_t c
);
239 decodeBocu1(Bocu1Rx
*pRx
, uint8_t b
);
241 /* icuhtml/design/conversion/bocu1/bocu1.c ---------------------------------- */
243 /* BOCU-1 implementation functions ------------------------------------------ */
246 * Compute the next "previous" value for differencing
247 * from the current code point.
249 * @param c current code point, 0..0x10ffff
250 * @return "previous code point" state value
253 bocu1Prev(int32_t c
) {
254 /* compute new prev */
255 if(0x3040<=c
&& c
<=0x309f) {
256 /* Hiragana is not 128-aligned */
258 } else if(0x4e00<=c
&& c
<=0x9fa5) {
260 return 0x4e00-BOCU1_REACH_NEG_2
;
261 } else if(0xac00<=c
&& c
<=0xd7a3) {
262 /* Korean Hangul (cast to int32_t to avoid wraparound on 16-bit compilers) */
263 return ((int32_t)0xd7a3+(int32_t)0xac00)/2;
265 /* mostly small scripts */
266 return (c
&~0x7f)+BOCU1_ASCII_PREV
;
271 * Encode a difference -0x10ffff..0x10ffff in 1..4 bytes
272 * and return a packed integer with them.
274 * The encoding favors small absolut differences with short encodings
275 * to compress runs of same-script characters.
277 * @param diff difference value -0x10ffff..0x10ffff
279 * 0x010000zz for 1-byte sequence zz
280 * 0x0200yyzz for 2-byte sequence yy zz
281 * 0x03xxyyzz for 3-byte sequence xx yy zz
282 * 0xwwxxyyzz for 4-byte sequence ww xx yy zz (ww>0x03)
285 packDiff(int32_t diff
) {
286 int32_t result
, m
, lead
, count
, shift
;
288 if(diff
>=BOCU1_REACH_NEG_1
) {
289 /* mostly positive differences, and single-byte negative ones */
290 if(diff
<=BOCU1_REACH_POS_1
) {
292 return 0x01000000|(BOCU1_MIDDLE
+diff
);
293 } else if(diff
<=BOCU1_REACH_POS_2
) {
295 diff
-=BOCU1_REACH_POS_1
+1;
296 lead
=BOCU1_START_POS_2
;
298 } else if(diff
<=BOCU1_REACH_POS_3
) {
300 diff
-=BOCU1_REACH_POS_2
+1;
301 lead
=BOCU1_START_POS_3
;
305 diff
-=BOCU1_REACH_POS_3
+1;
306 lead
=BOCU1_START_POS_4
;
310 /* two- and four-byte negative differences */
311 if(diff
>=BOCU1_REACH_NEG_2
) {
313 diff
-=BOCU1_REACH_NEG_1
;
314 lead
=BOCU1_START_NEG_2
;
316 } else if(diff
>=BOCU1_REACH_NEG_3
) {
318 diff
-=BOCU1_REACH_NEG_2
;
319 lead
=BOCU1_START_NEG_3
;
323 diff
-=BOCU1_REACH_NEG_3
;
324 lead
=BOCU1_START_NEG_4
;
329 /* encode the length of the packed result */
331 result
=(count
+1)<<24;
332 } else /* count==3, MSB used for the lead byte */ {
336 /* calculate trail bytes like digits in itoa() */
339 NEGDIVMOD(diff
, BOCU1_TRAIL_COUNT
, m
);
340 result
|=BOCU1_TRAIL_TO_BYTE(m
)<<shift
;
345 result
|=(lead
+diff
)<<shift
;
351 * BOCU-1 encoder function.
353 * @param pPrev pointer to the integer that holds
354 * the "previous code point" state;
355 * the initial value should be 0 which
356 * encodeBocu1 will set to the actual BOCU-1 initial state value
357 * @param c the code point to encode
358 * @return the packed 1/2/3/4-byte encoding, see packDiff(),
359 * or 0 if an error occurs
364 encodeBocu1(int32_t *pPrev
, int32_t c
) {
367 if(pPrev
==NULL
|| c
<0 || c
>0x10ffff) {
368 /* illegal argument */
374 /* lenient handling of initial value 0 */
375 prev
=*pPrev
=BOCU1_ASCII_PREV
;
380 * ISO C0 control & space:
381 * Encode directly for MIME compatibility,
382 * and reset state except for space, to not disrupt compression.
385 *pPrev
=BOCU1_ASCII_PREV
;
391 * all other Unicode code points c==U+0021..U+10ffff
392 * are encoded with the difference c-prev
394 * a new prev is computed from c,
395 * placed in the middle of a 0x80-block (for most small scripts) or
396 * in the middle of the Unihan and Hangul blocks
397 * to statistically minimize the following difference
400 return packDiff(c
-prev
);
404 * Function for BOCU-1 decoder; handles multi-byte lead bytes.
406 * @param pRx pointer to the decoder state structure
407 * @param b lead byte;
408 * BOCU1_MIN<=b<BOCU1_START_NEG_2 or BOCU1_START_POS_2<=b<=BOCU1_MAX_LEAD
409 * @return -1 (state change only)
414 decodeBocu1LeadByte(Bocu1Rx
*pRx
, uint8_t b
) {
417 if(b
>=BOCU1_START_NEG_2
) {
418 /* positive difference */
419 if(b
<BOCU1_START_POS_3
) {
421 c
=((int32_t)b
-BOCU1_START_POS_2
)*BOCU1_TRAIL_COUNT
+BOCU1_REACH_POS_1
+1;
423 } else if(b
<BOCU1_START_POS_4
) {
425 c
=((int32_t)b
-BOCU1_START_POS_3
)*BOCU1_TRAIL_COUNT
*BOCU1_TRAIL_COUNT
+BOCU1_REACH_POS_2
+1;
429 c
=BOCU1_REACH_POS_3
+1;
433 /* negative difference */
434 if(b
>=BOCU1_START_NEG_3
) {
436 c
=((int32_t)b
-BOCU1_START_NEG_2
)*BOCU1_TRAIL_COUNT
+BOCU1_REACH_NEG_1
;
438 } else if(b
>BOCU1_MIN
) {
440 c
=((int32_t)b
-BOCU1_START_NEG_3
)*BOCU1_TRAIL_COUNT
*BOCU1_TRAIL_COUNT
+BOCU1_REACH_NEG_2
;
444 c
=-BOCU1_TRAIL_COUNT
*BOCU1_TRAIL_COUNT
*BOCU1_TRAIL_COUNT
+BOCU1_REACH_NEG_3
;
449 /* set the state for decoding the trail byte(s) */
456 * Function for BOCU-1 decoder; handles multi-byte trail bytes.
458 * @param pRx pointer to the decoder state structure
459 * @param b trail byte
460 * @return result value, same as decodeBocu1
465 decodeBocu1TrailByte(Bocu1Rx
*pRx
, uint8_t b
) {
469 /* skip some C0 controls and make the trail byte range contiguous */
470 t
=bocu1ByteToTrail
[b
];
472 /* illegal trail byte value */
473 pRx
->prev
=BOCU1_ASCII_PREV
;
477 #if BOCU1_MAX_TRAIL<0xff
478 } else if(b
>BOCU1_MAX_TRAIL
) {
482 t
=(int32_t)b
-BOCU1_TRAIL_BYTE_OFFSET
;
485 /* add trail byte into difference and decrement count */
490 /* final trail byte, deliver a code point */
492 if(0<=c
&& c
<=0x10ffff) {
493 /* valid code point result */
494 pRx
->prev
=bocu1Prev(c
);
498 /* illegal code point result */
499 pRx
->prev
=BOCU1_ASCII_PREV
;
505 /* intermediate trail byte */
507 pRx
->diff
=c
+t
*BOCU1_TRAIL_COUNT
;
508 } else /* count==3 */ {
509 pRx
->diff
=c
+t
*BOCU1_TRAIL_COUNT
*BOCU1_TRAIL_COUNT
;
516 * BOCU-1 decoder function.
518 * @param pRx pointer to the decoder state structure;
519 * the initial values should be 0 which
520 * decodeBocu1 will set to actual initial state values
521 * @param b an input byte
523 * 0..0x10ffff for a result code point
524 * -1 if only the state changed without code point output
525 * <-1 if an error occurs
528 decodeBocu1(Bocu1Rx
*pRx
, uint8_t b
) {
529 int32_t prev
, c
, count
;
532 /* illegal argument */
538 /* lenient handling of initial 0 values */
539 prev
=pRx
->prev
=BOCU1_ASCII_PREV
;
546 /* byte in lead position */
549 * Direct-encoded C0 control code or space.
550 * Reset prev for C0 control codes but not for space.
553 pRx
->prev
=BOCU1_ASCII_PREV
;
559 * b is a difference lead byte.
561 * Return a code point directly from a single-byte difference.
563 * For multi-byte difference lead bytes, set the decoder state
564 * with the partial difference value from the lead byte and
565 * with the number of trail bytes.
567 * For four-byte differences, the signedness also affects the
568 * first trail byte, which has special handling farther below.
570 if(b
>=BOCU1_START_NEG_2
&& b
<BOCU1_START_POS_2
) {
571 /* single-byte difference */
572 c
=prev
+((int32_t)b
-BOCU1_MIDDLE
);
573 pRx
->prev
=bocu1Prev(c
);
575 } else if(b
==BOCU1_RESET
) {
576 /* only reset the state, no code point */
577 pRx
->prev
=BOCU1_ASCII_PREV
;
580 return decodeBocu1LeadByte(pRx
, b
);
583 /* trail byte in any position */
584 return decodeBocu1TrailByte(pRx
, b
);
588 /* icuhtml/design/conversion/bocu1/bocu1tst.c ------------------------------- */
590 /* test code ---------------------------------------------------------------- */
592 /* test code options */
594 /* ignore comma when processing name lists in testText() */
595 #define TEST_IGNORE_COMMA 1
598 * Write a packed BOCU-1 byte sequence into a byte array,
599 * without overflow check.
602 * @param packed packed BOCU-1 byte sequence, see packDiff()
603 * @param p pointer to byte array
604 * @return number of bytes
609 writePacked(int32_t packed
, uint8_t *p
) {
610 int32_t count
=BOCU1_LENGTH_FROM_PACKED(packed
);
613 *p
++=(uint8_t)(packed
>>24);
615 *p
++=(uint8_t)(packed
>>16);
617 *p
++=(uint8_t)(packed
>>8);
619 *p
++=(uint8_t)packed
;
628 * Unpack a packed BOCU-1 non-C0/space byte sequence and get
629 * the difference to initialPrev.
630 * Used only for round-trip testing of the difference encoding and decoding.
633 * @param initialPrev bogus "previous code point" value to make sure that
634 * the resulting code point is in the range 0..0x10ffff
635 * @param packed packed BOCU-1 byte sequence
636 * @return the difference to initialPrev
642 unpackDiff(int32_t initialPrev
, int32_t packed
) {
643 Bocu1Rx rx
={ 0, 0, 0 };
647 count
=BOCU1_LENGTH_FROM_PACKED(packed
);
650 decodeBocu1(&rx
, (uint8_t)(packed
>>24));
652 decodeBocu1(&rx
, (uint8_t)(packed
>>16));
654 decodeBocu1(&rx
, (uint8_t)(packed
>>8));
656 /* subtract initial prev */
657 return decodeBocu1(&rx
, (uint8_t)packed
)-initialPrev
;
664 * Encode one difference value -0x10ffff..+0x10ffff in 1..4 bytes,
665 * preserving lexical order.
666 * Also checks for roundtripping of the difference encoding.
669 * @param diff difference value to test, -0x10ffff..0x10ffff
670 * @param p pointer to output byte array
671 * @return p advanced by number of bytes output
676 writeDiff(int32_t diff
, uint8_t *p
) {
677 /* generate the difference as a packed value and serialize it */
678 int32_t packed
, initialPrev
;
680 packed
=packDiff(diff
);
683 * bogus initial "prev" to work around
684 * code point range check in decodeBocu1()
687 initialPrev
=0x10ffff;
692 if(diff
!=unpackDiff(initialPrev
, packed
)) {
693 log_err("error: unpackDiff(packDiff(diff=%ld)=0x%08lx)=%ld!=diff\n",
694 diff
, packed
, unpackDiff(initialPrev
, packed
));
696 return p
+writePacked(packed
, p
);
700 * Encode a UTF-16 string in BOCU-1.
701 * Does not check for overflows, but otherwise useful function.
703 * @param s input UTF-16 string
704 * @param length number of UChar code units in s
705 * @param p pointer to output byte array
706 * @return number of bytes output
709 writeString(const UChar
*s
, int32_t length
, uint8_t *p
) {
717 U16_NEXT(s
, i
, length
, c
);
718 p
+=writePacked(encodeBocu1(&prev
, c
), p
);
720 return (int32_t)(p
-p0
);
724 * Decode a BOCU-1 byte sequence to a UTF-16 string.
725 * Does not check for overflows, but otherwise useful function.
727 * @param p pointer to input BOCU-1 bytes
728 * @param length number of input bytes
729 * @param s point to output UTF-16 string array
730 * @return number of UChar code units output
733 readString(const uint8_t *p
, int32_t length
, UChar
*s
) {
734 Bocu1Rx rx
={ 0, 0, 0 };
735 int32_t c
, i
, sLength
;
739 c
=decodeBocu1(&rx
, p
[i
++]);
741 log_err("error: readString detects encoding error at string index %ld\n", i
);
745 U16_APPEND_UNSAFE(s
, sLength
, c
);
752 hexDigit(uint8_t digit
) {
753 return digit
<=9 ? (char)('0'+digit
) : (char)('a'-10+digit
);
757 * Pretty-print 0-terminated byte values.
758 * Helper function for test output.
760 * @param bytes 0-terminated byte array to print
763 printBytes(uint8_t *bytes
, char *out
) {
768 while((b
=*bytes
++)!=0) {
770 *out
++=hexDigit((uint8_t)(b
>>4));
771 *out
++=hexDigit((uint8_t)(b
&0xf));
783 * Basic BOCU-1 test function, called when there are no command line arguments.
784 * Prints some of the #define values and performs round-trip tests of the
785 * difference encoding and decoding.
788 TestBOCU1RefDiff(void) {
789 char buf1
[80], buf2
[80];
790 uint8_t prev
[5], level
[5];
791 int32_t i
, cmp
, countErrors
;
793 log_verbose("reach of single bytes: %ld\n", 1+BOCU1_REACH_POS_1
-BOCU1_REACH_NEG_1
);
794 log_verbose("reach of 2 bytes : %ld\n", 1+BOCU1_REACH_POS_2
-BOCU1_REACH_NEG_2
);
795 log_verbose("reach of 3 bytes : %ld\n\n", 1+BOCU1_REACH_POS_3
-BOCU1_REACH_NEG_3
);
797 log_verbose(" BOCU1_REACH_NEG_1 %8ld BOCU1_REACH_POS_1 %8ld\n", BOCU1_REACH_NEG_1
, BOCU1_REACH_POS_1
);
798 log_verbose(" BOCU1_REACH_NEG_2 %8ld BOCU1_REACH_POS_2 %8ld\n", BOCU1_REACH_NEG_2
, BOCU1_REACH_POS_2
);
799 log_verbose(" BOCU1_REACH_NEG_3 %8ld BOCU1_REACH_POS_3 %8ld\n\n", BOCU1_REACH_NEG_3
, BOCU1_REACH_POS_3
);
801 log_verbose(" BOCU1_MIDDLE 0x%02x\n", BOCU1_MIDDLE
);
802 log_verbose(" BOCU1_START_NEG_2 0x%02x BOCU1_START_POS_2 0x%02x\n", BOCU1_START_NEG_2
, BOCU1_START_POS_2
);
803 log_verbose(" BOCU1_START_NEG_3 0x%02x BOCU1_START_POS_3 0x%02x\n\n", BOCU1_START_NEG_3
, BOCU1_START_POS_3
);
805 /* test packDiff() & unpackDiff() with some specific values */
808 writeDiff(65, level
);
809 writeDiff(130, level
);
810 writeDiff(30000, level
);
811 writeDiff(1000000, level
);
812 writeDiff(-65, level
);
813 writeDiff(-130, level
);
814 writeDiff(-30000, level
);
815 writeDiff(-1000000, level
);
817 /* test that each value is smaller than any following one */
820 *writeDiff(i
, prev
)=0;
822 /* show first number and bytes */
823 printBytes(prev
, buf1
);
824 log_verbose(" wD(%8ld) %s\n", i
, buf1
);
826 for(++i
; i
<=0x10ffff; ++i
) {
827 *writeDiff(i
, level
)=0;
828 cmp
=strcmp((const char *)prev
, (const char *)level
);
829 if(BOCU1_LENGTH_FROM_LEAD(level
[0])!=(int32_t)strlen((const char *)level
)) {
830 log_verbose("BOCU1_LENGTH_FROM_LEAD(0x%02x)=%ld!=%ld=strlen(writeDiff(%ld))\n",
831 level
[0], BOCU1_LENGTH_FROM_LEAD(level
[0]), strlen((const char *)level
), i
);
834 if(i
==0 || i
==1 || strlen((const char *)prev
)!=strlen((const char *)level
)) {
836 * if the result is good, then print only if the length changed
837 * to get little but interesting output
839 printBytes(prev
, buf1
);
840 printBytes(level
, buf2
);
841 log_verbose("ok: strcmp(wD(%8ld), wD(%8ld))=%2d %s%s\n", i
-1, i
, cmp
, buf1
, buf2
);
845 printBytes(prev
, buf1
);
846 printBytes(level
, buf2
);
847 log_verbose("wrong: strcmp(wD(%8ld), wD(%8ld))=%2d %s%s\n", i
-1, i
, cmp
, buf1
, buf2
);
849 /* remember the previous bytes */
850 memcpy(prev
, level
, 4);
853 /* show last number and bytes */
854 printBytes((uint8_t *)"", buf1
);
855 printBytes(prev
, buf2
);
856 log_verbose(" wD(%8ld) %s%s\n", i
-1, buf1
, buf2
);
859 log_verbose("writeDiff(-0x10ffff..0x10ffff) works fine\n");
861 log_err("writeDiff(-0x10ffff..0x10ffff) violates lexical ordering in %d cases\n", countErrors
);
864 /* output signature byte sequence */
866 writePacked(encodeBocu1(&i
, 0xfeff), level
);
867 log_verbose("\nBOCU-1 signature byte sequence: %02x %02x %02x\n",
868 level
[0], level
[1], level
[2]);
871 /* cintltst code ------------------------------------------------------------ */
873 static const int32_t DEFAULT_BUFFER_SIZE
= 30000;
876 /* test one string with the ICU and the reference BOCU-1 implementations */
878 roundtripBOCU1(UConverter
*bocu1
, int32_t number
, const UChar
*text
, int32_t length
) {
879 UChar
*roundtripRef
, *roundtripICU
;
880 char *bocu1Ref
, *bocu1ICU
;
882 int32_t bocu1RefLength
, bocu1ICULength
, roundtripRefLength
, roundtripICULength
;
883 UErrorCode errorCode
;
885 roundtripRef
= malloc(DEFAULT_BUFFER_SIZE
* sizeof(UChar
));
886 roundtripICU
= malloc(DEFAULT_BUFFER_SIZE
* sizeof(UChar
));
887 bocu1Ref
= malloc(DEFAULT_BUFFER_SIZE
);
888 bocu1ICU
= malloc(DEFAULT_BUFFER_SIZE
);
890 /* Unicode -> BOCU-1 */
891 bocu1RefLength
=writeString(text
, length
, (uint8_t *)bocu1Ref
);
893 errorCode
=U_ZERO_ERROR
;
894 bocu1ICULength
=ucnv_fromUChars(bocu1
, bocu1ICU
, DEFAULT_BUFFER_SIZE
, text
, length
, &errorCode
);
895 if(U_FAILURE(errorCode
)) {
896 log_err("ucnv_fromUChars(BOCU-1, text(%d)[%d]) failed: %s\n", number
, length
, u_errorName(errorCode
));
900 if(bocu1RefLength
!=bocu1ICULength
|| 0!=uprv_memcmp(bocu1Ref
, bocu1ICU
, bocu1RefLength
)) {
901 log_err("Unicode(%d)[%d] -> BOCU-1: reference[%d]!=ICU[%d]\n", number
, length
, bocu1RefLength
, bocu1ICULength
);
905 /* BOCU-1 -> Unicode */
906 roundtripRefLength
=readString((uint8_t *)bocu1Ref
, bocu1RefLength
, roundtripRef
);
907 if(roundtripRefLength
<0) {
908 goto cleanup
; /* readString() found an error and reported it */
911 roundtripICULength
=ucnv_toUChars(bocu1
, roundtripICU
, DEFAULT_BUFFER_SIZE
, bocu1ICU
, bocu1ICULength
, &errorCode
);
912 if(U_FAILURE(errorCode
)) {
913 log_err("ucnv_toUChars(BOCU-1, text(%d)[%d]) failed: %s\n", number
, length
, u_errorName(errorCode
));
917 if(length
!=roundtripRefLength
|| 0!=u_memcmp(text
, roundtripRef
, length
)) {
918 log_err("BOCU-1 -> Unicode: original(%d)[%d]!=reference[%d]\n", number
, length
, roundtripRefLength
);
921 if(roundtripRefLength
!=roundtripICULength
|| 0!=u_memcmp(roundtripRef
, roundtripICU
, roundtripRefLength
)) {
922 log_err("BOCU-1 -> Unicode: reference(%d)[%d]!=ICU[%d]\n", number
, roundtripRefLength
, roundtripICULength
);
932 static const UChar feff
[]={ 0xfeff };
933 static const UChar ascii
[]={ 0x61, 0x62, 0x20, 0x63, 0x61 };
934 static const UChar crlf
[]={ 0xd, 0xa, 0x20 };
935 static const UChar nul
[]={ 0 };
936 static const UChar latin
[]={ 0xdf, 0xe6 };
937 static const UChar devanagari
[]={ 0x930, 0x20, 0x918, 0x909 };
938 static const UChar hiragana
[]={ 0x3086, 0x304d, 0x20, 0x3053, 0x4000 };
939 static const UChar unihan
[]={ 0x4e00, 0x7777, 0x20, 0x9fa5, 0x4e00 };
940 static const UChar hangul
[]={ 0xac00, 0xbcde, 0x20, 0xd7a3 };
941 static const UChar surrogates
[]={ 0xdc00, 0xd800 }; /* single surrogates, unmatched! */
942 static const UChar plane1
[]={ 0xd800, 0xdc00 };
943 static const UChar plane2
[]={ 0xd845, 0xdddd };
944 static const UChar plane15
[]={ 0xdbbb, 0xddee, 0x20 };
945 static const UChar plane16
[]={ 0xdbff, 0xdfff };
946 static const UChar c0
[]={ 1, 0xe40, 0x20, 9 };
948 static const struct {
952 { feff
, UPRV_LENGTHOF(feff
) },
953 { ascii
, UPRV_LENGTHOF(ascii
) },
954 { crlf
, UPRV_LENGTHOF(crlf
) },
955 { nul
, UPRV_LENGTHOF(nul
) },
956 { latin
, UPRV_LENGTHOF(latin
) },
957 { devanagari
, UPRV_LENGTHOF(devanagari
) },
958 { hiragana
, UPRV_LENGTHOF(hiragana
) },
959 { unihan
, UPRV_LENGTHOF(unihan
) },
960 { hangul
, UPRV_LENGTHOF(hangul
) },
961 { surrogates
, UPRV_LENGTHOF(surrogates
) },
962 { plane1
, UPRV_LENGTHOF(plane1
) },
963 { plane2
, UPRV_LENGTHOF(plane2
) },
964 { plane15
, UPRV_LENGTHOF(plane15
) },
965 { plane16
, UPRV_LENGTHOF(plane16
) },
966 { c0
, UPRV_LENGTHOF(c0
) }
970 * Verify that the ICU BOCU-1 implementation produces the same results as
971 * the reference implementation from the design folder.
972 * Generate some texts and convert them with both converters, verifying
973 * identical results and roundtripping.
981 UErrorCode errorCode
;
983 errorCode
=U_ZERO_ERROR
;
984 bocu1
=ucnv_open("BOCU-1", &errorCode
);
985 if(U_FAILURE(errorCode
)) {
986 log_data_err("error: unable to open BOCU-1 converter: %s\n", u_errorName(errorCode
));
990 text
= malloc(DEFAULT_BUFFER_SIZE
* sizeof(UChar
));
992 /* text 1: each of strings[] once */
994 for(i
=0; i
<UPRV_LENGTHOF(strings
); ++i
) {
995 u_memcpy(text
+length
, strings
[i
].s
, strings
[i
].length
);
996 length
+=strings
[i
].length
;
998 roundtripBOCU1(bocu1
, 1, text
, length
);
1000 /* text 2: each of strings[] twice */
1002 for(i
=0; i
<UPRV_LENGTHOF(strings
); ++i
) {
1003 u_memcpy(text
+length
, strings
[i
].s
, strings
[i
].length
);
1004 length
+=strings
[i
].length
;
1005 u_memcpy(text
+length
, strings
[i
].s
, strings
[i
].length
);
1006 length
+=strings
[i
].length
;
1008 roundtripBOCU1(bocu1
, 2, text
, length
);
1010 /* text 3: each of strings[] many times (set step vs. |strings| so that all strings are used) */
1012 for(i
=1; length
<5000; i
+=7) {
1013 if(i
>=UPRV_LENGTHOF(strings
)) {
1014 i
-=UPRV_LENGTHOF(strings
);
1016 u_memcpy(text
+length
, strings
[i
].s
, strings
[i
].length
);
1017 length
+=strings
[i
].length
;
1019 roundtripBOCU1(bocu1
, 3, text
, length
);
1025 U_CFUNC
void addBOCU1Tests(TestNode
** root
);
1028 addBOCU1Tests(TestNode
** root
) {
1029 addTest(root
, TestBOCU1RefDiff
, "tsconv/bocu1tst/TestBOCU1RefDiff");
1030 addTest(root
, TestBOCU1
, "tsconv/bocu1tst/TestBOCU1");