+/*
+ Based on genstruct by Andrew Tridgell:
+
+ Copyright (C) Andrew Tridgell <genstruct@tridgell.net> 2002
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+#include <ctype.h>
+#include <stddef.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <ccan/talloc/talloc.h>
+#include "cdump.h"
+#include "cdump_internal.h"
+
+/* intermediate dumps are stored in one of these */
+struct cdump_string {
+ size_t length;
+ char *s;
+};
+
+/* see if a range of memory is all zero. Used to prevent dumping of zero elements */
+static bool all_zero(const char *ptr, size_t size)
+{
+ int i;
+
+ for (i=0;i<size;i++) {
+ if (ptr[i]) return false;
+ }
+ return true;
+}
+
+/* encode a buffer of bytes into a escaped string */
+static char *encode_bytes(const void *ctx, const char *ptr, size_t len)
+{
+ const char *hexdig = "0123456789abcdef";
+ char *ret, *p;
+ size_t i;
+ ret = talloc_array(ctx, char, len*3 + 1); /* worst case size */
+ if (!ret)
+ return NULL;
+ for (p=ret,i=0;i<len;i++) {
+ if (isalnum(ptr[i]) || isspace(ptr[i]) ||
+ (ispunct(ptr[i]) && !strchr("\\{}", ptr[i]))) {
+ *p++ = ptr[i];
+ } else {
+ unsigned char c = *(unsigned char *)(ptr+i);
+ if (c == 0 && all_zero(ptr+i, len-i)) break;
+ p[0] = '\\';
+ p[1] = hexdig[c>>4];
+ p[2] = hexdig[c&0xF];
+ p += 3;
+ }
+ }
+
+ *p = 0;
+
+ return ret;
+}
+
+/* decode an escaped string from encode_bytes() into a buffer */
+static char *decode_bytes(const void *ctx, const char *s)
+{
+ char *ret, *p;
+ size_t i;
+
+ ret = talloc_array(ctx, char, strlen(s)+1); /* worst case length */
+ if (!ret)
+ return NULL;
+
+ if (*s == '{') s++;
+
+ for (p=ret,i=0;s[i];i++) {
+ if (s[i] == '}') {
+ break;
+ } else if (s[i] == '\\') {
+ unsigned v;
+ if (sscanf(&s[i+1], "%02x", &v) != 1 || v > 255) {
+ talloc_free(ret);
+ return NULL;
+ }
+ *(unsigned char *)p = v;
+ p++;
+ i += 2;
+ } else {
+ *p++ = s[i];
+ }
+ }
+ /* Nul-terminate in case we're being used as a string. */
+ *p = 0;
+ return ret;
+}
+
+static char *bundle(const void *ctx,
+ const struct cdump_desc *info,
+ const void *data,
+ unsigned indent);
+
+/* the add*() functions deal with adding things to a struct
+ cdump_string */
+
+/* allocate more space if needed */
+static bool addgen_alloc(struct cdump_string *p, int n)
+{
+ if (p->length + n <= talloc_get_size(p->s)) return true;
+ p->s = talloc_realloc(p, p->s, char, p->length + n + 200);
+ return p->s != NULL;
+}
+
+/* add a character to the buffer */
+static bool addchar(struct cdump_string *p, char c)
+{
+ if (!addgen_alloc(p, 2)) {
+ return false;
+ }
+ p->s[p->length++] = c;
+ p->s[p->length] = 0;
+ return true;
+}
+
+/* add a string to the buffer */
+static bool addstr(struct cdump_string *p, const char *s)
+{
+ int len = strlen(s);
+ if (!addgen_alloc(p, len+1)) {
+ return false;
+ }
+ memcpy(p->s + p->length, s, len+1);
+ p->length += len;
+ return true;
+}
+
+/* add a string to the buffer with a tab prefix */
+static bool addtabbed(struct cdump_string *p, const char *s, unsigned indent)
+{
+ int len = strlen(s);
+ if (!addgen_alloc(p, indent+len+1)) {
+ return false;
+ }
+ while (indent--) {
+ p->s[p->length++] = '\t';
+ }
+ memcpy(p->s + p->length, s, len+1);
+ p->length += len;
+ return true;
+}
+
+/* note! this can only be used for results up to 60 chars wide! */
+static bool addshort(struct cdump_string *p, const char *fmt, ...)
+{
+ char buf[60];
+ int n;
+ va_list ap;
+ va_start(ap, fmt);
+ n = vsnprintf(buf, sizeof(buf), fmt, ap);
+ va_end(ap);
+ if (!addgen_alloc(p, n + 1)) {
+ return false;
+ }
+ memcpy(p->s + p->length, buf, n);
+ p->length += n;
+ p->s[p->length] = 0;
+ return true;
+}
+
+/*
+ this is here to make it easier for people to write dump functions
+ for their own types
+ */
+bool cdump_addstr(struct cdump_string *p, const char *fmt, ...)
+{
+ char *buf;
+ bool ret;
+ va_list ap;
+ va_start(ap, fmt);
+ buf = talloc_vasprintf(NULL, fmt, ap);
+ va_end(ap);
+ if (!buf)
+ return false;
+ ret = addstr(p, buf);
+ talloc_free(buf);
+ return ret;
+}
+
+/* dump a enumerated type */
+bool cdump_bundle_enum(const struct cdump_enum *einfo,
+ struct cdump_string *p,
+ const void *ptr,
+ unsigned indent)
+{
+ unsigned v = *(unsigned *)ptr;
+ int i;
+ for (i=0;einfo[i].name;i++) {
+ if (v == einfo[i].value) {
+ return addstr(p, einfo[i].name);
+ }
+ }
+ /* hmm, maybe we should just fail? */
+ return cdump_bundle_unsigned(p, ptr, indent);
+}
+
+/* dump a single non-array element, handling struct and enum */
+static bool bundle_one(struct cdump_string *p,
+ const struct cdump_desc *info,
+ const void *ptr,
+ unsigned indent)
+{
+ if (info->bundle == cdump_bundle_char && info->ptr_count == 1) {
+ char *s = encode_bytes(p, ptr, strlen(ptr));
+ if (!s)
+ return false;
+ if (!addchar(p,'{') ||
+ !addstr(p, s) ||
+ !addstr(p, "}")) {
+ return false;
+ }
+ return true;
+ }
+
+ return info->bundle(p, ptr, indent);
+}
+
+/* handle dumping of an array of arbitrary type */
+static bool bundle_array(struct cdump_string *p,
+ const struct cdump_desc *info,
+ const void *ptr,
+ size_t array_len,
+ unsigned indent)
+{
+ size_t i, count=0;
+
+ /* special handling of fixed length strings */
+ if (array_len != 0 &&
+ info->ptr_count == 0 &&
+ info->bundle == cdump_bundle_char) {
+ char *s = encode_bytes(p, ptr, array_len);
+ if (!s) return false;
+ if (!addtabbed(p, info->name, indent) ||
+ !addstr(p, " = {") ||
+ !addstr(p, s) ||
+ !addstr(p, "}\n")) {
+ return false;
+ }
+ return true;
+ }
+
+ for (i=0;i<array_len;i++) {
+ const char *p2 = ptr;
+ size_t size = info->size;
+
+ /* generic pointer dereference */
+ if (info->ptr_count) {
+ p2 = *(const char **)ptr;
+ size = sizeof(void *);
+ }
+
+ if ((count || info->ptr_count) &&
+ !(info->flags & CDUMP_FLAG_ALWAYS) &&
+ all_zero(ptr, size)) {
+ ptr += size;
+ continue;
+ }
+ if (count == 0) {
+ if (!addtabbed(p, info->name, indent) ||
+ !addshort(p, " = %u:", i)) {
+ return false;
+ }
+ } else {
+ if (!addshort(p, ", %u:", i) != 0) {
+ return false;
+ }
+ }
+ if (!bundle_one(p, info, p2, indent)) {
+ return false;
+ }
+ ptr += size;
+ count++;
+ }
+ if (count) {
+ return addstr(p, "\n");
+ }
+ return true;
+}
+
+/* find a variable by name in a loaded structure and return its value
+ as an integer. Used to support dynamic arrays */
+static ssize_t find_var(const struct cdump_desc *info,
+ const char *data,
+ const char *var)
+{
+ unsigned int i;
+ const char *ptr;
+
+ /* this allows for constant lengths */
+ if (isdigit(*var)) {
+ return atol(var);
+ }
+
+ for (i=0;info[i].name;i++) {
+ if (strcmp(info[i].name, var) == 0) break;
+ }
+ if (!info[i].name) return -1;
+
+ ptr = data + info[i].offset;
+
+ if (info[i].size == sizeof(int))
+ return *(int *)ptr;
+ else if (info[i].size == sizeof(size_t))
+ return *(ssize_t *)ptr;
+ else if (info[i].size == sizeof(char))
+ return *(char *)ptr;
+
+ return -1;
+}
+
+
+bool cdump_bundle_struct(const struct cdump_desc *info,
+ struct cdump_string *p,
+ const void *ptr,
+ unsigned indent)
+{
+ char *s = bundle(p, info, ptr, indent+1);
+ if (!s)
+ return false;
+
+ return addstr(p, "{\n") && addstr(p,s) && addtabbed(p, "}", indent);
+}
+
+static bool bundle_string(struct cdump_string *p,
+ const struct cdump_desc *info,
+ const char *data,
+ unsigned indent)
+{
+ const char *ptr = *(char **)data;
+ char *s = encode_bytes(p, ptr, strlen(ptr));
+ if (!s)
+ return false;
+
+ return addtabbed(p, info->name, indent)
+ && addstr(p, " = ")
+ && addchar(p,'{')
+ && addstr(p, s)
+ && addstr(p, "}\n");
+}
+
+/* the generic dump routine. Scans the parse information for this structure
+ and processes it recursively */
+static char *bundle(const void *ctx,
+ const struct cdump_desc *info,
+ const void *data,
+ unsigned indent)
+{
+ struct cdump_string *p;
+ char *s = NULL;
+ int i;
+
+ p = talloc(ctx, struct cdump_string);
+ if (!p)
+ return NULL;
+ p->length = 0;
+ p->s = NULL;
+
+ for (i=0;info[i].name;i++) {
+ const void *ptr = (char *)data + info[i].offset;
+ unsigned size = info[i].size;
+
+ if (info[i].ptr_count) {
+ size = sizeof(void *);
+ }
+
+ /* special handling for array types */
+ if (info[i].array_len) {
+ unsigned len = info[i].array_len;
+ if (!bundle_array(p, &info[i], ptr, len, indent)) {
+ goto out;
+ }
+ continue;
+ }
+
+ /* and dynamically sized arrays */
+ if (info[i].dynamic_len) {
+ ssize_t len = find_var(info, data, info[i].dynamic_len);
+ struct cdump_desc p2 = info[i];
+ if (len < 0) {
+ goto out;
+ }
+ if (len > 0) {
+ p2.ptr_count--;
+ p2.dynamic_len = NULL;
+ if (!bundle_array(p, &p2, *(void **)ptr,
+ len, indent)) {
+ goto out;
+ }
+ }
+ continue;
+ }
+
+ /* don't dump zero elements */
+ if (!(info[i].flags & CDUMP_FLAG_ALWAYS) && all_zero(ptr, size))
+ continue;
+
+ /* assume char* is a null terminated string */
+ if (info[i].size == 1 && info[i].ptr_count == 1 &&
+ info[i].bundle == cdump_bundle_char) {
+ if (!bundle_string(p, &info[i], ptr, indent)) {
+ goto out;
+ }
+ continue;
+ }
+
+ /* generic pointer dereference */
+ if (info[i].ptr_count) {
+ ptr = *(const void **)ptr;
+ }
+
+ if (!addtabbed(p, info[i].name, indent) ||
+ !addstr(p, " = ") ||
+ !bundle_one(p, &info[i], ptr, indent) ||
+ !addstr(p, "\n")) {
+ goto out;
+ }
+ }
+ s = talloc_steal(ctx, p->s);
+out:
+ talloc_free(p);
+ return s;
+}
+
+char *cdump_bundle(const void *ctx,
+ const struct cdump_desc *info, const void *data)
+{
+ return bundle(ctx, info, data, 0);
+}
+
+/* parse routine for enumerated types */
+bool cdump_unbundle_enum(const struct cdump_enum *einfo,
+ void *ptr,
+ const char *str)
+{
+ unsigned v;
+ int i;
+
+ if (isdigit(*str)) {
+ if (sscanf(str, "%u", &v) != 1) {
+ errno = EINVAL;
+ return false;
+ }
+ *(unsigned *)ptr = v;
+ return 0;
+ }
+
+ for (i=0;einfo[i].name;i++) {
+ if (strcmp(einfo[i].name, str) == 0) {
+ *(unsigned *)ptr = einfo[i].value;
+ return true;
+ }
+ }
+
+ /* unknown enum value?? */
+ return false;
+}
+
+
+/* parse all base types */
+static bool unbundle_base(const void *ctx,
+ const struct cdump_desc *info,
+ void *ptr,
+ const char *str)
+{
+ if (info->unbundle == cdump_unbundle_char && info->ptr_count==1) {
+ char *s = decode_bytes(ctx, str);
+ if (!s)
+ return false;
+ *(char **)ptr = s;
+ return true;
+ }
+
+ if (info->ptr_count) {
+ struct cdump_desc p2 = *info;
+ *(void **)ptr = talloc_zero_size(ctx,
+ info->ptr_count>1?sizeof(void *):info->size);
+ if (! *(void **)ptr) {
+ return false;
+ }
+ ptr = *(char **)ptr;
+ p2.ptr_count--;
+ return unbundle_base(ctx, &p2, ptr, str);
+ }
+
+ return info->unbundle(ctx, ptr, str);
+}
+
+/* search for a character in a string, skipping over sections within
+ matching braces */
+static char *match_braces(char *s, char c)
+{
+ int depth = 0;
+ while (*s) {
+ switch (*s) {
+ case '}':
+ depth--;
+ break;
+ case '{':
+ depth++;
+ break;
+ }
+ if (depth == 0 && *s == c) {
+ return s;
+ }
+ s++;
+ }
+ return s;
+}
+
+/* parse a generic array */
+static bool unbundle_array(const void *ctx,
+ const struct cdump_desc *info,
+ char *ptr,
+ const char *str,
+ size_t array_len)
+{
+ char *p, *p2;
+ size_t size = info->size;
+
+ /* special handling of fixed length strings */
+ if (array_len != 0 &&
+ info->ptr_count == 0 &&
+ info->bundle == cdump_bundle_char) {
+ char *s = decode_bytes(ctx, str);
+ if (!s)
+ return false;
+ memset(ptr, 0, array_len);
+ memcpy(ptr, s, array_len);
+ talloc_free(s);
+ return true;
+ }
+
+ if (info->ptr_count) {
+ size = sizeof(void *);
+ }
+
+ while (*str) {
+ size_t idx;
+ bool done;
+
+ idx = atol(str);
+ p = strchr(str,':');
+ if (!p) break;
+ p++;
+ p2 = match_braces(p, ',');
+ done = (*p2 != ',');
+ *p2 = 0;
+
+ if (*p == '{') {
+ p++;
+ p[strlen(p)-1] = 0;
+ }
+
+ if (!unbundle_base(ctx, info, ptr + idx*size, p)) {
+ return false;
+ }
+
+ if (done)
+ break;
+ str = p2+1;
+ }
+
+ return true;
+}
+
+/* parse one element, handling dynamic and static arrays */
+static bool unbundle_one(const void *ctx,
+ const struct cdump_desc *info,
+ const char *name,
+ void *data,
+ const char *str)
+{
+ int i;
+ for (i=0;info[i].name;i++) {
+ if (strcmp(info[i].name, name) == 0) {
+ break;
+ }
+ }
+ if (info[i].name == NULL) {
+ return false;
+ }
+
+ if (info[i].array_len) {
+ return unbundle_array(ctx, &info[i], data+info[i].offset,
+ str, info[i].array_len);
+ }
+
+ if (info[i].dynamic_len) {
+ ssize_t len = find_var(info, data, info[i].dynamic_len);
+ if (len < 0) {
+ errno = EINVAL;
+ return false;
+ }
+ if (len > 0) {
+ unsigned size;
+ struct cdump_desc p2 = info[i];
+ char *ptr;
+ size = info[i].ptr_count>1?sizeof(void*):info[i].size;
+ ptr = talloc_zero_size(ctx, len * size);
+ if (!ptr)
+ return false;
+ *((char **)(data + info[i].offset)) = ptr;
+ p2.ptr_count--;
+ p2.dynamic_len = NULL;
+ return unbundle_array(ctx, &p2, ptr, str, len);
+ }
+ return true;
+ }
+
+ return unbundle_base(ctx, &info[i], data + info[i].offset, str);
+}
+
+/* the main parse routine */
+bool cdump_unbundle_struct(const void *ctx,
+ const struct cdump_desc *info,
+ void *data, const char *s)
+{
+ char *str, *s0;
+
+ s0 = talloc_strdup(ctx, s);
+ str = s0;
+
+ while (*str) {
+ char *p;
+ char *name;
+ char *value;
+
+ /* skip leading whitespace */
+ while (isspace(*str)) str++;
+
+ p = strchr(str, '=');
+ if (!p) break;
+ value = p+1;
+ while (p > str && isspace(*(p-1))) {
+ p--;
+ }
+
+ *p = 0;
+ name = str;
+
+ while (isspace(*value)) value++;
+
+ if (*value == '{') {
+ str = match_braces(value, '}');
+ value++;
+ } else {
+ str = match_braces(value, '\n');
+ }
+
+ *str++ = 0;
+
+ if (!unbundle_one(ctx, info, name, data, value)) {
+ talloc_free(s0);
+ return false;
+ }
+ }
+
+ talloc_free(s0);
+ return true;
+}
+
+bool cdump_unbundle(const void *ctx,
+ const struct cdump_desc *info,
+ void *data, const char *s)
+{
+ return cdump_unbundle_struct(ctx, info, data, s);
+}
+
+/* for convenience supply some standard dumpers and parsers here */
+bool cdump_unbundle_char(const void *ctx, void *ptr, const char *str)
+{
+ *(unsigned char *)ptr = atoi(str);
+ return true;
+}
+
+bool cdump_unbundle_int(const void *ctx, void *ptr, const char *str)
+{
+ *(int *)ptr = atoi(str);
+ return true;
+}
+
+bool cdump_unbundle_unsigned(const void *ctx, void *ptr, const char *str)
+{
+ *(unsigned *)ptr = strtoul(str, NULL, 10);
+ return true;
+}
+
+bool cdump_unbundle_time_t(const void *ctx, void *ptr, const char *str)
+{
+ *(time_t *)ptr = strtoul(str, NULL, 10);
+ return true;
+}
+
+bool cdump_unbundle_double(const void *ctx, void *ptr, const char *str)
+{
+ *(double *)ptr = atof(str);
+ return true;
+}
+
+bool cdump_unbundle_float(const void *ctx, void *ptr, const char *str)
+{
+ *(float *)ptr = atof(str);
+ return true;
+}
+
+bool cdump_bundle_char(struct cdump_string *p, const void *ptr, unsigned indent)
+{
+ return addshort(p, "%u", *(unsigned char *)ptr);
+}
+
+bool cdump_bundle_int(struct cdump_string *p, const void *ptr, unsigned indent)
+{
+ return addshort(p, "%d", *(int *)ptr);
+}
+
+bool cdump_bundle_unsigned(struct cdump_string *p, const void *ptr, unsigned indent)
+{
+ return addshort(p, "%u", *(unsigned *)ptr);
+}
+
+bool cdump_bundle_time_t(struct cdump_string *p, const void *ptr, unsigned indent)
+{
+ return addshort(p, "%lu", (long int)*(time_t *)ptr);
+}
+
+bool cdump_bundle_double(struct cdump_string *p, const void *ptr, unsigned indent)
+{
+ return addshort(p, "%lg", *(double *)ptr);
+}
+
+bool cdump_bundle_float(struct cdump_string *p, const void *ptr, unsigned indent)
+{
+ return addshort(p, "%g", *(float *)ptr);
+}