Home | History | Annotate | Download | only in xmlwf
      1 /*
      2                             __  __            _
      3                          ___\ \/ /_ __   __ _| |_
      4                         / _ \\  /| '_ \ / _` | __|
      5                        |  __//  \| |_) | (_| | |_
      6                         \___/_/\_\ .__/ \__,_|\__|
      7                                  |_| XML parser
      8 
      9    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
     10    Copyright (c) 2000-2017 Expat development team
     11    Licensed under the MIT license:
     12 
     13    Permission is  hereby granted,  free of charge,  to any  person obtaining
     14    a  copy  of  this  software   and  associated  documentation  files  (the
     15    "Software"),  to  deal in  the  Software  without restriction,  including
     16    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
     17    distribute, sublicense, and/or sell copies of the Software, and to permit
     18    persons  to whom  the Software  is  furnished to  do so,  subject to  the
     19    following conditions:
     20 
     21    The above copyright  notice and this permission notice  shall be included
     22    in all copies or substantial portions of the Software.
     23 
     24    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
     25    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
     26    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
     27    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
     28    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
     29    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     30    USE OR OTHER DEALINGS IN THE SOFTWARE.
     31 */
     32 
     33 #include <string.h>
     34 #include "xmlmime.h"
     35 
     36 static const char *
     37 getTok(const char **pp)
     38 {
     39   /* inComment means one level of nesting; inComment+1 means two levels etc */
     40   enum { inAtom, inString, init, inComment };
     41   int state = init;
     42   const char *tokStart = 0;
     43   for (;;) {
     44     switch (**pp) {
     45     case '\0':
     46       if (state == inAtom)
     47         return tokStart;
     48       return 0;
     49     case ' ':
     50     case '\r':
     51     case '\t':
     52     case '\n':
     53       if (state == inAtom)
     54         return tokStart;
     55       break;
     56     case '(':
     57       if (state == inAtom)
     58         return tokStart;
     59       if (state != inString)
     60         state++;
     61       break;
     62     case ')':
     63       if (state > init)
     64         --state;
     65       else if (state != inString)
     66         return 0;
     67       break;
     68     case ';':
     69     case '/':
     70     case '=':
     71       if (state == inAtom)
     72         return tokStart;
     73       if (state == init)
     74         return (*pp)++;
     75       break;
     76     case '\\':
     77       ++*pp;
     78       if (**pp == '\0')
     79         return 0;
     80       break;
     81     case '"':
     82       switch (state) {
     83       case inString:
     84         ++*pp;
     85         return tokStart;
     86       case inAtom:
     87         return tokStart;
     88       case init:
     89         tokStart = *pp;
     90         state = inString;
     91         break;
     92       }
     93       break;
     94     default:
     95       if (state == init) {
     96         tokStart = *pp;
     97         state = inAtom;
     98       }
     99       break;
    100     }
    101     ++*pp;
    102   }
    103   /* not reached */
    104 }
    105 
    106 /* key must be lowercase ASCII */
    107 
    108 static int
    109 matchkey(const char *start, const char *end, const char *key)
    110 {
    111   if (!start)
    112     return 0;
    113   for (; start != end; start++, key++)
    114     if (*start != *key && *start != 'A' + (*key - 'a'))
    115       return 0;
    116   return *key == '\0';
    117 }
    118 
    119 void
    120 getXMLCharset(const char *buf, char *charset)
    121 {
    122   const char *next, *p;
    123 
    124   charset[0] = '\0';
    125   next = buf;
    126   p = getTok(&next);
    127   if (matchkey(p, next, "text"))
    128     strcpy(charset, "us-ascii");
    129   else if (!matchkey(p, next, "application"))
    130     return;
    131   p = getTok(&next);
    132   if (!p || *p != '/')
    133     return;
    134   p = getTok(&next);
    135 #if 0
    136   if (!matchkey(p, next, "xml") && charset[0] == '\0')
    137     return;
    138 #endif
    139   p = getTok(&next);
    140   while (p) {
    141     if (*p == ';') {
    142       p = getTok(&next);
    143       if (matchkey(p, next, "charset")) {
    144         p = getTok(&next);
    145         if (p && *p == '=') {
    146           p = getTok(&next);
    147           if (p) {
    148             char *s = charset;
    149             if (*p == '"') {
    150               while (++p != next - 1) {
    151                 if (*p == '\\')
    152                   ++p;
    153                 if (s == charset + CHARSET_MAX - 1) {
    154                   charset[0] = '\0';
    155                   break;
    156                 }
    157                 *s++ = *p;
    158               }
    159               *s++ = '\0';
    160             }
    161             else {
    162               if (next - p > CHARSET_MAX - 1)
    163                 break;
    164               while (p != next)
    165                 *s++ = *p++;
    166               *s = 0;
    167               break;
    168             }
    169           }
    170         }
    171         break;
    172       }
    173     }
    174   else
    175     p = getTok(&next);
    176   }
    177 }
    178 
    179 #ifdef TEST
    180 
    181 #include <stdio.h>
    182 
    183 int
    184 main(int argc, char *argv[])
    185 {
    186   char buf[CHARSET_MAX];
    187   if (argc <= 1)
    188     return 1;
    189   printf("%s\n", argv[1]);
    190   getXMLCharset(argv[1], buf);
    191   printf("charset=\"%s\"\n", buf);
    192   return 0;
    193 }
    194 
    195 #endif /* TEST */
    196