Пример #1
0
char *bsEscape(char *bs)
{
    char *copy = bsNew(bs);
    char *res  = bsNew("");

    char *c = copy;
    char *p = copy;

    while (*c != '\0') {
        if (*c == '<') {
            *c = '\0';
            bsLCat(&res, p);
            bsLCat(&res, "&lt;");
            p = c + 1;
        } else if (*c == '>') {
            *c = '\0';
            bsLCat(&res, p);
            bsLCat(&res, "&gt;");
            p = c + 1;
        }
        c++;
    }

    bsLCat(&res, p);
    bsDel(copy);
    return res;
}
Пример #2
0
// TODO: Make this less shitty.
static inline char *urldecode(char *segment) {
    char *cc   = segment;
    char *bs   = bsNew("");
    char cb[3] = "\0\0\0";
    char c[2]  = "\0\0";

    while (*cc != '\0') {
        if (*cc == '+') *cc = ' ';
        if (*cc == '%') {
            *cc = '\0';

            bsLCat(&bs, segment);

            cb[0] = *(cc + 1);
            cb[1] = *(cc + 2);
            c[0]  = (char)strtol(cb, NULL, 16);

            cc     += 2;
            segment = cc + 1;

            bsLCat(&bs, c);
        }

        cc++;
    }

    bsLCat(&bs, segment);

    return bs;
}
Пример #3
0
char *bsRandom(uint32_t len, char *suffix)
{
    char table[62] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                      'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                      'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                      'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
                      'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
                      'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                      'Y', 'Z'
                     };

    char *bs = BS_HEADER_LEN +
               malloc(sizeof(char) * BS_HEADER_LEN +
                      sizeof(char) * (len + 1));

    bsSetLen(bs, len);

    for (uint32_t i = 0; i < len; i++)
        bs[i] = table[rand() % sizeof(table)];

    if (suffix)
        bsLCat(&bs, suffix);

    return bs;
}