Added SILC Thread Queue API
[silc.git] / lib / silcasn1 / silcasn1.c
index eec926c49ec4fd1b2d789dc9f9b0b141f19fc945..ea1408e5508d1d01980229f13a129d3e7eb41e97 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 - 2005 Pekka Riikonen
+  Copyright (C) 2003 - 2007 Pekka Riikonen
 
   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
 
 */
 
-#include "silcincludes.h"
+#include "silc.h"
 #include "silcasn1.h"
 #include "silcber.h"
 
 /* Allocate ASN.1 context. */
 
-SilcAsn1 silc_asn1_alloc(void)
+SilcAsn1 silc_asn1_alloc(SilcStack stack)
 {
-  SilcAsn1 asn1 = silc_calloc(1, sizeof(*asn1));
-  if (!asn1)
+  SilcAsn1 asn1;
+  SilcStack stack1;
+
+  stack1 = silc_stack_alloc(0, stack);
+  if (!stack1)
     return NULL;
 
-  if (!silc_asn1_init(asn1))
+  /* Allocate itself from the stack */
+  asn1 = silc_scalloc(stack1, 1, sizeof(*asn1));
+  if (!asn1) {
+    silc_stack_free(stack1);
     return NULL;
+  }
+
+  asn1->orig_stack = stack;
+  asn1->stack1 = stack1;
+  asn1->stack2 = silc_stack_alloc(0, stack);
+  if (!asn1->stack2) {
+    silc_stack_free(stack1);
+    return NULL;
+  }
+
+  asn1->accumul = 0;
 
   return asn1;
 }
@@ -40,20 +57,21 @@ SilcAsn1 silc_asn1_alloc(void)
 void silc_asn1_free(SilcAsn1 asn1)
 {
   silc_asn1_uninit(asn1);
-  silc_free(asn1);
 }
 
 /* Init pre-allocated ASN.1 context */
 
-bool silc_asn1_init(SilcAsn1 asn1)
+SilcBool silc_asn1_init(SilcAsn1 asn1, SilcStack stack)
 {
-  asn1->stack1 = silc_stack_alloc(768);
+  asn1->orig_stack = stack;
+
+  asn1->stack1 = silc_stack_alloc(0, stack);
   if (!asn1->stack1)
     return FALSE;
 
-  asn1->stack2 = silc_stack_alloc(768);
+  asn1->stack2 = silc_stack_alloc(0, stack);
   if (!asn1->stack2) {
-    silc_stack_free(asn1->stack2);
+    silc_stack_free(asn1->stack1);
     return FALSE;
   }
 
@@ -66,15 +84,18 @@ bool silc_asn1_init(SilcAsn1 asn1)
 
 void silc_asn1_uninit(SilcAsn1 asn1)
 {
-#if 1
-  silc_stack_stats(asn1->stack1);
-  silc_stack_stats(asn1->stack2);
-#endif
-  silc_stack_free(asn1->stack1);
+  SilcStack stack1 = asn1->stack1;
+
+  if (asn1->switched) {
+    stack1 = asn1->stack2;
+    asn1->stack2 = asn1->stack1;
+  }
+
   silc_stack_free(asn1->stack2);
+  silc_stack_free(stack1);
 }
 
-#ifdef SILC_DIST_INPLACE
+#if defined(SILC_DEBUG)
 /* Returns string representation of a tag */
 
 const char *silc_asn1_tag_name(SilcAsn1Tag tag)
@@ -88,15 +109,18 @@ const char *silc_asn1_tag_name(SilcAsn1Tag tag)
     return "choice";
   case SILC_ASN1_TAG_ANY:
     return "any";
+  case SILC_ASN1_TAG_ANY_PRIMITIVE:
+    return "any primitive";
   case SILC_ASN1_TAG_SEQUENCE_OF:
     return "sequence of";
-
   case SILC_ASN1_TAG_SEQUENCE:
     return "sequence";
   case SILC_ASN1_TAG_SET:
     return "set";
   case SILC_ASN1_TAG_INTEGER:
     return "integer";
+  case SILC_ASN1_TAG_SHORT_INTEGER:
+    return "short integer";
   case SILC_ASN1_TAG_OID:
     return "oid";
   case SILC_ASN1_TAG_BOOLEAN:
@@ -152,23 +176,26 @@ const char *silc_asn1_tag_name(SilcAsn1Tag tag)
   }
   return "unknown";
 }
+#endif /* SILC_DEBUG */
 
+#ifdef SILC_DIST_TOOLKIT
+#ifdef SILC_DEBUG
 /* Dumps the ASN.1 data block into standard output (stdout). */
 
-bool silc_asn1_dump(SilcAsn1 asn1, SilcBuffer src)
+SilcBool silc_asn1_dump(SilcAsn1 asn1, SilcBuffer src)
 {
-  bool ret = FALSE;
+  SilcBool ret = FALSE;
   SilcBerEncoding renc;
-  SilcAsn1Tag rtag;
+  SilcUInt32 rtag;
   const unsigned char *rdata;
   SilcUInt32 rdata_len, len = 0;
-  bool rindef;
+  SilcBool rindef;
 
   SILC_LOG_DEBUG(("Dumping ASN.1"));
 
   while (silc_buffer_len(src)) {
     /* Decode the BER block */
-    ret = silc_ber_decode(src, NULL, &renc, (SilcUInt32 *)&rtag, &rdata,
+    ret = silc_ber_decode(src, NULL, &renc, &rtag, &rdata,
                          &rdata_len, &rindef, &len);
     if (!ret) {
       SILC_LOG_DEBUG(("Error parsing BER block, malformed ASN.1 data"));
@@ -176,7 +203,7 @@ bool silc_asn1_dump(SilcAsn1 asn1, SilcBuffer src)
     }
 
     fprintf(stdout, "Type %s [%d]\n",
-           silc_asn1_tag_name(rtag), rtag);
+           silc_asn1_tag_name(rtag), (int)rtag);
 
     if (renc == SILC_BER_ENC_PRIMITIVE)
       len = len + rdata_len;
@@ -189,5 +216,5 @@ bool silc_asn1_dump(SilcAsn1 asn1, SilcBuffer src)
 
   return TRUE;
 }
-
-#endif /* SILC_DIST_INPLACE */
+#endif /* SILC_DEBUG */
+#endif /* SILC_DIST_TOOLKIT */