libisdn
asn1_codec.c
Go to the documentation of this file.
00001 /*
00002  * ASN.1 codec
00003  * Copyright (C) 2009 Stefan Knoblich <s.knoblich@axsentis.de>
00004  */
00005 #ifdef HAVE_CONFIG_H
00006 #include "config.h"
00007 #endif
00008 
00009 #include <stdio.h>
00010 #include <strings.h>
00011 
00012 #include "asn1.h"
00013 #include "asn1_common.h"
00014 #include "asn1_types.h"
00015 
00016 /* codecs */
00017 #include "asn1_codec_ber.h"
00018 
00019 /*************************************************************************************************
00020  * Codec registry
00021  *************************************************************************************************/
00022 
00023 static const struct asn1_codec asn1_codecs[] = {
00024         { ASN1_CODEC_BER, "BER", &ber_decode_header, &ber_encode_header, &ber_decode_value, &ber_encode_value, &ber_header_size },
00025 };
00026 
00027 
00028 /*************************************************************************************************
00029  * Codec handling API
00030  *************************************************************************************************/
00031 
00032 const char *asn1_get_codec_name(const struct asn1_tree *tree)
00033 {
00034         if (!tree || !tree->codec)
00035                 return NULL;
00036 
00037         return tree->codec->name;
00038 }
00039 
00040 asn1_codec_t asn1_get_codec_id(const struct asn1_tree *tree)
00041 {
00042         if (!tree || !tree->codec)
00043                 return ASN1_CODEC_NONE;
00044 
00045         return tree->codec->id;
00046 }
00047 
00048 int asn1_set_codec_by_name(struct asn1_tree *tree, const char *name)
00049 {
00050         if (!tree || !name || !name[0])
00051                 return -1;
00052 
00053         for (int i = 0; i < ARRAY_SIZE(asn1_codecs); i++) {
00054                 if (!strcasecmp(asn1_codecs[i].name, name)) {
00055                         tree->codec = &asn1_codecs[i];
00056                         return 0;
00057                 }
00058         }
00059         return -1;
00060 }
00061 
00062 int asn1_set_codec_by_id(struct asn1_tree *tree, const asn1_codec_t id)
00063 {
00064         if (!tree || id < ASN1_CODEC_NONE || id >= ASN1_CODEC_MAX)
00065                 return -1;
00066 
00067         for (int i = 0; i < ARRAY_SIZE(asn1_codecs); i++) {
00068                 if (asn1_codecs[i].id == id) {
00069                         tree->codec = &asn1_codecs[i];
00070                         return 0;
00071                 }
00072         }
00073         return -1;
00074 }