diff options
author | Paul Kocialkowski <contact@paulk.fr> | 2014-07-26 18:49:31 +0200 |
---|---|---|
committer | Paul Kocialkowski <contact@paulk.fr> | 2014-07-26 18:49:31 +0200 |
commit | 379045ee0cc5d136639504cbf9c57de4ee9078d4 (patch) | |
tree | 6a1253d52f63bb3091a98399cff29e1a3615342e /samsung-ipc/utils.c | |
parent | bfb20b0c1fb891360524f02eed79c8dbe7278626 (diff) | |
download | external_libsamsung-ipc-379045ee0cc5d136639504cbf9c57de4ee9078d4.zip external_libsamsung-ipc-379045ee0cc5d136639504cbf9c57de4ee9078d4.tar.gz external_libsamsung-ipc-379045ee0cc5d136639504cbf9c57de4ee9078d4.tar.bz2 |
utils: Dedicated function for length/size
Diffstat (limited to 'samsung-ipc/utils.c')
-rw-r--r-- | samsung-ipc/utils.c | 53 |
1 files changed, 43 insertions, 10 deletions
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; } |