Debian Codegraph @

common/unicode.c

raw symbols SCIP
1 /* $Id: unicode.c,v 1.10 2016/10/11 13:48:24 ragge Exp $ */
2 /*
3 * Copyright (c) 2014 Eric Olson <[email protected]>
4 * Some rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <stdio.h>
32 #include <ctype.h>
33 #include "pass1.h"
34 #include "manifest.h"
35 #include "unicode.h"
36
37 /*
38 * decode 32-bit code point from UTF-8
39 * move pointer
40 */
41 long
42 u82cp(char **q)
43 {
44 unsigned char *t = (unsigned char *)*q;
45 unsigned long c, r;
46 int i, sz;
47
48 if (*t == '\\')
49 c = esccon((char **)&t);
50 else
51 c = *t++;
52
53 /* always eat the first value */
54 *q = (char *)t;
55
56 if (c > 0x7F) {
57 if ((c & 0xE0) == 0xC0) {
58 sz = 2;
59 r = c & 0x1F;
60 } else if ((c & 0xF0) == 0xE0) {
61 sz = 3;
62 r = c & 0x0F;
63 } else if ((c & 0xF8) == 0xF0) {
64 sz = 4;
65 r = c & 0x07;
66 } else if ((c & 0xFC) == 0xF8) {
67 sz = 5;
68 r = c & 0x03;
69 } else if ((c & 0xFE) == 0xFC) {
70 sz = 6;
71 r = c & 0x01;
72 } else {
73 u8error("invalid utf-8 prefix");
74 return 0xFFFFUL;
75 }
76
77 for (i = 1; i < sz; i++) {
78 if (*t == '\\')
79 c = esccon((char **)&t);
80 else
81 c = *t++;
82
83 if ((c & 0xC0) == 0x80) {
84 r = (r << 6) + (c & 0x3F);
85 } else {
86 u8error("utf-8 encoding %d bytes too short", sz - i);
87 return 0xFFFFUL;
88 }
89 }
90
91 *q = (char *)t;
92 } else {
93 r = c;
94 }
95
96 return r;
97 }
98
99 /*
100 * Create UTF-16 from unicode number.
101 * Expects s to point to two words.
102 */
103 void
104 cp2u16(long num, unsigned short *s)
105 {
106 s[0] = s[1] = 0;
107 if (num <= 0xd7ff || (num >= 0xe000 && num <= 0xffffL)) {
108 *s = (unsigned short)num;
109 } else if (num >= 0x010000L && num <= 0x10ffffL) {
110 num -= 0x010000L;
111 s[0] = (unsigned short)((num >> 10) + 0xd800);
112 s[1] = (unsigned short)((num & 0x3ff) + 0xdc00);
113 } else if (num > 0x10ffffL)
114 werror("illegal UTF-16 value");
115 }