aboutsummaryrefslogtreecommitdiffstats
path: root/asn1c/tests/check-48.c
blob: 510a259ea187773656fed67a497915314e04faf7 (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
#undef	NDEBUG
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include <errno.h>

#include <UserIdentifier.h>

static unsigned char buf[4096];
static int buf_offset;

static int
_buf_writer(const void *buffer, size_t size, void *app_key) {
	unsigned char *b, *bend;
	(void)app_key;
	assert(buf_offset + size < sizeof(buf));
	memcpy(buf + buf_offset, buffer, size);
	b = buf + buf_offset;
	bend = b + size;
	printf("=> [");
	for(; b < bend; b++)
		printf(" %02X", *b);
	printf("]:%ld\n", (long)size);
	buf_offset += size;
	return 0;
}

static int
save_object(void *bs, asn_TYPE_descriptor_t *td) {
	asn_enc_rval_t rval; /* Return value */
	int i;
	
	rval = der_encode(td, bs, _buf_writer, 0);
	if (rval.encoded == -1) {
		fprintf(stderr,
			"Cannot encode %s: %s\n",
			rval.failed_type->name, strerror(errno));
		assert(rval.encoded != -1);
		return -1;	/* JIC */
	}

	buf[buf_offset++] = 123;	/* Finalize with garbage */

	asn_fprint(stderr, td, bs);
	xer_fprint(stderr, td, bs);

	printf("OUT: [");
	for(i = 0; i < buf_offset; i++)
		printf(" %02x", buf[i]);
	printf("]\n");

	return 0;
}

static int
load_object(void *bs, asn_TYPE_descriptor_t *td) {
	asn_dec_rval_t rval;

	fprintf(stderr, "\nLOADING OBJECT OF SIZE %d\n", buf_offset);

	rval = ber_decode(0, td, (void **)&bs, buf, buf_offset);
	assert(rval.code == RC_OK);

	asn_fprint(stderr, td, bs);
	xer_fprint(stderr, td, bs);

	return (rval.code == RC_OK)?0:-1;
}

int
main() {
	asn_TYPE_descriptor_t *td = &asn_DEF_UserIdentifier;
	UserIdentifier_t user;
	UserIdentifier_t user_new;
	int ret;

	memset(&user, 0, sizeof user);
	memset(&user_new, 0, sizeof user_new);

	user.present = UserIdentifier_PR_phoneNumber;
	OCTET_STRING_fromBuf(
		&user.choice.phoneNumber,
		"0123456789", -1);

	/* Save->Load must succeed */
	save_object(&user, td);
	ret = load_object(&user_new, td);

	assert(user_new.present == UserIdentifier_PR_phoneNumber);

	assert(ret == 0);

	printf("OK\n");

	return ret;
}