00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "syncml.h"
00023 #include "sml_error.h"
00024 #include "sml_error_internals.h"
00025
00026 #include "syncml_internals.h"
00027
00028 static SmlBool _smlBase64DecodeBinary(const char *input, unsigned int size, char **output, unsigned int *outsize, SmlError **error);
00029 static SmlBool _smlBase64EncodeBinary(const char *input, unsigned int size, char **output, SmlError **error);
00030
00031
00032 SmlBool smlBase64Decode(const char *input, char **output, unsigned int *outsize, SmlError **error)
00033 {
00034 smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, input, output, outsize, error);
00035 CHECK_ERROR_REF
00036 smlAssert(output);
00037 smlAssert(outsize);
00038
00039 if (!input) {
00040 *output = NULL;
00041 *outsize = 0;
00042 goto out;
00043 }
00044
00045 if (!_smlBase64DecodeBinary(input, strlen(input), output, outsize, error))
00046 goto error;
00047
00048 out:
00049 smlTrace(TRACE_EXIT, "%s", __func__);
00050 return TRUE;
00051 error:
00052 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00053 return FALSE;
00054 }
00055
00056
00057 SmlBool smlBase64DecodeBinary(const char *input, unsigned int size, char **output, unsigned int *outsize, SmlError **error)
00058 {
00059 return _smlBase64DecodeBinary(input, size, output, outsize, error);
00060 }
00061
00062
00063
00064
00065
00066 static SmlBool _smlBase64DecodeBinary(const char *input, unsigned int size, char **output, unsigned int *outsize, SmlError **error)
00067 {
00068 smlTrace(TRACE_ENTRY, "%s(%p, %i, %p, %p, %p)", __func__, input, size, output, outsize, error);
00069 CHECK_ERROR_REF
00070
00071 if (!input || !size) {
00072 *output = NULL;
00073 *outsize = 0;
00074 goto out;
00075 }
00076
00077 size_t length = 0;
00078 *output = (char *) g_base64_decode(input, &length);
00079 if (length < 1) {
00080 smlErrorSet(error, SML_ERROR_GENERIC, "Invalid base64 input");
00081 goto error;
00082 }
00083 *outsize = length;
00084
00085
00086
00087
00088 char *result = smlTryMalloc0(*outsize + 1, error);
00089 if (result == NULL)
00090 goto error;
00091 memcpy(result, *output, *outsize);
00092 g_free(*output);
00093 *output = result;
00094 *outsize = *outsize + 1;
00095 out:
00096 smlTrace(TRACE_EXIT, "%s", __func__);
00097 return TRUE;
00098 error:
00099 *output = NULL;
00100 *outsize = 0;
00101 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00102 return FALSE;
00103 }
00104
00105
00106 SmlBool smlBase64Encode(const char *input, char **output, SmlError **error)
00107 {
00108 smlTrace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, input, output, error);
00109 CHECK_ERROR_REF
00110 smlAssert(output);
00111
00112 if (!input) {
00113 *output = NULL;
00114 smlTrace(TRACE_EXIT, "%s", __func__);
00115 return TRUE;
00116 }
00117
00118 if (_smlBase64EncodeBinary(input, strlen(input), output, error)) {
00119 smlTrace(TRACE_EXIT, "%s", __func__);
00120 return TRUE;
00121 }
00122
00123 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00124 return FALSE;
00125 }
00126
00127
00128 SmlBool smlBase64EncodeBinary(const char *input, unsigned int size, char **output, SmlError **error)
00129 {
00130 return _smlBase64EncodeBinary(input, size, output, error);
00131 }
00132
00133
00134
00135
00136
00137 static SmlBool _smlBase64EncodeBinary(const char *input, unsigned int size, char **output, SmlError **error)
00138 {
00139 smlTrace(TRACE_ENTRY, "%s(%p, %i, %p, %p)", __func__, input, size, output, error);
00140 CHECK_ERROR_REF
00141 smlAssert(output);
00142
00143
00144 if (!input) {
00145 *output = NULL;
00146 goto out;
00147 }
00148
00149 *output = g_base64_encode((const unsigned char *) input, size);
00150 if (!*output) {
00151 smlErrorSet(error, SML_ERROR_GENERIC, "Base64 encoding failed.");
00152 goto error;
00153 }
00154 out:
00155 smlTrace(TRACE_EXIT, "%s", __func__);
00156 return TRUE;
00157
00158 error:
00159 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00160 return FALSE;
00161 }