cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
Cristy | 7ce65e7 | 2015-12-12 18:03:16 -0500 | [diff] [blame] | 2 | Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | |
cristy | 7832dc2 | 2011-09-05 01:21:53 +0000 | [diff] [blame] | 31 | extern MagickPrivate MagickBooleanType |
| 32 | IsGlob(const char *); |
| 33 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 34 | typedef struct |
| 35 | { |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 36 | int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 37 | code_mask, |
| 38 | code_value, |
| 39 | utf_mask, |
| 40 | utf_value; |
| 41 | } UTFInfo; |
| 42 | |
| 43 | static UTFInfo |
| 44 | utf_info[MaxMultibyteCodes] = |
| 45 | { |
| 46 | { 0x80, 0x00, 0x000007f, 0x0000000 }, /* 1 byte sequence */ |
| 47 | { 0xE0, 0xC0, 0x00007ff, 0x0000080 }, /* 2 byte sequence */ |
| 48 | { 0xF0, 0xE0, 0x000ffff, 0x0000800 }, /* 3 byte sequence */ |
| 49 | { 0xF8, 0xF0, 0x01fffff, 0x0010000 }, /* 4 byte sequence */ |
| 50 | { 0xFC, 0xF8, 0x03fffff, 0x0200000 }, /* 5 byte sequence */ |
| 51 | { 0xFE, 0xFC, 0x7ffffff, 0x4000000 }, /* 6 byte sequence */ |
| 52 | }; |
| 53 | |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 54 | static inline unsigned char *ConvertLatin1ToUTF8(const unsigned char *content) |
| 55 | { |
cristy | ccd8a48 | 2011-09-19 17:02:13 +0000 | [diff] [blame] | 56 | int |
| 57 | c; |
| 58 | |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 59 | register const unsigned char |
| 60 | *p; |
| 61 | |
| 62 | register unsigned char |
| 63 | *q; |
| 64 | |
| 65 | size_t |
| 66 | length; |
| 67 | |
| 68 | unsigned char |
| 69 | *utf8; |
| 70 | |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 71 | length=0; |
| 72 | for (p=content; *p != '\0'; p++) |
| 73 | length+=(*p & 0x80) != 0 ? 2 : 1; |
| 74 | utf8=(unsigned char *) NULL; |
| 75 | if (~length >= 1) |
| 76 | utf8=(unsigned char *) AcquireQuantumMemory(length+1UL,sizeof(*utf8)); |
| 77 | if (utf8 == (unsigned char *) NULL) |
| 78 | return((unsigned char *) NULL); |
| 79 | q=utf8; |
| 80 | for (p=content; *p != '\0'; p++) |
| 81 | { |
| 82 | c=(*p); |
| 83 | if ((c & 0x80) == 0) |
cristy | ccd8a48 | 2011-09-19 17:02:13 +0000 | [diff] [blame] | 84 | *q++=(unsigned char) c; |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 85 | else |
| 86 | { |
cristy | ccd8a48 | 2011-09-19 17:02:13 +0000 | [diff] [blame] | 87 | *q++=(unsigned char) (0xc0 | ((c >> 6) & 0x3f)); |
| 88 | *q++=(unsigned char) (0x80 | (c & 0x3f)); |
cristy | 2857ffc | 2010-03-27 23:44:00 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | *q='\0'; |
| 92 | return(utf8); |
| 93 | } |
| 94 | |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 95 | static inline int GetNextUTFCode(const char *text,unsigned int *octets) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 96 | { |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 97 | int |
cristy | 68b5916 | 2010-02-13 02:17:52 +0000 | [diff] [blame] | 98 | code; |
| 99 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 100 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 101 | i; |
| 102 | |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 103 | register int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 104 | c, |
| 105 | unicode; |
| 106 | |
cristy | 386ad9b | 2010-03-19 23:46:32 +0000 | [diff] [blame] | 107 | *octets=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 108 | if (text == (const char *) NULL) |
| 109 | { |
| 110 | errno=EINVAL; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 111 | return(-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 112 | } |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 113 | code=(int) (*text++) & 0xff; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 114 | unicode=code; |
| 115 | for (i=0; i < MaxMultibyteCodes; i++) |
| 116 | { |
| 117 | if ((code & utf_info[i].code_mask) == utf_info[i].code_value) |
| 118 | { |
| 119 | unicode&=utf_info[i].utf_mask; |
| 120 | if (unicode < utf_info[i].utf_value) |
| 121 | { |
| 122 | errno=EILSEQ; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 123 | return(-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 124 | } |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 125 | *octets=(unsigned int) (i+1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 126 | return(unicode); |
| 127 | } |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 128 | c=(int) (*text++ ^ 0x80) & 0xff; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 129 | if ((c & 0xc0) != 0) |
| 130 | { |
| 131 | errno=EILSEQ; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 132 | return(-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 133 | } |
| 134 | unicode=(unicode << 6) | c; |
| 135 | } |
| 136 | errno=EILSEQ; |
cristy | bd51246 | 2010-02-13 02:13:14 +0000 | [diff] [blame] | 137 | return(-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 138 | } |
| 139 | |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 140 | static inline int GetUTFCode(const char *text) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 141 | { |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 142 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 143 | octets; |
| 144 | |
| 145 | return(GetNextUTFCode(text,&octets)); |
| 146 | } |
| 147 | |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 148 | static inline unsigned int GetUTFOctets(const char *text) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 149 | { |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 150 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 151 | octets; |
| 152 | |
| 153 | (void) GetNextUTFCode(text,&octets); |
| 154 | return(octets); |
| 155 | } |
| 156 | |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 157 | static inline MagickBooleanType IsUTFSpace(int code) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 158 | { |
| 159 | if (((code >= 0x0009) && (code <= 0x000d)) || (code == 0x0020) || |
| 160 | (code == 0x0085) || (code == 0x00a0) || (code == 0x1680) || |
| 161 | (code == 0x180e) || ((code >= 0x2000) && (code <= 0x200a)) || |
| 162 | (code == 0x2028) || (code == 0x2029) || (code == 0x202f) || |
| 163 | (code == 0x205f) || (code == 0x3000)) |
| 164 | return(MagickTrue); |
| 165 | return(MagickFalse); |
| 166 | } |
| 167 | |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 168 | static inline MagickBooleanType IsUTFValid(int code) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 169 | { |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 170 | int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 171 | mask; |
| 172 | |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 173 | mask=(int) 0x7fffffff; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 174 | if (((code & ~mask) != 0) && ((code < 0xd800) || (code > 0xdfff)) && |
| 175 | (code != 0xfffe) && (code != 0xffff)) |
| 176 | return(MagickFalse); |
| 177 | return(MagickTrue); |
| 178 | } |
| 179 | |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 180 | static inline MagickBooleanType IsUTFAscii(int code) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 181 | { |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 182 | int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 183 | mask; |
| 184 | |
cristy | 5ed838e | 2010-05-31 00:05:35 +0000 | [diff] [blame] | 185 | mask=(int) 0x7f; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 186 | if ((code & ~mask) != 0) |
| 187 | return(MagickFalse); |
| 188 | return(MagickTrue); |
| 189 | } |
| 190 | |
| 191 | #if defined(__cplusplus) || defined(c_plusplus) |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | #endif |