diff options
Diffstat (limited to 'src/pack.c')
-rw-r--r-- | src/pack.c | 148 |
1 files changed, 67 insertions, 81 deletions
@@ -118,12 +118,16 @@ getunum(char *s, uintmax_t maxval, uintmax_t *retval) for (; *s && isspace(*s); s++); for (; *s; s++) { - if (!isdigit(*s)) + if (!isdigit(*s)) { + errno = EINVAL; return -1; + } x = x*10 + *s - '0'; } - if (x > maxval) + if (x > maxval) { + errno = ERANGE; return -1; + } *retval = x; return 0; } @@ -140,18 +144,23 @@ getsnum(char *s, intmax_t minval, intmax_t maxval, intmax_t *retval) ++s; } for (; *s; s++) { - if (!isdigit(*s)) + if (!isdigit(*s)) { + errno = EINVAL; return -1; + } x = x*10 + *s - '0'; } if (neg) { - if (x > -minval) + if (x > -minval) { + errno = ERANGE; return -1; + } *retval = -x; - } else if (x > maxval) + } else if (x > maxval) { + errno = ERANGE; return -1; - else + } else *retval = x; return 0; } @@ -165,8 +174,11 @@ getsnum(char *s, intmax_t minval, intmax_t maxval, intmax_t *retval) type v = (type) x; \ memcpy(env->buf_base + env->buf_pos, \ &v, sizeof(v)); \ - } \ - } \ + } else \ + packerror("error converting %s to %s: %s", \ + arg, #type, strerror(errno)); \ + } else \ + packerror("out of arguments"); \ } \ } while(0) @@ -179,8 +191,11 @@ getsnum(char *s, intmax_t minval, intmax_t maxval, intmax_t *retval) type v = (type) x; \ memcpy(env->buf_base + env->buf_pos, \ &v, sizeof(v)); \ - } \ - } \ + } else \ + packerror("error converting %s to %s: %s", \ + arg, #type, strerror(errno)); \ + } else \ + packerror("out of arguments"); \ } \ } while(0) @@ -197,7 +212,8 @@ Z_packer(struct packenv *env, int rep) if (len > rep - 1) len = rep - 1; memcpy(env->buf_base + env->buf_pos, arg, len); - } + } else + packerror("out of arguments"); } env->buf_pos += rep; } @@ -216,6 +232,8 @@ c_packer(struct packenv *env, int rep) char *arg = packenv_get(env); if (arg) env->buf_base[env->buf_pos] = *arg; + else + packerror("out of arguments"); } env->buf_pos++; } @@ -350,8 +368,12 @@ n_packer(struct packenv *env, int rep) uint16_t v = htons((uint16_t) x); memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); - } - } + } else + packerror("error converting %s to %s: %s", + arg, "uint16_t (network order)", + strerror(errno)); + } else + packerror("out of arguments"); } env->buf_pos += sizeof(uint16_t); } @@ -376,8 +398,12 @@ N_packer(struct packenv *env, int rep) uint32_t v = htonl((uint32_t) x); memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); - } - } + } else + packerror("error converting %s to %s: %s", + arg, "uint32_t (network order)", + strerror(errno)); + } else + packerror("out of arguments"); } env->buf_pos += sizeof(uint32_t); } @@ -402,8 +428,12 @@ v_packer(struct packenv *env, int rep) uint16_t v = ntohs((uint16_t) x); memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); - } - } + } else + packerror("error converting %s to %s: %s", + arg, "uint16_t (host order)", + strerror(errno)); + } else + packerror("out of arguments"); } env->buf_pos += sizeof(uint16_t); } @@ -428,8 +458,12 @@ V_packer(struct packenv *env, int rep) uint32_t v = ntohl((uint32_t) x); memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); - } - } + } else + packerror("error converting %s to %s: %s", + arg, "uint32_t (host order)", + strerror(errno)); + } else + packerror("out of arguments"); } env->buf_pos += sizeof(uint32_t); } @@ -454,7 +488,13 @@ f_packer(struct packenv *env, int rep) errno = 0; v = strtof(arg, &p); - if (*p == 0 && errno == 0) + if (*p) + packerror("error converting %s to %s (near %s)", + arg, "float", p); + else if (errno) + packerror("error converting %s to %s: %s", + arg, "float", strerror(errno)); + else memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); } @@ -482,7 +522,13 @@ d_packer(struct packenv *env, int rep) errno = 0; v = strtod(arg, &p); - if (*p == 0 && errno == 0) + if (*p) + packerror("error converting %s to %s (near %s)", + arg, "double", p); + else if (errno) + packerror("error converting %s to %s: %s", + arg, "double", strerror(errno)); + else memcpy(env->buf_base + env->buf_pos, &v, sizeof(v)); } @@ -760,63 +806,3 @@ packenv_init(struct packenv *env) memset(env->buf_base, 0, env->buf_size); env->buf_pos = 0; } - -#ifdef STANDALONE -#include <unistd.h> - -int -main(int argc, char **argv) -{ - void (*fn)(struct packinst *pi, struct packenv *env) = packin; - struct packinst *pi; - struct packenv *env; - char *end; - int c; - - while ((c = getopt(argc, argv, "d")) != EOF) { - switch (c) { - case 'd': - fn = packout; - break; - default: - exit(1); - } - } - - argc -= optind; - argv += optind; - - if (argc == 0) - abort(); - - pi = packcomp(argv[0], &end); - if (!pi) { - fprintf(stderr, "out of memory\n"); - abort(); - } - if (*end) { - fprintf(stderr, "compile error near %s\n", end); - exit(1); - } - env = packenv_create(packsize(pi)); - if (!env) { - fprintf(stderr, "out of memory\n"); - abort(); - } - - env->fp = stdout; - env->argv = argv + 1; - env->argc = argc - 1; - - if (fn == packout) - fread(env->buf_base, env->buf_size, 1, stdin); - - fn(pi, env); - if (fn == packin) { - fwrite(env->buf_base, env->buf_size, 1, stdout); - } - - packenv_free(env); - packfree(pi); -} -#endif |