374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
|| (c&0xFFFFF800)==0xD800
|| (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
}
return c;
}
/*
** Encode a UTF8 string for JSON. All special characters are escaped.
*/
void blob_append_json_string(Blob *pBlob, const char *zStr){
const unsigned char *z;
char *zOut;
u32 c;
int n, i, j;
z = (const unsigned char*)zStr;
n = 0;
while( (c = fossil_utf8_read(&z))!=0 ){
if( c=='\\' || c=='"' ){
n += 2;
}else if( c<' ' || c>=0x7f ){
if( c=='\n' || c=='\r' ){
n += 2;
}else{
n += 6;
}
}else{
n++;
}
}
i = blob_size(pBlob);
blob_resize(pBlob, i+n);
zOut = blob_buffer(pBlob);
z = (const unsigned char*)zStr;
while( (c = fossil_utf8_read(&z))!=0 ){
if( c=='\\' ){
zOut[i++] = '\\';
zOut[i++] = c;
}else if( c<' ' || c>=0x7f ){
zOut[i++] = '\\';
if( c=='\n' ){
|
|
>
>
|
|
<
|
>
|
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
|| (c&0xFFFFF800)==0xD800
|| (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
}
return c;
}
/*
** Encode a UTF8 string as a JSON string literal (without the surrounding
** "...") and return a pointer to the encoding. Space to hold the encoding
** is obtained from fossil_malloc() and must be freed by the caller.
*/
char *encode_json_string_literal(const char *zStr){
const unsigned char *z;
char *zOut;
u32 c;
int n, i, j;
z = (const unsigned char*)zStr;
n = 0;
while( (c = fossil_utf8_read(&z))!=0 ){
if( c=='\\' || c=='"' ){
n += 2;
}else if( c<' ' || c>=0x7f ){
if( c=='\n' || c=='\r' ){
n += 2;
}else{
n += 6;
}
}else{
n++;
}
}
zOut = fossil_malloc(n+1);
if( zOut==0 ) return 0;
z = (const unsigned char*)zStr;
i = 0;
while( (c = fossil_utf8_read(&z))!=0 ){
if( c=='\\' ){
zOut[i++] = '\\';
zOut[i++] = c;
}else if( c<' ' || c>=0x7f ){
zOut[i++] = '\\';
if( c=='\n' ){
|
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
i += 4;
}
}else{
zOut[i++] = c;
}
}
zOut[i] = 0;
}
/*
** The characters used for HTTP base64 encoding.
*/
static unsigned char zBase[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
>
|
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
i += 4;
}
}else{
zOut[i++] = c;
}
}
zOut[i] = 0;
return zOut;
}
/*
** The characters used for HTTP base64 encoding.
*/
static unsigned char zBase[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|