/* scriptasylum dot com's JS encoder */ static void handle_df(const yystype *tokens, size_t start, struct decode_result *res) { char *str, *s1; size_t len, s1_len, i; unsigned char clast; char *R; if(tokens[start].type != TOK_StringLiteral) return; str = TOKEN_GET(&tokens[start], string); if(!str) return; len = strlen(str); if(!len) return; clast = str[len-1] - '0'; str[len-1] = '\0'; s1 = cli_unescape(str); s1_len = strlen(s1); for(i=0;i<s1_len;i++) { s1[i] -= clast; } R = cli_unescape(s1); free(s1); res->pos_begin = start-2; res->pos_end = start+2; res->txtbuf.data = R; res->txtbuf.pos = strlen(R); res->append = 1; }
END_TEST START_TEST (test_unescape_unicode) { char *str = cli_unescape("%u05D0"); /* unicode is converted to utf-8 representation */ fail_unless(str && !strcmp(str,"\xd7\x90"), "cli_unescape unicode aleph"); free(str); str = cli_unescape("%u00a2%u007f%u0080%u07ff%u0800%ue000"); fail_unless(str && !strcmp(str,"\xc2\xa2\x7f\xc2\x80\xdf\xbf\xe0\xa0\x80\xee\x80\x80"), "cli_unescape utf-8 test"); free(str); str = cli_unescape("%%u123%u12%u1%u%u1234"); fail_unless(str && !strcmp(str,"%%u123%u12%u1%u\xe1\x88\xb4"), "cli_unescape unicode truncated"); free(str); }
END_TEST START_TEST (test_unescape_hex) { char *str = cli_unescape("%5a"); fail_unless(str && !strcmp(str,"\x5a"), "cli_unescape hex"); free(str); str = cli_unescape("%b5%8"); fail_unless(str && !strcmp(str,"\xb5%8"), "cli_unescape truncated"); free(str); str = cli_unescape("%b5%"); fail_unless(str && !strcmp(str,"\xb5%"), "cli_unescape truncated/2"); free(str); str = cli_unescape("%00"); fail_unless(str && !strcmp(str,"\x1"), "cli_unescape %00"); free(str); }
static int handle_unescape(struct tokens *tokens, size_t start) { if(tokens->data[start].type == TOK_StringLiteral) { char *R; struct tokens new_tokens; yystype tok; R = cli_unescape(TOKEN_GET(&tokens->data[start], cstring)); tok.type = TOK_StringLiteral; TOKEN_SET(&tok, string, R); new_tokens.capacity = new_tokens.cnt = 1; new_tokens.data = &tok; if(replace_token_range(tokens, start-2, start+2, &new_tokens) < 0) return CL_EMEM; } return CL_SUCCESS; }