]>
Commit | Line | Data |
---|---|---|
224c7076 | 1 | /* |
b5d655f7 | 2 | * Copyright (c) 2006-2008 Apple Inc. All rights reserved. |
224c7076 A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
18 | * License for the specific language governing rights and limitations | |
19 | * under the License." | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | /* | |
24 | * These routines needs to be separate from asl.c, so that dyld can build | |
25 | * and suck in these without the rest of asl. | |
26 | */ | |
27 | ||
28 | #include <string.h> | |
29 | #include <sys/types.h> | |
30 | #include <sys/socket.h> | |
31 | #include <sys/un.h> | |
32 | #include <fcntl.h> | |
33 | #include <stdint.h> | |
b5d655f7 | 34 | #include <stdlib.h> |
224c7076 | 35 | #include <errno.h> |
b5d655f7 | 36 | #include <unistd.h> |
224c7076 | 37 | |
224c7076 A |
38 | static uint8_t *b64charset = (uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
39 | ||
224c7076 A |
40 | __private_extern__ const char * |
41 | _asl_escape(unsigned char c) | |
42 | { | |
43 | switch(c) | |
44 | { | |
45 | case '\\': | |
46 | return "\\\\"; | |
47 | case '[': | |
48 | return "\\["; | |
49 | case ']': | |
50 | return "\\]"; | |
51 | case '\n': | |
52 | return "\\n"; | |
53 | } | |
54 | return NULL; | |
55 | } | |
56 | ||
57 | static int | |
58 | asl_is_utf8_char(const unsigned char *p, int *state, int *ctype) | |
59 | { | |
60 | switch (*state) | |
61 | { | |
62 | case 0: | |
63 | { | |
64 | *ctype = 0; | |
65 | ||
66 | if (*p >= 0x80) | |
67 | { | |
68 | *state = 1; | |
69 | if ((*p >= 0xc2) && (*p <= 0xdf)) *ctype = 1; | |
70 | else if (*p == 0xe0) *ctype = 2; | |
71 | else if ((*p >= 0xe1) && (*p <= 0xef)) *ctype = 3; | |
72 | else if (*p == 0xf0) *ctype = 4; | |
73 | else if ((*p >= 0xf1) && (*p <= 0xf3)) *ctype = 5; | |
74 | else if (*p == 0xf4) *ctype = 6; | |
75 | else return 0; | |
76 | } | |
77 | ||
78 | break; | |
79 | } | |
80 | ||
81 | case 1: | |
82 | { | |
83 | switch (*ctype) | |
84 | { | |
85 | case 1: | |
86 | { | |
87 | if ((*p >= 0x80) && (*p <= 0xbf)) *state = 0; | |
88 | else return 0; | |
89 | break; | |
90 | } | |
91 | ||
92 | case 2: | |
93 | { | |
94 | if ((*p >= 0xa0) && (*p <= 0xbf)) *state = 2; | |
95 | else return 0; | |
96 | break; | |
97 | } | |
98 | ||
99 | case 3: | |
100 | { | |
101 | if ((*p >= 0x80) && (*p <= 0xbf)) *state = 2; | |
102 | else return 0; | |
103 | break; | |
104 | } | |
105 | ||
106 | case 4: | |
107 | { | |
108 | if ((*p >= 0x90) && (*p <= 0xbf)) *state = 2; | |
109 | else return 0; | |
110 | break; | |
111 | } | |
112 | ||
113 | case 5: | |
114 | { | |
115 | if ((*p >= 0x80) && (*p <= 0xbf)) *state = 2; | |
116 | else return 0; | |
117 | break; | |
118 | } | |
119 | ||
120 | case 6: | |
121 | { | |
122 | if ((*p >= 0x80) && (*p <= 0x8f)) *state = 2; | |
123 | else return 0; | |
124 | break; | |
125 | } | |
126 | ||
127 | default: return 0; | |
128 | } | |
129 | ||
130 | break; | |
131 | } | |
132 | ||
133 | case 2: | |
134 | { | |
135 | if ((*ctype >= 2) && (*ctype <= 3)) | |
136 | { | |
137 | if ((*p >= 0x80) && (*p <= 0xbf)) *state = 0; | |
138 | else return 0; | |
139 | } | |
140 | else if ((*ctype >= 4) && (*ctype <= 6)) | |
141 | { | |
142 | if ((*p >= 0x80) && (*p <= 0xbf)) *state = 3; | |
143 | else return 0; | |
144 | } | |
145 | else | |
146 | { | |
147 | return 0; | |
148 | } | |
149 | ||
150 | break; | |
151 | } | |
152 | ||
153 | case 3: | |
154 | { | |
155 | if ((*ctype >= 4) && (*ctype <= 6)) | |
156 | { | |
157 | if ((*p >= 0x80) && (*p <= 0xbf)) *state = 0; | |
158 | else return 0; | |
159 | } | |
160 | else | |
161 | { | |
162 | return 0; | |
163 | } | |
164 | ||
165 | break; | |
166 | } | |
167 | ||
168 | default: return 0; | |
169 | } | |
170 | ||
171 | return 1; | |
172 | } | |
173 | ||
174 | __private_extern__ int | |
175 | asl_is_utf8(const char *str) | |
176 | { | |
177 | const unsigned char *p; | |
178 | int flag = 1; | |
179 | int state = 0; | |
180 | int ctype = 0; | |
181 | ||
182 | if (str == NULL) return flag; | |
183 | ||
184 | for (p = (const unsigned char *)str; (*p != '\0') && (flag == 1); p++) | |
185 | { | |
186 | flag = asl_is_utf8_char(p, &state, &ctype); | |
187 | } | |
188 | ||
189 | return flag; | |
190 | } | |
191 | ||
192 | __private_extern__ uint8_t * | |
193 | asl_b64_encode(const uint8_t *buf, size_t len) | |
194 | { | |
195 | uint8_t *out; | |
196 | uint8_t b; | |
197 | size_t i0, i1, i2, j, outlen; | |
198 | ||
199 | if (buf == NULL) return NULL; | |
200 | if (len == 0) return NULL; | |
201 | ||
202 | outlen = ((len + 2) / 3) * 4; | |
203 | out = (uint8_t *)malloc(outlen + 1); | |
204 | if (out == NULL) | |
205 | { | |
206 | errno = ENOMEM; | |
207 | return NULL; | |
208 | } | |
209 | ||
210 | out[outlen] = 0; | |
211 | ||
212 | i0 = 0; | |
213 | i1 = 1; | |
214 | i2 = 2; | |
215 | j = 0; | |
216 | ||
217 | while (i2 < len) | |
218 | { | |
219 | b = buf[i0] >> 2; | |
220 | out[j++] = b64charset[b]; | |
221 | ||
222 | b = ((buf[i0] & 0x03) << 4) | (buf[i1] >> 4); | |
223 | out[j++] = b64charset[b]; | |
224 | ||
225 | b = ((buf[i1] & 0x0f) << 2) | ((buf[i2] & 0xc0) >> 6); | |
226 | out[j++] = b64charset[b]; | |
227 | ||
228 | b = buf[i2] & 0x3f; | |
229 | out[j++] = b64charset[b]; | |
230 | ||
231 | i0 += 3; | |
232 | i1 = i0 + 1; | |
233 | i2 = i1 + 1; | |
234 | } | |
235 | ||
236 | if (i0 < len) | |
237 | { | |
238 | b = buf[i0] >> 2; | |
239 | out[j++] = b64charset[b]; | |
240 | ||
241 | b = (buf[i0] & 0x03) << 4; | |
242 | ||
243 | if (i1 < len) b |= (buf[i1] >> 4); | |
244 | out[j++] = b64charset[b]; | |
245 | ||
246 | if (i1 >= len) | |
247 | { | |
248 | out[j++] = '='; | |
249 | out[j++] = '='; | |
250 | return out; | |
251 | } | |
252 | ||
253 | b = (buf[i1] & 0x0f) << 2; | |
254 | out[j++] = b64charset[b]; | |
255 | out[j++] = '='; | |
256 | } | |
257 | ||
258 | return out; | |
259 | } |