34
34
#include "others.h"
35
35
#include "matcher.h"
36
36
#include "cltypes.h"
38
static int cli_hex2int(int c)
37
#include "jsparse/textbuf.h"
39
static const int hex_chars[256] = {
40
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
41
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
42
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
43
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
44
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
45
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
46
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
47
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
48
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
49
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
50
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
51
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
52
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
53
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
54
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
55
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
58
static inline int cli_hex2int(const char c)
49
if((l >= 'a') && (l <= 'f'))
52
cli_errmsg("hex2int() translation problem (%d)\n", l);
60
return hex_chars[(const unsigned char)c];
56
63
uint16_t *cli_hex2ui(const char *hex)
453
/* encodes the unicode character as utf-8 */
454
static inline size_t output_utf8(uint16_t u, unsigned char* dst)
457
*dst = 0x1; /* don't add \0, add \1 instead */
465
*dst++ = 0xc0 | (u>>6); /* 110yyyyy */
466
*dst = 0x80 | (u & 0x3f); /* 10zzzzzz */
469
/* u < 0x10000 because we only handle utf-16,
470
* values in range 0xd800 - 0xdfff aren't valid, but we don't check for
472
*dst++ = 0xe0 | (u>>12); /* 1110xxxx */
473
*dst++ = 0x80 | ((u>>6)&0x3f); /* 10yyyyyy */
474
*dst = 0x80 | (u & 0x3f); /* 10zzzzzz */
478
/* javascript-like unescape() function */
479
char *cli_unescape(const char *str)
483
const size_t len = strlen(str);
484
/* unescaped string is at most as long as original,
485
* it will usually be shorter */
486
R = cli_malloc(len + 1);
489
for(k=0;k < len;k++) {
490
unsigned char c = str[k];
492
if(k+5 >= len || str[k+1] != 'u' || !isxdigit(str[k+2]) || !isxdigit(str[k+3])
493
|| !isxdigit(str[k+4]) || !isxdigit(str[k+5])) {
494
if(k+2 < len && isxdigit(str[k+1]) && isxdigit(str[k+2])) {
495
c = (cli_hex2int(str[k+1])<<4) | cli_hex2int(str[k+2]);
499
uint16_t u = (cli_hex2int(str[k+2])<<12) | (cli_hex2int(str[k+3])<<8) |
500
(cli_hex2int(str[k+4])<<4) | cli_hex2int(str[k+5]);
501
i += output_utf8(u, (unsigned char*)&R[i]);
506
if(!c) c = 1; /* don't add \0 */
510
R = cli_realloc2(R, i);
514
/* handle javascript's escape sequences inside strings */
515
int cli_textbuffer_append_normalize(struct text_buffer *buf, const char *str, size_t len)
518
for(i=0;i < len;i++) {
520
if (c == '\\' && i+1 < len) {
546
c = (cli_hex2int(str[i+1])<<4)|cli_hex2int(str[i+2]);
551
uint16_t u = (cli_hex2int(str[i+1])<<12) | (cli_hex2int(str[i+2])<<8) |
552
(cli_hex2int(str[i+3])<<4) | cli_hex2int(str[i+4]);
553
if(textbuffer_ensure_capacity(buf, 4) == -1)
555
buf->pos += output_utf8(u, (unsigned char*)&buf->data[buf->pos]);
565
if(!c) c = 1; /* we don't insert \0 */
566
if(textbuffer_putc(buf, c) == -1)