2 * Copyright (c) 2000,2001,2002 Japan Network Information Center.
5 * By using this file, you agree to the terms and conditions set forth bellow.
7 * LICENSE TERMS AND CONDITIONS
9 * The following License Terms and Conditions apply, unless a different
10 * license is obtained from Japan Network Information Center ("JPNIC"),
11 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
12 * Chiyoda-ku, Tokyo 101-0047, Japan.
14 * 1. Use, Modification and Redistribution (including distribution of any
15 * modified or derived work) in source and/or binary forms is permitted
16 * under this License Terms and Conditions.
18 * 2. Redistribution of source code must retain the copyright notices as they
19 * appear in each source code file, this License Terms and Conditions.
21 * 3. Redistribution in binary form must reproduce the Copyright Notice,
22 * this License Terms and Conditions, in the documentation and/or other
23 * materials provided with the distribution. For the purposes of binary
24 * distribution the "Copyright Notice" refers to the following language:
25 * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
27 * 4. The name of JPNIC may not be used to endorse or promote products
28 * derived from this Software without specific prior written approval of
31 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
34 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
38 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
39 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
49 #include "nsIDNKitInterface.h"
52 #define RACE_2OCTET_MODE 0xd8
53 #define RACE_ESCAPE 0xff
54 #define RACE_ESCAPE_2ND 0x99
60 compress_one
, /* all characters are in a single row */
61 compress_two
, /* row 0 and another row */
62 compress_none
/* nope */
67 race_decode_decompress(const char *from
, uint16_t *buf
, size_t buflen
)
70 unsigned int bitbuf
= 0;
75 while (*from
!= '\0') {
79 if ('a' <= c
&& c
<= 'z')
81 else if ('A' <= c
&& c
<= 'Z')
83 else if ('2' <= c
&& c
<= '7')
86 return (idn_invalid_encoding
);
88 bitbuf
= (bitbuf
<< 5) + x
;
91 *p
++ = (bitbuf
>> (bitlen
- 8)) & 0xff;
98 * Now 'buf' holds the decoded string.
104 if (buf
[0] == RACE_2OCTET_MODE
) {
105 if ((len
- 1) % 2 != 0)
106 return (idn_invalid_encoding
);
107 for (i
= 1, j
= 0; i
< len
; i
+= 2, j
++)
108 buf
[j
] = (buf
[i
] << 8) + buf
[i
+ 1];
111 uint16_t c
= buf
[0] << 8; /* higher octet */
113 for (i
= 1, j
= 0; i
< len
; j
++) {
114 if (buf
[i
] == RACE_ESCAPE
) {
116 return (idn_invalid_encoding
);
117 else if (buf
[i
+ 1] == RACE_ESCAPE_2ND
)
123 } else if (buf
[i
] == 0x99 && c
== 0x00) {
125 * The RACE specification says this is error.
127 return (idn_invalid_encoding
);
130 buf
[j
] = c
| buf
[i
++];
137 return (idn_success
);
141 race_compress_encode(const uint16_t *p
, int compress_mode
,
142 char *to
, size_t tolen
)
144 uint32_t bitbuf
= *p
++; /* bit stream buffer */
145 int bitlen
= 8; /* # of bits in 'bitbuf' */
147 while (*p
!= '\0' || bitlen
> 0) {
151 /* End of data. Flush. */
152 bitbuf
<<= (5 - bitlen
);
154 } else if (compress_mode
== compress_none
) {
155 /* Push 16 bit data. */
156 bitbuf
= (bitbuf
<< 16) | c
;
159 } else {/* compress_mode == compress_one/compress_two */
160 /* Push 8 or 16 bit data. */
161 if (compress_mode
== compress_two
&&
163 /* Upper octet is zero (and not U1). */
164 bitbuf
= (bitbuf
<< 16) | 0xff00 | c
;
166 } else if ((c
& 0xff) == 0xff) {
167 /* Lower octet is 0xff. */
168 bitbuf
= (bitbuf
<< 16) |
169 (RACE_ESCAPE
<< 8) | RACE_ESCAPE_2ND
;
172 /* Just output lower octet. */
173 bitbuf
= (bitbuf
<< 8) | (c
& 0xff);
180 * Output bits in 'bitbuf' in 5-bit unit.
182 while (bitlen
>= 5) {
185 /* Get top 5 bits. */
186 x
= (bitbuf
>> (bitlen
- 5)) & 0x1f;
196 return (idn_buffer_overflow
);
204 return (idn_buffer_overflow
);
207 return (idn_success
);
211 get_compress_mode(uint16_t *p
) {
213 unsigned int upper
= 0;
214 uint16_t *modepos
= p
- 1;
217 unsigned int hi
= *p
++ & 0xff00;
221 } else if (hi
== upper
) {
223 } else if (upper
== 0) {
226 *modepos
= RACE_2OCTET_MODE
;
227 return (compress_none
);
230 *modepos
= upper
>> 8;
231 if (upper
> 0 && zero
> 0)
232 return (compress_two
);
234 return (compress_one
);