]>
git.saurik.com Git - apple/libc.git/blob - gen/asl_util.c
108fa175e2ad9c927eee77386c0481f52d7519c0
2 * Copyright (c) 2006-2011 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
21 * @APPLE_LICENSE_HEADER_END@
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.
29 #include <sys/types.h>
30 #include <sys/socket.h>
38 static uint8_t *b64charset
= (uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
40 __private_extern__
const char *
41 _asl_escape(unsigned char c
)
58 asl_is_utf8_char(const unsigned char *p
, int *state
, int *ctype
)
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;
87 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 0;
94 if ((*p
>= 0xa0) && (*p
<= 0xbf)) *state
= 2;
101 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 2;
108 if ((*p
>= 0x90) && (*p
<= 0xbf)) *state
= 2;
115 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 2;
122 if ((*p
>= 0x80) && (*p
<= 0x8f)) *state
= 2;
135 if ((*ctype
>= 2) && (*ctype
<= 3))
137 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 0;
140 else if ((*ctype
>= 4) && (*ctype
<= 6))
142 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 3;
155 if ((*ctype
>= 4) && (*ctype
<= 6))
157 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 0;
174 __private_extern__
int
175 asl_is_utf8(const char *str
)
177 const unsigned char *p
;
182 if (str
== NULL
) return flag
;
184 for (p
= (const unsigned char *)str
; (*p
!= '\0') && (flag
== 1); p
++)
186 flag
= asl_is_utf8_char(p
, &state
, &ctype
);
192 __private_extern__
uint8_t *
193 asl_b64_encode(const uint8_t *buf
, size_t len
)
197 size_t i0
, i1
, i2
, j
, outlen
;
199 if (buf
== NULL
) return NULL
;
200 if (len
== 0) return NULL
;
202 outlen
= ((len
+ 2) / 3) * 4;
203 out
= (uint8_t *)malloc(outlen
+ 1);
220 out
[j
++] = b64charset
[b
];
222 b
= ((buf
[i0
] & 0x03) << 4) | (buf
[i1
] >> 4);
223 out
[j
++] = b64charset
[b
];
225 b
= ((buf
[i1
] & 0x0f) << 2) | ((buf
[i2
] & 0xc0) >> 6);
226 out
[j
++] = b64charset
[b
];
229 out
[j
++] = b64charset
[b
];
239 out
[j
++] = b64charset
[b
];
241 b
= (buf
[i0
] & 0x03) << 4;
243 if (i1
< len
) b
|= (buf
[i1
] >> 4);
244 out
[j
++] = b64charset
[b
];
253 b
= (buf
[i1
] & 0x0f) << 2;
254 out
[j
++] = b64charset
[b
];