ASN.1 Decoder part 1
Every day i am dealing with this encoding decoding thing because i am working in a telco company and my position is a Software Architech in billing departement so what i am doing is design and develop billing system. The data that billed by billing is data that come from network elements like Mobile Switching Center (MSC) orServing GPRS Support Node (SGSN).All data or call detail record come from network element is encoded in ASN.1 format. My billing system should read this ASN.1 format.From this data i have to get information about the calling number, called number, event time, duration, volume (gprs only), trunk (for MSC), cgi, MSC id (for msc), sgsn address (for sgsn).
The challenge of ASN.1 is that this format is in hexadecimal format. If we open this ASN.1 file we will get hexadecimal number like this:
a0 82 01 30 a4 82 01 2c 9f 21 01 03 8d 03 00 00 36 8a 03 0a 01 04 94 0f 49 44 30 32 4d 53 4d 44 33 20 42 2f 31 33 36 8e 03 00 00 00 82 03 b5 44 e9 92 02 00 01 93 01 00 8b 03 17 30 2e 8c 03 17 31 28 9f 66 01 04 9f 42 02 24 22 86 08 15 10 01 05 72 00 85 f9 9b 07 15 f0 11 56 d0 77 1b 9f 74 08 53 57 89 00 56 87 20 60 87 08 53 57 89 00 56 87 20 f0 9a 03 17 30 28 88 07 11 26 18 08 41 58 84 9f 22 01 00 9d 01 11 9f 44 01 06 95 07 11 26 18 48 54 47 f9 99 08 11 26 18 48 54 47 29 f8 9f 49 05 30 06 f0 23 c3 9f 41 07 11 26 18 48 54 08 f9 96 06 4f 55 48 42 39 31 85 08 11 26 78 68 58 35 73 f2 9f 75 07 00 22 24 7d d5 5e 84 84 07 41 78 68 58 38 25 f7 97 06 49 41 48 4e 4d 31 9f 39 01 30 9f 26 01 00 81 03 75 92 4f 83 01 01 9f 3a 01 00 80 03 01 02 0e 9f 3f 01 00 91 01 00 90 01 00 8f 03 00 00 0b 9f 23 02 03 03 9f 20 01 01 9f 3d 01 05 9f 3e 05 02 01 00 05 03 9c 07 15 f0 11 56 d0 77 1b 9f 4e 06 4f 55 48 42 39 31 9f 4a 01 02 89 01 00
a0 81 a2 a5 81 9f 87 03 0a 01 04 8b 0f 49 44 30 32 4d 53 4d 44 33 20 42 2f 31 33 36 82 03 b5 44 ea 88 03 17 31 29 9f 66 01 04 9f 1f 02 24 22 81 03 75 e3 f2 89 01 00 85 08 15 10 11 12 96 13 07 f3 8e 07 15 f0 11 56 cf 77 11 8f 01 22 80 03 03 02 09 83 01 01 86 08 53 28 96 30 19 83 01 f0 91 08 53 28 96 30 19 83 01 03 8d 06 49 55 48 42 4d 39 8a 01 00 84 08 11 26 78 68 09 37 19 f6 99 01 06 8c 07 11 26 18 48 54 47 f9 90 07 11 26 18 48 54 00 f9 9f 21 01 42 9f 22 01 04 9f 20 07 21 80 31 57 42 70 96
Above is a sample of call detail record from msc.Ofcourse all encoded format have rule to decode it. ASN.1 have a grammar that describe the rule to encode and decode it. Every hexadecimal number have meaning that described in asn.1 grammar.
My company have a application that can decode the CDR into text file. But the decoder is very slow to decode the cdr. It takes about 15 to 30 minutes just to decode one cdr files with size 16 MB. I need a fast decoder because the billing must decode at least 14.000 cdr files a day andthe billing system should be near real time. Then i am googling to search faster ASN.1 decoder. Because the limited budget my company didn’twant to spend a lot for this decoder so i search for open source ASN.1 decoder. I found http://lionet.info/asn1c/blog/ but it is not working when asn1c compile my ASN.1 grammar so i decide to write my own decoder.
My decoder doesn’t use the grammar at all. But it solve the TLV pattern of ASN.1. I see that ASN.1 constructed in TLV (tag length value). Tag is the tag number of record, length is the length of value and the value is the value of tag number. The value may contains another TLV that it called nested TLV
T L VT L [ T L V ]T L [ T L [ T L V ] ]
My method is just simply read each byte of ASN.1 file then decide what tag that will be decoded and write the value into appropriate type.
below is my code to get each CDR types like mobile originating call, mobile teminating call, roaming call forwarding call, call forwarding call, transit call, sms originating and sms terminating.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, const char * argv[]) {
// insert code here…
FILE *erifile;
erifile = fopen(“/Users/gentur/Documents/eridecoder/EB_MDN3_100104017257.DAT”, “rb”);
if( erifile == NULL ) {
exit(0);
}
int byte_read;
int y = 0;
while( feof(erifile) == 0 ) {
byte_read = getc(erifile);
if( byte_read != 0×00 ) {
if( byte_read == 0xA0 ) {
printf(“<%02d>\n”, y);
int length = getc(erifile);
int length_flag = (length & 0×80 ) >> 7;
if( length_flag == 1 ) {
int num_len_byte = (length & 0x7F );
int i;
length = 0;
for( i=0; i<num_len_byte ; i++ ) {
if( i>0 ) {
length <<= 8;
}
length = (getc(erifile) | length);
}
}
char *cdr_value = malloc(length);
fread(cdr_value, sizeof(char), length, erifile);
int j;
for( j=0; j<length; j++ ) {
if((j%16)==0) printf(“\n”);
printf(” %02X “, cdr_value[j]&0xFF);
}
printf(“\n===================================================\n”);
free(cdr_value);
y++;
}
}
}
fclose(erifile);
return 0;
}
i think this is enough for to day
just for fun 1
#include<stdio.h>
int main(int argc, char* argv) {printf(“masukkan angka ganjil > 3:”); int element; scanf(“%d”, &element); if(element%2!=0&&element>3) {
int center=element/2; int k,l; int m=center-1;
for(k=0;k<element;k++) {
for(l=0;l<element;l++) {
if(k<=center) {
if(l==(center-k)||l==(center+k))
{ printf(“#”); } else { printf(” “); } } else if(k>center) { if(l==(center-m)||l==(center+m)) { printf(“#”); } else { printf(” “); } } } if(k>center) { m–; } printf(“\n”); } } else { printf(“input genab atau kurang dari 5″); } return 0;
}
just to fill my spare time. I think the if condition can be more simple, even it can be done with a single if.
![]()
me and yii (part 1)
Recently i wanted to create a web sites. A simple online store web site. But i was too lazy to code PHP from scratch. Then i started to search a framework. I wrote “the best php framework” in google. I got “Top ten Ranking PHP framework” in the top of google search result. Then i followed that link. I found many interesting php framework in that web site. Here is the list of to ten php framework:
- Yii
- CodeIgniter
- CakePHP
- Zend
- Symfony
- PhpDevShell
- Prado
- Akelos
- Zoop
- QPHP
I was surprised that the top framework in the list was yii. What was that framework? I have ever used cakePhp and codeIgniter before and i think codeIgniter was the best php framework i have ever used, but yii?!
I was curious with yii. Then i went to yii web site to get information about this framework. At first i thought it was just the same with other php framework. Yii has MVC, DAO/ActiveRecord, I18N/L10N, caching, authentication and role-based access control, scaffolding, testing, etc.”There is one way to know yii, try yii”. Then i downloaded it. The latest version i download was yii-1.1.5.
After i unpacked it i read the README. There was just short read me files. Then i followed the instruction in QUICK START part.
QUICK START———–
Yii comes with a command line tool called “yiic” that can createa skeleton Yii application for you to start with.
On command line, type in the following commands:
$ cd YiiPath/framework (Linux) cd YiiPath\framework (Windows)
$ ./yiic webapp ../testdrive (Linux) yiic webapp ..\testdrive (Windows)
The new Yii application will be created at “YiiPath/testdrive”.You can access it with the following URL:http://hostname/YiiPath/testdrive/index.php
Very simple and short quick start guide. Then i checked the result in web browser. I got this:
There were four links created by yii, which are Home, About, Contact, and Login. All of those pages showed me the basic frame that i might customize later. And the interesting think was it has a captcha in contact menu. But i still didn’t get why yii is special.
