NativeInteger.c 10.4 KB
Newer Older
Lev Walkin's avatar
Lev Walkin committed
1
/*-
Lev Walkin's avatar
Lev Walkin committed
2 3
 * Copyright (c) 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
 * All rights reserved.
Lev Walkin's avatar
Lev Walkin committed
4 5 6
 * Redistribution and modifications are permitted subject to BSD license.
 */
/*
Lev Walkin's avatar
Lev Walkin committed
7
 * Read the NativeInteger.h for the explanation wrt. differences between
Lev Walkin's avatar
Lev Walkin committed
8 9 10 11 12
 * INTEGER and NativeInteger.
 * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this
 * implementation deals with the standard (machine-specific) representation
 * of them instead of using the platform-independent buffer.
 */
Lev Walkin's avatar
Lev Walkin committed
13
#include <asn_internal.h>
Lev Walkin's avatar
Lev Walkin committed
14 15 16 17 18
#include <NativeInteger.h>

/*
 * NativeInteger basic type description.
 */
19
static const ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
Lev Walkin's avatar
Lev Walkin committed
20 21
	(ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
};
22
asn_TYPE_operation_t asn_OP_NativeInteger = {
Lev Walkin's avatar
Lev Walkin committed
23 24
	NativeInteger_free,
	NativeInteger_print,
25
	NativeInteger_compare,
Lev Walkin's avatar
Lev Walkin committed
26 27
	NativeInteger_decode_ber,
	NativeInteger_encode_der,
Lev Walkin's avatar
Lev Walkin committed
28
	NativeInteger_decode_xer,
Lev Walkin's avatar
Lev Walkin committed
29
	NativeInteger_encode_xer,
Lev Walkin's avatar
Lev Walkin committed
30 31 32 33
#ifdef	ASN_DISABLE_OER_SUPPORT
	0,
	0,
#else
Lev Walkin's avatar
Lev Walkin committed
34 35
	NativeInteger_decode_oer,	/* OER decoder */
	NativeInteger_encode_oer,	/* Canonical OER encoder */
Lev Walkin's avatar
Lev Walkin committed
36
#endif  /* ASN_DISABLE_OER_SUPPORT */
Lev Walkin's avatar
Lev Walkin committed
37 38 39 40 41 42 43
#ifdef	ASN_DISABLE_PER_SUPPORT
	0,
	0,
#else
	NativeInteger_decode_uper,	/* Unaligned PER decoder */
	NativeInteger_encode_uper,	/* Unaligned PER encoder */
#endif	/* ASN_DISABLE_PER_SUPPORT */
44 45 46 47 48 49 50
	0	/* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
	"INTEGER",			/* The ASN.1 type is still INTEGER */
	"INTEGER",
	&asn_OP_NativeInteger,
	asn_generic_no_constraint,
Lev Walkin's avatar
Lev Walkin committed
51 52 53 54
	asn_DEF_NativeInteger_tags,
	sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
	asn_DEF_NativeInteger_tags,	/* Same as above */
	sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
Lev Walkin's avatar
Lev Walkin committed
55
	0,	/* No OER visible constraints */
Lev Walkin's avatar
Lev Walkin committed
56
	0,	/* No PER visible constraints */
Lev Walkin's avatar
Lev Walkin committed
57
	0, 0,	/* No members */
58
	0	/* No specifics */
Lev Walkin's avatar
Lev Walkin committed
59 60 61 62 63
};

/*
 * Decode INTEGER type.
 */
Lev Walkin's avatar
Lev Walkin committed
64
asn_dec_rval_t
Lev Walkin's avatar
Lev Walkin committed
65 66
NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
	asn_TYPE_descriptor_t *td,
Lev Walkin's avatar
Lev Walkin committed
67
	void **nint_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin's avatar
Lev Walkin committed
68 69 70
    const asn_INTEGER_specifics_t *specs =
        (const asn_INTEGER_specifics_t *)td->specifics;
    long *native = (long *)*nint_ptr;
Lev Walkin's avatar
Lev Walkin committed
71
	asn_dec_rval_t rval;
Lev Walkin's avatar
Lev Walkin committed
72 73 74 75 76
	ber_tlv_len_t length;

	/*
	 * If the structure is not there, allocate it.
	 */
Lev Walkin's avatar
Lev Walkin committed
77 78 79
	if(native == NULL) {
		native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
		if(native == NULL) {
Lev Walkin's avatar
Lev Walkin committed
80 81 82 83 84 85 86 87 88 89 90 91
			rval.code = RC_FAIL;
			rval.consumed = 0;
			return rval;
		}
	}

	ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
		td->name, tag_mode);

	/*
	 * Check tags.
	 */
Lev Walkin's avatar
Lev Walkin committed
92 93
	rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
			tag_mode, 0, &length, 0);
Lev Walkin's avatar
Lev Walkin committed
94 95 96 97 98 99 100 101
	if(rval.code != RC_OK)
		return rval;

	ASN_DEBUG("%s length is %d bytes", td->name, (int)length);

	/*
	 * Make sure we have this length.
	 */
Lev Walkin's avatar
Lev Walkin committed
102
	buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkin's avatar
Lev Walkin committed
103
	size -= rval.consumed;
104
	if(length > (ber_tlv_len_t)size) {
Lev Walkin's avatar
Lev Walkin committed
105 106 107 108 109 110 111
		rval.code = RC_WMORE;
		rval.consumed = 0;
		return rval;
	}

	/*
	 * ASN.1 encoded INTEGER: buf_ptr, length
Lev Walkin's avatar
Lev Walkin committed
112
	 * Fill the native, at the same time checking for overflow.
Lev Walkin's avatar
Lev Walkin committed
113 114 115 116
	 * If overflow occured, return with RC_FAIL.
	 */
	{
		INTEGER_t tmp;
117 118 119 120
		union {
			const void *constbuf;
			void *nonconstbuf;
		} unconst_buf;
Lev Walkin's avatar
Lev Walkin committed
121
		long l;
122 123 124

		unconst_buf.constbuf = buf_ptr;
		tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkin's avatar
Lev Walkin committed
125 126
		tmp.size = length;

127
		if((specs&&specs->field_unsigned)
Lev Walkin's avatar
Lev Walkin committed
128
			? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */
129
			: asn_INTEGER2long(&tmp, &l)) {
Lev Walkin's avatar
Lev Walkin committed
130 131 132 133 134
			rval.code = RC_FAIL;
			rval.consumed = 0;
			return rval;
		}

Lev Walkin's avatar
Lev Walkin committed
135
		*native = l;
Lev Walkin's avatar
Lev Walkin committed
136 137 138 139 140
	}

	rval.code = RC_OK;
	rval.consumed += length;

Lev Walkin's avatar
Lev Walkin committed
141 142
	ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
		(long)rval.consumed, (long)length, td->name, (long)*native);
Lev Walkin's avatar
Lev Walkin committed
143 144 145 146 147 148 149

	return rval;
}

/*
 * Encode the NativeInteger using the standard INTEGER type DER encoder.
 */
Lev Walkin's avatar
Lev Walkin committed
150
asn_enc_rval_t
Lev Walkin's avatar
Lev Walkin committed
151
NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr,
Lev Walkin's avatar
Lev Walkin committed
152 153
	int tag_mode, ber_tlv_tag_t tag,
	asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin's avatar
Lev Walkin committed
154
	unsigned long native = *(unsigned long *)ptr;	/* Disable sign ext. */
Lev Walkin's avatar
Lev Walkin committed
155
	asn_enc_rval_t erval;
Lev Walkin's avatar
Lev Walkin committed
156 157 158 159
	INTEGER_t tmp;

#ifdef	WORDS_BIGENDIAN		/* Opportunistic optimization */

Lev Walkin's avatar
Lev Walkin committed
160 161
	tmp.buf = (uint8_t *)&native;
	tmp.size = sizeof(native);
Lev Walkin's avatar
Lev Walkin committed
162 163

#else	/* Works even if WORDS_BIGENDIAN is not set where should've been */
Lev Walkin's avatar
Lev Walkin committed
164
	uint8_t buf[sizeof(native)];
Lev Walkin's avatar
Lev Walkin committed
165 166
	uint8_t *p;

Lev Walkin's avatar
Lev Walkin committed
167
	/* Prepare a fake INTEGER */
Lev Walkin's avatar
Lev Walkin committed
168
	for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
Lev Walkin's avatar
Lev Walkin committed
169
		*p = (uint8_t)native;
Lev Walkin's avatar
Lev Walkin committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183

	tmp.buf = buf;
	tmp.size = sizeof(buf);
#endif	/* WORDS_BIGENDIAN */
	
	/* Encode fake INTEGER */
	erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
	if(erval.encoded == -1) {
		assert(erval.structure_ptr == &tmp);
		erval.structure_ptr = ptr;
	}
	return erval;
}

Lev Walkin's avatar
Lev Walkin committed
184 185 186 187 188 189
/*
 * Decode the chunk of XML text encoding INTEGER.
 */
asn_dec_rval_t
NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
	asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin's avatar
Lev Walkin committed
190
		const void *buf_ptr, size_t size) {
Lev Walkin's avatar
Lev Walkin committed
191 192 193
    const asn_INTEGER_specifics_t *specs =
        (const asn_INTEGER_specifics_t *)td->specifics;
    asn_dec_rval_t rval;
Lev Walkin's avatar
Lev Walkin committed
194
	INTEGER_t st;
Lev Walkin's avatar
Lev Walkin committed
195
	void *st_ptr = (void *)&st;
Lev Walkin's avatar
Lev Walkin committed
196
	long *native = (long *)*sptr;
Lev Walkin's avatar
Lev Walkin committed
197

Lev Walkin's avatar
Lev Walkin committed
198
	if(!native) {
Lev Walkin's avatar
Lev Walkin committed
199
		native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
200
		if(!native) ASN__DECODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
201 202
	}

Lev Walkin's avatar
Lev Walkin committed
203 204
	memset(&st, 0, sizeof(st));
	rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr, 
Lev Walkin's avatar
Lev Walkin committed
205
		opt_mname, buf_ptr, size);
Lev Walkin's avatar
Lev Walkin committed
206 207
	if(rval.code == RC_OK) {
		long l;
208
		if((specs&&specs->field_unsigned)
Lev Walkin's avatar
Lev Walkin committed
209
			? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
210
			: asn_INTEGER2long(&st, &l)) {
Lev Walkin's avatar
Lev Walkin committed
211 212 213
			rval.code = RC_FAIL;
			rval.consumed = 0;
		} else {
Lev Walkin's avatar
Lev Walkin committed
214
			*native = l;
Lev Walkin's avatar
Lev Walkin committed
215 216
		}
	} else {
Lev Walkin's avatar
Lev Walkin committed
217 218 219 220 221
		/*
		 * Cannot restart from the middle;
		 * there is no place to save state in the native type.
		 * Request a continuation from the very beginning.
		 */
Lev Walkin's avatar
Lev Walkin committed
222 223
		rval.consumed = 0;
	}
Lev Walkin's avatar
Lev Walkin committed
224
	ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
Lev Walkin's avatar
Lev Walkin committed
225 226 227 228
	return rval;
}


Lev Walkin's avatar
Lev Walkin committed
229
asn_enc_rval_t
Lev Walkin's avatar
Lev Walkin committed
230
NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin's avatar
Lev Walkin committed
231 232
	int ilevel, enum xer_encoder_flags_e flags,
		asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin's avatar
Lev Walkin committed
233 234 235
    const asn_INTEGER_specifics_t *specs =
        (const asn_INTEGER_specifics_t *)td->specifics;
    char scratch[32];	/* Enough for 64-bit int */
Lev Walkin's avatar
Lev Walkin committed
236
	asn_enc_rval_t er;
Lev Walkin's avatar
Lev Walkin committed
237
	const long *native = (const long *)sptr;
Lev Walkin's avatar
Lev Walkin committed
238 239 240 241

	(void)ilevel;
	(void)flags;

242
	if(!native) ASN__ENCODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
243

244 245 246
	er.encoded = snprintf(scratch, sizeof(scratch),
			(specs && specs->field_unsigned)
			? "%lu" : "%ld", *native);
Lev Walkin's avatar
Lev Walkin committed
247 248
	if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
		|| cb(scratch, er.encoded, app_key) < 0)
249
		ASN__ENCODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
250

251
	ASN__ENCODED_OK(er);
Lev Walkin's avatar
Lev Walkin committed
252 253
}

254 255
#ifndef  ASN_DISABLE_PER_SUPPORT

Lev Walkin's avatar
Lev Walkin committed
256 257
asn_dec_rval_t
NativeInteger_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin's avatar
Lev Walkin committed
258 259 260
                          asn_TYPE_descriptor_t *td,
                          const asn_per_constraints_t *constraints, void **sptr,
                          asn_per_data_t *pd) {
Lev Walkin's avatar
Lev Walkin committed
261 262 263
    const asn_INTEGER_specifics_t *specs =
        (const asn_INTEGER_specifics_t *)td->specifics;
    asn_dec_rval_t rval;
Lev Walkin's avatar
Lev Walkin committed
264 265 266 267 268 269 270 271 272
	long *native = (long *)*sptr;
	INTEGER_t tmpint;
	void *tmpintptr = &tmpint;

	(void)opt_codec_ctx;
	ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name);

	if(!native) {
		native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
273
		if(!native) ASN__DECODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
274 275 276 277 278
	}

	memset(&tmpint, 0, sizeof tmpint);
	rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints,
				   &tmpintptr, pd);
Lev Walkin's avatar
Lev Walkin committed
279
	if(rval.code == RC_OK) {
280
		if((specs&&specs->field_unsigned)
Lev Walkin's avatar
Lev Walkin committed
281
			? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
282
			: asn_INTEGER2long(&tmpint, native))
Lev Walkin's avatar
Lev Walkin committed
283 284 285 286
			rval.code = RC_FAIL;
		else
			ASN_DEBUG("NativeInteger %s got value %ld",
				td->name, *native);
Lev Walkin's avatar
Lev Walkin committed
287
	}
Lev Walkin's avatar
Lev Walkin committed
288
	ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
Lev Walkin's avatar
Lev Walkin committed
289 290

	return rval;
Lev Walkin's avatar
Lev Walkin committed
291 292
}

Lev Walkin's avatar
Lev Walkin committed
293 294
asn_enc_rval_t
NativeInteger_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin's avatar
Lev Walkin committed
295 296
                          const asn_per_constraints_t *constraints, void *sptr,
                          asn_per_outp_t *po) {
Lev Walkin's avatar
Lev Walkin committed
297 298 299
    const asn_INTEGER_specifics_t *specs =
        (const asn_INTEGER_specifics_t *)td->specifics;
    asn_enc_rval_t er;
Lev Walkin's avatar
Lev Walkin committed
300 301 302
	long native;
	INTEGER_t tmpint;

303
	if(!sptr) ASN__ENCODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
304 305 306 307 308 309

	native = *(long *)sptr;

	ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native);

	memset(&tmpint, 0, sizeof(tmpint));
310 311 312
	if((specs&&specs->field_unsigned)
		? asn_ulong2INTEGER(&tmpint, native)
		: asn_long2INTEGER(&tmpint, native))
313
		ASN__ENCODE_FAILED;
Lev Walkin's avatar
Lev Walkin committed
314 315 316 317 318
	er = INTEGER_encode_uper(td, constraints, &tmpint, po);
	ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
	return er;
}

319 320
#endif  /* ASN_DISABLE_PER_SUPPORT */

Lev Walkin's avatar
Lev Walkin committed
321 322 323 324
/*
 * INTEGER specific human-readable output.
 */
int
Lev Walkin's avatar
Lev Walkin committed
325
NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin's avatar
Lev Walkin committed
326
	asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin's avatar
Lev Walkin committed
327 328 329
    const asn_INTEGER_specifics_t *specs =
        (const asn_INTEGER_specifics_t *)td->specifics;
    const long *native = (const long *)sptr;
Lev Walkin's avatar
Lev Walkin committed
330
	char scratch[32];	/* Enough for 64-bit int */
Lev Walkin's avatar
Lev Walkin committed
331 332
	int ret;

333 334 335
	(void)td;	/* Unused argument */
	(void)ilevel;	/* Unused argument */

Lev Walkin's avatar
Lev Walkin committed
336
	if(native) {
337 338 339
		ret = snprintf(scratch, sizeof(scratch),
			(specs && specs->field_unsigned)
			? "%lu" : "%ld", *native);
Lev Walkin's avatar
Lev Walkin committed
340
		assert(ret > 0 && (size_t)ret < sizeof(scratch));
341
		return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
Lev Walkin's avatar
Lev Walkin committed
342
	} else {
343
		return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkin's avatar
Lev Walkin committed
344 345 346 347
	}
}

void
348
NativeInteger_free(const asn_TYPE_descriptor_t *td, void *ptr,
349
                   enum asn_struct_free_method method) {
350
    if(!td || !ptr)
Lev Walkin's avatar
Lev Walkin committed
351 352 353
		return;

	ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
354 355 356 357 358 359 360 361 362 363 364 365
		td->name, method, ptr);

    switch(method) {
    case ASFM_FREE_EVERYTHING:
        FREEMEM(ptr);
        break;
    case ASFM_FREE_UNDERLYING:
        break;
    case ASFM_FREE_UNDERLYING_AND_RESET:
        memset(ptr, 0, sizeof(long));
        break;
    }
Lev Walkin's avatar
Lev Walkin committed
366 367
}

368 369 370 371 372 373 374
int
NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
    (void)td;

    if(aptr && bptr) {
        const asn_INTEGER_specifics_t *specs =
            (const asn_INTEGER_specifics_t *)td->specifics;
375
        if(specs && specs->field_unsigned) {
376 377 378 379 380 381 382
            const unsigned long *a = aptr;
            const unsigned long *b = bptr;
            if(*a < *b) {
                return -1;
            } else if(*a > *b) {
                return 1;
            } else {
383
                return 0;
384 385 386 387 388 389 390 391 392
            }
        } else {
            const long *a = aptr;
            const long *b = bptr;
            if(*a < *b) {
                return -1;
            } else if(*a > *b) {
                return 1;
            } else {
393
                return 0;
394 395 396 397 398 399 400 401
            }
        }
    } else if(!aptr) {
        return -1;
    } else {
        return 1;
    }
}