summaryrefslogtreecommitdiff
path: root/src/base64/libwebidoidc-base64.c
blob: 51255b07167010425c5acd2e40c0bb4a6464a04a (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
  webid-oidc, implementation of the Solid specification
  Copyright (C) 2020, 2021  Vivien Kraus

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#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 */
}