MagickCore  6.9.11-60
Convert, Edit, Or Compose Bitmap Images
token-private.h
Go to the documentation of this file.
1 /*
2  Copyright 1999-2021 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)
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
38 } UTFInfo;
39 
40 static UTFInfo
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(
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 
93 static 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 
135 static inline int GetUTFCode(const char *magick_restrict text)
136 {
137  unsigned int
138  octets;
139 
140  return(GetNextUTFCode(text,&octets));
141 }
142 
143 static 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 
152 static inline MagickBooleanType IsUTFSpace(int code)
153 {
154  if (((code >= 0x0009) && (code <= 0x000d)) || (code == 0x0020) ||
155  (code == 0x0085) || (code == 0x00a0) || (code == 0x1680) ||
156  (code == 0x180e) || ((code >= 0x2000) && (code <= 0x200a)) ||
157  (code == 0x2028) || (code == 0x2029) || (code == 0x202f) ||
158  (code == 0x205f) || (code == 0x3000))
159  return(MagickTrue);
160  return(MagickFalse);
161 }
162 
163 static inline MagickBooleanType IsUTFValid(int code)
164 {
165  int
166  mask;
167 
168  mask=(int) 0x7fffffff;
169  if (((code & ~mask) != 0) && ((code < 0xd800) || (code > 0xdfff)) &&
170  (code != 0xfffe) && (code != 0xffff))
171  return(MagickFalse);
172  return(MagickTrue);
173 }
174 
175 static inline MagickBooleanType IsUTFAscii(int code)
176 {
177  int
178  mask;
179 
180  mask=(int) 0x7f;
181  if ((code & ~mask) != 0)
182  return(MagickFalse);
183  return(MagickTrue);
184 }
185 
186 #if defined(__cplusplus) || defined(c_plusplus)
187 }
188 #endif
189 
190 #endif
#define magick_restrict
Definition: MagickCore.h:41
MagickBooleanType
Definition: magick-type.h:203
@ MagickFalse
Definition: magick-type.h:204
@ MagickTrue
Definition: magick-type.h:205
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:665
Definition: token-private.h:32
int utf_value
Definition: token-private.h:37
int utf_mask
Definition: token-private.h:36
int code_value
Definition: token-private.h:35
int code_mask
Definition: token-private.h:34
static MagickBooleanType IsUTFValid(int code)
Definition: token-private.h:163
static unsigned int GetUTFOctets(const char *magick_restrict text)
Definition: token-private.h:143
static int GetNextUTFCode(const char *magick_restrict text, unsigned int *magick_restrict octets)
Definition: token-private.h:93
static unsigned char * ConvertLatin1ToUTF8(const unsigned char *magick_restrict content)
Definition: token-private.h:51
static MagickBooleanType IsUTFAscii(int code)
Definition: token-private.h:175
static MagickBooleanType IsUTFSpace(int code)
Definition: token-private.h:152
#define MaxMultibyteCodes
Definition: token-private.h:29
#define EILSEQ
Definition: token-private.h:26
static UTFInfo utf_info[MaxMultibyteCodes]
Definition: token-private.h:41
static int GetUTFCode(const char *magick_restrict text)
Definition: token-private.h:135