1656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* crypto/asn1/a_utf8.c */
2656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * All rights reserved.
4656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
5656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This package is an SSL implementation written
6656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * by Eric Young (eay@cryptsoft.com).
7656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The implementation was written so as to conform with Netscapes SSL.
8656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
9656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This library is free for commercial and non-commercial use as long as
10656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the following conditions are aheared to.  The following conditions
11656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * apply to all code found in this distribution, be it the RC4, RSA,
12656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * included with this distribution is covered by the same copyright terms
14656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
16656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Copyright remains Eric Young's, and as such any Copyright notices in
17656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the code are not to be removed.
18656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * If this package is used in a product, Eric Young should be given attribution
19656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * as the author of the parts of the library used.
20656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This can be in the form of a textual message at program startup or
21656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * in documentation (online or textual) provided with the package.
22656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
23656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Redistribution and use in source and binary forms, with or without
24656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modification, are permitted provided that the following conditions
25656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are met:
26656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 1. Redistributions of source code must retain the copyright
27656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer.
28656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
29656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
30656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    documentation and/or other materials provided with the distribution.
31656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 3. All advertising materials mentioning features or use of this software
32656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    must display the following acknowledgement:
33656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes cryptographic software written by
34656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *     Eric Young (eay@cryptsoft.com)"
35656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    The word 'cryptographic' can be left out if the rouines from the library
36656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    being used are not cryptographic related :-).
37656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 4. If you include any Windows specific code (or a derivative thereof) from
38656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    the apps directory (application code) you must include an acknowledgement:
39656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
41656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * SUCH DAMAGE.
52656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
53656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The licence and distribution terms for any publically available version or
54656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * derivative of this code cannot be changed.  i.e. this code cannot simply be
55656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * copied and put under another distribution licence
56656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * [including the GNU Public Licence.]
57656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
58656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
59656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <stdio.h>
60656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include "cryptlib.h"
61656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/asn1.h>
62656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
63656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
64656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* UTF8 utilities */
65656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
66656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* This parses a UTF8 string one character at a time. It is passed a pointer
67656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * to the string and the length of the string. It sets 'value' to the value of
68656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the current character. It returns the number of characters read or a
69656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * negative error code:
70656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * -1 = string too short
71656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * -2 = illegal character
72656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * -3 = subsequent characters not of the form 10xxxxxx
73656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * -4 = character encoded incorrectly (not minimal length).
74656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
75656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
76656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint UTF8_getc(const unsigned char *str, int len, unsigned long *val)
77656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
78656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	const unsigned char *p;
79656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	unsigned long value;
80656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	int ret;
81656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(len <= 0) return 0;
82656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	p = str;
83656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
84656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	/* Check syntax and work out the encoded value (if correct) */
85656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if((*p & 0x80) == 0) {
86656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value = *p++ & 0x7f;
87656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		ret = 1;
88656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	} else if((*p & 0xe0) == 0xc0) {
89656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(len < 2) return -1;
90656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if((p[1] & 0xc0) != 0x80) return -3;
91656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value = (*p++ & 0x1f) << 6;
92656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= *p++ & 0x3f;
93656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(value < 0x80) return -4;
94656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		ret = 2;
95656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	} else if((*p & 0xf0) == 0xe0) {
96656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(len < 3) return -1;
97656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if( ((p[1] & 0xc0) != 0x80)
98656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		   || ((p[2] & 0xc0) != 0x80) ) return -3;
99656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value = (*p++ & 0xf) << 12;
100656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= (*p++ & 0x3f) << 6;
101656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= *p++ & 0x3f;
102656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(value < 0x800) return -4;
103656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		ret = 3;
104656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	} else if((*p & 0xf8) == 0xf0) {
105656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(len < 4) return -1;
106656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if( ((p[1] & 0xc0) != 0x80)
107656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		   || ((p[2] & 0xc0) != 0x80)
108656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		   || ((p[3] & 0xc0) != 0x80) ) return -3;
109656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value = ((unsigned long)(*p++ & 0x7)) << 18;
110656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= (*p++ & 0x3f) << 12;
111656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= (*p++ & 0x3f) << 6;
112656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= *p++ & 0x3f;
113656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(value < 0x10000) return -4;
114656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		ret = 4;
115656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	} else if((*p & 0xfc) == 0xf8) {
116656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(len < 5) return -1;
117656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if( ((p[1] & 0xc0) != 0x80)
118656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		   || ((p[2] & 0xc0) != 0x80)
119656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		   || ((p[3] & 0xc0) != 0x80)
120656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		   || ((p[4] & 0xc0) != 0x80) ) return -3;
121656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value = ((unsigned long)(*p++ & 0x3)) << 24;
122656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= ((unsigned long)(*p++ & 0x3f)) << 18;
123656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= ((unsigned long)(*p++ & 0x3f)) << 12;
124656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= (*p++ & 0x3f) << 6;
125656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= *p++ & 0x3f;
126656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(value < 0x200000) return -4;
127656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		ret = 5;
128656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	} else if((*p & 0xfe) == 0xfc) {
129656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(len < 6) return -1;
130656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if( ((p[1] & 0xc0) != 0x80)
131656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		   || ((p[2] & 0xc0) != 0x80)
132656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		   || ((p[3] & 0xc0) != 0x80)
133656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		   || ((p[4] & 0xc0) != 0x80)
134656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		   || ((p[5] & 0xc0) != 0x80) ) return -3;
135656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value = ((unsigned long)(*p++ & 0x1)) << 30;
136656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= ((unsigned long)(*p++ & 0x3f)) << 24;
137656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= ((unsigned long)(*p++ & 0x3f)) << 18;
138656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= ((unsigned long)(*p++ & 0x3f)) << 12;
139656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= (*p++ & 0x3f) << 6;
140656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		value |= *p++ & 0x3f;
141656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(value < 0x4000000) return -4;
142656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		ret = 6;
143656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	} else return -2;
144656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	*val = value;
145656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return ret;
146656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
147656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
148656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* This takes a character 'value' and writes the UTF8 encoded value in
149656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 'str' where 'str' is a buffer containing 'len' characters. Returns
150656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the number of characters written or -1 if 'len' is too small. 'str' can
151656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * be set to NULL in which case it just returns the number of characters.
152656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * It will need at most 6 characters.
153656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
154656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
155656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint UTF8_putc(unsigned char *str, int len, unsigned long value)
156656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
157656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!str) len = 6;	/* Maximum we will need */
158656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	else if(len <= 0) return -1;
159656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(value < 0x80) {
160656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(str) *str = (unsigned char)value;
161656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return 1;
162656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
163656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(value < 0x800) {
164656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(len < 2) return -1;
165656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(str) {
166656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
167656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str = (unsigned char)((value & 0x3f) | 0x80);
168656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
169656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return 2;
170656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
171656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(value < 0x10000) {
172656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(len < 3) return -1;
173656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(str) {
174656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
175656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
176656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str = (unsigned char)((value & 0x3f) | 0x80);
177656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
178656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return 3;
179656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
180656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(value < 0x200000) {
181656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(len < 4) return -1;
182656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(str) {
183656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
184656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
185656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
186656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str = (unsigned char)((value & 0x3f) | 0x80);
187656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
188656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return 4;
189656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
190656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(value < 0x4000000) {
191656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(len < 5) return -1;
192656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(str) {
193656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
194656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
195656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
196656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
197656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			*str = (unsigned char)((value & 0x3f) | 0x80);
198656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
199656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return 5;
200656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
201656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(len < 6) return -1;
202656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(str) {
203656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		*str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
204656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		*str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
205656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		*str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
206656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		*str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
207656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		*str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
208656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		*str = (unsigned char)((value & 0x3f) | 0x80);
209656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
210656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return 6;
211656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
212