]> granicus.if.org Git - imagemagick/blob - magick/token-private.h
minor note
[imagemagick] / magick / token-private.h
1 /*
2   Copyright 1999-2011 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.
6   obtain a copy of the License at
7
8     http://www.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)
22 extern "C" {
23 #endif
24
25 #ifndef EILSEQ
26   #define EILSEQ  ENOENT
27 #endif
28
29 #define MaxMultibyteCodes  6
30
31 typedef struct
32 {
33   int
34     code_mask,
35     code_value,
36     utf_mask,
37     utf_value;
38 } UTFInfo;
39
40 static 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
51 static inline unsigned char *ConvertLatin1ToUTF8(const unsigned char *content)
52 {
53   register const unsigned char
54     *p;
55
56   register unsigned char
57     *q;
58
59   size_t
60     length;
61
62   unsigned char
63     *utf8;
64
65   unsigned int
66     c;
67
68   length=0;
69   for (p=content; *p != '\0'; p++)
70     length+=(*p & 0x80) != 0 ? 2 : 1;
71   utf8=(unsigned char *) NULL;
72   if (~length >= 1)
73     utf8=(unsigned char *) AcquireQuantumMemory(length+1UL,sizeof(*utf8));
74   if (utf8 == (unsigned char *) NULL)
75     return((unsigned char *) NULL);
76   q=utf8;
77   for (p=content; *p != '\0'; p++)
78   {
79     c=(*p);
80     if ((c & 0x80) == 0)
81       *q++=c;
82     else
83       {
84         *q++=0xc0 | ((c >> 6) & 0x3f);
85         *q++=0x80 | (c & 0x3f);
86       }
87   }
88   *q='\0';
89   return(utf8);
90 }
91
92 static inline int GetNextUTFCode(const char *text,unsigned int *octets)
93 {
94   int
95     code;
96
97   register ssize_t
98     i;
99
100   register int
101     c,
102     unicode;
103
104   *octets=1;
105   if (text == (const char *) NULL)
106     {
107       errno=EINVAL;
108       return(-1);
109     }
110   code=(int) (*text++) & 0xff;
111   unicode=code;
112   for (i=0; i < MaxMultibyteCodes; i++)
113   {
114     if ((code & utf_info[i].code_mask) == utf_info[i].code_value)
115       {
116         unicode&=utf_info[i].utf_mask;
117         if (unicode < utf_info[i].utf_value)
118           {
119             errno=EILSEQ;
120             return(-1);
121           }
122         *octets=(unsigned int) (i+1);
123         return(unicode);
124       }
125     c=(int) (*text++ ^ 0x80) & 0xff;
126     if ((c & 0xc0) != 0)
127       {
128         errno=EILSEQ;
129         return(-1);
130       }
131     unicode=(unicode << 6) | c;
132   }
133   errno=EILSEQ;
134   return(-1);
135 }
136
137 static inline int GetUTFCode(const char *text)
138 {
139   unsigned int
140     octets;
141
142   return(GetNextUTFCode(text,&octets));
143 }
144
145 static inline unsigned int GetUTFOctets(const char *text)
146 {
147   unsigned int
148     octets;
149
150   (void) GetNextUTFCode(text,&octets);
151   return(octets);
152 }
153
154 static inline MagickBooleanType IsUTFSpace(int code)
155 {
156   if (((code >= 0x0009) && (code <= 0x000d)) || (code == 0x0020) ||
157       (code == 0x0085) || (code == 0x00a0) || (code == 0x1680) ||
158       (code == 0x180e) || ((code >= 0x2000) && (code <= 0x200a)) ||
159       (code == 0x2028) || (code == 0x2029) || (code == 0x202f) ||
160       (code == 0x205f) || (code == 0x3000))
161     return(MagickTrue);
162   return(MagickFalse);
163 }
164
165 static inline MagickBooleanType IsUTFValid(int code)
166 {
167   int
168     mask;
169
170   mask=(int) 0x7fffffff;
171   if (((code & ~mask) != 0) && ((code < 0xd800) || (code > 0xdfff)) &&
172       (code != 0xfffe) && (code != 0xffff))
173     return(MagickFalse);
174   return(MagickTrue);
175 }
176
177 static inline MagickBooleanType IsUTFAscii(int code)
178 {
179   int
180     mask;
181
182   mask=(int) 0x7f;
183   if ((code & ~mask) != 0)
184     return(MagickFalse);
185   return(MagickTrue);
186 }
187
188 #if defined(__cplusplus) || defined(c_plusplus)
189 }
190 #endif
191
192 #endif