summaryrefslogtreecommitdiff
path: root/src/base64.c
diff options
context:
space:
mode:
authorJannis Hoffmann <jannis@fehcom.de>2024-07-03 15:52:39 +0200
committerJannis Hoffmann <jannis@fehcom.de>2024-07-03 15:52:39 +0200
commita6a7d6ce079cabdaf2fa502b2e2cf15e5428ac6f (patch)
treeb88cc7a8457658d67e0321718556ac807f6bccf3 /src/base64.c
parent00be7622c428f279872f84569f098ce16150f8a8 (diff)
format files
Diffstat (limited to 'src/base64.c')
-rw-r--r--src/base64.c69
1 files changed, 40 insertions, 29 deletions
diff --git a/src/base64.c b/src/base64.c
index fd38fe3..fca31c9 100644
--- a/src/base64.c
+++ b/src/base64.c
@@ -1,13 +1,13 @@
#include "base64.h"
+
#include "str.h"
-static char *b64alpha =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+static char *b64alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define B64PAD '='
/* returns 0 ok, 1 illegal, -1 problem */
-int b64decode(const unsigned char *in,int l,stralloc *out)
+int b64decode(const unsigned char *in, int l, stralloc *out)
/* stralloc *out => not null terminated */
{
int p = 0;
@@ -18,18 +18,18 @@ int b64decode(const unsigned char *in,int l,stralloc *out)
unsigned char b[3];
if (l == 0) {
- if (!stralloc_copys(out,"")) return -1;
+ if (!stralloc_copys(out, "")) return -1;
return 0;
}
- while (in[l-1] == B64PAD) {
- p ++;
+ while (in[l - 1] == B64PAD) {
+ p++;
l--;
}
n = (l + p) / 4;
i = (n * 3) - p;
- if (!stralloc_ready(out,i)) return -1;
+ if (!stralloc_ready(out, i)) return -1;
out->len = i;
s = out->s;
@@ -48,13 +48,18 @@ int b64decode(const unsigned char *in,int l,stralloc *out)
x = (x << 6) + 63;
else if (in[j] == '=')
x = (x << 6);
- else return 1;
+ else
+ return 1;
}
- s[2] = (unsigned char)(x & 255); x >>= 8;
- s[1] = (unsigned char)(x & 255); x >>= 8;
- s[0] = (unsigned char)(x & 255); x >>= 8;
- s += 3; in += 4;
+ s[2] = (unsigned char)(x & 255);
+ x >>= 8;
+ s[1] = (unsigned char)(x & 255);
+ x >>= 8;
+ s[0] = (unsigned char)(x & 255);
+ x >>= 8;
+ s += 3;
+ in += 4;
}
x = 0;
@@ -71,33 +76,35 @@ int b64decode(const unsigned char *in,int l,stralloc *out)
x = (x << 6) + 63;
else if (in[j] == '=')
x = (x << 6);
- else return 1;
+ else
+ return 1;
}
- b[2] = (unsigned char)(x & 255); x >>= 8;
- b[1] = (unsigned char)(x & 255); x >>= 8;
- b[0] = (unsigned char)(x & 255); x >>= 8;
+ b[2] = (unsigned char)(x & 255);
+ x >>= 8;
+ b[1] = (unsigned char)(x & 255);
+ x >>= 8;
+ b[0] = (unsigned char)(x & 255);
+ x >>= 8;
- for (i = 0; i < 3 - p; i++)
- s[i] = b[i];
+ for (i = 0; i < 3 - p; i++) s[i] = b[i];
return 0;
}
-int b64encode(stralloc *in,stralloc *out)
+int b64encode(stralloc *in, stralloc *out)
{
unsigned char a, b, c;
int i;
char *s;
- if (in->len == 0)
- {
- if (!stralloc_copys(out,"")) return -1;
+ if (in->len == 0) {
+ if (!stralloc_copys(out, "")) return -1;
return 0;
}
- i = in->len / 3 * 4 + 4;
- if (!stralloc_ready(out,i)) return -1;
+ i = in->len / 3 * 4 + 4;
+ if (!stralloc_ready(out, i)) return -1;
s = out->s;
for (i = 0; i < in->len; i += 3) {
@@ -106,13 +113,17 @@ int b64encode(stralloc *in,stralloc *out)
c = i + 2 < in->len ? in->s[i + 2] : 0;
*s++ = b64alpha[a >> 2];
- *s++ = b64alpha[((a & 3 ) << 4) | (b >> 4)];
+ *s++ = b64alpha[((a & 3) << 4) | (b >> 4)];
- if (i + 1 >= in->len) *s++ = B64PAD;
- else *s++ = b64alpha[((b & 0x0f) << 2) | (c >> 6)];
+ if (i + 1 >= in->len)
+ *s++ = B64PAD;
+ else
+ *s++ = b64alpha[((b & 0x0f) << 2) | (c >> 6)];
- if (i + 2 >= in->len) *s++ = B64PAD;
- else *s++ = b64alpha[c & 0x3f];
+ if (i + 2 >= in->len)
+ *s++ = B64PAD;
+ else
+ *s++ = b64alpha[c & 0x3f];
}
out->len = s - out->s;
return 0;