return 0;
}
+static bool
+base64_eq (const char * a,
+ const char * b)
+{
+ for (; ; ++a, ++b)
+ {
+ while (*a == '\r' || *a == '\n')
+ ++a;
+ while (*b == '\r' || *b == '\n')
+ ++b;
+ if (*a == '\0' || *b == '\0' || *a != *b)
+ break;
+ }
+
+ return *a == *b;
+}
+
static int
test_base64 (void)
{
int i;
out = tr_base64_encode_str ("YOYO!", &len);
- check_int_eq (8, len);
- check_streq ("WU9ZTyE=", out);
- in = tr_base64_decode_str_ (out, &len);
- check_int_eq (5, len);
- check_streq ("YOYO!", in);
- tr_free (in);
- tr_free (out);
-
- out = tr_base64_encode_str_ ("YOYO!", &len);
- check_int_eq (8, len);
- check_streq ("WU9ZTyE=", out);
+ check_int_eq (strlen (out), len);
+ check (base64_eq ("WU9ZTyE=", out));
in = tr_base64_decode_str (out, &len);
check_int_eq (5, len);
check_streq ("YOYO!", in);
check_int_eq (0, len);
check_streq ("", out);
tr_free (out);
- out = tr_base64_decode_ ("", 0, &len);
- check_int_eq (0, len);
- check_streq ("", out);
- tr_free (out);
-
- out = tr_base64_encode_ ("", 0, &len);
- check_int_eq (0, len);
- check_streq ("", out);
- tr_free (out);
out = tr_base64_decode ("", 0, &len);
check_int_eq (0, len);
check_streq ("", out);
out = tr_base64_encode (NULL, 0, &len);
check_int_eq (0, len);
check (out == NULL);
- tr_free (out);
- out = tr_base64_decode_ (NULL, 0, &len);
- check_int_eq (0, len);
- check (out == NULL);
- tr_free (out);
-
- out = tr_base64_encode_ (NULL, 0, &len);
- check_int_eq (0, len);
- check (out == NULL);
- tr_free (out);
out = tr_base64_decode (NULL, 0, &len);
check_int_eq (0, len);
check (out == NULL);
- tr_free (out);
#define MAX_BUF_SIZE 1024
buf[j] = tr_rand_int_weak (256);
out = tr_base64_encode (buf, j, &len);
- check_int_eq ((j + 2) / 3 * 4, len);
- in = tr_base64_decode_ (out, len, &len);
- check_int_eq (j, len);
- check (memcmp (in, buf, len) == 0);
- tr_free (in);
- tr_free (out);
-
- out = tr_base64_encode_ (buf, j, &len);
- check_int_eq ((j + 2) / 3 * 4, len);
+ check_int_eq (strlen (out), len);
in = tr_base64_decode (out, len, &len);
check_int_eq (j, len);
check (memcmp (in, buf, len) == 0);
buf[j] = '\0';
out = tr_base64_encode_str (buf, &len);
- check_int_eq ((j + 2) / 3 * 4, len);
- in = tr_base64_decode_str_ (out, &len);
- check_int_eq (j, len);
- check_streq (in, buf);
- tr_free (in);
- tr_free (out);
-
- out = tr_base64_encode_str_ (buf, &len);
- check_int_eq ((j + 2) / 3 * 4, len);
+ check_int_eq (strlen (out), len);
in = tr_base64_decode_str (out, &len);
check_int_eq (j, len);
check_streq (in, buf);
{
if (input_length != 0)
{
- size_t ret_length;
+ size_t ret_length = 4 * ((input_length + 2) / 3);
base64_encodestate state;
- ret = tr_new (char, 4 * ((input_length + 2) / 3) + 1);
+#ifdef USE_SYSTEM_B64
+ /* Additional space is needed for newlines if we're using unpatched libb64 */
+ ret_length += ret_length / 72 + 1;
+#endif
+
+ ret = tr_new (char, ret_length + 1);
base64_init_encodestate (&state);
ret_length = base64_encode_block (input, input_length, ret, &state);
{
if (input_length != 0)
{
- size_t ret_length;
+ size_t ret_length = input_length / 4 * 3;
base64_decodestate state;
- ret = tr_new (char, input_length / 4 * 3 + 1);
+ ret = tr_new (char, ret_length + 1);
base64_init_decodestate (&state);
ret_length = base64_decode_block (input, input_length, ret, &state);