Codebergdnkl
foot
master
1#include "base64.h"
3#include <stdlib.h>
4#include <string.h>
5#include <stdint.h>
6#include <stdbool.h>
7#include <errno.h>
9#define LOG_MODULE "base64"
10#define LOG_ENABLE_DBG 0
11#include "log.h"
12#include "debug.h"
14enum {
15 P = 1 << 6, // Padding byte (=)
16 I = 1 << 7, // Invalid byte ([^A-Za-z0-9+/=])
17};
19static const uint8_t reverse_lookup[256] = {
20 I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,
21 I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,
22 I, I, I, I, I, I, I, I, I, I, I, 62, I, I, I, 63,
23 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, I, I, I, P, I, I,
24 I, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
25 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, I, I, I, I, I,
26 I, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
27 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, I, I, I, I, I,
28 I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,
29 I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,
30 I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,
31 I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,
32 I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,
33 I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,
34 I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,
35 I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I
36};
38static const char lookup[64] = {
39 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 "abcdefghijklmnopqrstuvwxyz"
41 "0123456789+/"
42};
44char *
45base64_decode(const char *s, size_t *size)
46{
47 const size_t len = strlen(s);
48 if (unlikely(len % 4 != 0)) {
49 errno = EINVAL;
50 return NULL;
51 }
53 char *ret = malloc(len / 4 * 3 + 1);
54 if (unlikely(ret == NULL))
55 return NULL;
57 if (unlikely(size != NULL))
58 *size = len / 4 * 3;
60 for (size_t i = 0, o = 0; i < len; i += 4, o += 3) {
61 unsigned a = reverse_lookup[(unsigned char)s[i + 0]];
62 unsigned b = reverse_lookup[(unsigned char)s[i + 1]];
63 unsigned c = reverse_lookup[(unsigned char)s[i + 2]];
64 unsigned d = reverse_lookup[(unsigned char)s[i + 3]];
66 unsigned u = a | b | c | d;
67 if (unlikely(u & I))
68 goto invalid;
70 if (unlikely(u & P)) {
71 if (unlikely(i + 4 != len || (a | b) & P || (c & P && !(d & P))))
72 goto invalid;
74 if (unlikely(size != NULL)) {
75 if (c & P)
76 *size = len / 4 * 3 - 2;
77 else
78 *size = len / 4 * 3 - 1;
79 }
81 c &= 63;
82 d &= 63;
83 }
85 uint32_t v = a << 18 | b << 12 | c << 6 | d << 0;
86 char x = (v >> 16) & 0xff;
87 char y = (v >> 8) & 0xff;
88 char z = (v >> 0) & 0xff;
90 LOG_DBG("%c%c%c", x, y, z);
91 ret[o + 0] = x;
92 ret[o + 1] = y;
93 ret[o + 2] = z;
94 }
96 ret[len / 4 * 3] = '\0';
97 return ret;
99invalid:
100 free(ret);
101 errno = EINVAL;
102 return NULL;
103}
105char *
106base64_encode(const uint8_t *data, size_t size)
107{
108 xassert(size % 3 == 0);
109 if (unlikely(size % 3 != 0))
110 return NULL;
112 char *ret = malloc(size / 3 * 4 + 1);
113 if (unlikely(ret == NULL))
114 return NULL;
116 for (size_t i = 0, o = 0; i < size; i += 3, o += 4) {
117 int x = data[i + 0];
118 int y = data[i + 1];
119 int z = data[i + 2];
121 uint32_t v = x << 16 | y << 8 | z << 0;
123 unsigned a = (v >> 18) & 0x3f;
124 unsigned b = (v >> 12) & 0x3f;
125 unsigned c = (v >> 6) & 0x3f;
126 unsigned d = (v >> 0) & 0x3f;
128 char c0 = lookup[a];
129 char c1 = lookup[b];
130 char c2 = lookup[c];
131 char c3 = lookup[d];
133 ret[o + 0] = c0;
134 ret[o + 1] = c1;
135 ret[o + 2] = c2;
136 ret[o + 3] = c3;
138 LOG_DBG("base64: encode: %c%c%c%c", c0, c1, c2, c3);
139 }
141 ret[size / 3 * 4] = '\0';
142 return ret;
143}
145void
146base64_encode_final(const uint8_t *data, size_t size, char result[4])
147{
148 xassert(size > 0);
149 xassert(size < 3);
151 uint32_t v = 0;
152 if (size >= 1)
153 v |= data[0] << 16;
154 if (size >= 2)
155 v |= data[1] << 8;
157 unsigned a = (v >> 18) & 0x3f;
158 unsigned b = (v >> 12) & 0x3f;
159 unsigned c = (v >> 6) & 0x3f;
161 char c0 = lookup[a];
162 char c1 = lookup[b];
163 char c2 = size == 2 ? lookup[c] : '=';
164 char c3 = '=';
166 result[0] = c0;
167 result[1] = c1;
168 result[2] = c2;
169 result[3] = c3;
171 LOG_DBG("base64: encode: %c%c%c%c", c0, c1, c2, c3);
172}
Diagnostics5 git calls · 707 ms
CallStatusSizeTime0707 ms
discover200178 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=178 git.server.agent=git/2.56.1-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=178 git.pkt.payload.max=35
ls-refs20010 KiB193 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=193 git.response.bytes=10322 git.response.cache=EXPIRED git.response.duration_ms=193 git.protocol.version=2 git.pkt.frames=108 git.pkt.data=107 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=10322 git.pkt.payload.max=106 git.ls_refs.count=107 git.ls_refs.head=refs/heads/master
fetchtree:0 · deepen 102009.3 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=9489 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=9489 git.pkt.payload.max=9389 git.sideband.band1.bytes=9389 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=9389 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a git.pack.full=10Open pack map
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=8830 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=8830 git.pkt.payload.max=8730 git.sideband.band1.bytes=8730 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=14 git.pack.bytes=8730 git.fetch.wide.objects=14 git.fetch.wide.extra_bytes=9909 git.fetch.wide.blobs=0
fetch2001.3 KiB512 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=512 git.response.bytes=1371 git.response.cache=MISS git.response.duration_ms=512 git.fetch.wants=db697cb069a929fc755474b5718d0340ff7cd1bc git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1371 git.pkt.payload.max=1344 git.sideband.band1.bytes=1344 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://codeberg.org/dnkl/foot
- Ref
- refs/heads/master
- Commit
- c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
- cf-ray
- a5e28a272e43bd4a
- Request
- d6cbf567…
- Protocol
- v2 · sha1
- Agent
- git/2.56.1-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format