/******************************************************************************/ /* application.c */ /* */ /* Application example for how to use output from TACO. */ /* */ /* Encoding/Decoding/Compare done using TARE (libtare). */ /* */ /* Updated 2002-05-10 */ /* */ /******************************************************************************/ /* standard header files */ #include #include #include /* proprietary header files */ #include "example.h" /* global declarations (private) */ static ASN1_Octet_t PDU[1024]; /* Container big enough to hold the encoded data */ static size_t length; /* function prototypes */ int test_encoding(void); int test_decoding(void); /* functions */ int main() { initialize_example(); /* Initialize/load the protocol/project */ printf("Encoding...\n"); test_encoding(); printf("End Encoding...\n"); printf("Decoding...\n"); test_decoding(); printf("End Decoding...\n"); return 0; } int test_encoding(void) { size_t result; encode_t scope; /* Encode example: Type: M1Example1, Value: M1example1 */ result = EncExec(PDU, 1024, M1Example1, &M1example1, "ber", &scope); EncFree(scope); if(result == (size_t)(-1)) { fprintf(stderr, "Error in encoding\n"); exit(EXIT_FAILURE); } /* result of encoding is the length of the encoded data */ length = result; printf("Length: %d\n", length); printf("Index - dec - hex\n"); for(size_t i = 0; i < length; i++) { /* Let's see the encoded data */ printf("%4zd - %3u - 0x%02X\n", i, (unsigned int)PDU[i], (unsigned int)PDU[i]); } return 0; } int test_decoding(void) { decode_t scope; M1Example1__t * example1_ptr; /* Decode PDU (The previously encoded data) */ example1_ptr = DecExec(PDU, length, M1Example1, "ber", &scope); if(example1_ptr == NULL) { fprintf(stderr, "Error in decoding\n"); exit(EXIT_FAILURE); } /* Let's look at the values */ /* Mandatory component values */ printf("alpha1: %jd\n", example1_ptr->alpha1); /* Integer */ printf("beta1: %Lf\n", example1_ptr->beta1); /* Real */ /* Now we have a sequence of integers */ printf("gamma1: %zd\n", Instances(example1_ptr->gamma1)); /* Instances */ for(size_t i = 1; i <= Instances(example1_ptr->gamma1); i++) { printf("gamma1[%zd]: %jd\n", i, example1_ptr->gamma1[i]); /* Integer */ } /* Choice */ switch(Alternative(example1_ptr->delta1)) { case 1: /* epsilon */ { printf("epsilon - bits: %zd\n", example1_ptr->delta1.epsilon->length); /* Length in bits */ for(size_t i = 0; i < (example1_ptr->delta1.epsilon->length + 7)/8; i++) { printf("%02X", (unsigned int)example1_ptr->delta1.epsilon->value[i]); } printf("'H\n"); break; } case 2: /* zeta */ { printf("zeta - octets: %zd\n", example1_ptr->delta1.zeta->length); /* Length in octets */ for(size_t i = 0; i < example1_ptr->delta1.zeta->length; i++) { printf("%02X", (unsigned int)example1_ptr->delta1.zeta->value[i]); } printf("'H\n"); break; } default: { /* Error */ fprintf(stderr, "Invalid alternative in choice\n"); exit(EXIT_FAILURE); } } /* Optional component values */ if(example1_ptr->alpha2 != NULL) /* Check existence */ { printf("alpha2: %jd\n", *example1_ptr->alpha2); /* Integer (pointer) */ } if(example1_ptr->beta2 != NULL) /* Check existence */ { printf("beta2: %Lf\n", *example1_ptr->beta2); /* Real (pointer) */ } /* Now we have a sequence of integers */ if(example1_ptr->gamma2 != NULL) /* Check existence */ { printf("gamma2: %zd\n", Instances(example1_ptr->gamma2)); /* Instances */ for(size_t i = 1; i <= Instances(example1_ptr->gamma2); i++) { printf("gamma2[%zd]: %jd\n", i, example1_ptr->gamma2[i]); /* Integer */ } } /* Choice */ if(example1_ptr->delta2 != NULL) /* Check existence */ { switch(Alternative(*example1_ptr->delta2)) { case 1: /* epsilon */ { printf("epsilon - bits: %zd\n", example1_ptr->delta2->epsilon->length); /* Length in bits */ for(size_t i = 0; i < (example1_ptr->delta2->epsilon->length + 7)/8; i++) { printf("%02X", (unsigned int)example1_ptr->delta2->epsilon->value[i]); } printf("'H\n"); break; } case 2: /* zeta */ { printf("zeta - octets: %zd\n", example1_ptr->delta2->zeta->length); /* Length in octets */ for(size_t i = 0; i < example1_ptr->delta2->zeta->length; i++) { printf("%02X", (unsigned int)example1_ptr->delta2->zeta->value[i]); } printf("'H\n"); break; } default: { /* Error */ fprintf(stderr, "Invalid alternative in choice\n"); exit(EXIT_FAILURE); } } } /* Let's check that the value given to the encoder is the same as the value decoded by the decoder */ switch(Compare(M1Example1, example1_ptr, &M1example1)) { case 0: { /* Equal */ printf("Equal\n"); break; } case -1: { /* Less */ printf("less than M1example1\n"); break; } case 1: { /* Greater */ printf("greater than M1example1\n"); break; } case 2: { /* Couldn't compare values */ printf("Couldn't compare values\n"); break; } default: { /* Uups - definitely an error! */ exit(EXIT_FAILURE); } } /* We don't need the decoded values any more - free allocated memory */ DecFree(scope); /* Now all decoded values are undefined!!! */ return 0; }