token-private.h

Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization
00003   dedicated to making software imaging solutions freely available.
00004 
00005   You may not use this file except in compliance with the License.
00006   obtain a copy of the License at
00007 
00008     http://www.imagemagick.org/script/license.php
00009 
00010   Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013   See the License for the specific language governing permissions and
00014   limitations under the License.
00015 
00016   MagickCore private token methods.
00017 */
00018 #ifndef _MAGICKCORE_TOKEN_PRIVATE_H
00019 #define _MAGICKCORE_TOKEN_PRIVATE_H
00020 
00021 #if defined(__cplusplus) || defined(c_plusplus)
00022 extern "C" {
00023 #endif
00024 
00025 #ifndef EILSEQ
00026   #define EILSEQ  ENOENT
00027 #endif
00028 
00029 #define MaxMultibyteCodes  6
00030 
00031 typedef struct
00032 {
00033   unsigned long
00034     code_mask,
00035     code_value,
00036     utf_mask,
00037     utf_value;
00038 } UTFInfo;
00039 
00040 static UTFInfo
00041   utf_info[MaxMultibyteCodes] =
00042   {
00043     { 0x80, 0x00, 0x000007f, 0x0000000 },  /* 1 byte sequence */
00044     { 0xE0, 0xC0, 0x00007ff, 0x0000080 },  /* 2 byte sequence */
00045     { 0xF0, 0xE0, 0x000ffff, 0x0000800 },  /* 3 byte sequence */
00046     { 0xF8, 0xF0, 0x01fffff, 0x0010000 },  /* 4 byte sequence */
00047     { 0xFC, 0xF8, 0x03fffff, 0x0200000 },  /* 5 byte sequence */
00048     { 0xFE, 0xFC, 0x7ffffff, 0x4000000 },  /* 6 byte sequence */
00049   };
00050 
00051 static inline unsigned long GetNextUTFCode(const char *text,size_t *octets)
00052 {
00053   register long
00054     i;
00055 
00056   register unsigned long
00057     c,
00058     unicode;
00059 
00060   unsigned long
00061     code;
00062 
00063   *octets=0;
00064   if (text == (const char *) NULL)
00065     {
00066       errno=EINVAL;
00067       return(0);
00068     }
00069   code=(unsigned long) (*text++) & 0xff;
00070   unicode=code;
00071   for (i=0; i < MaxMultibyteCodes; i++)
00072   {
00073     if ((code & utf_info[i].code_mask) == utf_info[i].code_value)
00074       {
00075         unicode&=utf_info[i].utf_mask;
00076         if (unicode < utf_info[i].utf_value)
00077           {
00078             errno=EILSEQ;
00079             return(0);
00080           }
00081         *octets=(size_t) (i+1);
00082         return(unicode);
00083       }
00084     c=(unsigned long) (*text++ ^ 0x80) & 0xff;
00085     if ((c & 0xc0) != 0)
00086       {
00087         errno=EILSEQ;
00088         return(0);
00089       }
00090     unicode=(unicode << 6) | c;
00091   }
00092   errno=EILSEQ;
00093   return(0);
00094 }
00095 
00096 static unsigned long GetUTFCode(const char *text)
00097 {
00098   size_t
00099     octets;
00100 
00101   return(GetNextUTFCode(text,&octets));
00102 }
00103 
00104 static size_t GetUTFOctets(const char *text)
00105 {
00106   size_t
00107     octets;
00108 
00109   (void) GetNextUTFCode(text,&octets);
00110   return(octets);
00111 }
00112 
00113 static inline MagickBooleanType IsUTFSpace(unsigned long code)
00114 {
00115   if (((code >= 0x0009) && (code <= 0x000d)) || (code == 0x0020) ||
00116       (code == 0x0085) || (code == 0x00a0) || (code == 0x1680) ||
00117       (code == 0x180e) || ((code >= 0x2000) && (code <= 0x200a)) ||
00118       (code == 0x2028) || (code == 0x2029) || (code == 0x202f) ||
00119       (code == 0x205f) || (code == 0x3000))
00120     return(MagickTrue);
00121   return(MagickFalse);
00122 }
00123 
00124 static inline MagickBooleanType IsUTFValid(unsigned long code)
00125 {
00126   unsigned long
00127     mask;
00128 
00129   mask=(unsigned long) 0x7fffffff;
00130   if (((code & ~mask) != 0) && ((code < 0xd800) || (code > 0xdfff)) &&
00131       (code != 0xfffe) && (code != 0xffff))
00132     return(MagickFalse);
00133   return(MagickTrue);
00134 }
00135 
00136 static inline MagickBooleanType IsUTFAscii(unsigned long code)
00137 {
00138   unsigned long
00139     mask;
00140 
00141   mask=(unsigned long) 0x7f;
00142   if ((code & ~mask) != 0)
00143     return(MagickFalse);
00144   return(MagickTrue);
00145 }
00146 
00147 #if defined(__cplusplus) || defined(c_plusplus)
00148 }
00149 #endif
00150 
00151 #endif

Generated on 19 Nov 2009 for MagickCore by  doxygen 1.6.1