summaryrefslogtreecommitdiffstats
path: root/src/tests/asn.1/utility.c
blob: 00d7f6298b3cc8608b7891870da376b2749d880c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "utility.h"
#include "krb5.h"
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

krb5int_access acc;

char hexchar (const unsigned int digit);

asn1_error_code asn1_krb5_data_unparse(code, s)
    const krb5_data * code;
    char ** s;
{
    if (*s != NULL) free(*s);
  
    if (code==NULL) {
	*s = strdup("<NULL>");
	if (*s == NULL) return ENOMEM;
    } else if (code->data == NULL || ((int) code->length) <= 0) {
	*s = strdup("<EMPTY>");
	if (*s==NULL) return ENOMEM;
    } else {
	unsigned int i;

	*s = (char*)calloc((size_t) 3*(code->length), sizeof(char));
	if (*s == NULL) return ENOMEM;
	for (i = 0; i < code->length; i++) {
	    (*s)[3*i] = hexchar((unsigned char) (((code->data)[i]&0xF0)>>4));
	    (*s)[3*i+1] = hexchar((unsigned char) ((code->data)[i]&0x0F));
	    (*s)[3*i+2] = ' ';
	}
	(*s)[3*(code->length)-1] = '\0';
    }
    return 0;
}

char hexchar(digit)
    const unsigned int digit;
{
    if (digit<=9)
	return '0'+digit;
    else if (digit<=15)
	return 'A'+digit-10;
    else
	return 'X';
}

krb5_error_code krb5_data_parse(d, s)
    krb5_data * d;
    const char * s;
{
    /*if (d->data != NULL) {
      free(d->data);
      d->length = 0;
      }*/
    d->data = (char*)calloc(strlen(s),sizeof(char));
    if (d->data == NULL) return ENOMEM;
    d->length = strlen(s);
    memcpy(d->data,s,strlen(s));
    return 0;
}

krb5_error_code krb5_data_hex_parse(krb5_data *d, const char *s)
{
    int lo;
    long v;
    const char *cp;
    char *dp;
    char buf[2];

    d->data = calloc((strlen(s) / 2 + 1), 1);
    if (d->data == NULL)
	return ENOMEM;
    d->length = 0;
    buf[1] = '\0';
    for (lo = 0, dp = d->data, cp = s; *cp; cp++) {
	if (*cp < 0)
	    return ASN1_PARSE_ERROR;
	else if (isspace((unsigned char) *cp))
	    continue;
	else if (isxdigit((unsigned char) *cp)) {
	    buf[0] = *cp;
	    v = strtol(buf, NULL, 16);
	} else
	    return ASN1_PARSE_ERROR;
	if (lo) {
	    *dp++ |= v;
	    lo = 0;
	} else {
	    *dp = v << 4;
	    lo = 1;
	}
    }

    d->length = dp - d->data;
    return 0;
}

#if 0
void asn1buf_print(buf)
    const asn1buf * buf;
{
    asn1buf bufcopy;
    char *s=NULL;
    int length;
    int i;
  
    bufcopy.base = bufcopy.next = buf->next;
    bufcopy.bound = buf->bound;
    length = asn1buf_len(&bufcopy);

    s = calloc(3*length, sizeof(char));
    if (s == NULL) return;
    for (i=0; i<length; i++) {
	s[3*i] = hexchar(((bufcopy.base)[i]&0xF0)>>4);
	s[3*i+1] = hexchar((bufcopy.base)[i]&0x0F);
	s[3*i+2] = ' ';
    }
    s[3*length-1] = '\0';

    printf("%s\n",s);
    free(s);
}
#endif

void init_access(const char *progname)
{
    krb5_error_code ret;
    ret = krb5int_accessor(&acc, KRB5INT_ACCESS_VERSION);
    if (ret) {
	com_err(progname, ret, "while initializing accessor");
	exit(1);
    }
}