]>
git.saurik.com Git - apple/libc.git/blob - gen/asl_util.c
4a0130b2e604fa9ca5ac2ebe53690d4971886b22
2 * Copyright (c) 2006-2008 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 #define _PATH_ASL_IN "/var/run/asl_input"
40 static uint8_t *b64charset
= (uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
42 __private_extern__
int
43 _asl_server_socket(int *sock
, struct sockaddr_un
*server
)
48 *sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
49 if (*sock
< 0) return -1;
51 memset(server
, 0, sizeof(struct sockaddr_un
));
52 server
->sun_family
= AF_UNIX
;
54 strcpy(server
->sun_path
, _PATH_ASL_IN
);
55 server
->sun_len
= strlen(server
->sun_path
) + 1;
56 len
= sizeof(server
->sun_len
) + sizeof(server
->sun_family
) + server
->sun_len
;
58 status
= connect(*sock
, (const struct sockaddr
*)server
, len
);
67 /* set close-on-exec flag */
68 fcntl(*sock
, F_SETFD
, 1);
70 /* set non-blocking flag */
71 flags
= fcntl(*sock
, F_GETFL
, 0);
72 if (flags
>= 0) fcntl(*sock
, F_SETFL
, flags
| O_NONBLOCK
);
77 __private_extern__
const char *
78 _asl_escape(unsigned char c
)
95 asl_is_utf8_char(const unsigned char *p
, int *state
, int *ctype
)
106 if ((*p
>= 0xc2) && (*p
<= 0xdf)) *ctype
= 1;
107 else if (*p
== 0xe0) *ctype
= 2;
108 else if ((*p
>= 0xe1) && (*p
<= 0xef)) *ctype
= 3;
109 else if (*p
== 0xf0) *ctype
= 4;
110 else if ((*p
>= 0xf1) && (*p
<= 0xf3)) *ctype
= 5;
111 else if (*p
== 0xf4) *ctype
= 6;
124 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 0;
131 if ((*p
>= 0xa0) && (*p
<= 0xbf)) *state
= 2;
138 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 2;
145 if ((*p
>= 0x90) && (*p
<= 0xbf)) *state
= 2;
152 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 2;
159 if ((*p
>= 0x80) && (*p
<= 0x8f)) *state
= 2;
172 if ((*ctype
>= 2) && (*ctype
<= 3))
174 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 0;
177 else if ((*ctype
>= 4) && (*ctype
<= 6))
179 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 3;
192 if ((*ctype
>= 4) && (*ctype
<= 6))
194 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 0;
211 __private_extern__
int
212 asl_is_utf8(const char *str
)
214 const unsigned char *p
;
219 if (str
== NULL
) return flag
;
221 for (p
= (const unsigned char *)str
; (*p
!= '\0') && (flag
== 1); p
++)
223 flag
= asl_is_utf8_char(p
, &state
, &ctype
);
229 __private_extern__
uint8_t *
230 asl_b64_encode(const uint8_t *buf
, size_t len
)
234 size_t i0
, i1
, i2
, j
, outlen
;
236 if (buf
== NULL
) return NULL
;
237 if (len
== 0) return NULL
;
239 outlen
= ((len
+ 2) / 3) * 4;
240 out
= (uint8_t *)malloc(outlen
+ 1);
257 out
[j
++] = b64charset
[b
];
259 b
= ((buf
[i0
] & 0x03) << 4) | (buf
[i1
] >> 4);
260 out
[j
++] = b64charset
[b
];
262 b
= ((buf
[i1
] & 0x0f) << 2) | ((buf
[i2
] & 0xc0) >> 6);
263 out
[j
++] = b64charset
[b
];
266 out
[j
++] = b64charset
[b
];
276 out
[j
++] = b64charset
[b
];
278 b
= (buf
[i0
] & 0x03) << 4;
280 if (i1
< len
) b
|= (buf
[i1
] >> 4);
281 out
[j
++] = b64charset
[b
];
290 b
= (buf
[i1
] & 0x0f) << 2;
291 out
[j
++] = b64charset
[b
];