Hash :
fa18b5e2
Author :
Thomas de Grivel
Date :
2024-09-05T18:41:00
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
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <libkc3/kc3.h>
#include "url.h"
s_str * url_escape (const s_str *src, s_str *dest)
{
s_str *escapes;
s_tag escapes_tag;
s_ident ident;
s_str s;
sw size;
ident_init(&ident, sym_1("URL"), sym_1("escapes"));
if (! ident_get(&ident, &escapes_tag)) {
err_puts("url_escape: missing URL.escapes");
assert(! "url_escape: missing URL.escapes");
return NULL;
}
if (escapes_tag.type != TAG_STR) {
err_puts("url_escape: URL.escapes is not a Str");
assert(! "url_escape: URL.escapes is not a Str");
return NULL;
}
escapes = &escapes_tag.data.str;
if ((size = url_escape_size(src)) < 0)
return NULL;
if (! size)
return str_init_empty(dest);
if (! buf_init_alloc(&buf, size))
return NULL;
s = *src;
while (str_read_character_utf8(&s, &c) > 0) {
if (str_character_position(escapes, c) >= 0) {
if (buf_write_u8(buf, '%') < 0)
goto clean;
if (buf_write_u8_hex(buf, c) < 0)
goto clean;
}
else if (buf_write_character_utf8(buf, c) < 0)
goto clean;
}
tag_clean(&escapes_tag);
if (! buf_to_str(&buf, dest)) {
buf_clean(&buf);
return NULL;
}
return dest;
clean:
tag_clean(&escapes_tag);
return NULL;
}
sw url_escape_size (const s_str *src)
{
s_str *escapes;
s_tag escapes_tag;
s_ident ident;
sw r;
sw result = 0;
s_str s;
ident_init(&ident, sym_1("URL"), sym_1("escapes"));
if (! ident_get(&ident, &escapes_tag)) {
err_puts("url_escape_size: missing URL.escapes");
assert(! "url_escape_size: missing URL.escapes");
return NULL;
}
if (escapes_tag.type != TAG_STR) {
err_puts("url_escape_size: URL.escapes is not a Str");
assert(! "url_escape_size: URL.escapes is not a Str");
return NULL;
}
escapes = &escapes_tag.data.str;
s = *src;
while ((r = str_read_character_utf8(&s, &c)) > 0) {
if (str_character_position(escapes, c) >= 0)
result += 3;
else
result += r;
}
tag_clean(&escapes_tag);
return result;
}
s_str * url_unescape (const s_str *url, s_str *dest)
{
}