Back to index

lightning-sunbird  0.9+nobinonly
moz_extensions.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Peter Van der Beken.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Peter Van der Beken <peter@propagandism.org>
00024  *
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifdef IS_LITTLE_ENDIAN
00041 
00042 #define PREFIX(ident) little2_ ## ident
00043 #define BYTE_TYPE(p) LITTLE2_BYTE_TYPE(XmlGetUtf16InternalEncodingNS(), p)
00044 #define IS_NAME_CHAR_MINBPC(p) LITTLE2_IS_NAME_CHAR_MINBPC(0, p)
00045 #define IS_NMSTRT_CHAR_MINBPC(p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(0, p)
00046 
00047 #else
00048 
00049 #define PREFIX(ident) big2_ ## ident
00050 #define BYTE_TYPE(p) BIG2_BYTE_TYPE(XmlGetUtf16InternalEncodingNS(), p)
00051 #define IS_NAME_CHAR_MINBPC(p) BIG2_IS_NAME_CHAR_MINBPC(0, p)
00052 #define IS_NMSTRT_CHAR_MINBPC(p) BIG2_IS_NMSTRT_CHAR_MINBPC(0, p)
00053 
00054 #endif
00055 
00056 #define MOZ_EXPAT_VALID_QNAME       (0)
00057 #define MOZ_EXPAT_EMPTY_QNAME       (1 << 0)
00058 #define MOZ_EXPAT_INVALID_CHARACTER (1 << 1)
00059 #define MOZ_EXPAT_MALFORMED         (1 << 2)
00060 
00061 int MOZ_XMLCheckQName(const char* ptr, const char* end, int ns_aware,
00062                       const char** colon)
00063 {
00064   int result = MOZ_EXPAT_VALID_QNAME;
00065   int nmstrt = 1;
00066   *colon = 0;
00067   if (ptr == end) {
00068     return MOZ_EXPAT_EMPTY_QNAME;
00069   }
00070   do {
00071     switch (BYTE_TYPE(ptr)) {
00072     case BT_COLON:
00073       if (ns_aware) {
00074         if (*colon != 0 || nmstrt || ptr + 2 == end) {
00075           /* We already encountered a colon or this is the first or the last
00076              character so the QName is malformed. */
00077           result |= MOZ_EXPAT_MALFORMED;
00078         }
00079         *colon = ptr;
00080         nmstrt = 1;
00081       }
00082       else if (nmstrt) {
00083         /* This is the first character so the QName is malformed. */
00084         result |= MOZ_EXPAT_MALFORMED;
00085         nmstrt = 0;
00086       }
00087       break;
00088     case BT_NONASCII:
00089       if (nmstrt) {
00090         if (!IS_NMSTRT_CHAR_MINBPC(ptr)) {
00091           /* If this is a valid name character the QName is malformed, 
00092              otherwise it contains an invalid character. */
00093           result |= IS_NAME_CHAR_MINBPC(ptr) ? MOZ_EXPAT_MALFORMED :
00094                                                MOZ_EXPAT_INVALID_CHARACTER;
00095         }
00096       }
00097       else if (!IS_NAME_CHAR_MINBPC(ptr)) {
00098         result |= MOZ_EXPAT_INVALID_CHARACTER;
00099       }
00100       nmstrt = 0;
00101       break;
00102     case BT_NMSTRT:
00103     case BT_HEX:
00104       nmstrt = 0;
00105       break;
00106     case BT_DIGIT:
00107     case BT_NAME:
00108     case BT_MINUS:
00109       if (nmstrt) {
00110         result |= MOZ_EXPAT_MALFORMED;
00111         nmstrt = 0;
00112       }
00113       break;
00114     default:
00115       result |= MOZ_EXPAT_INVALID_CHARACTER;
00116       nmstrt = 0;
00117     }
00118     ptr += 2;
00119   } while (ptr != end);
00120   return result;
00121 }
00122 
00123 int MOZ_XMLIsLetter(const char* ptr)
00124 {
00125   switch (BYTE_TYPE(ptr)) {
00126   case BT_NONASCII:
00127     if (!IS_NMSTRT_CHAR_MINBPC(ptr)) {
00128       return 0;
00129     }
00130   case BT_NMSTRT:
00131   case BT_HEX:
00132     return 1;
00133   default:
00134     return 0;
00135   }
00136 }
00137 
00138 int MOZ_XMLIsNCNameChar(const char* ptr)
00139 {
00140   switch (BYTE_TYPE(ptr)) {
00141   case BT_NONASCII:
00142     if (!IS_NAME_CHAR_MINBPC(ptr)) {
00143       return 0;
00144     }
00145   case BT_NMSTRT:
00146   case BT_HEX:
00147   case BT_DIGIT:
00148   case BT_NAME:
00149   case BT_MINUS:
00150     return 1;
00151   default:
00152     return 0;
00153   }
00154 }
00155 
00156 int MOZ_XMLTranslateEntity(const char* ptr, const char* end, const char** next,
00157                            XML_Char* result)
00158 {
00159   const ENCODING* enc = XmlGetUtf16InternalEncodingNS();
00160   int tok = PREFIX(scanRef)(enc, ptr, end, next);
00161   if (tok <= XML_TOK_INVALID) {
00162     return 0;
00163   }
00164 
00165   if (tok == XML_TOK_CHAR_REF) {
00166     int n = XmlCharRefNumber(enc, ptr);
00167 
00168     // We could get away with just < 0, but better safe than sorry.
00169     if (n <= 0) {
00170       return 0;
00171     }
00172 
00173     return XmlUtf16Encode(n, (unsigned short*)result);
00174   }
00175 
00176   if (tok == XML_TOK_ENTITY_REF) {
00177     XML_Char ch = (XML_Char)XmlPredefinedEntityName(enc, ptr, *next - 2);
00178     if (!ch) {
00179       return 0;
00180     }
00181 
00182     *result = ch;
00183     return 1;
00184   }
00185 
00186   return 0;
00187 }
00188 
00189 #undef PREFIX
00190 #undef BYTE_TYPE
00191 #undef IS_NAME_CHAR_MINBPC
00192 #undef IS_NMSTRT_CHAR_MINBPC