On 2015-04-17 01:13, Luka Perkov wrote:
> The base code has been taken from zstream project which was
> written by Steven Barth.
> 
> Signed-off-by: Luka Perkov <l...@openwrt.org>
> CC: Steven Barth <ste...@midlink.org>
> ---
> --- /dev/null
> +++ b/b64.c
> @@ -0,0 +1,117 @@
> +/*
> + * Copyright (C) 2011 Steven Barth <ste...@midlink.org>
> + * Copyright (C) 2015 Luka Perkov <l...@openwrt.org>
> + *
> + * Permission to use, copy, modify, and/or distribute this software for any
> + * purpose with or without fee is hereby granted, provided that the above
> + * copyright notice and this permission notice appear in all copies.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
> + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
> + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
> + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
> + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
> + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
> + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
> + */
> +
> +#include <stdint.h>
> +#include <stdlib.h>
> +
> +#include "b64.h"
> +
> +static const uint8_t b64decode_tbl[] = {
> +     0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36,
> +     0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff,
> +     0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x01,
> +     0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
> +     0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
> +     0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
> +     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1a, 0x1b,
> +     0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
> +     0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
> +     0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33
> +};
> +
> +size_t b64decode(void *out, const void *in, size_t len)
> +{
> +     uint8_t *o = (uint8_t *) out;
> +     const uint8_t *data = (const uint8_t *) in;
> +     size_t lenout, i, j;
> +     uint32_t cv = 0;
> +
> +     lenout = b64_decode_size(len);
> +     if (!lenout)
> +             return 0;
> +
> +     o[--lenout] = '\0';
> +
> +     for (i = 0; i < len; i += 4) {
> +             cv = 0;
> +             for (j = 0; j < 4; j++) {
> +                     uint8_t c = data[i + j] - 43;
> +                     if (c > 79 || (c = b64decode_tbl[c]) == 0xff)
> +                             return 0;
> +
> +                     cv |= c;
> +                     if (j != 3)
> +                             cv <<= 6;
> +             }
> +
> +             o[2] = (uint8_t)(cv & 0xff);
> +             o[1] = (uint8_t)((cv >> 8) & 0xff);
> +             o[0] = (uint8_t)((cv >> 16) & 0xff);
> +             o += 3;
> +     }
> +
> +     for (i = 1; i <= 2; i++) {
> +             if (data[len - i] == '=') {
> +                     o[-i] = '\0';
> +                     lenout--;
> +             } else
> +                     break;
> +     }
I think this function should match the capabilities and return code of
b64_pton from BSD.
It should return an int (or ssize_t) instead of size_t, and return -1 on
errors. It should also be able to skip whitespaces.

> +
> +     return lenout;
> +}
> +
> +static const uint8_t b64encode_tbl[] =
> +     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
> +
> +size_t b64encode(void *out, const void *in, size_t len)
> +{
> +     uint8_t *o = (uint8_t *) out;
> +     const uint8_t *data = (const uint8_t *) in;
> +     size_t lenout, pad, i;
> +     uint32_t cv;
> +
> +     lenout = b64_encode_size(len);
> +     if (!lenout)
> +             return 0;
Why call b64_encode_size instead of just setting *o = 0 at the end of
the function?

- Felix
_______________________________________________
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel

Reply via email to