asn1c/skeletons/BIT_STRING.c

188 lines
4.4 KiB
C
Raw Normal View History

2004-06-03 03:38:44 +00:00
/*-
* Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
2004-09-22 16:06:28 +00:00
#include <asn_internal.h>
2004-06-03 03:38:44 +00:00
#include <BIT_STRING.h>
#include <asn_internal.h>
2004-06-03 03:38:44 +00:00
/*
* BIT STRING basic type description.
*/
2004-09-29 13:26:15 +00:00
static ber_tlv_tag_t asn_DEF_BIT_STRING_tags[] = {
2004-06-03 03:38:44 +00:00
(ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
};
static asn_OCTET_STRING_specifics_t asn_DEF_BIT_STRING_specs = {
sizeof(BIT_STRING_t),
offsetof(BIT_STRING_t, _asn_ctx),
1, /* Special indicator that this is a BIT STRING type */
};
2004-09-29 13:26:15 +00:00
asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
2004-06-03 03:38:44 +00:00
"BIT STRING",
2004-10-20 15:50:55 +00:00
"BIT_STRING",
2004-09-22 16:06:28 +00:00
OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
BIT_STRING_print,
2004-06-03 03:38:44 +00:00
BIT_STRING_constraint,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
2004-10-20 15:50:55 +00:00
OCTET_STRING_decode_xer_binary,
2004-09-22 16:06:28 +00:00
BIT_STRING_encode_xer,
2005-11-26 11:25:14 +00:00
OCTET_STRING_decode_uper, /* Unaligned PER decoder */
2004-06-03 03:38:44 +00:00
0, /* Use generic outmost tag fetcher */
2004-09-29 13:26:15 +00:00
asn_DEF_BIT_STRING_tags,
sizeof(asn_DEF_BIT_STRING_tags)
/ sizeof(asn_DEF_BIT_STRING_tags[0]),
asn_DEF_BIT_STRING_tags, /* Same as above */
sizeof(asn_DEF_BIT_STRING_tags)
/ sizeof(asn_DEF_BIT_STRING_tags[0]),
2005-11-26 11:25:14 +00:00
0, /* No PER visible constraints */
2004-08-20 13:23:42 +00:00
0, 0, /* No members */
&asn_DEF_BIT_STRING_specs
2004-06-03 03:38:44 +00:00
};
/*
* BIT STRING generic constraint.
*/
int
2004-09-29 13:26:15 +00:00
BIT_STRING_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
2004-06-03 03:38:44 +00:00
asn_app_consume_bytes_f *app_errlog, void *app_key) {
2004-08-11 09:07:36 +00:00
const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
2004-06-03 03:38:44 +00:00
if(st && st->buf) {
if(st->size == 1 && st->bits_unused) {
2004-08-11 09:44:13 +00:00
_ASN_ERRLOG(app_errlog, app_key,
"%s: invalid padding byte (%s:%d)",
2004-08-22 13:47:59 +00:00
td->name, __FILE__, __LINE__);
2004-06-03 03:38:44 +00:00
return -1;
}
} else {
2004-08-11 09:44:13 +00:00
_ASN_ERRLOG(app_errlog, app_key,
2004-08-22 13:47:59 +00:00
"%s: value not given (%s:%d)",
td->name, __FILE__, __LINE__);
2004-06-03 03:38:44 +00:00
return -1;
}
return 0;
}
2004-09-22 16:06:28 +00:00
static char *_bit_pattern[16] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
};
asn_enc_rval_t
2004-09-29 13:26:15 +00:00
BIT_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
2004-09-22 16:06:28 +00:00
int ilevel, enum xer_encoder_flags_e flags,
asn_app_consume_bytes_f *cb, void *app_key) {
asn_enc_rval_t er;
char scratch[128];
char *p = scratch;
char *scend = scratch + (sizeof(scratch) - 10);
const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
2004-09-23 22:06:26 +00:00
int xcan = (flags & XER_F_CANONICAL);
2004-09-22 16:06:28 +00:00
uint8_t *buf;
uint8_t *end;
if(!st || !st->buf)
_ASN_ENCODE_FAILED;
er.encoded = 0;
buf = st->buf;
end = buf + st->size - 1; /* Last byte is special */
/*
* Binary dump
*/
for(; buf < end; buf++) {
2004-09-22 16:06:28 +00:00
int v = *buf;
int nline = xcan?0:(((buf - st->buf) % 8) == 0);
2004-09-22 16:06:28 +00:00
if(p >= scend || nline) {
er.encoded += p - scratch;
_ASN_CALLBACK(scratch, p - scratch);
p = scratch;
if(nline) _i_ASN_TEXT_INDENT(1, ilevel);
}
memcpy(p + 0, _bit_pattern[v >> 4], 4);
memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
p += 8;
}
if(!xcan && ((buf - st->buf) % 8) == 0)
2004-09-23 22:06:26 +00:00
_i_ASN_TEXT_INDENT(1, ilevel);
2004-09-24 20:56:25 +00:00
er.encoded += p - scratch;
_ASN_CALLBACK(scratch, p - scratch);
p = scratch;
2004-09-22 16:06:28 +00:00
2004-09-24 20:56:25 +00:00
if(buf == end) {
2004-09-22 16:06:28 +00:00
int v = *buf;
int ubits = st->bits_unused;
2004-09-22 16:06:28 +00:00
int i;
for(i = 7; i >= ubits; i--)
2004-09-29 13:26:15 +00:00
*p++ = (v & (1 << i)) ? 0x31 : 0x30;
2004-09-22 16:06:28 +00:00
er.encoded += p - scratch;
_ASN_CALLBACK(scratch, p - scratch);
}
2004-09-24 20:56:25 +00:00
if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
2004-09-23 22:06:26 +00:00
2005-11-26 11:25:14 +00:00
_ASN_ENCODED_OK(er);
2004-10-03 09:13:02 +00:00
cb_failed:
_ASN_ENCODE_FAILED;
2004-09-22 16:06:28 +00:00
}
2004-06-03 03:38:44 +00:00
/*
* BIT STRING specific contents printer.
*/
int
2004-09-29 13:26:15 +00:00
BIT_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
2004-06-03 03:38:44 +00:00
asn_app_consume_bytes_f *cb, void *app_key) {
2004-08-11 08:10:13 +00:00
static const char *h2c = "0123456789ABCDEF";
2004-06-03 03:38:44 +00:00
char scratch[64];
2004-08-11 09:07:36 +00:00
const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
2004-06-03 03:38:44 +00:00
uint8_t *buf;
uint8_t *end;
char *p = scratch;
2004-06-05 08:17:50 +00:00
(void)td; /* Unused argument */
if(!st || !st->buf)
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
2004-06-03 03:38:44 +00:00
ilevel++;
2004-06-03 03:38:44 +00:00
buf = st->buf;
end = buf + st->size;
/*
* Hexadecimal dump.
*/
for(; buf < end; buf++) {
if((buf - st->buf) % 16 == 0 && (st->size > 16)
&& buf != st->buf) {
_i_INDENT(1);
2004-06-03 03:38:44 +00:00
/* Dump the string */
if(cb(scratch, p - scratch, app_key) < 0) return -1;
2004-06-03 03:38:44 +00:00
p = scratch;
}
*p++ = h2c[*buf >> 4];
*p++ = h2c[*buf & 0x0F];
*p++ = 0x20;
}
2004-09-23 22:06:26 +00:00
if(p > scratch) {
p--; /* Eat the tailing space */
if((st->size > 16)) {
_i_INDENT(1);
2004-09-23 22:06:26 +00:00
}
/* Dump the incomplete 16-bytes row */
if(cb(scratch, p - scratch, app_key) < 0)
2004-09-23 22:06:26 +00:00
return -1;
}
return 0;
2004-06-03 03:38:44 +00:00
}