Added SILC errno API. Added SilcResult, generic error code and
[crypto.git] / lib / silcutil / silcbuffmt.c
index ff69ac6eab5acdfb8485a94eef1b7ea8c1672d16..3a789fc90c29935357036dcae871f38dbe43cc38 100644 (file)
@@ -32,12 +32,16 @@ do {                                                        \
 } while(0)
 
 /* Check that there is data to be unformatted */
-#define UNFORMAT_HAS_SPACE(b, req)             \
-do {                                           \
-  if (silc_unlikely(req > silc_buffer_len(b))) \
-    goto fail;                                 \
-  if (silc_unlikely((req + 1) <= 0))           \
-    goto fail;                                 \
+#define UNFORMAT_HAS_SPACE(b, req)                     \
+do {                                                   \
+  if (silc_unlikely(req > silc_buffer_len(b))) {       \
+    silc_set_errno(SILC_ERR_OVERFLOW);                 \
+    goto fail;                                         \
+  }                                                    \
+  if (silc_unlikely((req + 1) <= 0)) {                 \
+    silc_set_errno(SILC_ERR_UNDERFLOW);                        \
+    goto fail;                                         \
+  }                                                    \
 } while(0)
 
 
@@ -108,11 +112,11 @@ int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap)
     case SILC_PARAM_UI16_STRING_ALLOC:
     case SILC_PARAM_UI32_STRING_ALLOC:
       {
-       unsigned char *x = va_arg(ap, unsigned char *);
+       char *x = va_arg(ap, char *);
        SilcUInt32 tmp_len = x ? strlen(x) : 0;
        if (x && tmp_len) {
          FORMAT_HAS_SPACE(stack, dst, tmp_len);
-         silc_buffer_put(dst, x, tmp_len);
+         silc_buffer_put(dst, (unsigned char *)x, tmp_len);
          silc_buffer_pull(dst, tmp_len);
        }
        break;
@@ -179,7 +183,7 @@ int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap)
       {
        char x = (char)va_arg(ap, int);
        FORMAT_HAS_SPACE(stack, dst, 1);
-       silc_buffer_put(dst, &x, 1);
+       silc_buffer_put(dst, (unsigned char *)&x, 1);
        silc_buffer_pull(dst, 1);
        break;
       }
@@ -234,8 +238,10 @@ int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap)
        if (!offst)
          break;
        if (offst > 1) {
-         if (offst > silc_buffer_len(dst))
+         if (offst > silc_buffer_len(dst)) {
+           silc_set_errno(SILC_ERR_OVERFLOW);
            goto fail;
+         }
          silc_buffer_pull(dst, offst);
          flen += offst;
        } else {
@@ -253,6 +259,8 @@ int silc_buffer_sformat_vp(SilcStack stack, SilcBuffer dst, va_list ap)
     default:
       SILC_LOG_DEBUG(("Bad buffer formatting type `%d'. Could not "
                      "format the data.", fmt));
+      silc_set_errno_reason(SILC_ERR_INVALID_ARGUMENT,
+                           "Bad buffer formatting type %d", fmt);
       goto fail;
       break;
     }
@@ -644,6 +652,8 @@ int silc_buffer_sunformat_vp(SilcStack stack, SilcBuffer src, va_list ap)
     default:
       SILC_LOG_DEBUG(("Bad buffer formatting type `%d'. Could not "
                      "format the data.", fmt));
+      silc_set_errno_reason(SILC_ERR_INVALID_ARGUMENT,
+                           "Bad buffer formatting type %d", fmt);
       goto fail;
       break;
     }