MagickCore 6.9.13
Loading...
Searching...
No Matches
token-private.h
1/*
2 Copyright 1999 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
4
5 You may not use this file except in compliance with the License. You may
6 obtain a copy of the License at
7
8 https://imagemagick.org/script/license.php
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 MagickCore private token methods.
17*/
18#ifndef MAGICKCORE_TOKEN_PRIVATE_H
19#define MAGICKCORE_TOKEN_PRIVATE_H
20
21#if defined(__cplusplus) || defined(c_plusplus)
22extern "C" {
23#endif
24
25#ifndef EILSEQ
26 #define EILSEQ ENOENT
27#endif
28
29#define MaxMultibyteCodes 6
30
31typedef struct
32{
33 int
34 code_mask,
35 code_value,
36 utf_mask,
37 utf_value;
38} UTFInfo;
39
40static UTFInfo
41 utf_info[MaxMultibyteCodes] =
42 {
43 { 0x80, 0x00, 0x000007f, 0x0000000 }, /* 1 byte sequence */
44 { 0xE0, 0xC0, 0x00007ff, 0x0000080 }, /* 2 byte sequence */
45 { 0xF0, 0xE0, 0x000ffff, 0x0000800 }, /* 3 byte sequence */
46 { 0xF8, 0xF0, 0x01fffff, 0x0010000 }, /* 4 byte sequence */
47 { 0xFC, 0xF8, 0x03fffff, 0x0200000 }, /* 5 byte sequence */
48 { 0xFE, 0xFC, 0x7ffffff, 0x4000000 }, /* 6 byte sequence */
49 };
50
51static inline unsigned char *ConvertLatin1ToUTF8(
52 const unsigned char *magick_restrict content)
53{
54 const unsigned char
55 *magick_restrict p;
56
57 unsigned char
58 *magick_restrict q;
59
60 size_t
61 length;
62
63 unsigned char
64 *utf8;
65
66 unsigned int
67 c;
68
69 length=0;
70 for (p=content; *p != '\0'; p++)
71 length+=(*p & 0x80) != 0 ? 2 : 1;
72 utf8=(unsigned char *) NULL;
73 if (~length >= 1)
74 utf8=(unsigned char *) AcquireQuantumMemory(length+1UL,sizeof(*utf8));
75 if (utf8 == (unsigned char *) NULL)
76 return((unsigned char *) NULL);
77 q=utf8;
78 for (p=content; *p != '\0'; p++)
79 {
80 c=(*p);
81 if ((c & 0x80) == 0)
82 *q++=c;
83 else
84 {
85 *q++=0xc0 | ((c >> 6) & 0x3f);
86 *q++=0x80 | (c & 0x3f);
87 }
88 }
89 *q='\0';
90 return(utf8);
91}
92
93static inline int GetNextUTFCode(const char *magick_restrict text,
94 unsigned int *magick_restrict octets)
95{
96 int
97 code;
98
99 ssize_t
100 i;
101
102 int
103 c,
104 unicode;
105
106 *octets=1;
107 if (text == (const char *) NULL)
108 {
109 errno=EINVAL;
110 return(-1);
111 }
112 code=(int) (*text++) & 0xff;
113 unicode=code;
114 for (i=0; i < MaxMultibyteCodes; i++)
115 {
116 if ((code & utf_info[i].code_mask) == utf_info[i].code_value)
117 {
118 unicode&=utf_info[i].utf_mask;
119 if (unicode < utf_info[i].utf_value)
120 break;
121 *octets=(unsigned int) (i+1);
122 return(unicode);
123 }
124 c=(int) (*text++ ^ 0x80) & 0xff;
125 if ((c & 0xc0) != 0)
126 break;
127 if (unicode > 0x10FFFF)
128 break;
129 unicode=(unicode << 6) | c;
130 }
131 errno=EILSEQ;
132 return(-1);
133}
134
135static inline int GetUTFCode(const char *magick_restrict text)
136{
137 unsigned int
138 octets;
139
140 return(GetNextUTFCode(text,&octets));
141}
142
143static inline unsigned int GetUTFOctets(const char *magick_restrict text)
144{
145 unsigned int
146 octets;
147
148 (void) GetNextUTFCode(text,&octets);
149 return(octets);
150}
151
152static inline MagickBooleanType IsNonBreakingUTFSpace(const int code)
153{
154 if (code == 0x00a0)
155 return(MagickTrue);
156 return(MagickFalse);
157}
158
159static inline MagickBooleanType IsUTFSpace(int code)
160{
161 if (((code >= 0x0009) && (code <= 0x000d)) || (code == 0x0020) ||
162 (code == 0x0085) || (code == 0x00a0) || (code == 0x1680) ||
163 (code == 0x180e) || ((code >= 0x2000) && (code <= 0x200a)) ||
164 (code == 0x2028) || (code == 0x2029) || (code == 0x202f) ||
165 (code == 0x205f) || (code == 0x3000))
166 return(MagickTrue);
167 return(MagickFalse);
168}
169
170static inline MagickBooleanType IsUTFValid(const int code)
171{
172 int
173 mask;
174
175 mask=(int) 0x7fffffff;
176 if (((code & ~mask) != 0) && ((code < 0xd800) || (code > 0xdfff)) &&
177 (code != 0xfffe) && (code != 0xffff))
178 return(MagickFalse);
179 return(MagickTrue);
180}
181
182static inline MagickBooleanType IsUTFAscii(int code)
183{
184 int
185 mask;
186
187 mask=(int) 0x7f;
188 if ((code & ~mask) != 0)
189 return(MagickFalse);
190 return(MagickTrue);
191}
192
193#if defined(__cplusplus) || defined(c_plusplus)
194}
195#endif
196
197#endif