summaryrefslogtreecommitdiff
path: root/src/base64/libwebidoidc-base64.c
blob: 8eb29b88f57d8fa0024cafe1274cc31c6c0e2906 (plain)
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
#include <utilities.h>

#define _(s) dgettext (PACKAGE, s)

void init_webidoidc_base64 (void);

SCM_DEFINE (webidoidc_base64_encode_g, "base64-encode", 1, 0, 0, (SCM data),
	    "Encode @var{data} as base64. If @var{data} is a string, first encode it to UTF-8.")
{
  size_t c_size;
  uint8_t *c_data;
  if (scm_is_string (data))
    {
      return webidoidc_base64_encode_g (scm_string_to_utf8 (data));
    }
  c_size = scm_c_bytevector_length (data);
  c_data = scm_gc_malloc_pointerless (c_size, "data");
  memcpy (c_data, SCM_BYTEVECTOR_CONTENTS (data), c_size);
  return wrap_bytevector (c_size, c_data);
}

SCM_DEFINE (webidoidc_base64_decode_g, "base64-decode", 1, 0, 0, (SCM data),
	    "Decode @var{data} from base64.")
{
  size_t c_size;
  uint8_t *c_data = get_as_bytevector (data, &c_size, 1);
  SCM ret = scm_c_make_bytevector (c_size);
  memcpy (SCM_BYTEVECTOR_CONTENTS (ret), c_data, c_size);
  return ret;
}

void
init_webidoidc_base64 (void)
{
#ifndef SCM_MAGIC_SNARFER
#include "libwebidoidc-base64.x"
#endif /* not SCM_MAGIC_SNARFER */
}