diff options
author | Michele Calgaro <michele.calgaro@yahoo.it> | 2020-09-11 14:38:47 +0900 |
---|---|---|
committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2020-09-11 14:38:47 +0900 |
commit | 884c8093d63402a1ad0b502244b791e3c6782be3 (patch) | |
tree | a600d4ab0d431a2bdfe4c15b70df43c14fbd8dd0 /debian/opensync/opensync-0.22/tools/osyncbinary.c | |
parent | 14e1aa2006796f147f3f4811fb908a6b01e79253 (diff) | |
download | extra-dependencies-884c8093d63402a1ad0b502244b791e3c6782be3.tar.gz extra-dependencies-884c8093d63402a1ad0b502244b791e3c6782be3.zip |
Added debian extra dependency packages.
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
Diffstat (limited to 'debian/opensync/opensync-0.22/tools/osyncbinary.c')
-rw-r--r-- | debian/opensync/opensync-0.22/tools/osyncbinary.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/debian/opensync/opensync-0.22/tools/osyncbinary.c b/debian/opensync/opensync-0.22/tools/osyncbinary.c new file mode 100644 index 00000000..b712526b --- /dev/null +++ b/debian/opensync/opensync-0.22/tools/osyncbinary.c @@ -0,0 +1,147 @@ +#include <opensync/opensync.h> +#include <opensync/opensync_internals.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <glib.h> +#include <sys/time.h> + + +static void usage (char *name, int ecode) +{ + fprintf (stderr, "Usage: %s\n", name); + fprintf (stderr, "%s <Input> <Output>\n", name); + fprintf (stderr, "[--hex]\tConvert from a plain hex format\n"); + exit (ecode); +} + +osync_bool convert_hex(const char *input, unsigned int inpsize, char **output, unsigned int *outsize) +{ + int i = 0; + GString *string = g_string_new(""); + unsigned int character = 0; + for (i = 0; i < inpsize - 1; i = i + 2) { + printf("Current char is %c%c\n", input[i], input[i + 1]); + + char tmp[3]; + tmp[0] = input[i]; + tmp[1] = input[i + 1]; + tmp[2] = 0; + + sscanf(tmp, " %x", &character); + g_string_append_c(string, character); + } + + *outsize = string->len; + *output = g_string_free(string, FALSE); + return TRUE; +} + +osync_bool convert_bin(const char *input, unsigned int inpsize, char **output, unsigned int *outsize) +{ + int i = 0; + GString *string = g_string_new(""); + + for (i = 0; i < inpsize; i++) { + printf("\nCurrent char is %i (%c)\n", input[i], input[i]); + if (input[i] == '\r' || input[i] == '\n') { + printf("Invalid input\n"); + return FALSE; + } + + //char string1[2] = ""; + //char string2[3] = ""; + unsigned int character = 0; + //int ret = ; + printf("returned is %i\n", sscanf(input + i, " %x %*s", &character)); + printf("returned is %i\n", sscanf(input + i, " %x %*s", &character)); + if (g_pattern_match_simple(" ?? *", input + i) && sscanf(input + i, " %x %*s", &character) == 1) { + printf("returned is %i\n", sscanf(input + i, " %x %*s", &character)); + + //if () { //0-9][0-9] + + printf("escaped chars are %.4s %i\n", input + i, sscanf(input + i, " %x %*s", &character)); + printf("returned is %i\n", sscanf(input + i, " %x %*s", &character)); + if (sscanf(input + i, " %x %*s", &character) != 1) + return FALSE; + printf("Found a escaped char %i\n", character); + g_string_append_c(string, character); + i+=3; + } else { + if (input[i] != 0) { + printf("Appending normal char %i\n", input[i]); + g_string_append_c(string, input[i]); + } + } + } + + *outsize = string->len; + *output = g_string_free(string, FALSE); + return TRUE; +} + +int main (int argc, char *argv[]) +{ + OSyncError *error = NULL; + + osync_bool hex = FALSE; + + if (argc < 3) + usage (argv[0], 1); + + int i = 1; + for (i = 1; i < argc; i++) { + char *arg = argv[i]; + if (!strcmp (arg, "--hex")) { + hex = TRUE; + } else if (!strcmp (arg, "--help")) { + usage (argv[0], 0); + } else { + break; + } + } + + char *input = argv[i]; + char *output = argv[i + 1]; + + printf("input %s output %s\n", input, output); + + char *buffer = NULL; + int size = 0; + if (!osync_file_read(input, &buffer, &size, &error)) { + fprintf(stderr, "%s\n", osync_error_print(&error)); + osync_error_free(&error); + return 1; + } + + char **array = g_strsplit(buffer, "\n", 0); + g_free(buffer); + buffer = g_strjoinv(NULL, array); + size = strlen(buffer) + 1; + g_strfreev(array); + + char *outbuffer = NULL; + unsigned int outsize = 0; + if (hex) { + if (!convert_hex(buffer, size, &outbuffer, &outsize)) { + fprintf(stderr, "Unable to convert"); + return 1; + } + } else { + if (!convert_bin(buffer, size, &outbuffer, &outsize)) { + fprintf(stderr, "Unable to convert"); + return 1; + } + } + + if (!osync_file_write(output, outbuffer, outsize, 0644, &error)) { + fprintf(stderr, "Unable to write file %s: %s", output, osync_error_print(&error)); + osync_error_free(&error); + return 1; + } + + return 0; +} |