summaryrefslogtreecommitdiffstats
path: root/src/raptor_escaped.c
blob: b6c61d8a8f4b900620e073be1fcd6408f388b337 (plain)
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
/* -*- Mode: c; c-basic-offset: 2 -*-
 *
 * raptor_escaped.c - Raptor escaped writing utilities
 *
 * Copyright (C) 2013, David Beckett http://www.dajobe.org/
 * 
 * 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>

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


/**
 * raptor_string_escaped_write:
 * @string: UTF-8 string to write
 * @len: length of UTF-8 string
 * @delim: Terminating delimiter character for string (such as " or >) or \0 for no escaping.
 * @flags: bit flags - see #raptor_escaped_write_bitflags
 * @iostr: #raptor_iostream to write to
 *
 * Write a UTF-8 string formatted using different escapes to a #raptor_iostream
 *
 * Supports writing escapes in the Python, N-Triples, Turtle, JSON, mKR,
 * SPARQL styles to an iostream.
 * 
 * Return value: non-0 on failure such as bad UTF-8 encoding.
 **/
int
raptor_string_escaped_write(const unsigned char *string,
                            size_t len,
                            const char delim,
                            unsigned int flags,
                            raptor_iostream *iostr)
{
  unsigned char c;
  int unichar_len;
  raptor_unichar unichar;

  if(!string)
    return 1;
  
  for(; (c=*string); string++, len--) {
    if((delim && c == delim && (delim == '\'' || delim == '"')) ||
       c == '\\') {
      raptor_iostream_write_byte('\\', iostr);
      raptor_iostream_write_byte(c, iostr);
      continue;
    }

    if(delim && c == delim) {
      raptor_iostream_counted_string_write("\\u", 2, iostr);
      raptor_iostream_hexadecimal_write(c, 4, iostr);
      continue;
    }
    
    if(flags & RAPTOR_ESCAPED_WRITE_BITFLAG_SPARQL_URI_ESCAPES) {
      /* Must escape #x00-#x20<>\"{}|^` */
      if(c <= 0x20 ||
         c == '<' || c == '>' || c == '\\' || c == '"' || 
         c == '{' || c == '}' || c == '|' || c == '^' || c == '`') {
        raptor_iostream_counted_string_write("\\u", 2, iostr);
        raptor_iostream_hexadecimal_write(c, 4, iostr);
        continue;
      } else if(c < 0x7f) {
        raptor_iostream_write_byte(c, iostr);
        continue;
      }
    }

    if(flags & RAPTOR_ESCAPED_WRITE_BITFLAG_BS_ESCAPES_TNRU) {
      if(c == 0x09) {
        raptor_iostream_counted_string_write("\\t", 2, iostr);
        continue;
      } else if(c == 0x0a) {
        raptor_iostream_counted_string_write("\\n", 2, iostr);
        continue;
      } else if(c == 0x0d) {
        raptor_iostream_counted_string_write("\\r", 2, iostr);
        continue;
      } else if(c < 0x20 || c == 0x7f) {
        raptor_iostream_counted_string_write("\\u", 2, iostr);
        raptor_iostream_hexadecimal_write(c, 4, iostr);
        continue;
      }
    }
    
    if(flags & RAPTOR_ESCAPED_WRITE_BITFLAG_BS_ESCAPES_BF) {
      if(c == 0x08) {
        /* JSON has \b for backspace */
        raptor_iostream_counted_string_write("\\b", 2, iostr);
        continue;
      } else if(c == 0x0b) {
        /* JSON has \f for formfeed */
        raptor_iostream_counted_string_write("\\f", 2, iostr);
        continue;
      }
    }

    /* Just format remaining characters */
    if(c < 0x7f) {
      raptor_iostream_write_byte(c, iostr);
      continue;
    } 
    
    /* It is unicode */    
    unichar_len = raptor_unicode_utf8_string_get_char(string, len, &unichar);
    if(unichar_len < 0 || RAPTOR_GOOD_CAST(size_t, unichar_len) > len)
      /* UTF-8 encoding had an error or ended in the middle of a string */
      return 1;

    if(flags & RAPTOR_ESCAPED_WRITE_BITFLAG_UTF8) {
      /* UTF-8 is allowed so no need to escape */
      raptor_iostream_counted_string_write(string, unichar_len, iostr);
    } else {
      if(unichar < 0x10000) {
        raptor_iostream_counted_string_write("\\u", 2, iostr);
        raptor_iostream_hexadecimal_write(RAPTOR_GOOD_CAST(unsigned int, unichar), 4, iostr);
      } else {
        raptor_iostream_counted_string_write("\\U", 2, iostr);
        raptor_iostream_hexadecimal_write(RAPTOR_GOOD_CAST(unsigned int, unichar), 8, iostr);
      }
    }
    
    unichar_len--; /* since loop does len-- */
    string += unichar_len; len -= unichar_len;

  }

  return 0;
}


/**
 * raptor_string_python_write:
 * @string: UTF-8 string to write
 * @len: length of UTF-8 string
 * @delim: Terminating delimiter character for string (such as " or >)
 * or \0 for no escaping.
 * @mode: mode 0=N-Triples mode, 1=Turtle (allow raw UTF-8), 2=Turtle long string (allow raw UTF-8), 3=JSON
 * @iostr: #raptor_iostream to write to
 *
 * Write a UTF-8 string using Python-style escapes (N-Triples, Turtle, JSON, mKR) to a #raptor_iostream
 *
 * @Deprecated: use raptor_string_escaped_write() where the features
 * requested are bits that can be individually chosen.
 * 
 * Return value: non-0 on failure such as bad UTF-8 encoding.
 **/
int
raptor_string_python_write(const unsigned char *string,
                           size_t len,
                           const char delim,
                           unsigned int mode,
                           raptor_iostream *iostr)
{
  unsigned int flags = 0;

  switch(mode) {
    case 0:
      flags = RAPTOR_ESCAPED_WRITE_NTRIPLES_LITERAL;
      break;

    case 1:
      flags = RAPTOR_ESCAPED_WRITE_TURTLE_LITERAL;
      break;

    case 2:
      flags = RAPTOR_ESCAPED_WRITE_TURTLE_LONG_LITERAL;
      break;

    case 3:
      flags = RAPTOR_ESCAPED_WRITE_JSON_LITERAL;
      break;

    default:
      return 1;
  }

  return raptor_string_escaped_write(string, len, delim, flags, iostr);
}



/**
 * raptor_term_escaped_write:
 * @term: term to write
 * @flags: bit flags - see #raptor_escaped_write_bitflags
 * @iostr: raptor iostream
 * 
 * Write a #raptor_term formatted with escapes to a #raptor_iostream
 * 
 * Return value: non-0 on failure
 **/
int
raptor_term_escaped_write(const raptor_term *term,
                          unsigned int flags,
                          raptor_iostream* iostr)
{
  const char* quotes="\"\"\"\"";

  if(!term)
    return 1;

  switch(term->type) {
    case RAPTOR_TERM_TYPE_LITERAL:
      if(flags == RAPTOR_ESCAPED_WRITE_TURTLE_LONG_LITERAL) 
        raptor_iostream_counted_string_write(quotes, 3, iostr);
      else
        raptor_iostream_write_byte('"', iostr);
      raptor_string_escaped_write(term->value.literal.string,
                                  term->value.literal.string_len,
                                  '"',
                                  flags,
                                  iostr);
      if(flags == RAPTOR_ESCAPED_WRITE_TURTLE_LONG_LITERAL) 
        raptor_iostream_counted_string_write(quotes, 3, iostr);
      else
        raptor_iostream_write_byte('"', iostr);

      if(term->value.literal.language) {
        raptor_iostream_write_byte('@', iostr);
        raptor_iostream_counted_string_write(term->value.literal.language,
                                             term->value.literal.language_len,
                                             iostr);
      }
      if(term->value.literal.datatype) {
        if(flags == RAPTOR_ESCAPED_WRITE_NTRIPLES_LITERAL)
          flags = RAPTOR_ESCAPED_WRITE_NTRIPLES_URI;
        else if(flags == RAPTOR_ESCAPED_WRITE_TURTLE_LITERAL)
          flags = RAPTOR_ESCAPED_WRITE_TURTLE_URI;

        raptor_iostream_counted_string_write("^^", 2, iostr);
        raptor_uri_escaped_write(term->value.literal.datatype, NULL,
                                 flags, iostr);
      }

      break;
      
    case RAPTOR_TERM_TYPE_BLANK:
      raptor_iostream_counted_string_write("_:", 2, iostr);

      raptor_iostream_counted_string_write(term->value.blank.string,
                                           term->value.blank.string_len,
                                           iostr);
      break;
      
    case RAPTOR_TERM_TYPE_URI:
      if(flags == RAPTOR_ESCAPED_WRITE_NTRIPLES_LITERAL)
        flags = RAPTOR_ESCAPED_WRITE_NTRIPLES_URI;
      else if(flags == RAPTOR_ESCAPED_WRITE_TURTLE_LITERAL)
        flags = RAPTOR_ESCAPED_WRITE_TURTLE_URI;

      raptor_uri_escaped_write(term->value.uri, NULL, flags, iostr);
      break;
      
    case RAPTOR_TERM_TYPE_UNKNOWN:
    default:
      raptor_log_error_formatted(term->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                                 "Triple has unsupported term type %u",
                                 term->type);
      return 1;
  }

  return 0;
}