libtasn1-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SCM] GNU libtasn1 branch, master, updated. libtasn1_3_6-13-g12635af


From: Nikos Mavrogiannopoulos
Subject: [SCM] GNU libtasn1 branch, master, updated. libtasn1_3_6-13-g12635af
Date: Fri, 30 May 2014 13:22:48 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU libtasn1".

http://git.savannah.gnu.org/cgit/libtasn1.git/commit/?id=12635afc552622caaa63d91500d4c1d319785d7a

The branch, master has been updated
       via  12635afc552622caaa63d91500d4c1d319785d7a (commit)
       via  e7baaf725a397546be2ecc90d36b005f5d6d1061 (commit)
       via  e2e5b1d44456d6e99fb3765f2370630a2059994b (commit)
       via  0227dd9ef0b11773ff853a96d5135a189e20e83e (commit)
       via  7eb760ed9083d5af0b8cd14bd2f26b88691be9ae (commit)
       via  d14caccbe1f54c42437b41e5a1863339f5987fc6 (commit)
       via  dfc8f76ef9a9f9da0fead5777605af4003eb8018 (commit)
       via  b2bb0ef876b9ebb1c6af876fac2c1a8aee616dcb (commit)
      from  95a7335323883ac991d49bee5ad49d903d20909b (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 12635afc552622caaa63d91500d4c1d319785d7a
Author: Nikos Mavrogiannopoulos <address@hidden>
Date:   Fri May 30 15:22:41 2014 +0200

    removed unused variable

commit e7baaf725a397546be2ecc90d36b005f5d6d1061
Author: Nikos Mavrogiannopoulos <address@hidden>
Date:   Fri May 30 15:17:10 2014 +0200

    doc update

commit e2e5b1d44456d6e99fb3765f2370630a2059994b
Author: Nikos Mavrogiannopoulos <address@hidden>
Date:   Fri May 30 15:12:00 2014 +0200

    Added asn1_copy_node2()

commit 0227dd9ef0b11773ff853a96d5135a189e20e83e
Author: Nikos Mavrogiannopoulos <address@hidden>
Date:   Fri May 30 14:56:19 2014 +0200

    Added test for asn1_copy_node()

commit 7eb760ed9083d5af0b8cd14bd2f26b88691be9ae
Author: Nikos Mavrogiannopoulos <address@hidden>
Date:   Fri May 30 13:50:48 2014 +0200

    simplified and optimized asn1_der_decoding_startEnd().
    
    The second pass decoding is now avoided as the start and end
    values are cached during decoding.

commit d14caccbe1f54c42437b41e5a1863339f5987fc6
Author: Nikos Mavrogiannopoulos <address@hidden>
Date:   Fri May 30 14:07:02 2014 +0200

    Added check for decoding_startEnd().

commit dfc8f76ef9a9f9da0fead5777605af4003eb8018
Author: Nikos Mavrogiannopoulos <address@hidden>
Date:   Fri May 30 13:38:36 2014 +0200

    Simplify temporary value storage during coding.

commit b2bb0ef876b9ebb1c6af876fac2c1a8aee616dcb
Author: Nikos Mavrogiannopoulos <address@hidden>
Date:   Fri May 30 13:35:33 2014 +0200

    Simplify temporary value storage during decoding.

-----------------------------------------------------------------------

Summary of changes:
 NEWS                                |    3 +
 lib/coding.c                        |   22 +--
 lib/decoding.c                      |  442 ++++-------------------------------
 lib/int.h                           |    5 +
 lib/libtasn1.h                      |    2 +
 lib/libtasn1.map                    |    1 +
 lib/structure.c                     |   26 ++-
 tests/Makefile.am                   |    4 +-
 tests/Test_encdec.c                 |   40 ++++
 tests/{Test_encdec.c => copynode.c} |   52 ++++-
 10 files changed, 177 insertions(+), 420 deletions(-)
 copy tests/{Test_encdec.c => copynode.c} (74%)

diff --git a/NEWS b/NEWS
index 7fbeb31..95d76d5 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,9 @@ GNU Libtasn1 NEWS                                     -*- 
outline -*-
 
 * Noteworthy changes in release 3.7 (unreleased) [stable]
 - asn1_der_decoding_element() is an alias to asn1_der_decoding().
+- optimized asn1_der_decoding_startEnd(). It only performs the decoding
+  step, if it is not already done.
+- Added asn1_copy_node2().
 - Fixes in DER decoding.
 
 * Noteworthy changes in release 3.6 (released 2014-05-25) [stable]
diff --git a/lib/coding.c b/lib/coding.c
index d6b7cf4..9b90477 100644
--- a/lib/coding.c
+++ b/lib/coding.c
@@ -493,6 +493,7 @@ _asn1_complete_explicit_tag (asn1_node node, unsigned char 
*der,
                {
                  len2 = strtol (p->name, NULL, 10);
                  _asn1_set_name (p, NULL);
+
                  asn1_length_der (*counter - len2, temp, &len3);
                  if (len3 <= (*max_len))
                    {
@@ -1013,7 +1014,7 @@ asn1_der_coding (asn1_node element, const char *name, 
void *ider, int *len,
 {
   asn1_node node, p, p2;
   unsigned char temp[MAX(LTOSTR_MAX_SIZE, SIZEOF_UNSIGNED_LONG_INT * 3 + 1)];
-  int counter, counter_old, len2, len3, tlen, move, max_len, max_len_old;
+  int counter, counter_old, len2, len3, move, max_len, max_len_old;
   int err;
   unsigned char *der = ider;
 
@@ -1187,10 +1188,7 @@ asn1_der_coding (asn1_node element, const char *name, 
void *ider, int *len,
        case ASN1_ETYPE_SET:
          if (move != UP)
            {
-             _asn1_ltostr (counter, (char *) temp);
-             tlen = _asn1_strlen (temp);
-             if (tlen > 0)
-               _asn1_set_value (p, temp, tlen + 1);
+             p->tmp_ival = counter;
              if (p->down == NULL)
                {
                  move = UP;
@@ -1213,8 +1211,8 @@ asn1_der_coding (asn1_node element, const char *name, 
void *ider, int *len,
            }
          else
            {                   /* move==UP */
-             len2 = _asn1_strtol (p->value, NULL, 10);
-             _asn1_set_value (p, NULL, 0);
+             len2 = p->tmp_ival;
+             p->tmp_ival = 0;
              if ((type_field (p->type) == ASN1_ETYPE_SET) && (max_len >= 0))
                {
                  err = _asn1_ordering_set (der + len2, counter - len2, p);
@@ -1236,11 +1234,7 @@ asn1_der_coding (asn1_node element, const char *name, 
void *ider, int *len,
        case ASN1_ETYPE_SET_OF:
          if (move != UP)
            {
-             _asn1_ltostr (counter, (char *) temp);
-             tlen = _asn1_strlen (temp);
-
-             if (tlen > 0)
-               _asn1_set_value (p, temp, tlen + 1);
+             p->tmp_ival = counter;
              p = p->down;
              while ((type_field (p->type) == ASN1_ETYPE_TAG)
                     || (type_field (p->type) == ASN1_ETYPE_SIZE))
@@ -1257,8 +1251,8 @@ asn1_der_coding (asn1_node element, const char *name, 
void *ider, int *len,
            }
          if (move == UP)
            {
-             len2 = _asn1_strtol (p->value, NULL, 10);
-             _asn1_set_value (p, NULL, 0);
+             len2 = p->tmp_ival;
+             p->tmp_ival = 0;
              if ((type_field (p->type) == ASN1_ETYPE_SET_OF)
                  && (counter - len2 > 0) && (max_len >= 0))
                {
diff --git a/lib/decoding.c b/lib/decoding.c
index a9aecc2..8f6fe8a 100644
--- a/lib/decoding.c
+++ b/lib/decoding.c
@@ -880,11 +880,12 @@ static void delete_unneeded_choice_fields(asn1_node p)
  * @errorDescription: null-terminated string contains details when an
  *   error occurred.
  *
- * Fill the structure address@hidden with values of a DER encoding
+ * Fill the structure address@hidden with values of a DER encoding
  * string. The structure must just be created with function
- * asn1_create_element().  If an error occurs during the decoding
- * procedure, the address@hidden is deleted and set equal to
- * %NULL.
+ * asn1_create_element(). 
+ *
+ * Note that the address@hidden variable is provided as a pointer for
+ * historical reasons.
  *
  * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
  *   if @ELEMENT is %NULL, and %ASN1_TAG_ERROR or
@@ -900,7 +901,7 @@ asn1_der_decoding (asn1_node * element, const void *ider, 
int ider_len,
   int counter, len2, len3, len4, move, ris, tlen;
   unsigned char class;
   unsigned long tag;
-  int indefinite, result;
+  int indefinite, result, total_len = ider_len;
   const unsigned char *der = ider;
 
   node = *element;
@@ -929,7 +930,7 @@ asn1_der_decoding (asn1_node * element, const void *ider, 
int ider_len,
          if (p->type & CONST_SET)
            {
              p2 = _asn1_find_up (p);
-             len2 = _asn1_strtol (p2->value, NULL, 10);
+             len2 = p2->tmp_ival;
              if (len2 == -1)
                {
                  if (HAVE_TWO(ider_len) && !der[counter] && !der[counter + 1])
@@ -978,10 +979,14 @@ asn1_der_decoding (asn1_node * element, const void *ider, 
int ider_len,
                }
            }
 
+         /* the position in the DER structure this starts */
+         p->start = counter;
+         p->end = total_len;
+
          if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
            {
              p2 = _asn1_find_up (p);
-             len2 = _asn1_strtol (p2->value, NULL, 10);
+             len2 = p2->tmp_ival;
              if (counter == len2)
                {
                  if (p->right)
@@ -1037,12 +1042,14 @@ asn1_der_decoding (asn1_node * element, const void 
*ider, int ider_len,
                }
              else if (type_field (p->type) != ASN1_ETYPE_CHOICE)
                p = p->down;
+
+             p->start = counter;
            }
 
          if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
            {
              p2 = _asn1_find_up (p);
-             len2 = _asn1_strtol (p2->value, NULL, 10);
+             len2 = p2->tmp_ival;
 
              if ((len2 != -1) && (counter > len2))
                ris = ASN1_TAG_ERROR;
@@ -1208,8 +1215,8 @@ asn1_der_decoding (asn1_node * element, const void *ider, 
int ider_len,
            case ASN1_ETYPE_SET:
              if (move == UP)
                {
-                 len2 = _asn1_strtol (p->value, NULL, 10);
-                 _asn1_set_value (p, NULL, 0);
+                 len2 = p->tmp_ival;
+                 p->tmp_ival = 0;
                  if (len2 == -1)
                    {           /* indefinite length method */
                      DECR_LEN(ider_len, 2);
@@ -1248,10 +1255,7 @@ asn1_der_decoding (asn1_node * element, const void 
*ider, int ider_len,
 
                  if (len3 > 0)
                    {
-                     _asn1_ltostr (counter + len3, temp);
-                     tlen = strlen (temp);
-                     if (tlen > 0)
-                       _asn1_set_value (p, temp, tlen + 1);
+                     p->tmp_ival = counter + len3;
                      move = DOWN;
                    }
                  else if (len3 == 0)
@@ -1272,7 +1276,7 @@ asn1_der_decoding (asn1_node * element, const void *ider, 
int ider_len,
                    }
                  else
                    {           /* indefinite length method */
-                     _asn1_set_value (p, "-1", 3);
+                     p->tmp_ival = -1;
                      move = DOWN;
                    }
                }
@@ -1281,7 +1285,7 @@ asn1_der_decoding (asn1_node * element, const void *ider, 
int ider_len,
            case ASN1_ETYPE_SET_OF:
              if (move == UP)
                {
-                 len2 = _asn1_strtol (p->value, NULL, 10);
+                 len2 = p->tmp_ival;
                  if (len2 == -1)
                    {           /* indefinite length method */
                      if (!HAVE_TWO(ider_len) || ((der[counter]) || der[counter 
+ 1]))
@@ -1294,7 +1298,7 @@ asn1_der_decoding (asn1_node * element, const void *ider, 
int ider_len,
                          continue;
                        }
 
-                     _asn1_set_value (p, NULL, 0);
+                     p->tmp_ival = 0;
                      DECR_LEN(ider_len, 2);
                      counter += 2;
                    }
@@ -1310,7 +1314,7 @@ asn1_der_decoding (asn1_node * element, const void *ider, 
int ider_len,
                          continue;
                        }
 
-                     _asn1_set_value (p, NULL, 0);
+                     p->tmp_ival = 0;
                      if (len2 != counter)
                        {
                          result = ASN1_DER_ERROR;
@@ -1336,15 +1340,11 @@ asn1_der_decoding (asn1_node * element, const void 
*ider, int ider_len,
                    {
                      if (len3 > 0)
                        {       /* definite length method */
-                         _asn1_ltostr (counter + len3, temp);
-                         tlen = strlen (temp);
-
-                         if (tlen > 0)
-                           _asn1_set_value (p, temp, tlen + 1);
+                         p->tmp_ival = counter + len3;
                        }
                      else
                        {       /* indefinite length method */
-                         _asn1_set_value (p, "-1", 3);
+                         p->tmp_ival = -1;
                        }
                      p2 = p->down;
                      while ((type_field (p2->type) == ASN1_ETYPE_TAG)
@@ -1440,6 +1440,11 @@ asn1_der_decoding (asn1_node * element, const void 
*ider, int ider_len,
            }
        }
 
+      if (p)
+        {
+         p->end = counter - 1;
+        }
+
       if (p == node && move != DOWN)
        break;
 
@@ -1542,12 +1547,8 @@ int
 asn1_der_decoding_startEnd (asn1_node element, const void *ider, int ider_len,
                            const char *name_element, int *start, int *end)
 {
-  asn1_node node, node_to_find, p, p2;
-  int counter, len2, len3, len4, move, ris;
-  unsigned char class;
-  unsigned long tag;
-  int indefinite, result = ASN1_DER_ERROR;
-  const unsigned char *der = ider;
+  asn1_node node, node_to_find;
+  int result = ASN1_DER_ERROR;
 
   node = element;
 
@@ -1559,377 +1560,28 @@ asn1_der_decoding_startEnd (asn1_node element, const 
void *ider, int ider_len,
   if (node_to_find == NULL)
     return ASN1_ELEMENT_NOT_FOUND;
 
-  if (node_to_find == node)
-    {
-      *start = 0;
-      *end = ider_len - 1;
-      return ASN1_SUCCESS;
-    }
+  *start = node_to_find->start;
+  *end = node_to_find->end;
 
-  if (node->type & CONST_OPTION)
-    return ASN1_GENERIC_ERROR;
-
-  counter = 0;
-  move = DOWN;
-  p = node;
-  while (1)
+  if (*start == 0 && *end == 0)
     {
-      if (p == NULL)
-       return ASN1_DER_ERROR;
-
-      ris = ASN1_SUCCESS;
-
-      if (move != UP)
-       {
-         if (p->type & CONST_SET)
-           {
-             p2 = _asn1_find_up (p);
-             if (p2 == NULL)
-               {
-                 warn();
-                 return ASN1_DER_ERROR;
-               }
-
-             len2 = _asn1_strtol (p2->value, NULL, 10);
-             if (len2 == -1)
-               {
-                 if (HAVE_TWO(ider_len) && !der[counter] && !der[counter + 1])
-                   {
-                     p = p2;
-                     move = UP;
-                     counter += 2;
-                     DECR_LEN(ider_len, 2);
-                     continue;
-                   }
-               }
-             else if (counter == len2)
-               {
-                 p = p2;
-                 move = UP;
-                 continue;
-               }
-             else if (counter > len2)
-               {
-                 warn();
-                 return ASN1_DER_ERROR;
-               }
-
-             p2 = p2->down;
-
-             while (p2)
-               {
-                 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
-                   {           /* CONTROLLARE */
-                     ris =
-                         extract_tag_der_recursive (p2, der + counter,
-                                                ider_len, &len2);
-                     if (ris == ASN1_SUCCESS)
-                       {
-                         p2->type &= ~CONST_NOT_USED;
-                         p = p2;
-                         break;
-                       }
-                   }
-                 p2 = p2->right;
-               }
-             if (p2 == NULL)
-               {
-                 warn();
-                 return ASN1_DER_ERROR;
-               }
-           }
-
-         if (p == node_to_find)
-           *start = counter;
-
-         if (type_field (p->type) == ASN1_ETYPE_CHOICE)
-           {
-             p = p->down;
-             if (p == NULL)
-               {
-                 warn();
-                 return ASN1_DER_ERROR;
-               }
-
-             ris =
-               _asn1_extract_tag_der (p, der + counter, ider_len,
-                                      &len2);
-             if (p == node_to_find)
-               *start = counter;
-           }
-
-         if (ris == ASN1_SUCCESS)
-           ris =
-             _asn1_extract_tag_der (p, der + counter, ider_len, &len2);
-         if (ris != ASN1_SUCCESS)
-           {
-             if (p->type & CONST_OPTION)
-               {
-                 p->type |= CONST_NOT_USED;
-                 move = RIGHT;
-               }
-             else if (p->type & CONST_DEFAULT)
-               {
-                 move = RIGHT;
-               }
-             else
-               {
-                 warn();
-                 return ASN1_TAG_ERROR;
-               }
-           }
-         else
-           {
-             DECR_LEN(ider_len, len2);
-             counter += len2;
-           }
-       }
-
-      if (ris == ASN1_SUCCESS)
-       {
-         switch (type_field (p->type))
-           {
-           case ASN1_ETYPE_NULL:
-              DECR_LEN(ider_len, 1);
-
-             if (der[counter])
-               {
-                 warn();
-                 return ASN1_DER_ERROR;
-               }
-             counter++;
-             move = RIGHT;
-             break;
-           case ASN1_ETYPE_BOOLEAN:
-              DECR_LEN(ider_len, 2);
-
-             if (der[counter] != 1)
-               {
-                 warn();
-                 return ASN1_DER_ERROR;
-               }
-
-             counter += 2;
-             move = RIGHT;
-             break;
-           case ASN1_ETYPE_OCTET_STRING:
-             ris = _asn1_get_octet_string (NULL, der + counter, ider_len, 
&len3);
-             if (ris != ASN1_SUCCESS)
-               {
-                 warn();
-                 return ris;
-               }
-              DECR_LEN(ider_len, len3);
-             counter += len3;
-             move = RIGHT;
-             break;
-           case ASN1_ETYPE_UTC_TIME:
-           case ASN1_ETYPE_GENERALIZED_TIME:
-           case ASN1_ETYPE_OBJECT_ID:
-           case ASN1_ETYPE_INTEGER:
-           case ASN1_ETYPE_ENUMERATED:
-           case ASN1_ETYPE_GENERALSTRING:
-           case ASN1_ETYPE_NUMERIC_STRING:
-           case ASN1_ETYPE_IA5_STRING:
-           case ASN1_ETYPE_TELETEX_STRING:
-           case ASN1_ETYPE_PRINTABLE_STRING:
-           case ASN1_ETYPE_UNIVERSAL_STRING:
-           case ASN1_ETYPE_BMP_STRING:
-           case ASN1_ETYPE_UTF8_STRING:
-           case ASN1_ETYPE_VISIBLE_STRING:
-           case ASN1_ETYPE_BIT_STRING:
-             len2 =
-               asn1_get_length_der (der + counter, ider_len, &len3);
-             if (len2 < 0)
-               {
-                 warn();
-                 return ASN1_DER_ERROR;
-               }
-
-              DECR_LEN(ider_len, len3 + len2);
-             counter += len3 + len2;
-             move = RIGHT;
-             break;
-           case ASN1_ETYPE_SEQUENCE:
-           case ASN1_ETYPE_SET:
-             if (move != UP)
-               {
-                 len3 =
-                   asn1_get_length_der (der + counter, ider_len, &len2);
-                 if (len3 < -1)
-                   {
-                     warn();
-                     return ASN1_DER_ERROR;
-                   }
-
-                  DECR_LEN(ider_len, len2);
-                 counter += len2;
-
-                 if (len3 == 0)
-                   move = RIGHT;
-                 else
-                   move = DOWN;
-               }
-             else
-               {
-                 if (HAVE_TWO(ider_len) && !der[counter] && !der[counter + 1]) 
/* indefinite length method */
-                   {
-                     counter += 2;
-                     DECR_LEN(ider_len, 2);
-                   }
-                 move = RIGHT;
-               }
-             break;
-           case ASN1_ETYPE_SEQUENCE_OF:
-           case ASN1_ETYPE_SET_OF:
-             if (move != UP)
-               {
-                 len3 =
-                   asn1_get_length_der (der + counter, ider_len, &len2);
-                 if (len3 < -1)
-                   {
-                     warn();
-                     return ASN1_DER_ERROR;
-                   }
-
-                  DECR_LEN(ider_len, len2);
-                 counter += len2;
-
-                 if (len3 == -1)
-                   {
-                      if (HAVE_TWO(ider_len) && !der[counter] && !der[counter 
+ 1])
-                        {
-                          DECR_LEN(ider_len, 2);
-                          counter += 2;
-                        }
-                   }
-
-                 if (len3)
-                   {
-                     p2 = p->down;
-                     while ((type_field (p2->type) == ASN1_ETYPE_TAG) ||
-                            (type_field (p2->type) == ASN1_ETYPE_SIZE))
-                       p2 = p2->right;
-                     p = p2;
-                   }
-               }
-             else
-               {
-                 if (HAVE_TWO(ider_len) && !der[counter] && !der[counter + 1]) 
/* indefinite length method */
-                   {
-                     DECR_LEN(ider_len, 2);
-                     counter += 2;
-                   }
-               }
-             move = RIGHT;
-             break;
-           case ASN1_ETYPE_ANY:
-             if (asn1_get_tag_der
-                 (der + counter, ider_len, &class, &len2,
-                  &tag) != ASN1_SUCCESS)
-                {
-                   warn();
-                   return ASN1_DER_ERROR;
-                }
-
-             DECR_LEN(ider_len, len2);
-
-             len4 =
-               asn1_get_length_der (der + counter + len2,
-                                    ider_len, &len3);
-             if (len4 < -1)
-               {
-                 warn();
-                 return ASN1_DER_ERROR;
-               }
-
-             if (len4 != -1)
-               {
-                 DECR_LEN(ider_len, len3 + len4);
-                 counter += len2 + len3 + len4;
-               }
-             else
-               {               /* indefinite length */
-                 /* Check indefinite lenth method in an EXPLICIT TAG */
-                 ider_len += len2; /* undo DECR_LEN */
-
-                 if (counter == 0)
-                   {
-                     result = ASN1_DER_ERROR;
-                      warn();
-                     goto cleanup;
-                   }
-
-                 if ((p->type & CONST_TAG) && (der[counter - 1] == 0x80))
-                   indefinite = 1;
-                 else
-                   indefinite = 0;
-
-                 ris =
-                   _asn1_get_indefinite_length_string (der + counter, 
ider_len, &len2);
-                 if (ris != ASN1_SUCCESS)
-                   {
-                     warn();
-                     return ris;
-                   }
-                 counter += len2;
-                 DECR_LEN(ider_len, len2);
-
-                 /* Check if a couple of 0x00 are present due to an EXPLICIT 
TAG with
-                    an indefinite length method. */
-                 if (indefinite)
-                   {
-                     DECR_LEN(ider_len, 2);
-
-                     if (!der[counter] && !der[counter + 1])
-                       counter += 2;
-                     else
-                       {
-                         warn();
-                         return ASN1_DER_ERROR;
-                       }
-                   }
-               }
-             move = RIGHT;
-             break;
-           default:
-             move = (move == UP) ? RIGHT : DOWN;
-             break;
-           }
-       }
+      /* it seems asn1_der_decoding() wasn't called before. Do it now */
+      result = asn1_der_decoding (&node, ider, ider_len, NULL);
+      if (result != ASN1_SUCCESS)
+        {
+          warn();
+          return result;
+        }
 
-      if ((p == node_to_find) && (move == RIGHT))
-       {
-         *end = counter - 1;
-         return ASN1_SUCCESS;
-       }
+      node_to_find = asn1_find_node (node, name_element);
+      if (node_to_find == NULL)
+        return ASN1_ELEMENT_NOT_FOUND;
 
-      if (p == node && move != DOWN)
-       break;
-
-      if (move == DOWN)
-       {
-         if (p->down)
-           p = p->down;
-         else
-           move = RIGHT;
-       }
-      if ((move == RIGHT) && !(p->type & CONST_SET))
-       {
-         if (p->right)
-           p = p->right;
-         else
-           move = UP;
-       }
-      if (move == UP)
-       p = _asn1_find_up (p);
+      *start = node_to_find->start;
+      *end = node_to_find->end;
     }
 
-  warn();
-  return ASN1_ELEMENT_NOT_FOUND;
-
-cleanup:
-  return result;
+  return ASN1_SUCCESS;
 }
 
 /**
diff --git a/lib/int.h b/lib/int.h
index 3701e5f..82d0d3f 100644
--- a/lib/int.h
+++ b/lib/int.h
@@ -56,6 +56,11 @@ struct asn1_node_st
   asn1_node left;              /* Pointer to the next list element */
   /* private fields: */
   unsigned char small_value[ASN1_SMALL_VALUE_SIZE];    /* For small values */
+
+  /* values used during decoding/coding */
+  int tmp_ival;
+  unsigned start; /* the start of the DER sequence - if decoded */
+  unsigned end; /* the end of the DER sequence - if decoded */
 };
 
 typedef struct tag_and_class_st
diff --git a/lib/libtasn1.h b/lib/libtasn1.h
index a457068..064c10f 100644
--- a/lib/libtasn1.h
+++ b/lib/libtasn1.h
@@ -329,6 +329,8 @@ extern "C"
   extern ASN1_API int
     asn1_copy_node (asn1_node dst, const char *dst_name,
                    asn1_node src, const char *src_name);
+  extern ASN1_API asn1_node
+    asn1_copy_node2 (asn1_node src, const char *src_name);
 
   /* Internal and low-level DER utility functions. */
 
diff --git a/lib/libtasn1.map b/lib/libtasn1.map
index fa91638..a2b535e 100644
--- a/lib/libtasn1.map
+++ b/lib/libtasn1.map
@@ -54,6 +54,7 @@ LIBTASN1_0_3
     asn1_decode_simple_der;
     asn1_read_value_type;
     asn1_delete_structure2;
+    asn1_copy_node2;
   local:
     *;
 };
diff --git a/lib/structure.c b/lib/structure.c
index 27fbfe3..2184aa8 100644
--- a/lib/structure.c
+++ b/lib/structure.c
@@ -1128,12 +1128,13 @@ asn1_find_structure_from_oid (asn1_node definitions, 
const char *oidValue)
 
 /**
  * asn1_copy_node:
- * @dst: Destination asn1_node node.
+ * @dst: Destination asn1 node.
  * @dst_name: Field name in destination node.
- * @src: Source asn1_node node.
+ * @src: Source asn1 node.
  * @src_name: Field name in source node.
  *
- * Create a deep copy of a asn1_node variable.
+ * Create a deep copy of a asn1_node variable. That
+ * function requires @dst to be expanded using asn1_create_element().
  *
  * Returns: Return %ASN1_SUCCESS on success.
  **/
@@ -1141,9 +1142,6 @@ int
 asn1_copy_node (asn1_node dst, const char *dst_name,
                asn1_node src, const char *src_name)
 {
-/* FIXME: rewrite using copy_structure().
- * It seems quite hard to do.
- */
   int result;
   asn1_node dst_node;
   void *data = NULL;
@@ -1177,3 +1175,19 @@ asn1_copy_node (asn1_node dst, const char *dst_name,
 
   return result;
 }
+
+/**
+ * asn1_copy_node2:
+ * @src: Source asn1 node.
+ * @src_name: Field name in source node.
+ *
+ * Create a deep copy of a asn1_node variable. This function
+ * will return an exact copy of the provided structure.
+ *
+ * Returns: Return %NULL on failure.
+ **/
+asn1_node
+asn1_copy_node2 (asn1_node src, const char *src_name)
+{
+  return _asn1_copy_structure2(src, src_name);
+}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0277c70..d6c850a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -35,11 +35,11 @@ MOSTLYCLEANFILES = Test_parser_ERROR.asn
 
 check_PROGRAMS = Test_parser Test_tree Test_encoding Test_indefinite \
        Test_errors Test_simple Test_overflow Test_strings Test_choice \
-       Test_encdec
+       Test_encdec copynode
 
 TESTS = Test_parser Test_tree Test_encoding Test_indefinite    \
        Test_errors Test_simple Test_overflow crlf threadsafety \
-       Test_strings Test_choice Test_encdec
+       Test_strings Test_choice Test_encdec copynode
 
 TESTS_ENVIRONMENT = \
        ASN1PARSER=$(srcdir)/Test_parser.asn \
diff --git a/tests/Test_encdec.c b/tests/Test_encdec.c
index 363f2ff..9b49e72 100644
--- a/tests/Test_encdec.c
+++ b/tests/Test_encdec.c
@@ -47,6 +47,7 @@ main (int argc, char *argv[])
   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
   FILE *out, *fd;
   ssize_t size;
+  int start, end;
   int size2;
   const char *treefile = getenv ("ASN1PKIX");
   const char *derfile = getenv ("ASN1CRLDER");
@@ -116,6 +117,45 @@ main (int argc, char *argv[])
       exit (1);
     }
 
+  result = asn1_der_decoding_startEnd (asn1_element, buffer, size, 
"tbsCertList", &start, &end);
+  if (result != ASN1_SUCCESS)
+    {
+      asn1_perror (result);
+      printf ("Cannot find start End\n");
+      exit (1);
+    }
+  if (start != 4 && end != 358)
+    {
+      printf("Error in start and end values. Have: %d..%d\n", start, end);
+      exit(1);
+    }
+
+  result = asn1_der_decoding_startEnd (asn1_element, buffer, size, 
"signature", &start, &end);
+  if (result != ASN1_SUCCESS)
+    {
+      asn1_perror (result);
+      printf ("Cannot find start End\n");
+      exit (1);
+    }
+  if (start != 372 && end != 503)
+    {
+      printf("Error in start and end values for signature. Have: %d..%d\n", 
start, end);
+      exit(1);
+    }
+
+  result = asn1_der_decoding_startEnd (asn1_element, buffer, size, 
"tbsCertList.revokedCertificates.?1.userCertificate", &start, &end);
+  if (result != ASN1_SUCCESS)
+    {
+      asn1_perror (result);
+      printf ("Cannot find start End\n");
+      exit (1);
+    }
+  if (start != 326 && end != 343)
+    {
+      printf("Error in start and end values for userCertificate. Have: 
%d..%d\n", start, end);
+      exit(1);
+    }
+
   size2 = sizeof(buffer2);
   result = asn1_der_coding (asn1_element, "", buffer2, &size2, NULL);
   if (result != ASN1_SUCCESS)
diff --git a/tests/Test_encdec.c b/tests/copynode.c
similarity index 74%
copy from tests/Test_encdec.c
copy to tests/copynode.c
index 363f2ff..5e5821f 100644
--- a/tests/Test_encdec.c
+++ b/tests/copynode.c
@@ -43,7 +43,7 @@ main (int argc, char *argv[])
   char buffer[5 * 1024];
   char buffer2[5 * 1024];
   asn1_node definitions = NULL;
-  asn1_node asn1_element = NULL;
+  asn1_node asn1_element = NULL, cpy_node = NULL;
   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
   FILE *out, *fd;
   ssize_t size;
@@ -116,8 +116,53 @@ main (int argc, char *argv[])
       exit (1);
     }
 
+  /* test asn1_copy_node */
+  result =
+    asn1_create_element (definitions, "PKIX1.CertificateList", &cpy_node);
+  if (result != ASN1_SUCCESS)
+    {
+      asn1_perror (result);
+      printf ("Cannot create CRL element\n");
+      exit (1);
+    }
+
+  result = asn1_copy_node(cpy_node, "", asn1_element, "");
+  if (result != ASN1_SUCCESS)
+    {
+      asn1_perror (result);
+      printf ("Cannot copy node\n");
+      exit (1);
+    }
+
+  /* test whether the copied node encodes the same */
   size2 = sizeof(buffer2);
-  result = asn1_der_coding (asn1_element, "", buffer2, &size2, NULL);
+  result = asn1_der_coding (cpy_node, "", buffer2, &size2, NULL);
+  if (result != ASN1_SUCCESS)
+    {
+      asn1_perror (result);
+      printf ("Cannot encode data (size %ld)\n", (long) size);
+      exit (1);
+    }
+ 
+  if (size2 != size || memcmp(buffer, buffer2, size) != 0) 
+    {
+      printf("DER encoded data differ!\n");
+      exit(1);
+    }
+
+  asn1_delete_structure (&cpy_node);
+
+  /* Test asn1_copy_node2 */
+  cpy_node = asn1_copy_node2(asn1_element, "");
+  if (cpy_node == NULL)
+    {
+      printf ("Cannot copy node (copy_node2)\n");
+      exit (1);
+    }
+
+  /* test whether the copied node encodes the same */
+  size2 = sizeof(buffer2);
+  result = asn1_der_coding (cpy_node, "", buffer2, &size2, NULL);
   if (result != ASN1_SUCCESS)
     {
       asn1_perror (result);
@@ -132,8 +177,9 @@ main (int argc, char *argv[])
     }
 
   /* Clear the definition structures */
-  asn1_delete_structure (&definitions);
   asn1_delete_structure (&asn1_element);
+  asn1_delete_structure (&cpy_node);
+  asn1_delete_structure (&definitions);
 
   if (out != stdout)
     fclose (out);


hooks/post-receive
-- 
GNU libtasn1



reply via email to

[Prev in Thread] Current Thread [Next in Thread]