summaryrefslogtreecommitdiff
path: root/support/dktools/dk4binenc.c
diff options
context:
space:
mode:
Diffstat (limited to 'support/dktools/dk4binenc.c')
-rw-r--r--support/dktools/dk4binenc.c258
1 files changed, 258 insertions, 0 deletions
diff --git a/support/dktools/dk4binenc.c b/support/dktools/dk4binenc.c
new file mode 100644
index 0000000000..597dad1eb0
--- /dev/null
+++ b/support/dktools/dk4binenc.c
@@ -0,0 +1,258 @@
+/*
+ WARNING: This file was generated by dkct.
+ Changes you make here will be lost if dkct is run again!
+ You should modify the original source and run dkct on it.
+ Original source: dk4binenc.ctr
+*/
+
+/*
+Copyright (C) 2015-2017, Dirk Krause
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+* Redistributions in binary form must reproduce the above opyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+* Neither the name of the author nor the names of contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED.
+IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/** @file dk4binenc.c The dk4binenc module.
+*/
+
+
+#line 94 "dk4binenc.ctr"
+
+#include "dk4binenc.h"
+#include "dk4mem.h"
+#include "dk4memrs.h"
+#include "dk4a85e.h"
+#include "dk4maasz.h"
+#include "dk4strd.h"
+#include "dk4edstm.h"
+
+
+
+
+
+#line 106 "dk4binenc.ctr"
+
+
+
+/** Hexadecimal digits.
+*/
+static const char dk4binenc_hex_digits[] = { "0123456789abcdef" };
+
+
+
+/** Message digest type names.
+ Order must match the definition order in the enum in dk4md.h.
+*/
+static const dkChar * const dk4binenc_encoding_names[] = {
+/* 0 */
+dkT("bin"),
+
+/* 1 */
+dkT("binary"),
+
+/* 2 */
+dkT("hex"),
+
+/* 3 */
+dkT("hexa"),
+
+/* 4 */
+dkT("hexadecimal"),
+
+/* 5 */
+dkT("ascii-85"),
+
+/* 6 */
+dkT("ascii85"),
+
+/* 7 */
+dkT("a85"),
+
+NULL
+
+
+#line 129 "dk4binenc.ctr"
+};
+
+
+
+int
+dk4binenc(
+ char *dptr,
+ size_t szdptr,
+ const void *sptr,
+ size_t szsptr,
+ int enc,
+ dk4_er_t *erp
+)
+{
+ dk4_a85_enc_t a85;
+ dk4_er_t maerr;
+ const unsigned char *ucptr;
+ const char *resptr;
+ size_t ressize;
+ size_t oused;
+ size_t noused;
+ int back = 0;
+ if ((NULL != dptr) && (NULL != sptr) && (0 < szdptr) && (0 < szsptr)) {
+ switch (enc) {
+ case DK4_BINARY_TO_TEXT_ENCODING_NONE : {
+ if (szdptr >= szsptr) {
+ DK4_MEMCPY(dptr, sptr, szsptr);
+ back = 1;
+ } else {
+ dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
+ }
+ } break;
+ case DK4_BINARY_TO_TEXT_ENCODING_HEX : {
+ oused = 0;
+ ucptr = (const unsigned char *)sptr;
+ while (szsptr--) {
+ dk4error_init(&maerr);
+ noused = dk4ma_size_t_add(oused, 2, &maerr);
+ if (DK4_E_NONE == maerr.ec) {
+ dptr[oused] =
+ dk4binenc_hex_digits[(size_t)((((unsigned)(*ucptr)) >> 4) & 0x0FU)];
+ dptr[oused+1] =
+ dk4binenc_hex_digits[(size_t)(((unsigned)(*ucptr)) & 0x0FU)];
+ oused = noused;
+ } else {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ goto finished;
+ }
+ ucptr++;
+ }
+ if (oused < szdptr) {
+ dptr[oused] = '\0';
+ back = 1;
+ } else {
+ dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
+ }
+ } break;
+ case DK4_BINARY_TO_TEXT_ENCODING_A85 : {
+ oused = 0;
+ ucptr = (const unsigned char *)sptr;
+ dk4a85_enc_init(&a85, 0, erp);
+ while(szsptr--) {
+ switch (dk4a85_enc_add(&a85, *(ucptr++), erp)) {
+ case DK4_EDSTM_FINISHED : {
+ resptr = NULL; ressize = 0;
+ (void)dk4a85_enc_output(&resptr, &ressize, &a85, NULL);
+ if ((NULL != resptr) && (0 < ressize)) {
+ dk4error_init(&maerr);
+ noused = dk4ma_size_t_add(oused, ressize, &maerr);
+ if (DK4_E_NONE == maerr.ec) {
+ if (noused < szdptr) {
+ DK4_MEMCPY(&(dptr[oused]), resptr, ressize);
+ oused = noused;
+ } else {
+ dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
+ goto finished;
+ }
+ } else {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ goto finished;
+ }
+ }
+ } break;
+ case DK4_EDSTM_ERROR : {
+ dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED);
+ goto finished;
+ } break;
+ }
+ }
+ switch (dk4a85_enc_finish(&a85, erp)) {
+ case DK4_EDSTM_FINISHED : {
+ resptr = NULL; ressize = 0;
+ (void)dk4a85_enc_output(&resptr, &ressize, &a85, NULL);
+ if ((NULL != resptr) && (0 < ressize)) {
+ dk4error_init(&maerr);
+ noused = dk4ma_size_t_add(oused, ressize, &maerr);
+ if (DK4_E_NONE == maerr.ec) {
+ if (noused < szdptr) {
+ DK4_MEMCPY(&(dptr[oused]), resptr, ressize);
+ oused = noused;
+ } else {
+ dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
+ goto finished;
+ }
+ } else {
+ dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
+ goto finished;
+ }
+ }
+ } break;
+ case DK4_EDSTM_ERROR : {
+ dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED);
+ goto finished;
+ } break;
+ }
+ if (oused < szdptr) {
+ dptr[oused] = '\0';
+ back = 1;
+ } else {
+ dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
+ }
+ (void)dk4mem_reset_secure(&a85, sizeof(dk4_a85_enc_t), NULL);
+ } break;
+ default : {
+ dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
+ } break;
+ }
+ } else {
+ dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
+ }
+ finished:
+ if ((NULL != dptr) && (0 == back)) { *dptr = '\0'; }
+ return back;
+}
+
+
+
+int
+dk4binenc_choose_encoding(const dkChar *name, dk4_er_t *erp)
+{
+
+ int back = -1;
+ if (NULL != name) {
+ switch (dk4str_array_index(dk4binenc_encoding_names, name, 0)) {
+ case 0: case 1: {
+ back = DK4_BINARY_TO_TEXT_ENCODING_NONE;
+ } break;
+ case 2: case 3: case 4: {
+ back = DK4_BINARY_TO_TEXT_ENCODING_HEX;
+ } break;
+ case 5: case 6: case 7: {
+ back = DK4_BINARY_TO_TEXT_ENCODING_A85;
+ } break;
+ }
+ if (-1 == back) {
+ dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
+ }
+ } else {
+ dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
+ }
+ return back;
+}
+