| 1 | /* base64.c --- Base64 encoding/decoding functions. |
| 2 | * Copyright (C) 2002-2012 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 "internal.h" |
| 24 | |
| 25 | #include "base64.h" |
| 26 | |
| 27 | /** |
| 28 | * gsasl_base64_to: |
| 29 | * @in: input byte array |
| 30 | * @inlen: size of input byte array |
| 31 | * @out: pointer to newly allocated output byte array |
| 32 | * @outlen: pointer to size of newly allocated output byte array |
| 33 | * |
| 34 | * Encode data as base64. The string is zero terminated, and @outlen |
| 35 | * holds the length excluding the terminating zero. The @out buffer |
| 36 | * must be deallocated by the caller. |
| 37 | * |
| 38 | * Return value: Returns %GSASL_OK on success, or %GSASL_MALLOC_ERROR |
| 39 | * if input was too large or memory allocation fail. |
| 40 | * |
| 41 | * Since: 0.2.2 |
| 42 | **/ |
| 43 | int |
| 44 | gsasl_base64_to (const char *in, size_t inlen, char **out, size_t * outlen) |
| 45 | { |
| 46 | size_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 | |
| 57 | /** |
| 58 | * gsasl_base64_from: |
| 59 | * @in: input byte array |
| 60 | * @inlen: size of input byte array |
| 61 | * @out: pointer to newly allocated output byte array |
| 62 | * @outlen: pointer to size of newly allocated output byte array |
| 63 | * |
| 64 | * Decode Base64 data. The @out buffer must be deallocated by the |
| 65 | * caller. |
| 66 | * |
| 67 | * Return value: Returns %GSASL_OK on success, %GSASL_BASE64_ERROR if |
| 68 | * input was invalid, and %GSASL_MALLOC_ERROR on memory allocation |
| 69 | * errors. |
| 70 | * |
| 71 | * Since: 0.2.2 |
| 72 | **/ |
| 73 | int |
| 74 | gsasl_base64_from (const char *in, size_t inlen, char **out, size_t * outlen) |
| 75 | { |
| 76 | int ok = base64_decode_alloc (in, inlen, out, outlen); |
| 77 | |
| 78 | if (!ok) |
| 79 | return GSASL_BASE64_ERROR; |
| 80 | |
| 81 | if (*out == NULL) |
| 82 | return GSASL_MALLOC_ERROR; |
| 83 | |
| 84 | return GSASL_OK; |
| 85 | } |
| 86 | |