diff options
author | Colin Walters <walters@verbum.org> | 2023-05-03 08:24:21 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-03 08:24:21 -0400 |
commit | 8a2993a9d01cc358e4c2d936ca132174aabdc714 (patch) | |
tree | 5402001ab710dadfd51ba3e9071b044016030e3e /src/libotutil/zbase32.c | |
parent | dd70c9b78cc34ccc7e638eda4c2df1ddd8ee3add (diff) | |
parent | c1044a02b28982ef6dd58af98b23a40482d26487 (diff) | |
download | ostree-8a2993a9d01cc358e4c2d936ca132174aabdc714.tar.gz |
Merge pull request #2565 from cgwalters/clang-format
Add .clang-format file + tree-wide: Run `clang-format`
Diffstat (limited to 'src/libotutil/zbase32.c')
-rw-r--r-- | src/libotutil/zbase32.c | 157 |
1 files changed, 87 insertions, 70 deletions
diff --git a/src/libotutil/zbase32.c b/src/libotutil/zbase32.c index b92168b4..acd8a920 100644 --- a/src/libotutil/zbase32.c +++ b/src/libotutil/zbase32.c @@ -7,116 +7,133 @@ #include "zbase32.h" #include <math.h> -#include <string.h> -#include <stdlib.h> #include <stdio.h> /* XXX only for debug printfs */ +#include <stdlib.h> +#include <string.h> -static const char*const chars="ybndrfg8ejkmcpqxot1uwisza345h769"; +static const char *const chars = "ybndrfg8ejkmcpqxot1uwisza345h769"; /* Types from zstr */ /** * A zstr is simply an unsigned int length and a pointer to a buffer of * unsigned chars. */ -typedef struct { - size_t len; /* the length of the string (not counting the null-terminating character) */ - unsigned char* buf; /* pointer to the first byte */ +typedef struct +{ + size_t len; /* the length of the string (not counting the null-terminating character) */ + unsigned char *buf; /* pointer to the first byte */ } zstr; /** * A zstr is simply an unsigned int length and a pointer to a buffer of * const unsigned chars. */ -typedef struct { - size_t len; /* the length of the string (not counting the null-terminating character) */ - const unsigned char* buf; /* pointer to the first byte */ +typedef struct +{ + size_t len; /* the length of the string (not counting the null-terminating character) */ + const unsigned char *buf; /* pointer to the first byte */ } czstr; /* Functions from zstr */ static zstr -new_z(const size_t len) +new_z (const size_t len) { - zstr result; - result.buf = (unsigned char *)malloc(len+1); - if (result.buf == NULL) { - result.len = 0; - return result; - } - result.len = len; - result.buf[len] = '\0'; - return result; + zstr result; + result.buf = (unsigned char *)malloc (len + 1); + if (result.buf == NULL) + { + result.len = 0; + return result; + } + result.len = len; + result.buf[len] = '\0'; + return result; } /* Functions from zutil */ static size_t -divceil(size_t n, size_t d) +divceil (size_t n, size_t d) { - return n/d+((n%d)!=0); + return n / d + ((n % d) != 0); } -static zstr b2a_l_extra_Duffy(const czstr os, const size_t lengthinbits) +static zstr +b2a_l_extra_Duffy (const czstr os, const size_t lengthinbits) { - zstr result = new_z(divceil(os.len*8, 5)); /* if lengthinbits is not a multiple of 8 then this is allocating space for 0, 1, or 2 extra quintets that will be truncated at the end of this function if they are not needed */ - if (result.buf == NULL) - return result; + zstr result = new_z ( + divceil (os.len * 8, 5)); /* if lengthinbits is not a multiple of 8 then this is allocating + space for 0, 1, or 2 extra quintets that will be truncated at + the end of this function if they are not needed */ + if (result.buf == NULL) + return result; - unsigned char* resp = result.buf + result.len; /* pointer into the result buffer, initially pointing to the "one-past-the-end" quintet */ - const unsigned char* osp = os.buf + os.len; /* pointer into the os buffer, initially pointing to the "one-past-the-end" octet */ + unsigned char *resp = result.buf + result.len; /* pointer into the result buffer, initially + pointing to the "one-past-the-end" quintet */ + const unsigned char *osp = os.buf + os.len; /* pointer into the os buffer, initially pointing to + the "one-past-the-end" octet */ - /* Now this is a real live Duff's device. You gotta love it. */ - unsigned long x=0; /* to hold up to 32 bits worth of the input */ - switch ((osp - os.buf) % 5) { - case 0: - do { - x = *--osp; - *--resp = chars[x % 32]; /* The least sig 5 bits go into the final quintet. */ - x /= 32; /* ... now we have 3 bits worth in x... */ - case 4: - x |= ((unsigned long)(*--osp)) << 3; /* ... now we have 11 bits worth in x... */ - *--resp = chars[x % 32]; - x /= 32; /* ... now we have 6 bits worth in x... */ - *--resp = chars[x % 32]; - x /= 32; /* ... now we have 1 bits worth in x... */ - case 3: - x |= ((unsigned long)(*--osp)) << 1; /* The 8 bits from the 2-indexed octet. So now we have 9 bits worth in x... */ - *--resp = chars[x % 32]; - x /= 32; /* ... now we have 4 bits worth in x... */ - case 2: - x |= ((unsigned long)(*--osp)) << 4; /* The 8 bits from the 1-indexed octet. So now we have 12 bits worth in x... */ - *--resp = chars[x%32]; - x /= 32; /* ... now we have 7 bits worth in x... */ - *--resp = chars[x%32]; - x /= 32; /* ... now we have 2 bits worth in x... */ - case 1: - x |= ((unsigned long)(*--osp)) << 2; /* The 8 bits from the 0-indexed octet. So now we have 10 bits worth in x... */ - *--resp = chars[x%32]; - x /= 32; /* ... now we have 5 bits worth in x... */ - *--resp = chars[x]; - } while (osp > os.buf); - } /* switch ((osp - os.buf) % 5) */ + /* Now this is a real live Duff's device. You gotta love it. */ + unsigned long x = 0; /* to hold up to 32 bits worth of the input */ + switch ((osp - os.buf) % 5) + { + case 0: + do + { + x = *--osp; + *--resp = chars[x % 32]; /* The least sig 5 bits go into the final quintet. */ + x /= 32; /* ... now we have 3 bits worth in x... */ + case 4: + x |= ((unsigned long)(*--osp)) << 3; /* ... now we have 11 bits worth in x... */ + *--resp = chars[x % 32]; + x /= 32; /* ... now we have 6 bits worth in x... */ + *--resp = chars[x % 32]; + x /= 32; /* ... now we have 1 bits worth in x... */ + case 3: + x |= ((unsigned long)(*--osp)) << 1; /* The 8 bits from the 2-indexed octet. So now we + have 9 bits worth in x... */ + *--resp = chars[x % 32]; + x /= 32; /* ... now we have 4 bits worth in x... */ + case 2: + x |= ((unsigned long)(*--osp)) << 4; /* The 8 bits from the 1-indexed octet. So now we + have 12 bits worth in x... */ + *--resp = chars[x % 32]; + x /= 32; /* ... now we have 7 bits worth in x... */ + *--resp = chars[x % 32]; + x /= 32; /* ... now we have 2 bits worth in x... */ + case 1: + x |= ((unsigned long)(*--osp)) << 2; /* The 8 bits from the 0-indexed octet. So now we + have 10 bits worth in x... */ + *--resp = chars[x % 32]; + x /= 32; /* ... now we have 5 bits worth in x... */ + *--resp = chars[x]; + } + while (osp > os.buf); + } /* switch ((osp - os.buf) % 5) */ - /* truncate any unused trailing zero quintets */ - result.len = divceil(lengthinbits, 5); - result.buf[result.len] = '\0'; - return result; + /* truncate any unused trailing zero quintets */ + result.len = divceil (lengthinbits, 5); + result.buf[result.len] = '\0'; + return result; } -static zstr b2a_l(const czstr os, const size_t lengthinbits) +static zstr +b2a_l (const czstr os, const size_t lengthinbits) { - return b2a_l_extra_Duffy(os, lengthinbits); + return b2a_l_extra_Duffy (os, lengthinbits); } -static zstr b2a(const czstr os) +static zstr +b2a (const czstr os) { - return b2a_l(os, os.len*8); + return b2a_l (os, os.len * 8); } char * -zbase32_encode(const unsigned char *data, size_t length) +zbase32_encode (const unsigned char *data, size_t length) { - czstr input = { length, data }; - zstr output = b2a(input); - return (char *)output.buf; + czstr input = { length, data }; + zstr output = b2a (input); + return (char *)output.buf; } /** |