Commit d0c9fa2b authored by Nikos Mavrogiannopoulos's avatar Nikos Mavrogiannopoulos

Merge branch 'tmp-simplify-setof-sorting' into 'master'

simplify sorting of setof items

See merge request !2
parents 750f03a3 0dcbfb31
Pipeline #18488622 failed with stage
in 77 minutes and 29 seconds
......@@ -830,6 +830,39 @@ error:
return err;
}
struct vet
{
unsigned char *ptr;
int size;
};
static int setof_compar(const void *_e1, const void *_e2)
{
unsigned length;
const struct vet *e1 = _e1, *e2 = _e2;
int rval;
/* The encodings of the component values of a set-of value shall
* appear in ascending order, the encodings being compared
* as octet strings with the shorter components being
* padded at their trailing end with 0-octets.
* The padding octets are for comparison purposes and
* do not appear in the encodings.
*/
length = MIN(e1->size, e2->size);
rval = memcmp(e1->ptr, e2->ptr, length);
if (rval == 0 && e1->size != e2->size)
{
if (e1->size > e2->size)
rval = 1;
else if (e2->size > e1->size)
rval = -1;
}
return rval;
}
/******************************************************/
/* Function : _asn1_ordering_set_of */
/* Description: puts the elements of a SET OF type in */
......@@ -844,17 +877,14 @@ error:
static int
_asn1_ordering_set_of (unsigned char *der, int der_len, asn1_node node)
{
struct vet
{
int end;
struct vet *next, *prev;
};
int counter, len, len2, change;
struct vet *first, *last, *p_vet, *p2_vet;
int counter, len, len2;
struct vet *list = NULL, *tlist;
unsigned list_size = 0;
struct vet *p_vet;
asn1_node p;
unsigned char *temp, class;
unsigned long k, length;
unsigned char class;
unsigned i;
unsigned char *out = NULL;
int err;
counter = 0;
......@@ -873,33 +903,30 @@ _asn1_ordering_set_of (unsigned char *der, int der_len, asn1_node node)
if ((p == NULL) || (p->right == NULL))
return ASN1_SUCCESS;
first = last = NULL;
while (p)
{
p_vet = malloc (sizeof (struct vet));
if (p_vet == NULL)
list_size++;
tlist = realloc (list, list_size*sizeof(struct vet));
if (tlist == NULL)
{
err = ASN1_MEM_ALLOC_ERROR;
goto error;
}
list = tlist;
p_vet = &list[list_size-1];
p_vet->next = NULL;
p_vet->prev = last;
if (first == NULL)
first = p_vet;
else
last->next = p_vet;
last = p_vet;
p_vet->ptr = der+counter;
p_vet->size = 0;
/* extraction of tag and length */
if (der_len - counter > 0)
{
err = asn1_get_tag_der (der + counter, der_len - counter, &class,
&len, NULL);
if (err != ASN1_SUCCESS)
goto error;
counter += len;
p_vet->size += len;
len2 = asn1_get_length_der (der + counter, der_len - counter, &len);
if (len2 < 0)
......@@ -908,84 +935,46 @@ _asn1_ordering_set_of (unsigned char *der, int der_len, asn1_node node)
goto error;
}
counter += len + len2;
p_vet->size += len + len2;
}
else
{
err = ASN1_DER_ERROR;
goto error;
}
p_vet->end = counter;
p = p->right;
}
p_vet = first;
while (p_vet)
if (counter > der_len)
{
p2_vet = p_vet->next;
counter = 0;
while (p2_vet)
{
length = MIN(p_vet->end - counter, p2_vet->end - p_vet->end);
change = -1;
for (k = 0; k < length; k++)
if (der[counter + k] > der[p_vet->end + k])
{
change = 1;
break;
}
else if (der[counter + k] < der[p_vet->end + k])
{
change = 0;
break;
}
if ((change == -1)
&& ((p_vet->end - counter) > (p2_vet->end - p_vet->end)))
change = 1;
if (change == 1)
{
/* change position */
temp = malloc (p_vet->end - counter);
if (temp == NULL)
{
err = ASN1_MEM_ALLOC_ERROR;
goto error;
}
err = ASN1_DER_ERROR;
goto error;
}
memcpy (temp, der + counter, (p_vet->end) - counter);
memcpy (der + counter, der + (p_vet->end),
(p2_vet->end) - (p_vet->end));
memcpy (der + counter + (p2_vet->end) - (p_vet->end), temp,
(p_vet->end) - counter);
free (temp);
qsort(list, list_size, sizeof(struct vet), setof_compar);
p_vet->end = counter + (p2_vet->end - p_vet->end);
}
counter = p_vet->end;
out = malloc(der_len);
if (out == NULL)
{
err = ASN1_MEM_ERROR;
goto error;
}
p2_vet = p2_vet->next;
p_vet = p_vet->next;
}
/* the sum of p_vet->size == der_len */
counter = 0;
for (i=0;i<list_size;i++) {
p_vet = &list[i];
memcpy(out+counter, p_vet->ptr, p_vet->size);
counter += p_vet->size;
}
memcpy(der, out, der_len);
free(out);
if (p_vet != first)
p_vet->prev->next = NULL;
else
first = NULL;
free (p_vet);
p_vet = first;
}
return ASN1_SUCCESS;
err = ASN1_SUCCESS;
error:
while (first != NULL)
{
p_vet = first;
first = first->next;
free(p_vet);
}
free(list);
return err;
}
......
......@@ -25,7 +25,7 @@ LDADD = ../lib/libtasn1.la ../gl/libgnu.la
EXTRA_DIST = Test_parser.asn Test_tree.asn Test_tree_asn1_tab.c mscat.asn \
Test_encoding.asn pkix.asn TestIndef.p12 choice.asn coding-decoding2.asn \
TestIndef2.p12 TestIndef3.der TestCertOctetOverflow.der \
cert-invalid-time.der \
cert-invalid-time.der setof.asn \
libtasn1.supp ocsp-basic-response.der spc_pe_image_data.der \
invalid-x509/id-000000.der invalid-x509/id-000001.der \
invalid-x509/id-000002.der invalid-x509/id-000003.der \
......@@ -60,14 +60,14 @@ check_PROGRAMS = Test_parser Test_tree Test_encoding Test_indefinite \
Test_errors Test_simple Test_overflow Test_strings Test_choice \
Test_encdec copynode coding-decoding2 strict-der Test_choice_ocsp \
ocsp-basic-response octet-string coding-long-oid object-id-decoding \
spc_pe_image_data
spc_pe_image_data setof
TESTS = Test_parser Test_tree Test_encoding Test_indefinite \
Test_errors Test_simple Test_overflow crlf threadsafety \
Test_strings Test_choice Test_encdec copynode coding-decoding2 \
strict-der Test_choice_ocsp decoding decoding-invalid-x509 \
ocsp-basic-response octet-string coding-long-oid object-id-decoding \
spc_pe_image_data decoding-invalid-pkcs7 coding
spc_pe_image_data decoding-invalid-pkcs7 coding setof
TESTS_ENVIRONMENT = \
ASN1PARSER=$(srcdir)/Test_parser.asn \
......@@ -75,6 +75,7 @@ TESTS_ENVIRONMENT = \
ASN1CHOICE=$(srcdir)/choice.asn \
ASN1CODINGDECODING2=$(srcdir)/coding-decoding2.asn \
ASN1PKIX=$(srcdir)/pkix.asn \
ASN1SETOF=$(srcdir)/setof.asn \
ASN1CRLDER=$(srcdir)/crl.der \
ASN1INDEF=$(srcdir)/TestIndef.p12 \
ASN1INDEF2=$(srcdir)/TestIndef2.p12 \
......
TEST {}
DEFINITIONS IMPLICIT TAGS ::=
BEGIN
Seq ::= SEQUENCE {
val OCTET STRING
}
Set ::= SET OF Seq
END
/*
* Copyright (C) 2002-2018 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/****************************************************************/
/* File: Test_setof.c */
/* Description: Test writing of SET OF values and DER encoding. */
/****************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "libtasn1.h"
static unsigned char expected_der[] = {
0x31, 0x40, 0x30, 0x03, 0x04, 0x01, 0x01, 0x30,
0x03, 0x04, 0x01, 0x05, 0x30, 0x04, 0x04, 0x02,
0x00, 0x02, 0x30, 0x06, 0x04, 0x04, 0x00, 0x00,
0x00, 0x00, 0x30, 0x06, 0x04, 0x04, 0x00, 0x00,
0x00, 0x01, 0x30, 0x06, 0x04, 0x04, 0x00, 0x00,
0x00, 0x02, 0x30, 0x06, 0x04, 0x04, 0x00, 0x01,
0x00, 0x00, 0x30, 0x06, 0x04, 0x04, 0x01, 0x00,
0x00, 0x00, 0x30, 0x06, 0x04, 0x04, 0x01, 0x01,
0x00, 0x00
};
static unsigned char data[1024];
int data_size = sizeof (data);
int
main (int argc, char *argv[])
{
int result, verbose = 0;
asn1_node definitions = NULL;
asn1_node asn1_element = NULL;
char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
const char *treefile = getenv ("ASN1SETOF");
unsigned i;
if (argc > 1)
verbose = 1;
if (!treefile)
treefile = "setof.asn";
if (verbose != 0)
{
printf ("\n\n/****************************************/\n");
printf ("/* Test sequence : coding-decoding */\n");
printf ("/****************************************/\n\n");
}
/* Check version */
if (asn1_check_version ("0.3.3") == NULL)
printf ("\nLibrary version check ERROR:\n actual version: %s\n\n",
asn1_check_version (NULL));
result = asn1_parser2tree (treefile, &definitions, errorDescription);
if (result != ASN1_SUCCESS)
{
asn1_perror (result);
printf ("ErrorDescription = %s\n\n", errorDescription);
exit (1);
}
result = asn1_create_element (definitions, "TEST.Set", &asn1_element);
if (result != ASN1_SUCCESS)
{
fprintf (stderr, "asn1_create_element(): ");
asn1_perror (result);
exit (1);
}
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x02", 2);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x01\x00\x00", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x00\x00\x00", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x00\x00\x02", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x00\x00\x01", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x01\x00\x00\x00", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x01\x01\x00\x00", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x05", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x01", 1);
assert(result == ASN1_SUCCESS);
/* Clear the definition structures */
result = asn1_der_coding (asn1_element, "", data, &data_size, NULL);
if (result != ASN1_SUCCESS)
{
fprintf (stderr, "Encoding error.\n");
asn1_perror (result);
exit (1);
}
asn1_delete_structure (&asn1_element);
asn1_delete_structure (&definitions);
if (data_size != sizeof(expected_der) || memcmp(data, expected_der, data_size) != 0)
{
fprintf(stderr, "encoded data differ to expected [%d - %d]!\n", data_size, (int)sizeof(expected_der));
printf("static unsigned char got[] = {\n");
for (i=0;i<(unsigned)data_size;i++) {
printf("0x%.2x, ", (unsigned)data[i]);
if ((i+1) % 8 == 0)
printf("\n");
}
printf("};\n");
printf("static unsigned char expected[] = {\n");
for (i=0;i<(unsigned)sizeof(expected_der);i++) {
printf("0x%.2x, ", (unsigned)expected_der[i]);
if ((i+1) % 8 == 0)
printf("\n");
}
printf("};\n");
exit(1);
}
if (verbose)
printf ("Success\n");
exit (0);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment