File: turtle_common.c

package info (click to toggle)
raptor2 2.0.14-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 18,384 kB
  • ctags: 4,734
  • sloc: ansic: 36,918; sh: 11,996; xml: 6,686; makefile: 2,825; yacc: 2,435; perl: 1,121; lex: 1,050
file content (325 lines) | stat: -rw-r--r-- 9,790 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* -*- Mode: c; c-basic-offset: 2 -*-
 *
 * turtle_common.c - Raptor Turtle common code
 *
 * Copyright (C) 2003-2007, David Beckett http://www.dajobe.org/
 * Copyright (C) 2003-2005, University of Bristol, UK http://www.bristol.ac.uk/
 * 
 * This package is Free Software and part of Redland http://librdf.org/
 * 
 * It is licensed under the following three licenses as alternatives:
 *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
 *   2. GNU General Public License (GPL) V2 or any newer version
 *   3. Apache License, V2.0 or any newer version
 * 
 * You may not use this file except in compliance with at least one of
 * the above three licenses.
 * 
 * See LICENSE.html or LICENSE.txt at the top of this package for the
 * complete terms and further detail along with the license texts for
 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
 * 
 * 
 */

#ifdef HAVE_CONFIG_H
#include <raptor_config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

/* Raptor includes */
#include "raptor2.h"
#include "raptor_internal.h"

#include <turtle_parser.h>
#include <turtle_lexer.h>
#include <turtle_common.h>

/**
 * raptor_stringbuffer_append_turtle_string:
 * @stringbuffer: String buffer to add to
 * @text: turtle string to decode
 * @len: length of string
 * @delim: terminating delimiter for string - only ', " or &gt; are allowed
 * @error_handler: error handling function
 * @error_data: error handler data
 *
 * Append to a stringbuffer a Turtle-escaped string.
 *
 * The passed in string is handled according to the Turtle string
 * escape rules giving a UTF-8 encoded output of the Unicode codepoints.
 *
 * The Turtle escapes are \b \f \n \r \t \\
 * \uXXXX \UXXXXXXXX where X is [A-F0-9]
 *
 * Turtle 2013 allows \ with -_~.!$&\'()*+,;=/?#@%
 *
 * Return value: non-0 on failure
 **/
int
raptor_stringbuffer_append_turtle_string(raptor_stringbuffer* stringbuffer,
                                         const unsigned char *text,
                                         size_t len, int delim,
                                         raptor_simple_message_handler error_handler, 
                                         void *error_data)
{
  size_t i;
  const unsigned char *s;
  unsigned char *d;
  unsigned char *string = RAPTOR_MALLOC(unsigned char*, len + 1);
  
  if(!string)
    return -1;

  for(s = text, d = string, i = 0; i < len; s++, i++) {
    unsigned char c=*s;

    if(c == '\\' ) {
      s++; i++;
      c = *s;
      if(c == 'n')
        *d++ = '\n';
      else if(c == 'r')
        *d++ = '\r';
      else if(c == 't')
        *d++ = '\t';
      else if(c == 'b')
        *d++ = '\b';
      else if(c == 'f')
        *d++ = '\f';
      else if(c == '\\' || c == delim ||
              c == '-' || c == '_' || c == '~' || c == '.' || c == '!' ||
              c == '$' || c == '&' || c == '\'' || c == '(' || c == ')' ||
              c == '*' || c == '+' || c == ',' || c == ';' ||c == '=' ||
              c == '/' || c == '?' || c == '#' || c == '@' ||c == '%')
        *d++ = c;
      else if(c == 'u' || c == 'U') {
        size_t ulen = (c == 'u') ? 4 : 8;
        unsigned long unichar = 0;
        int n;
        int unichar_width;
        size_t ii;

        s++; i++;
        if(i+ulen > len) {
          error_handler(error_data,
                        "Turtle string error - \\%c over end of line", c);
          RAPTOR_FREE(char*, string);
          return 1;
        }

        for(ii = 0; ii < ulen; ii++) {
          char cc = s[ii];
          if(!isxdigit(RAPTOR_GOOD_CAST(char, cc))) {
            error_handler(error_data,
                          "Turtle string error - illegal hex digit %c in Unicode escape '%c%s...'",
                          cc, c, s);
            RAPTOR_FREE(char*, string);
            return 1;
          }
        }

        n = sscanf((const char*)s, ((ulen == 4) ? "%04lx" : "%08lx"), &unichar);
        if(n != 1) {
          error_handler(error_data,
                        "Turtle string error - illegal Unicode escape '%c%s...'",
                        c, s);
          RAPTOR_FREE(char*, string);
          return 1;
        }

        s+= ulen-1;
        i+= ulen-1;
        
        if(unichar > raptor_unicode_max_codepoint) {
          error_handler(error_data,
                        "Turtle string error - illegal Unicode character with code point #x%lX (max #x%lX).", 
                        unichar, raptor_unicode_max_codepoint);
          RAPTOR_FREE(char*, string);
          return 1;
        }
          
        unichar_width = raptor_unicode_utf8_string_put_char(unichar, d, 
                                                            len-(d-string));
        if(unichar_width < 0) {
          error_handler(error_data,
                        "Turtle string error - illegal Unicode character with code point #x%lX.", 
                        unichar);
          RAPTOR_FREE(char*, string);
          return 1;
        }
        d += (size_t)unichar_width;

      } else {
        /* don't handle \x where x isn't one of: \t \n \r \\ (delim) */
        error_handler(error_data,
                      "Turtle string error - illegal escape \\%c (#x%02X) in \"%s\"", 
                      c, c, text);
      }
    } else
      *d++=c;
  }
  *d='\0';

  /* calculate output string size */
  len = d-string;
  
  /* string gets owned by the stringbuffer after this */
  return raptor_stringbuffer_append_counted_string(stringbuffer, 
                                                   string, len, 0);

}


/**
 * raptor_turtle_expand_name_escapes:
 * @name: turtle name to decode
 * @len: length of name
 * @error_handler: error handling function
 * @error_data: error handler data
 *
 * Expands Turtle escapes for the given name
 *
 * The passed in string is handled according to the Turtle string
 * escape rules giving a UTF-8 encoded output of the Unicode codepoints.
 *
 * The Turtle escapes are \b \f \n \r \t \\
 * \uXXXX \UXXXXXXXX where X is [A-F0-9]
 *
 * Turtle 2013 allows \ with -_~.!$&\'()*+,;=/?#@%
 *
 * Return value: new length or 0 on failure
 **/
size_t
raptor_turtle_expand_name_escapes(unsigned char *name,
                                  size_t len,
                                  raptor_simple_message_handler error_handler, 
                                  void *error_data)
{
  size_t i;
  const unsigned char *s;
  unsigned char *d;
  
  if(!name)
    return -1;

  for(s = name, d = name, i = 0; i < len; s++, i++) {
    unsigned char c=*s;

    if(c == '\\' ) {
      s++; i++;
      c = *s;
      if(c == 'n')
        *d++ = '\n';
      else if(c == 'r')
        *d++ = '\r';
      else if(c == 't')
        *d++ = '\t';
      else if(c == 'b')
        *d++ = '\b';
      else if(c == 'f')
        *d++ = '\f';
      else if(c == '\\' ||
              c == '-' || c == '_' || c == '~' || c == '.' || c == '!' ||
              c == '$' || c == '&' || c == '\'' || c == '(' || c == ')' ||
              c == '*' || c == '+' || c == ',' || c == ';' ||c == '=' ||
              c == '/' || c == '?' || c == '#' || c == '@' ||c == '%')
        *d++ = c;
      else if(c == 'u' || c == 'U') {
        size_t ulen = (c == 'u') ? 4 : 8;
        unsigned long unichar = 0;
        int n;
        int unichar_width;
        size_t ii;

        s++; i++;
        if(i+ulen > len) {
          error_handler(error_data,
                        "Turtle string error - \\%c over end of line", c);
          return 1;
        }
        
        for(ii = 0; ii < ulen; ii++) {
          char cc = s[ii];
          if(!isxdigit(RAPTOR_GOOD_CAST(char, cc))) {
            error_handler(error_data,
                          "Turtle string error - illegal hex digit %c in Unicode escape '%c%s...'",
                          cc, c, s);
            return 1;
          }
        }

        n = sscanf((const char*)s, ((ulen == 4) ? "%04lx" : "%08lx"), &unichar);
        if(n != 1) {
          error_handler(error_data,
                        "Turtle string error - illegal Uncode escape '%c%s...'",
                        c, s);
          return 1;
        }

        s+= ulen-1;
        i+= ulen-1;
        
        if(unichar > raptor_unicode_max_codepoint) {
          error_handler(error_data,
                        "Turtle string error - illegal Unicode character with code point #x%lX (max #x%lX).", 
                        unichar, raptor_unicode_max_codepoint);
          return 1;
        }
          
        unichar_width = raptor_unicode_utf8_string_put_char(unichar, d, 
                                                            len - (d-name));
        if(unichar_width < 0) {
          error_handler(error_data,
                        "Turtle string error - illegal Unicode character with code point #x%lX.", 
                        unichar);
          return 1;
        }
        d += (size_t)unichar_width;

      } else {
        /* don't handle \x where x isn't one of: \t \n \r \\ (delim) */
        error_handler(error_data,
                      "Turtle string error - illegal escape \\%c (#x%02X) in \"%s\"", 
                      c, c, name);
      }
    } else
      *d++ = c;
  }
  *d='\0';

  /* calculate output string size */
  len = d - name;
  
  /* string gets owned by the stringbuffer after this */
  return len;
}


int
raptor_turtle_check_uri_string(unsigned char *string)
{
  unsigned char c;

  if(!string)
    return 0;
  
  while((c = *string++)) {
    if(((c <= 0x20) ||
        c == '<' || c == '>' || c == '"' || c == '{' || c == '}' || 
        c == '|' || c == '^' || c == '`' || c == '\\'))
      return 0;
  }
  return 1;
}