/src/ghostpdl/xps/xpsutf.c
Line | Count | Source |
1 | | /* Copyright (C) 2001-2023 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* XPS interpreter - unicode text functions */ |
18 | | |
19 | | #include "ghostxps.h" |
20 | | |
21 | | /* |
22 | | * http://tools.ietf.org/html/rfc3629 |
23 | | */ |
24 | | |
25 | | int |
26 | | xps_utf8_to_ucs(int *p, const char *ss, int n) |
27 | 66 | { |
28 | 66 | unsigned char *s = (unsigned char *)ss; |
29 | | |
30 | 66 | if (s == NULL) |
31 | 0 | goto bad; |
32 | | |
33 | 66 | if ((s[0] & 0x80) == 0) |
34 | 0 | { |
35 | 0 | *p = (s[0] & 0x7f); |
36 | 0 | return 1; |
37 | 0 | } |
38 | | |
39 | 66 | if ((s[0] & 0xe0) == 0xc0) |
40 | 0 | { |
41 | 0 | if (n < 2 || s[1] < 0x80) |
42 | 0 | goto bad; |
43 | 0 | *p = (s[0] & 0x1f) << 6; |
44 | 0 | *p |= (s[1] & 0x3f); |
45 | 0 | return 2; |
46 | 0 | } |
47 | | |
48 | 66 | if ((s[0] & 0xf0) == 0xe0) |
49 | 66 | { |
50 | 66 | if (n < 3 || s[1] < 0x80 || s[2] < 0x80) |
51 | 0 | goto bad; |
52 | 66 | *p = (s[0] & 0x0f) << 12; |
53 | 66 | *p |= (s[1] & 0x3f) << 6; |
54 | 66 | *p |= (s[2] & 0x3f); |
55 | 66 | return 3; |
56 | 66 | } |
57 | | |
58 | 0 | if ((s[0] & 0xf8) == 0xf0) |
59 | 0 | { |
60 | 0 | if (n < 4 || s[1] < 0x80 || s[2] < 0x80 || s[3] < 0x80) |
61 | 0 | goto bad; |
62 | 0 | *p = (s[0] & 0x07) << 18; |
63 | 0 | *p |= (s[1] & 0x3f) << 12; |
64 | 0 | *p |= (s[2] & 0x3f) << 6; |
65 | 0 | *p |= (s[3] & 0x3f); |
66 | 0 | return 4; |
67 | 0 | } |
68 | | |
69 | 0 | bad: |
70 | 0 | *p = 0x80; |
71 | 0 | return 1; |
72 | 0 | } |