File: percent_decode.c

package info (click to toggle)
putty 0.83-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,216 kB
  • sloc: ansic: 148,476; python: 8,466; perl: 1,830; makefile: 128; sh: 117
file content (41 lines) | stat: -rw-r--r-- 891 bytes parent folder | download | duplicates (2)
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
/*
 * Decode %-encoding in URL style.
 */

#include <ctype.h>

#include "misc.h"

void percent_decode_bs(BinarySink *bs, ptrlen data)
{
    for (const char *p = data.ptr, *e = ptrlen_end(data); p < e; p++) {
        char c = *p;
        if (c == '%' && e-p >= 3 &&
            isxdigit((unsigned char)p[1]) &&
            isxdigit((unsigned char)p[2])) {
            char hex[3];
            hex[0] = p[1];
            hex[1] = p[2];
            hex[2] = '\0';
            put_byte(bs, strtoul(hex, NULL, 16));
            p += 2;
        } else {
            put_byte(bs, c);
        }
    }

}

void percent_decode_fp(FILE *fp, ptrlen data)
{
    stdio_sink ss;
    stdio_sink_init(&ss, fp);
    percent_decode_bs(BinarySink_UPCAST(&ss), data);
}

strbuf *percent_decode_sb(ptrlen data)
{
    strbuf *sb = strbuf_new();
    percent_decode_bs(BinarySink_UPCAST(sb), data);
    return sb;
}