Introduce git_buf_decode_base64 Decode base64-encoded text into a git_buf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
diff --git a/src/buffer.c b/src/buffer.c
index 1bee9d7..e9c420e 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -189,10 +189,10 @@ int git_buf_puts(git_buf *buf, const char *string)
return git_buf_put(buf, string, strlen(string));
}
-static const char b64str[] =
+static const char base64_encode[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-int git_buf_put_base64(git_buf *buf, const char *data, size_t len)
+int git_buf_encode_base64(git_buf *buf, const char *data, size_t len)
{
size_t extra = len % 3;
uint8_t *write, a, b, c;
@@ -207,19 +207,19 @@ int git_buf_put_base64(git_buf *buf, const char *data, size_t len)
b = *read++;
c = *read++;
- *write++ = b64str[a >> 2];
- *write++ = b64str[(a & 0x03) << 4 | b >> 4];
- *write++ = b64str[(b & 0x0f) << 2 | c >> 6];
- *write++ = b64str[c & 0x3f];
+ *write++ = base64_encode[a >> 2];
+ *write++ = base64_encode[(a & 0x03) << 4 | b >> 4];
+ *write++ = base64_encode[(b & 0x0f) << 2 | c >> 6];
+ *write++ = base64_encode[c & 0x3f];
}
if (extra > 0) {
a = *read++;
b = (extra > 1) ? *read++ : 0;
- *write++ = b64str[a >> 2];
- *write++ = b64str[(a & 0x03) << 4 | b >> 4];
- *write++ = (extra > 1) ? b64str[(b & 0x0f) << 2] : '=';
+ *write++ = base64_encode[a >> 2];
+ *write++ = base64_encode[(a & 0x03) << 4 | b >> 4];
+ *write++ = (extra > 1) ? base64_encode[(b & 0x0f) << 2] : '=';
*write++ = '=';
}
@@ -229,10 +229,56 @@ int git_buf_put_base64(git_buf *buf, const char *data, size_t len)
return 0;
}
+/* The inverse of base64_encode, offset by '+' == 43. */
+static const int8_t base64_decode[] = {
+ 62,
+ -1, -1, -1,
+ 63,
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
+ -1, -1, -1, 0, -1, -1, -1,
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
+ 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
+ -1, -1, -1, -1, -1, -1,
+ 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
+ 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
+};
+
+#define BASE64_DECODE_VALUE(c) (((c) < 43 || (c) > 122) ? -1 : base64_decode[c - 43])
+
+int git_buf_decode_base64(git_buf *buf, const char *base64, size_t len)
+{
+ size_t i;
+ int8_t a, b, c, d;
+ size_t orig_size = buf->size;
+
+ assert(len % 4 == 0);
+ ENSURE_SIZE(buf, buf->size + (len / 4 * 3) + 1);
+
+ for (i = 0; i < len; i += 4) {
+ if ((a = BASE64_DECODE_VALUE(base64[i])) < 0 ||
+ (b = BASE64_DECODE_VALUE(base64[i+1])) < 0 ||
+ (c = BASE64_DECODE_VALUE(base64[i+2])) < 0 ||
+ (d = BASE64_DECODE_VALUE(base64[i+3])) < 0) {
+ buf->size = orig_size;
+ buf->ptr[buf->size] = '\0';
+
+ giterr_set(GITERR_INVALID, "Invalid base64 input");
+ return -1;
+ }
+
+ buf->ptr[buf->size++] = ((a << 2) | (b & 0x30) >> 4);
+ buf->ptr[buf->size++] = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2);
+ buf->ptr[buf->size++] = (c & 0x03) << 6 | (d & 0x3f);
+ }
+
+ buf->ptr[buf->size] = '\0';
+ return 0;
+}
+
static const char b85str[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
-int git_buf_put_base85(git_buf *buf, const char *data, size_t len)
+int git_buf_encode_base85(git_buf *buf, const char *data, size_t len)
{
ENSURE_SIZE(buf, buf->size + (5 * ((len / 4) + !!(len % 4))) + 1);
diff --git a/src/buffer.h b/src/buffer.h
index 70d6d73..8ee4b53 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -156,10 +156,12 @@ void git_buf_rtrim(git_buf *buf);
int git_buf_cmp(const git_buf *a, const git_buf *b);
/* Write data as base64 encoded in buffer */
-int git_buf_put_base64(git_buf *buf, const char *data, size_t len);
+int git_buf_encode_base64(git_buf *buf, const char *data, size_t len);
+/* Decode the given bas64 and write the result to the buffer */
+int git_buf_decode_base64(git_buf *buf, const char *base64, size_t len);
/* Write data as "base85" encoded in buffer */
-int git_buf_put_base85(git_buf *buf, const char *data, size_t len);
+int git_buf_encode_base85(git_buf *buf, const char *data, size_t len);
/*
* Insert, remove or replace a portion of the buffer.
diff --git a/src/diff_print.c b/src/diff_print.c
index fb62a5f..43a90b3 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -352,7 +352,7 @@ static int print_binary_hunk(diff_print_info *pi, git_blob *old, git_blob *new)
else
git_buf_putc(pi->buf, (char)chunk_len - 26 + 'a' - 1);
- git_buf_put_base85(pi->buf, scan, chunk_len);
+ git_buf_encode_base85(pi->buf, scan, chunk_len);
git_buf_putc(pi->buf, '\n');
if (git_buf_oom(pi->buf)) {
diff --git a/src/transports/http.c b/src/transports/http.c
index 6c80020..ffa293e 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -98,7 +98,7 @@ static int apply_basic_credential(git_buf *buf, git_cred *cred)
if (git_buf_oom(&raw) ||
git_buf_puts(buf, "Authorization: Basic ") < 0 ||
- git_buf_put_base64(buf, git_buf_cstr(&raw), raw.size) < 0 ||
+ git_buf_encode_base64(buf, git_buf_cstr(&raw), raw.size) < 0 ||
git_buf_puts(buf, "\r\n") < 0)
goto on_error;
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index 32641cd..1e46dfa 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -101,7 +101,7 @@ static int apply_basic_credential(HINTERNET request, git_cred *cred)
if (git_buf_oom(&raw) ||
git_buf_puts(&buf, "Authorization: Basic ") < 0 ||
- git_buf_put_base64(&buf, git_buf_cstr(&raw), raw.size) < 0)
+ git_buf_encode_base64(&buf, git_buf_cstr(&raw), raw.size) < 0)
goto on_error;
if ((wide_len = git__utf8_to_16_alloc(&wide, git_buf_cstr(&buf))) < 0) {
diff --git a/tests/core/buffer.c b/tests/core/buffer.c
index 8310dea..7482dad 100644
--- a/tests/core/buffer.c
+++ b/tests/core/buffer.c
@@ -748,7 +748,7 @@ void test_core_buffer__unescape(void)
assert_unescape("", "");
}
-void test_core_buffer__base64(void)
+void test_core_buffer__encode_base64(void)
{
git_buf buf = GIT_BUF_INIT;
@@ -759,33 +759,54 @@ void test_core_buffer__base64(void)
* 0x 1d 06 21 29 1c 30
* d G h p c w
*/
- cl_git_pass(git_buf_put_base64(&buf, "this", 4));
+ cl_git_pass(git_buf_encode_base64(&buf, "this", 4));
cl_assert_equal_s("dGhpcw==", buf.ptr);
git_buf_clear(&buf);
- cl_git_pass(git_buf_put_base64(&buf, "this!", 5));
+ cl_git_pass(git_buf_encode_base64(&buf, "this!", 5));
cl_assert_equal_s("dGhpcyE=", buf.ptr);
git_buf_clear(&buf);
- cl_git_pass(git_buf_put_base64(&buf, "this!\n", 6));
+ cl_git_pass(git_buf_encode_base64(&buf, "this!\n", 6));
cl_assert_equal_s("dGhpcyEK", buf.ptr);
git_buf_free(&buf);
}
-void test_core_buffer__base85(void)
+void test_core_buffer__decode_base64(void)
{
git_buf buf = GIT_BUF_INIT;
- cl_git_pass(git_buf_put_base85(&buf, "this", 4));
+ cl_git_pass(git_buf_decode_base64(&buf, "dGhpcw==", 8));
+ cl_assert_equal_s("this", buf.ptr);
+
+ git_buf_clear(&buf);
+ cl_git_pass(git_buf_decode_base64(&buf, "dGhpcyE=", 8));
+ cl_assert_equal_s("this!", buf.ptr);
+
+ git_buf_clear(&buf);
+ cl_git_pass(git_buf_decode_base64(&buf, "dGhpcyEK", 8));
+ cl_assert_equal_s("this!\n", buf.ptr);
+
+ cl_git_fail(git_buf_decode_base64(&buf, "This is not a valid base64 string!!!", 36));
+ cl_assert_equal_s("this!\n", buf.ptr);
+
+ git_buf_free(&buf);
+}
+
+void test_core_buffer__encode_base85(void)
+{
+ git_buf buf = GIT_BUF_INIT;
+
+ cl_git_pass(git_buf_encode_base85(&buf, "this", 4));
cl_assert_equal_s("bZBXF", buf.ptr);
git_buf_clear(&buf);
- cl_git_pass(git_buf_put_base85(&buf, "two rnds", 8));
+ cl_git_pass(git_buf_encode_base85(&buf, "two rnds", 8));
cl_assert_equal_s("ba!tca&BaE", buf.ptr);
git_buf_clear(&buf);
- cl_git_pass(git_buf_put_base85(&buf, "this is base 85 encoded",
+ cl_git_pass(git_buf_encode_base85(&buf, "this is base 85 encoded",
strlen("this is base 85 encoded")));
cl_assert_equal_s("bZBXFAZc?TVqtS-AUHK3Wo~0{WMyOk", buf.ptr);
git_buf_clear(&buf);