mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-06 21:44:58 +00:00
Introduce git_buf_decode_base64
Decode base64-encoded text into a git_buf
This commit is contained in:
parent
40867266bf
commit
e003f83a58
66
src/buffer.c
66
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);
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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)) {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user