~ubuntu-branches/ubuntu/dapper/asn1c/dapper

« back to all changes in this revision

Viewing changes to libasn1parser/asn1p_oid.c

  • Committer: Bazaar Package Importer
  • Author(s): W. Borgert
  • Date: 2005-05-28 12:36:42 UTC
  • Revision ID: james.westby@ubuntu.com-20050528123642-3h6kstws5u0xcovl
Tags: upstream-0.9.14
ImportĀ upstreamĀ versionĀ 0.9.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
 
 
5
#include "asn1parser.h"
 
6
 
 
7
asn1p_oid_t *
 
8
asn1p_oid_new() {
 
9
        return calloc(1, sizeof(asn1p_oid_t));
 
10
}
 
11
 
 
12
int
 
13
asn1p_oid_add_arc(asn1p_oid_t *oid, asn1p_oid_arc_t *template) {
 
14
        void *p;
 
15
        p = realloc(oid->arcs, (oid->arcs_count + 1) * sizeof(oid->arcs[0]));
 
16
        if(p) {
 
17
                oid->arcs = p;
 
18
                oid->arcs[oid->arcs_count].name
 
19
                                = template->name?strdup(template->name):0;
 
20
                oid->arcs[oid->arcs_count].number = template->number;
 
21
                oid->arcs_count++;
 
22
                return 0;
 
23
        } else {
 
24
                return -1;
 
25
        }
 
26
}
 
27
 
 
28
void
 
29
asn1p_oid_free(asn1p_oid_t *oid) {
 
30
        if(oid) {
 
31
                if(oid->arcs) {
 
32
                        while(oid->arcs_count--) {
 
33
                                if(oid->arcs[oid->arcs_count].name)
 
34
                                free(oid->arcs[oid->arcs_count].name);
 
35
                        }
 
36
                }
 
37
                free(oid);
 
38
        }
 
39
}
 
40
 
 
41
asn1p_oid_arc_t *
 
42
asn1p_oid_arc_new(const char *optName, asn1c_integer_t optNumber /* = -1 */) {
 
43
        asn1p_oid_arc_t *arc;
 
44
 
 
45
        arc = calloc(1, sizeof *arc);
 
46
        if(arc) {
 
47
                if(optName)
 
48
                        arc->name = strdup(optName);
 
49
                arc->number = optNumber;
 
50
        }
 
51
 
 
52
        return arc;
 
53
}
 
54
 
 
55
void
 
56
asn1p_oid_arc_free(asn1p_oid_arc_t *arc) {
 
57
        if(arc) {
 
58
                if(arc->name)
 
59
                        free(arc->name);
 
60
                free(arc);
 
61
        }
 
62
}
 
63
 
 
64
int
 
65
asn1p_oid_compare(asn1p_oid_t *a, asn1p_oid_t *b) {
 
66
        int i;
 
67
 
 
68
        for(i = 0; ; i++) {
 
69
                asn1c_integer_t cmp;
 
70
 
 
71
                if(b->arcs_count > i) {
 
72
                        if(a->arcs_count <= i)
 
73
                                return -1;
 
74
                } else if(a->arcs_count > i) {
 
75
                        if(b->arcs_count <= i)
 
76
                                return 1;
 
77
                } else if(b->arcs_count <= i && a->arcs_count <= i) {
 
78
                        cmp = b->arcs_count - a->arcs_count;
 
79
                        if(cmp < 0)
 
80
                                return -1;
 
81
                        else if(cmp > 0)
 
82
                                return 1;
 
83
                        return 0;
 
84
                }
 
85
 
 
86
                cmp = b->arcs[i].number - a->arcs[i].number;
 
87
                if(cmp < 0)
 
88
                        return -1;
 
89
                else if(cmp > 0)
 
90
                        return 1;
 
91
        }
 
92
 
 
93
}
 
94
 
 
95