~ubuntu-branches/ubuntu/raring/sip-tester/raring

« back to all changes in this revision

Viewing changes to xp_parser.c

  • Committer: Bazaar Package Importer
  • Author(s): ARAKI Yasuhiro
  • Date: 2005-04-11 11:53:53 UTC
  • Revision ID: james.westby@ubuntu.com-20050411115353-o33qn3noyjwjgnpd
Tags: upstream-1.1rc1
ImportĀ upstreamĀ versionĀ 1.1rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This program is free software; you can redistribute it and/or modify
 
3
 *  it under the terms of the GNU General Public License as published by
 
4
 *  the Free Software Foundation; either version 2 of the License, or
 
5
 *  (at your option) any later version.
 
6
 *
 
7
 *  This program is distributed in the hope that it will be useful,
 
8
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *  GNU General Public License for more details.
 
11
 *
 
12
 *  You should have received a copy of the GNU General Public License
 
13
 *  along with this program; if not, write to the Free Software
 
14
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
 *
 
16
 *  Copyright (C) 2003 - The Authors
 
17
 *
 
18
 *  Author : Richard GAYRAUD - 04 Nov 2003
 
19
 *           From Hewlett Packard Company.
 
20
 */
 
21
 
 
22
/*
 
23
 * Mini xml parser:
 
24
 *
 
25
 * WARNING 1: Only supports printable
 
26
 * ASCII characters in xml files. '\0'
 
27
 * is not a valid character. Returned string are
 
28
 * NULL-terminated.
 
29
 *
 
30
 * WARNING 2: Does not supports multithreading. Works
 
31
 * with static buffer, no memory allocation.
 
32
 */
 
33
 
 
34
/*******************  Include files *********************/
 
35
 
 
36
#include <stdio.h>
 
37
#include <string.h>
 
38
#include <ctype.h>
 
39
 
 
40
#include <xp_parser.h>
 
41
 
 
42
/************* Constants and Global variables ***********/
 
43
 
 
44
#define XP_MAX_NAME_LEN   256
 
45
#define XP_MAX_FILE_LEN   65536
 
46
#define XP_MAX_STACK_LEN  256
 
47
 
 
48
char   xp_file     [XP_MAX_FILE_LEN + 1];
 
49
char * xp_position [XP_MAX_STACK_LEN];
 
50
int    xp_stack    = 0;
 
51
 
 
52
/****************** Internal routines ********************/
 
53
int xp_replace(char *source, char *dest, char *search, char *replace)
 
54
{
 
55
  char *position;
 
56
  char *occurances;
 
57
  int number = 0;
 
58
 
 
59
  if (!source || !dest || !search || !replace) {
 
60
    return -1;
 
61
  }
 
62
  dest[0] = '\0';
 
63
  position = source;
 
64
  occurances = strstr(position, search);
 
65
  while (occurances) {
 
66
    strncat(dest, position, occurances - position);
 
67
    strcat(dest, replace); 
 
68
    position = occurances + strlen(search);
 
69
    occurances = strstr(position, search);
 
70
    number++;
 
71
  }
 
72
  strcat(dest, position);
 
73
  return number;
 
74
}
 
75
 
 
76
char * xp_find_local_end()
 
77
{
 
78
  char * ptr = xp_position[xp_stack];
 
79
  int level = 0;
 
80
  
 
81
  while(*ptr) {
 
82
    if (*ptr == '<') {
 
83
      if ((*(ptr+1) == '!') && 
 
84
          (*(ptr+2) == '[') &&
 
85
          (strstr(ptr,"<![CDATA[") == ptr)) {
 
86
        char * cdata_end = strstr(ptr, "]]>");
 
87
        if(!cdata_end) return NULL;
 
88
        ptr = cdata_end + 3;
 
89
      } else if ((*(ptr+1) == '!') && 
 
90
          (*(ptr+2) == '-') &&
 
91
          (strstr(ptr,"<!--") == ptr)) {
 
92
        char * comment_end = strstr(ptr, "-->");
 
93
        if(!comment_end) return NULL;
 
94
        ptr = comment_end + 3;
 
95
      } else if(*(ptr+1) == '/') {
 
96
        level--;
 
97
        if(level < 0) return ptr;
 
98
      } else {
 
99
        level ++;
 
100
      }
 
101
    } else  if((*ptr == '/') && (*(ptr+1) == '>')) {
 
102
      level --;
 
103
      if(level < 0) return ptr;
 
104
    }
 
105
    ptr++;
 
106
  }
 
107
  return ptr;
 
108
}
 
109
 
 
110
/********************* Interface routines ********************/
 
111
 
 
112
int xp_set_xml_buffer_from_string(char * str)
 
113
{
 
114
  size_t len = strlen(str);
 
115
 
 
116
  if(len > XP_MAX_FILE_LEN) {
 
117
    return 0;
 
118
  }
 
119
 
 
120
  strcpy(xp_file, str);
 
121
  xp_stack = 0;
 
122
  xp_position[xp_stack] = xp_file;
 
123
  
 
124
  if(strstr(xp_position[xp_stack], "<?xml") != xp_position[xp_stack]) return 0;
 
125
  if(!strstr(xp_position[xp_stack], "?>")) return 0;
 
126
  xp_position[xp_stack] = xp_position[xp_stack] + 2;
 
127
 
 
128
  return 1;
 
129
}
 
130
 
 
131
int xp_set_xml_buffer_from_file(char * filename)
 
132
{
 
133
  FILE * f = fopen(filename, "rb");
 
134
  int index = 0;
 
135
  int c;
 
136
 
 
137
  if(!f) { return 0; }
 
138
 
 
139
  while((c = fgetc(f)) != EOF) {
 
140
    if(c == '\r') continue;
 
141
    xp_file[index++] = c;
 
142
    if(index >= XP_MAX_FILE_LEN) {
 
143
      xp_file[index++] = 0;
 
144
      xp_stack = 0;
 
145
      xp_position[xp_stack] = xp_file;
 
146
      return 0;
 
147
    }
 
148
  }
 
149
  xp_file[index++] = 0;
 
150
  fclose(f);
 
151
 
 
152
  xp_stack = 0;
 
153
  xp_position[xp_stack] = xp_file;
 
154
 
 
155
  if(strstr(xp_position[xp_stack], "<?xml") != xp_position[xp_stack]) return 0;
 
156
  if(!strstr(xp_position[xp_stack], "?>")) return 0;
 
157
  xp_position[xp_stack] = xp_position[xp_stack] + 2;
 
158
 
 
159
  return 1;
 
160
}
 
161
 
 
162
char * xp_open_element(int index)
 
163
{
 
164
  char * ptr = xp_position[xp_stack];
 
165
  int level = 0;
 
166
  static char name[XP_MAX_NAME_LEN];
 
167
 
 
168
  while(*ptr) {
 
169
    if (*ptr == '<') {
 
170
      if ((*(ptr+1) == '!') && 
 
171
          (*(ptr+2) == '[') &&
 
172
          (strstr(ptr,"<![CDATA[") == ptr)) {
 
173
        char * cdata_end = strstr(ptr, "]]>");
 
174
        if(!cdata_end) return NULL;
 
175
        ptr = cdata_end + 3;
 
176
      } else if ((*(ptr+1) == '!') && 
 
177
          (*(ptr+2) == '-') &&
 
178
          (strstr(ptr,"<!--") == ptr)) {
 
179
        char * comment_end = strstr(ptr, "-->");
 
180
        if(!comment_end) return NULL;
 
181
        ptr = comment_end + 3;
 
182
      } else if (strstr(ptr,"<!DOCTYPE") == ptr) {
 
183
        char * doctype_end = strstr(ptr, ">");
 
184
        if(!doctype_end) return NULL;
 
185
        ptr = doctype_end + 3;
 
186
      } else if(*(ptr+1) == '/') {
 
187
        level--;
 
188
        if(level < 0) return NULL;
 
189
      } else {
 
190
        if(level==0) {
 
191
          if(index) {
 
192
            index--;
 
193
          } else {
 
194
            char * end = strchr(ptr, '>');
 
195
            char * p;
 
196
            if(!end) return NULL;
 
197
 
 
198
            p = strchr(ptr, ' ');
 
199
            if(p && (p < end))  { end = p; }
 
200
            p = strchr(ptr, '\t');
 
201
            if(p && (p < end))  { end = p; }
 
202
            p = strchr(ptr, '\r');
 
203
            if(p && (p < end))  { end = p; }
 
204
            p = strchr(ptr, '\n');
 
205
            if(p && (p < end))  { end = p; }
 
206
            p = strchr(ptr, '/');
 
207
            if(p && (p < end))  { end = p; }
 
208
 
 
209
            memcpy(name, ptr + 1, end-ptr-1);
 
210
            name[end-ptr-1] = 0;
 
211
            
 
212
            xp_position[++xp_stack] = end;
 
213
            return name;
 
214
          }
 
215
        }
 
216
        level ++;
 
217
      }
 
218
    } else if((*ptr == '/') && (*(ptr+1) == '>')) {
 
219
      level --;
 
220
      if(level < 0) return NULL;
 
221
    }
 
222
    ptr++;
 
223
  }
 
224
  return NULL;
 
225
}
 
226
 
 
227
void xp_close_element()
 
228
{
 
229
  if(xp_stack) {
 
230
    xp_stack--;
 
231
  }
 
232
}
 
233
 
 
234
void xp_root()
 
235
{
 
236
  xp_stack = 0;
 
237
}
 
238
 
 
239
char * xp_get_value(char * name)
 
240
{
 
241
  int         index = 0;
 
242
  static char buffer[XP_MAX_FILE_LEN + 1]; 
 
243
  char      * ptr, *end, *check;
 
244
  
 
245
  end = strchr(xp_position[xp_stack], '>');
 
246
  if(!end) return NULL;
 
247
 
 
248
  ptr = xp_position[xp_stack];
 
249
  
 
250
  while(*ptr) {
 
251
    ptr = strstr(ptr, name);
 
252
 
 
253
    if(!ptr) return NULL;
 
254
    if(ptr > end) return NULL;
 
255
    // FIXME: potential BUG in parser: we must retrieve full word,
 
256
    // so the use of strstr as it is is not enough.
 
257
    // we should check that the retrieved word is not a piece of another one.
 
258
    check = ptr-1;
 
259
    if(check >= xp_position[xp_stack])
 
260
    {
 
261
      if((*check != '\r') && 
 
262
         (*check != '\n') && 
 
263
         (*check != '\t') && 
 
264
         (*check != ' ' )) { return NULL; }
 
265
    }
 
266
    else
 
267
      return(NULL);
 
268
 
 
269
    ptr += strlen(name);
 
270
    while((*ptr == '\r') || 
 
271
          (*ptr == '\n') || 
 
272
          (*ptr == '\t') || 
 
273
          (*ptr == ' ' )    ) { ptr ++; }
 
274
    if(*ptr != '=') continue;
 
275
    ptr ++;
 
276
    while((*ptr == '\r') || 
 
277
          (*ptr == '\n') || 
 
278
          (*ptr == '\t') || 
 
279
          (*ptr ==  ' ')    ) { ptr ++; }
 
280
    ptr++;
 
281
    if(*ptr) {
 
282
      while((*ptr) && ((*ptr!='"') || (*(ptr-1)=='\\'))) {
 
283
        buffer[index++] = *ptr++;
 
284
        if(index > XP_MAX_FILE_LEN) return NULL;
 
285
      }
 
286
      buffer[index] = 0;
 
287
      return buffer;
 
288
    }
 
289
  }
 
290
  return NULL;
 
291
}
 
292
 
 
293
char * xp_get_cdata()
 
294
{
 
295
  static char buffer[XP_MAX_FILE_LEN + 1]; 
 
296
  char      * end = xp_find_local_end();
 
297
  char      * ptr;
 
298
  
 
299
  ptr = strstr(xp_position[xp_stack],"<![CDATA[");
 
300
  if(!ptr) { return NULL; }
 
301
  ptr += 9;
 
302
  if(ptr > end) return NULL;
 
303
  end = strstr(ptr, "]]>");
 
304
  if(!end) { return NULL; }
 
305
  if((end -ptr) > XP_MAX_FILE_LEN) return NULL;
 
306
  memcpy(buffer, ptr, (end-ptr));
 
307
  buffer[end-ptr] = 0;
 
308
  return buffer;
 
309
}
 
310
 
 
311
int xp_get_content_length(char * P_buffer) 
 
312
{
 
313
  char * L_ctl_hdr;
 
314
  int    L_content_length = -1 ; 
 
315
 
 
316
  L_ctl_hdr = strstr(P_buffer, "Content-Length:");
 
317
  if(!L_ctl_hdr) L_ctl_hdr = strstr(P_buffer, "Content-length:");
 
318
  if(!L_ctl_hdr) L_ctl_hdr = strstr(P_buffer, "content-Length:");
 
319
  if(!L_ctl_hdr) L_ctl_hdr = strstr(P_buffer, "content-length:");
 
320
  if(!L_ctl_hdr) L_ctl_hdr = strstr(P_buffer, "CONTENT-LENGTH:");
 
321
 
 
322
  if( L_ctl_hdr ){
 
323
    L_ctl_hdr += 15;
 
324
    while(isspace(*L_ctl_hdr)) L_ctl_hdr++;
 
325
    sscanf(L_ctl_hdr, "%d", &L_content_length);
 
326
  } 
 
327
  // L_content_length = -1 the message does not contain content-length
 
328
  return (L_content_length); 
 
329
}
 
330