/*-
* Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <OCTET_STRING.h>
#include <BIT_STRING.h> /* for .bits_unused member */
#include <errno.h>
/*
* OCTET STRING basic type description.
*/
static ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
};
static asn_OCTET_STRING_specifics_t asn_DEF_OCTET_STRING_specs = {
sizeof(OCTET_STRING_t),
offsetof(OCTET_STRING_t, _asn_ctx),
ASN_OSUBV_STR
};
static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
{ APC_CONSTRAINED, 8, 8, 0, 255 },
{ APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
0, 0
};
asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
"OCTET STRING", /* Canonical name */
"OCTET_STRING", /* XML tag name */
OCTET_STRING_free,
OCTET_STRING_print, /* non-ascii stuff, generally */
asn_generic_no_constraint,
OCTET_STRING_decode_ber,
OCTET_STRING_encode_der,
OCTET_STRING_decode_xer_hex,
OCTET_STRING_encode_xer,
OCTET_STRING_decode_uper, /* Unaligned PER decoder */
OCTET_STRING_encode_uper, /* Unaligned PER encoder */
0, /* Use generic outmost tag fetcher */
asn_DEF_OCTET_STRING_tags,
sizeof(asn_DEF_OCTET_STRING_tags)
/ sizeof(asn_DEF_OCTET_STRING_tags[0]),
asn_DEF_OCTET_STRING_tags, /* Same as above */
sizeof(asn_DEF_OCTET_STRING_tags)
/ sizeof(asn_DEF_OCTET_STRING_tags[0]),
0, /* No PER visible constraints */
0, 0, /* No members */
&asn_DEF_OCTET_STRING_specs
};
#undef _CH_PHASE
#undef NEXT_PHASE
#undef PREV_PHASE
#define _CH_PHASE(ctx, inc) do { \
if(ctx->phase == 0) \
ctx->context = 0; \
ctx->phase += inc; \
} while(0)
#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
#undef ADVANCE
#define ADVANCE(num_bytes) do { \
size_t num = (num_bytes); \
buf_ptr = ((const char *)buf_ptr) + num; \
size -= num; \
consumed_myself += num; \
} while(0)
#undef RETURN
#define RETURN(_code) do { \
asn_dec_rval_t tmprval; \
tmprval.code = _code; \
tmprval.consumed = consumed_myself; \
return tmprval; \
} while(0)
#undef APPEND
#define APPEND(bufptr, bufsize) do { \
size_t _bs = (bufsize); /* Append size */ \
size_t _ns = ctx->context; /* Allocated now */ \
size_t _es = st->size + _bs; /* Expected size */ \
/* int is really a typeof(st->size): */ \
if((int)_es < 0) RETURN(RC_FAIL); \
if(_ns <= _es) { \
void *ptr; \
/* Be nice and round to the memory allocator */ \
do { _ns = _ns ? _ns << 1 : 16; } \
while(_ns <= _es); \
/* int is really a typeof(st->size): */ \
if((int)_ns < 0) RETURN(RC_FAIL); \
ptr = REALLOC(st->buf, _ns); \
if(ptr) { \
st->buf = (uint8_t *)ptr; \
ctx->context = _ns; \
} else { \
RETURN(RC_FAIL); \
} \
ASN_DEBUG("Reallocating into %ld", (long)_ns); \
} \
memcpy(st->buf + st->size, bufptr, _bs); \
/* Convenient nul-termination */ \
st->buf[_es] = '\0'; \
st->size = _es; \
} while(0)
/*
* The main reason why ASN.1 is still alive is that too much time and effort
* is necessary for learning it more or less adequately, thus creating a gut
* necessity to demonstrate that aquired skill everywhere afterwards.
* No, I am not going to explain what the following stuff is.
*/
struct _stack_el {
ber_tlv_len_t left; /* What's left to read (or -1) */
ber_tlv_len_t got; /* What was actually processed */
int cont_level; /* Depth of subcontainment */
int want_nulls; /* Want null "end of content" octets? */
int bits_chopped; /* Flag in BIT STRING mode */
ber_tlv_tag_t tag; /* For debugging purposes */
struct _stack_el *prev;
struct _stack_el *next;
};
struct _stack {
struct _stack_el *tail;
struct _stack_el *cur_ptr;
};
static struct _stack_el *
OS__add_stack_el(struct _stack *st) {
struct _stack_el *nel;
/*
* Reuse the old stack frame or allocate a new one.
*/
if(st->cur_ptr && st->cur_ptr->next) {
nel = st->cur_ptr->next;
nel->bits_chopped = 0;
nel->got = 0;
/* Retain the nel->cont_level, it's correct. */
} else {
nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
if(nel == NULL)
return NULL;
if(st->tail) {
/* Increase a subcontainment depth */
nel->cont_level = st->tail->cont_level + 1;
st->tail->next = nel;
}
nel->prev = st->tail;
st->tail = nel;
}
st->cur_ptr = nel;
return nel;
}
static struct _stack *
_new_stack() {
return (struct _stack *)CALLOC(1, sizeof(struct _stack));
}
/*
* Decode OCTET STRING type.
*/
asn_dec_rval_t
OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
asn_TYPE_descriptor_t *td,
void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
asn_OCTET_STRING_specifics_t *specs = td->specifics
? (asn_OCTET_STRING_specifics_t *)td->specifics
: &asn_DEF_OCTET_STRING_specs;
BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
asn_dec_rval_t rval;
asn_struct_ctx_t *ctx;
ssize_t consumed_myself = 0;
struct _stack *stck; /* Expectations stack structure */
struct _stack_el *sel = 0; /* Stack element */
int tlv_constr;
enum asn_OS_Subvariant type_variant = specs->subvariant;
ASN_DEBUG("Decoding %s as %s (frame %ld)",
td->name,
(type_variant == ASN_OSUBV_STR) ?
"OCTET STRING" : "OS-SpecialCase",
(long)size);
/*
* Create the string if does not exist.
*/
if(st == NULL) {
st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
if(st == NULL) RETURN(RC_FAIL);
}
/* Restore parsing context */
ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
switch(ctx->phase) {
case 0:
/*
* Check tags.
*/
rval = ber_check_tags(opt_codec_ctx, td, ctx,
buf_ptr, size, tag_mode, -1,
&ctx->left, &tlv_constr);
if(rval.code != RC_OK)
return rval;
if(tlv_constr) {
/*
* Complex operation, requires stack of expectations.
*/
ctx->ptr = _new_stack();
if(ctx->ptr) {
stck = (struct _stack *)ctx->ptr;
} else {
RETURN(RC_FAIL);
}
} else {
/*
* Jump into stackless primitive decoding.
*/
_CH_PHASE(ctx, 3);
if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
APPEND(buf_ptr, rval.consumed);
ADVANCE(rval.consumed);
goto phase3;
}
NEXT_PHASE(ctx);
/* Fall through */
case 1:
phase1:
/*
* Fill the stack with expectations.
*/
stck = (struct _stack *)ctx->ptr;
sel = stck->cur_ptr;
do {
ber_tlv_tag_t tlv_tag;
ber_tlv_len_t tlv_len;
ber_tlv_tag_t expected_tag;
ssize_t tl, ll, tlvl;
/* This one works even if (sel->left == -1) */
ssize_t Left = ((!sel||(size_t)sel->left >= size)
?(ssize_t)size:sel->left);
ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
(long)(sel?sel->left:0),
(long)(sel?sel->want_nulls:0),
(long)(sel?sel->got:0)
);
if(sel && sel->left <= 0 && sel->want_nulls == 0) {
if(sel->prev) {
struct _stack_el *prev = sel->prev;
if(prev->left != -1) {
if(prev->left < sel->got)
RETURN(RC_FAIL);
prev->left -= sel->got;
}
prev->got += sel->got;
sel = stck->cur_ptr = prev;
if(!sel) break;
tlv_constr = 1;
continue;
} else {
sel = stck->cur_ptr = 0;
break; /* Nothing to wait */
}
}
tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
(long)size, (long)Left, sel?"":"!",
(long)(sel?sel->left:0),
(long)(sel?sel->want_nulls:0),
(long)tl);
switch(tl) {
case -1: RETURN(RC_FAIL);
case 0: RETURN(RC_WMORE);
}
tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
ll = ber_fetch_length(tlv_constr,
(const char *)buf_ptr + tl,Left - tl,&tlv_len);
ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
ber_tlv_tag_string(tlv_tag), tlv_constr,
(long)Left, (long)tl, (long)tlv_len, (long)ll);
switch(ll) {
case -1: RETURN(RC_FAIL);
case 0: RETURN(R
没有合适的资源?快使用搜索试试~ 我知道了~
asn.1.rar_asn_asn.1_asn.1 协议_asn_codecs_prim.o
共70个文件
h:31个
c:28个
o:5个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 123 浏览量
2022-09-24
18:10:43
上传
评论
收藏 121KB RAR 举报
温馨提示
关于asn.1协议规约的C代码源程序。对于了解和开发asn.1有帮助。
资源详情
资源评论
资源推荐
收起资源包目录
asn.1.rar (70个子文件)
asn.1
Makefile.am.sample 3KB
NativeEnumerated.c 6KB
asn_SET_OF.c 2KB
constr_SET_OF.c 23KB
Makefile.win 4KB
asn_codecs_prim.c 7KB
NativeInteger.h 1KB
ber_tlv_length.h 1KB
per_opentype.c 9KB
constr_SEQUENCE.c 36KB
ber_decoder.o 4KB
dllmain.o 698B
per_support.c 10KB
asn_application.h 1KB
xer_decoder.h 3KB
VisibleString.c 2KB
BIT_STRING.c 4KB
test.layout 2KB
module.asn1 189B
NativeEnumerated.h 897B
test.dev 8KB
per_opentype.h 666B
constr_TYPE.h 7KB
CertainStructure.c 4KB
asn_SET_OF.o 981B
per_encoder.c 4KB
xer_support.h 2KB
REAL.h 1KB
dll.h 266B
xer_encoder.h 2KB
ber_tlv_tag.c 3KB
ber_decoder.h 2KB
constraints.h 2KB
constr_TYPE.c 2KB
asn_codecs_prim.h 2KB
asn_system.h 3KB
asn_internal.h 3KB
converter-sample.c 22KB
CertainStructure.h 979B
asn_SET_OF.h 2KB
xer_encoder.c 2KB
+Compiler.Log 3KB
constraints.c 2KB
der_encoder.c 5KB
NativeReal.h 1003B
constr_SEQUENCE.h 1KB
asn_codecs.h 3KB
xer_support.c 5KB
per_support.h 4KB
ber_tlv_length.o 2KB
INTEGER.h 2KB
per_decoder.c 2KB
dllmain.c 788B
constr_SET_OF.h 1KB
BIT_STRING.h 824B
OCTET_STRING.c 46KB
xer_decoder.c 9KB
ber_tlv_tag.o 2KB
INTEGER.c 22KB
OCTET_STRING.h 3KB
per_decoder.h 2KB
per_encoder.h 2KB
VisibleString.h 511B
NativeReal.c 7KB
ber_decoder.c 7KB
ber_tlv_tag.h 2KB
der_encoder.h 2KB
NativeInteger.c 8KB
REAL.c 16KB
ber_tlv_length.c 4KB
共 70 条
- 1
我虽横行却不霸道
- 粉丝: 72
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0