libsyncml  0.5.4
sml_base64.c
1 /*
2  * libsyncml - A syncml protocol implementation
3  * Copyright (C) 2005 Armin Bauer <armin.bauer@opensync.org>
4  * Copyright (C) 2008 Michael Bell <michael.bell@opensync.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This 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 this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "syncml.h"
23 #include "sml_error.h"
24 #include "sml_error_internals.h"
25 
26 #include "syncml_internals.h"
27 
28 static SmlBool _smlBase64DecodeBinary(const char *input, unsigned int size, char **output, unsigned int *outsize, SmlError **error);
29 static SmlBool _smlBase64EncodeBinary(const char *input, unsigned int size, char **output, SmlError **error);
30 
31 /* FIXME: DEPRECATED*/
32 SmlBool smlBase64Decode(const char *input, char **output, unsigned int *outsize, SmlError **error)
33 {
34  smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, input, output, outsize, error);
35  CHECK_ERROR_REF
36  smlAssert(output);
37  smlAssert(outsize);
38 
39  if (!input) {
40  *output = NULL;
41  *outsize = 0;
42  goto out;
43  }
44 
45  if (!_smlBase64DecodeBinary(input, strlen(input), output, outsize, error))
46  goto error;
47 
48 out:
49  smlTrace(TRACE_EXIT, "%s", __func__);
50  return TRUE;
51 error:
52  smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
53  return FALSE;
54 }
55 
56 /* FIXME: DEPRECATED*/
57 SmlBool smlBase64DecodeBinary(const char *input, unsigned int size, char **output, unsigned int *outsize, SmlError **error)
58 {
59  return _smlBase64DecodeBinary(input, size, output, outsize, error);
60 }
61 
62 /* The only reason for this internal function is
63  * to avoid compiler warnings during the normal build process
64  * even if the whole base64 API is deprecated.
65  */
66 static SmlBool _smlBase64DecodeBinary(const char *input, unsigned int size, char **output, unsigned int *outsize, SmlError **error)
67 {
68  smlTrace(TRACE_ENTRY, "%s(%p, %i, %p, %p, %p)", __func__, input, size, output, outsize, error);
69  CHECK_ERROR_REF
70 
71  if (!input || !size) {
72  *output = NULL;
73  *outsize = 0;
74  goto out;
75  }
76 
77  size_t length = 0;
78  *output = (char *) g_base64_decode(input, &length);
79  if (length < 1) {
80  smlErrorSet(error, SML_ERROR_GENERIC, "Invalid base64 input");
81  goto error;
82  }
83  *outsize = length;
84 
85  /* This is some kind of special for libsyncml.
86  * We have to add a padding NULL byte.
87  */
88  char *result = smlTryMalloc0(*outsize + 1, error);
89  if (result == NULL)
90  goto error;
91  memcpy(result, *output, *outsize);
92  g_free(*output);
93  *output = result;
94  *outsize = *outsize + 1;
95 out:
96  smlTrace(TRACE_EXIT, "%s", __func__);
97  return TRUE;
98 error:
99  *output = NULL;
100  *outsize = 0;
101  smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
102  return FALSE;
103 }
104 
105 /* FIXME: DEPRECATED*/
106 SmlBool smlBase64Encode(const char *input, char **output, SmlError **error)
107 {
108  smlTrace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, input, output, error);
109  CHECK_ERROR_REF
110  smlAssert(output);
111 
112  if (!input) {
113  *output = NULL;
114  smlTrace(TRACE_EXIT, "%s", __func__);
115  return TRUE;
116  }
117 
118  if (_smlBase64EncodeBinary(input, strlen(input), output, error)) {
119  smlTrace(TRACE_EXIT, "%s", __func__);
120  return TRUE;
121  }
122 
123  smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
124  return FALSE;
125 }
126 
127 /* FIXME: DEPRECATED*/
128 SmlBool smlBase64EncodeBinary(const char *input, unsigned int size, char **output, SmlError **error)
129 {
130  return _smlBase64EncodeBinary(input, size, output, error);
131 }
132 
133 /* The only reason for this internal function is
134  * to avoid compiler warnings during the normal build process
135  * even if the whole base64 API is deprecated.
136  */
137 static SmlBool _smlBase64EncodeBinary(const char *input, unsigned int size, char **output, SmlError **error)
138 {
139  smlTrace(TRACE_ENTRY, "%s(%p, %i, %p, %p)", __func__, input, size, output, error);
140  CHECK_ERROR_REF
141  smlAssert(output);
142 
143 
144  if (!input) {
145  *output = NULL;
146  goto out;
147  }
148 
149  *output = g_base64_encode((const unsigned char *) input, size);
150  if (!*output) {
151  smlErrorSet(error, SML_ERROR_GENERIC, "Base64 encoding failed.");
152  goto error;
153  }
154 out:
155  smlTrace(TRACE_EXIT, "%s", __func__);
156  return TRUE;
157 
158 error:
159  smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
160  return FALSE;
161 }
const char * smlErrorPrint(SmlError **error)
Returns the message of the error.
Definition: sml_error.c:299
void smlTrace(SmlTraceType type, const char *message,...)
Used for tracing the application.
Definition: sml_support.c:120
void * smlTryMalloc0(long n_bytes, SmlError **error)
Safely mallocs.
Definition: sml_support.c:335
void smlErrorSet(SmlError **error, SmlErrorType type, const char *format,...)
Sets the error.
Definition: sml_error.c:355
Represent an error.