This is Unofficial EPICS BASE Doxygen Site
yajl_encode.h File Reference
#include "yajl_buf.h"
#include "yajl_gen.h"
+ Include dependency graph for yajl_encode.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void yajl_string_encode (const yajl_print_t printer, void *ctx, const unsigned char *str, size_t length, int escape_solidus)
 
void yajl_string_decode (yajl_buf buf, const unsigned char *str, size_t length)
 
int yajl_string_validate_utf8 (const unsigned char *s, size_t len)
 

Function Documentation

void yajl_string_decode ( yajl_buf  buf,
const unsigned char *  str,
size_t  length 
)

Definition at line 116 of file yajl_encode.c.

118 {
119  size_t beg = 0;
120  size_t end = 0;
121 
122  while (end < len) {
123  if (str[end] == '\\') {
124  char utf8Buf[5];
125  const char * unescaped = "?";
126  yajl_buf_append(buf, str + beg, end - beg);
127  switch (str[++end]) {
128  case 'r': unescaped = "\r"; break;
129  case 'n': unescaped = "\n"; break;
130  case '\\': unescaped = "\\"; break;
131  case '/': unescaped = "/"; break;
132  case '"': unescaped = "\""; break;
133  case 'f': unescaped = "\f"; break;
134  case 'b': unescaped = "\b"; break;
135  case 't': unescaped = "\t"; break;
136  case 'u': {
137  unsigned int codepoint = 0;
138  hexToDigit(&codepoint, str + ++end);
139  end+=3;
140  /* check if this is a surrogate */
141  if ((codepoint & 0xFC00) == 0xD800) {
142  end++;
143  if (str[end] == '\\' && str[end + 1] == 'u') {
144  unsigned int surrogate = 0;
145  hexToDigit(&surrogate, str + end + 2);
146  codepoint =
147  (((codepoint & 0x3F) << 10) |
148  ((((codepoint >> 6) & 0xF) + 1) << 16) |
149  (surrogate & 0x3FF));
150  end += 5;
151  } else {
152  unescaped = "?";
153  break;
154  }
155  }
156 
157  Utf32toUtf8(codepoint, utf8Buf);
158  unescaped = utf8Buf;
159 
160  if (codepoint == 0) {
161  yajl_buf_append(buf, unescaped, 1);
162  beg = ++end;
163  continue;
164  }
165 
166  break;
167  }
168  default:
169  assert("this should never happen" == NULL);
170  }
171  yajl_buf_append(buf, unescaped, (unsigned int)strlen(unescaped));
172  beg = ++end;
173  } else {
174  end++;
175  }
176  }
177  yajl_buf_append(buf, str + beg, end - beg);
178 }
#define assert(exp)
Declare that a condition should be true.
Definition: epicsAssert.h:70
#define NULL
Definition: catime.c:38
#define str(v)
void yajl_buf_append(yajl_buf buf, const void *data, size_t len)
Definition: yajl_buf.c:75
void yajl_string_encode ( const yajl_print_t  printer,
void *  ctx,
const unsigned char *  str,
size_t  length,
int  escape_solidus 
)

Definition at line 32 of file yajl_encode.c.

37 {
38  size_t beg = 0;
39  size_t end = 0;
40  char hexBuf[7];
41  hexBuf[0] = '\\'; hexBuf[1] = 'u'; hexBuf[2] = '0'; hexBuf[3] = '0';
42  hexBuf[6] = 0;
43 
44  while (end < len) {
45  const char * escaped = NULL;
46  switch (str[end]) {
47  case '\r': escaped = "\\r"; break;
48  case '\n': escaped = "\\n"; break;
49  case '\\': escaped = "\\\\"; break;
50  /* it is not required to escape a solidus in JSON:
51  * read sec. 2.5: http://www.ietf.org/rfc/rfc4627.txt
52  * specifically, this production from the grammar:
53  * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
54  */
55  case '/': if (escape_solidus) escaped = "\\/"; break;
56  case '"': escaped = "\\\""; break;
57  case '\f': escaped = "\\f"; break;
58  case '\b': escaped = "\\b"; break;
59  case '\t': escaped = "\\t"; break;
60  default:
61  if ((unsigned char) str[end] < 32) {
62  CharToHex(str[end], hexBuf + 4);
63  escaped = hexBuf;
64  }
65  break;
66  }
67  if (escaped != NULL) {
68  print(ctx, (const char *) (str + beg), end - beg);
69  print(ctx, escaped, (unsigned int)strlen(escaped));
70  beg = ++end;
71  } else {
72  ++end;
73  }
74  }
75  print(ctx, (const char *) (str + beg), end - beg);
76 }
#define NULL
Definition: catime.c:38
#define str(v)
int yajl_string_validate_utf8 ( const unsigned char *  s,
size_t  len 
)

Definition at line 182 of file yajl_encode.c.

183 {
184  if (!len) return 1;
185  if (!s) return 0;
186 
187  while (len--) {
188  /* single byte */
189  if (*s <= 0x7f) {
190  /* noop */
191  }
192  /* two byte */
193  else if ((*s >> 5) == 0x6) {
194  ADV_PTR;
195  if (!((*s >> 6) == 0x2)) return 0;
196  }
197  /* three byte */
198  else if ((*s >> 4) == 0x0e) {
199  ADV_PTR;
200  if (!((*s >> 6) == 0x2)) return 0;
201  ADV_PTR;
202  if (!((*s >> 6) == 0x2)) return 0;
203  }
204  /* four byte */
205  else if ((*s >> 3) == 0x1e) {
206  ADV_PTR;
207  if (!((*s >> 6) == 0x2)) return 0;
208  ADV_PTR;
209  if (!((*s >> 6) == 0x2)) return 0;
210  ADV_PTR;
211  if (!((*s >> 6) == 0x2)) return 0;
212  } else {
213  return 0;
214  }
215 
216  s++;
217  }
218 
219  return 1;
220 }
#define ADV_PTR
Definition: yajl_encode.c:180