gsasl  2.2.1
base64.c
Go to the documentation of this file.
1 /* base64.c --- Base64 encoding/decoding functions.
2  * Copyright (C) 2002-2024 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 License along with GNU SASL Library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include <config.h>
24 #include "internal.h"
25 
26 #include "base64.h"
27 
44 int
45 gsasl_base64_to (const char *in, size_t inlen, char **out, size_t *outlen)
46 {
47  idx_t len = base64_encode_alloc (in, inlen, out);
48 
49  if (outlen)
50  *outlen = len;
51 
52  if (*out == NULL)
53  return GSASL_MALLOC_ERROR;
54 
55  return GSASL_OK;
56 }
57 
74 int
75 gsasl_base64_from (const char *in, size_t inlen, char **out, size_t *outlen)
76 {
77  idx_t ol;
78  int ok = base64_decode_alloc (in, inlen, out, &ol);
79 
80  if (!ok)
81  return GSASL_BASE64_ERROR;
82 
83  if (outlen)
84  *outlen = ol;
85 
86  if (*out == NULL)
87  return GSASL_MALLOC_ERROR;
88 
89  return GSASL_OK;
90 }
91 
92 #include "mechtools.h"
93 
110 int
111 gsasl_hex_to (const char *in, size_t inlen, char **out, size_t *outlen)
112 {
113  size_t len = 2 * inlen;
114 
115  if (outlen)
116  *outlen = len;
117 
118  *out = malloc (len + 1);
119  if (*out == NULL)
120  return GSASL_MALLOC_ERROR;
121 
122  _gsasl_hex_encode (in, inlen, *out);
123  (*out)[len] = '\0';
124 
125  return GSASL_OK;
126 }
127 
143 int
144 gsasl_hex_from (const char *in, char **out, size_t *outlen)
145 {
146  size_t inlen = strlen (in);
147  size_t l = inlen / 2;
148 
149  if (inlen % 2 != 0)
150  return GSASL_BASE64_ERROR;
151 
152  if (!_gsasl_hex_p (in))
153  return GSASL_BASE64_ERROR;
154 
155  *out = malloc (l);
156  if (!*out)
157  return GSASL_MALLOC_ERROR;
158 
159  _gsasl_hex_decode (in, *out);
160 
161  if (outlen)
162  *outlen = l;
163 
164  return GSASL_OK;
165 }
int gsasl_base64_from(const char *in, size_t inlen, char **out, size_t *outlen)
Definition: base64.c:75
int gsasl_base64_to(const char *in, size_t inlen, char **out, size_t *outlen)
Definition: base64.c:45
int gsasl_hex_from(const char *in, char **out, size_t *outlen)
Definition: base64.c:144
int gsasl_hex_to(const char *in, size_t inlen, char **out, size_t *outlen)
Definition: base64.c:111
@ GSASL_OK
Definition: gsasl.h:129
@ GSASL_BASE64_ERROR
Definition: gsasl.h:134
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:133
void _gsasl_hex_decode(const char *hexstr, char *bin)
Definition: mechtools.c:256
bool _gsasl_hex_p(const char *hexstr)
Definition: mechtools.c:268
void _gsasl_hex_encode(const char *in, size_t inlen, char *out)
Definition: mechtools.c:221