diff options
-rw-r--r-- | include/samsung-ipc.h | 4 | ||||
-rw-r--r-- | samsung-ipc/utils.c | 53 |
2 files changed, 46 insertions, 11 deletions
diff --git a/include/samsung-ipc.h b/include/samsung-ipc.h index 5be2ac9..3aa6808 100644 --- a/include/samsung-ipc.h +++ b/include/samsung-ipc.h @@ -139,8 +139,10 @@ int sysfs_value_read(const char *path); int sysfs_value_write(const char *path, int value); char *sysfs_string_read(const char *path, size_t length); int sysfs_string_write(const char *path, const char *buffer, size_t length); +size_t data2string_length(const void *data, size_t size); char *data2string(const void *data, size_t size); -void *string2data(const char *string, size_t *size_p); +size_t string2data_size(const char *string); +void *string2data(const char *string); /* * Samsung-IPC protocol diff --git a/samsung-ipc/utils.c b/samsung-ipc/utils.c index d6481c2..d0cc059 100644 --- a/samsung-ipc/utils.c +++ b/samsung-ipc/utils.c @@ -331,6 +331,18 @@ complete: return rc; } +size_t data2string_length(const void *data, size_t size) +{ + size_t length; + + if (data == NULL || size == 0) + return 0; + + length = size * 2 + 1; + + return length; +} + char *data2string(const void *data, size_t size) { char *string; @@ -341,7 +353,10 @@ char *data2string(const void *data, size_t size) if (data == NULL || size == 0) return NULL; - length = size * 2 + 1; + length = data2string_length(data, size); + if (length == 0) + return NULL; + string = (char *) calloc(1, length); p = string; @@ -354,7 +369,27 @@ char *data2string(const void *data, size_t size) return string; } -void *string2data(const char *string, size_t *size_p) +size_t string2data_size(const char *string) +{ + size_t length; + size_t size; + + if (string == NULL) + return 0; + + length = strlen(string); + if (length == 0) + return 0; + + if (length % 2 == 0) + size = length / 2; + else + size = (length - (length % 2)) / 2 + 1; + + return size; +} + +void *string2data(const char *string) { void *data; size_t size; @@ -372,13 +407,14 @@ void *string2data(const char *string, size_t *size_p) if (length == 0) return NULL; - if (length % 2 == 0) { - size = length / 2; + if (length % 2 == 0) shift = 0; - } else { - size = (length - (length % 2)) / 2 + 1; + else shift = 1; - } + + size = string2data_size(string); + if (size == 0) + return NULL; data = calloc(1, size); @@ -397,9 +433,6 @@ void *string2data(const char *string, size_t *size_p) shift++; } - if (size_p != NULL) - *size_p = size; - return data; } |