From dc61f8f79e924ba3ce3a0badd591a58254788370 Mon Sep 17 00:00:00 2001 From: Daniel Kutik Date: Fri, 5 Oct 2018 12:55:56 +0800 Subject: [PATCH] Moved attr_as_unsigned and attr_as_string to util Moved the two functions to util.c to remove duplicate code. The previous error handling in some of the implemenations was incomplete as it caused qdl to crash. While the variable errors was incremented we still tried to return the regular result. Now returning 0/NULL in case of error. Signed-off-by: Bjorn Andersson --- patch.c | 23 +---------------------- program.c | 37 +------------------------------------ qdl.h | 3 +++ ufs.c | 25 ------------------------- util.c | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 37 insertions(+), 83 deletions(-) diff --git a/patch.c b/patch.c index d2bc461..175397f 100644 --- a/patch.c +++ b/patch.c @@ -34,32 +34,11 @@ #include #include "patch.h" +#include "qdl.h" static struct patch *patches; static struct patch *patches_last; -static unsigned attr_as_unsigned(xmlNode *node, const char *attr, int *errors) -{ - xmlChar *value; - - value = xmlGetProp(node, (xmlChar*)attr); - if (!value) - (*errors)++; - - return strtoul((char*)value, NULL, 10); -} - -static const char *attr_as_string(xmlNode *node, const char *attr, int *errors) -{ - xmlChar *value; - - value = xmlGetProp(node, (xmlChar*)attr); - if (!value) - (*errors)++; - - return strdup((char*)value); -} - int patch_load(const char *patch_file) { struct patch *patch; diff --git a/program.c b/program.c index 739305a..aeeec33 100644 --- a/program.c +++ b/program.c @@ -36,46 +36,11 @@ #include #include "program.h" +#include "qdl.h" static struct program *programes; static struct program *programes_last; -static unsigned attr_as_unsigned(xmlNode *node, const char *attr, int *errors) -{ - xmlChar *value; - - value = xmlGetProp(node, (xmlChar*)attr); - if (!value) - (*errors)++; - - return strtoul((char*)value, NULL, 10); -} - -static const char *attr_as_string(xmlNode *node, const char *attr, int *errors) -{ - xmlChar *value; - - value = xmlGetProp(node, (xmlChar*)attr); - if (!value) - (*errors)++; - - if (value && value[0] == '\0') - return NULL; - - return strdup((char*)value); -} - -static bool attr_as_bool(xmlNode *node, const char *attr, int *errors) -{ - xmlChar *value; - - value = xmlGetProp(node, (xmlChar*)attr); - if (!value) - (*errors)++; - - return xmlStrcmp(value, (xmlChar*)"true") == 0; -} - int program_load(const char *program_file) { struct program *program; diff --git a/qdl.h b/qdl.h index 0ed2607..73ea84c 100644 --- a/qdl.h +++ b/qdl.h @@ -5,10 +5,13 @@ #include "patch.h" #include "program.h" +#include int firehose_run(int fd, const char *incdir); int sahara_run(int fd, char *prog_mbn); void print_hex_dump(const char *prefix, const void *buf, size_t len); +unsigned attr_as_unsigned(xmlNode *node, const char *attr, int *errors); +const char *attr_as_string(xmlNode *node, const char *attr, int *errors); extern bool qdl_debug; diff --git a/ufs.c b/ufs.c index c48c976..5389be3 100644 --- a/ufs.c +++ b/ufs.c @@ -55,32 +55,7 @@ static const char notice_bconfigdescrlock[] = "\n" " and don't use command line parameter --finalize-provisioning.\n\n" "In case of mismatch between CL and XML provisioning is not performed.\n\n"; -// ToDo: These 2 functions must be moved to a common module (refactoring required) -static unsigned attr_as_unsigned(xmlNode *node, const char *attr, int *errors) -{ - xmlChar *value; - value = xmlGetProp(node, (xmlChar*)attr); - if (!value) { - (*errors)++; - return 0; - } - return strtoul((char*)value, NULL, 0); -} - -static const char *attr_as_string(xmlNode *node, const char *attr, int *errors) -{ - xmlChar *value; - - value = xmlGetProp(node, (xmlChar*)attr); - if (!value) - (*errors)++; - - if (value && value[0] == '\0') - return NULL; - - return strdup((char*)value); -} bool ufs_need_provisioning(void) { diff --git a/util.c b/util.c index 03029ca..3a297e8 100644 --- a/util.c +++ b/util.c @@ -31,6 +31,9 @@ #include #include #include +#include +#include +#include #define MIN(x, y) ((x) < (y) ? (x) : (y)) @@ -77,3 +80,32 @@ void print_hex_dump(const char *prefix, const void *buf, size_t len) printf("%s %04x: %s\n", prefix, i, line); } } + +unsigned attr_as_unsigned(xmlNode *node, const char *attr, int *errors) +{ + xmlChar *value; + + value = xmlGetProp(node, (xmlChar*)attr); + if (!value) { + (*errors)++; + return 0; + } + + return (unsigned int) strtoul((char*)value, NULL, 10); +} + +const char *attr_as_string(xmlNode *node, const char *attr, int *errors) +{ + xmlChar *value; + + value = xmlGetProp(node, (xmlChar*)attr); + if (!value) { + (*errors)++; + return NULL; + } + + if (value[0] == '\0') + return NULL; + + return strdup((char*)value); +}