From e41cf5236ac5d858a2b7bceaefcc31a8d481dd39 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 22 Sep 2012 19:37:21 +0200 Subject: device: Fix device_list_alloc_to_bufs --- src/device.c | 98 +++++++++++++++++++++++++++++++++++++++++------------------- src/device.h | 7 +++-- 2 files changed, 72 insertions(+), 33 deletions(-) diff --git a/src/device.c b/src/device.c index 0922a65..8baa201 100644 --- a/src/device.c +++ b/src/device.c @@ -148,53 +148,87 @@ char * hwrevs_alloc_to_string(const int16_t * hwrevs) { } -char ** device_hwrevs_alloc_to_256bufs(const struct device_hwrevs * device_hwrevs) { +char ** device_list_alloc_to_bufs(const struct device_list * device_list) { - int16_t * hwrevs = device_hwrevs->hwrevs; - const char * device; int count = 0; + int size = 0; + const char * device; + const struct device_list * device_first; char ** ret; + char * last_ptr; int i, j, k; - device = device_to_string(device_hwrevs->device); - if ( ! device ) - return NULL; + device_first = device_list; - for ( i = 0; hwrevs[i] != -1; ++i ) - if ( hwrevs[i] >= 0 && hwrevs[i] <= 9999 ) + while ( device_first ) { + + int local = 0; + + if ( ! device_to_string(device_first->device) ) + continue; + + for ( i = 0; device_first->hwrevs[i] != -1; ++i ) + if ( device_first->hwrevs[i] >= 0 && device_first->hwrevs[i] <= 9999 ) + ++local; + + size += 1+16+local*8; + count += local/30; + if ( local%30 != 0 || local == 0 ) ++count; - count = count/30 + 2; + device_first = device_first->next; + + } - ret = malloc(count*sizeof(char *) + count*256); + ret = calloc(1, count*sizeof(char *) + size); if ( ! ret ) return NULL; - i = -1; j = 0; + last_ptr = (char *)ret + count*sizeof(char *); + device_first = device_list; - while ( hwrevs[i+1] != -1 ) { + while ( device_first ) { - ret[j] = (char *)ret + count*sizeof(char *) + j*256; + i = -1; - memset(ret[j], 0, 256); - strncpy(ret[j], device, 16); + device = device_to_string(device_first->device); + if ( ! device ) { + device_first = device_first->next; + continue; + } + + while ( device_first->hwrevs[i+1] != -1 ) { + + uint8_t len = 0; + ret[j] = last_ptr; + + strncpy(ret[j]+1, device, 16); + last_ptr += 16; + len += 16; + + k = 0; - k = 0; + for ( k = 0; k < 30; ++k ) { - for ( k = 0; k < 30; ++k ) { + ++i; - ++i; + if ( device_first->hwrevs[i] < 0 || device_first->hwrevs[i] > 9999 ) + continue; - if ( hwrevs[i] < 0 || hwrevs[i] > 9999 ) - continue; + snprintf(ret[j]+1+k*8, 8, "%d", device_first->hwrevs[i]); + last_ptr += 8; + len += 8; - memset(ret[j] + k*8, 0, 8); - snprintf(ret[j] + k*8, 8, "%d", hwrevs[i]); + } + + ret[j][0] = len; + + ++j; } - ++j; + device_first = device_first->next; } @@ -204,7 +238,7 @@ char ** device_hwrevs_alloc_to_256bufs(const struct device_hwrevs * device_hwrev } -struct device_hwrevs device_hwrevs_alloc_from_256buf(const char * buf, size_t size) { +struct device_list * device_list_alloc_from_buf(const char * buf, size_t size) { int i; int len; @@ -212,7 +246,11 @@ struct device_hwrevs device_hwrevs_alloc_from_256buf(const char * buf, size_t si char str[17]; const char * ptr1; const char * ptr; - struct device_hwrevs ret; + struct device_list * ret; + + ret = calloc(1, sizeof(struct device_list)); + if ( ! ret ) + return NULL; len = strnlen(buf, size); if ( len > 16 ) @@ -222,7 +260,7 @@ struct device_hwrevs device_hwrevs_alloc_from_256buf(const char * buf, size_t si memcpy(str, buf, len); ptr1 = buf + len; - ret.device = device_from_string(str); + ret->device = device_from_string(str); ptr = ptr1; count = 1; @@ -243,8 +281,8 @@ struct device_hwrevs device_hwrevs_alloc_from_256buf(const char * buf, size_t si } - ret.hwrevs = calloc(count, sizeof(int16_t)); - if ( ! ret.hwrevs ) + ret->hwrevs = calloc(count, sizeof(int16_t)); + if ( ! ret->hwrevs ) return ret; ptr = ptr1; @@ -264,11 +302,11 @@ struct device_hwrevs device_hwrevs_alloc_from_256buf(const char * buf, size_t si memset(str, 0, sizeof(str)); memcpy(str, ptr, len); ptr += len + 1; - ret.hwrevs[i++] = atoi(str); + ret->hwrevs[i++] = atoi(str); } - ret.hwrevs[i] = -1; + ret->hwrevs[i] = -1; return ret; } diff --git a/src/device.h b/src/device.h index d614d4e..875b1ab 100644 --- a/src/device.h +++ b/src/device.h @@ -36,9 +36,10 @@ enum device { - terminated by -1 - valid numbers: 0-9999 */ -struct device_hwrevs { +struct device_list { enum device device; int16_t * hwrevs; + struct device_list * next; }; enum device device_from_string(const char * device); @@ -50,7 +51,7 @@ int hwrevs_is_valid(const int16_t * hwrevs, int16_t hwrev); int16_t * hwrevs_alloc_from_string(const char * str); char * hwrevs_alloc_to_string(const int16_t * hwrevs); -char ** device_hwrevs_alloc_to_256bufs(const struct device_hwrevs * device_hwrevs); -struct device_hwrevs device_hwrevs_alloc_from_256buf(const char * buf, size_t size); +char ** device_list_alloc_to_bufs(const struct device_list * device_list); +struct device_list * device_list_alloc_from_buf(const char * buf, size_t size); #endif -- cgit v1.2.3