diff options
Diffstat (limited to 'unpack.c')
-rw-r--r-- | unpack.c | 23 |
1 files changed, 13 insertions, 10 deletions
@@ -50,6 +50,7 @@ enum pack_status unpack(const void *buf_, size_t size, const char *fmt, ...) for (int i = 0; fmt[i] != '\0'; i++) { bool sign; size_t count = 1, s; + enum pack_type t; union { #define T(type, sign, c_type, va_type) \ sign c_type *type; @@ -91,19 +92,20 @@ enum pack_status unpack(const void *buf_, size_t size, const char *fmt, ...) break; ITYPE_MACROS #undef T - case 'f': arg.f = va_arg(ap, float *); break; - case 'd': arg.d = va_arg(ap, double *); break; - case 'x': break; + case PACK_TYPE_FLOAT: arg.f = va_arg(ap, float *); break; + case PACK_TYPE_DOUBLE: arg.d = va_arg(ap, double *); break; + case PACK_TYPE_PADDING: break; default: return PACK_FMTINVAL; } + t = fmt[i]; - s = getsize(fmt[i]); + s = getsize(t); tr_debug("s: %" PRIuSIZE, s); if (s == (size_t)-1) return PACK_FMTINVAL; if (size - offset < s * count) return PACK_TOOSMALL; - if (fmt[i] == 'x') goto skip; + if (t == PACK_TYPE_PADDING) goto skip; for (size_t j = 0; j < count; j++) { val.unsigned_ = @@ -111,11 +113,11 @@ enum pack_status unpack(const void *buf_, size_t size, const char *fmt, ...) tr_debug("val.u: %" PRIuMAX ", at: %" PRIuSIZE, val.unsigned_, offset + s * j); - if (fmt[i] == 'f') { + if (t == PACK_TYPE_FLOAT) { float f = ieee754b32_deserialise(val.unsigned_); val.f = f; tr_debug("val.f: %f", val.f); - } else if (fmt[i] == 'd') { + } else if (t == PACK_TYPE_DOUBLE) { double d = ieee754b64_deserialise(val.unsigned_); val.d = d; tr_debug("val.d: %f", val.d); @@ -131,15 +133,16 @@ enum pack_status unpack(const void *buf_, size_t size, const char *fmt, ...) tr_debug("val.s: %" PRIdMAX, val.signed_); } - switch (fmt[i]) { + switch (t) { #define T(type, sign, c_type, va_type) \ case (char)PACK_TYPE_##type: \ arg.type[j] = val.sign##_; \ break; ITYPE_MACROS #undef T - case 'f': arg.f[j] = val.f; break; - case 'd': arg.d[j] = val.d; break; + case PACK_TYPE_FLOAT: arg.f[j] = val.f; break; + case PACK_TYPE_DOUBLE: arg.d[j] = val.d; break; + case PACK_TYPE_PADDING: break; } } skip: |