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 <bjorn.andersson@linaro.org>
This commit is contained in:
Daniel Kutik
2018-10-05 12:55:56 +08:00
committed by Bjorn Andersson
parent cfce0beeab
commit dc61f8f79e
5 changed files with 37 additions and 83 deletions

23
patch.c
View File

@@ -34,32 +34,11 @@
#include <libxml/tree.h>
#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;

View File

@@ -36,46 +36,11 @@
#include <libxml/tree.h>
#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;

3
qdl.h
View File

@@ -5,10 +5,13 @@
#include "patch.h"
#include "program.h"
#include <libxml/tree.h>
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;

25
ufs.c
View File

@@ -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)
{

32
util.c
View File

@@ -31,6 +31,9 @@
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#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);
}