gsasl  2.2.2
base64.c
Go to the documentation of this file.
1 /* base64.c --- Base64 encoding/decoding functions.
2  * Copyright (C) 2002-2025 Simon Josefsson
3  *
4  * This file is part of GNU SASL Library.
5  *
6  * GNU SASL Library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GNU SASL Library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with GNU SASL Library; if not, see
18  * <https://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include <config.h>
23 #include "internal.h"
24 
25 #include "base64.h"
26 
43 int
44 gsasl_base64_to (const char *in, size_t inlen, char **out, size_t *outlen)
45 {
46  idx_t len = base64_encode_alloc (in, inlen, out);
47 
48  if (outlen)
49  *outlen = len;
50 
51  if (*out == NULL)
52  return GSASL_MALLOC_ERROR;
53 
54  return GSASL_OK;
55 }
56 
73 int
74 gsasl_base64_from (const char *in, size_t inlen, char **out, size_t *outlen)
75 {
76  idx_t ol;
77  int ok = base64_decode_alloc (in, inlen, out, &ol);
78 
79  if (!ok)
80  return GSASL_BASE64_ERROR;
81 
82  if (outlen)
83  *outlen = ol;
84 
85  if (*out == NULL)
86  return GSASL_MALLOC_ERROR;
87 
88  return GSASL_OK;
89 }
90 
91 #include "mechtools.h"
92 
109 int
110 gsasl_hex_to (const char *in, size_t inlen, char **out, size_t *outlen)
111 {
112  size_t len = 2 * inlen;
113 
114  if (outlen)
115  *outlen = len;
116 
117  *out = malloc (len + 1);
118  if (*out == NULL)
119  return GSASL_MALLOC_ERROR;
120 
121  _gsasl_hex_encode (in, inlen, *out);
122  (*out)[len] = '\0';
123 
124  return GSASL_OK;
125 }
126 
142 int
143 gsasl_hex_from (const char *in, char **out, size_t *outlen)
144 {
145  size_t inlen = strlen (in);
146  size_t l = inlen / 2;
147 
148  if (inlen % 2 != 0)
149  return GSASL_BASE64_ERROR;
150 
151  if (!_gsasl_hex_p (in))
152  return GSASL_BASE64_ERROR;
153 
154  *out = malloc (l);
155  if (!*out)
156  return GSASL_MALLOC_ERROR;
157 
158  _gsasl_hex_decode (in, *out);
159 
160  if (outlen)
161  *outlen = l;
162 
163  return GSASL_OK;
164 }
int gsasl_base64_from(const char *in, size_t inlen, char **out, size_t *outlen)
Definition: base64.c:74
int gsasl_base64_to(const char *in, size_t inlen, char **out, size_t *outlen)
Definition: base64.c:44
int gsasl_hex_from(const char *in, char **out, size_t *outlen)
Definition: base64.c:143
int gsasl_hex_to(const char *in, size_t inlen, char **out, size_t *outlen)
Definition: base64.c:110
@ GSASL_OK
Definition: gsasl.h:128
@ GSASL_BASE64_ERROR
Definition: gsasl.h:133
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:132
void _gsasl_hex_decode(const char *hexstr, char *bin)
Definition: mechtools.c:255
bool _gsasl_hex_p(const char *hexstr)
Definition: mechtools.c:267
void _gsasl_hex_encode(const char *in, size_t inlen, char *out)
Definition: mechtools.c:220