花了一晚上时间写了个ASN.1语法中TAG值计算工具,有图有源码!!!

1、介绍下ASN.1语法

http://download.csdn.net/detail/nnm2000/951821

2、工具功能介绍

计算TLV结构中的T,即tag值。

3、工具运行效果图:

#include <iostream>
using namespace std;

typedef enum _tagClass
{
    UNIVERSAL        = 0,
    APPLICATION      = 1,
    CONTEXT_SPECIFIC = 2,
    PRIVATE          = 3
}TagClass;


typedef enum _tagStructType
{
    PRIMITIVE        = 0,
    CONSTRUCTED      = 1
}TagStructType;

unsigned int CalTagValue(int tag, TagClass cls, TagStructType pc, unsigned int *pResult);

int main(int argc, char **argv)
{
    int tag;
    unsigned int ResultLow = 0, ResultHigh = 0;
    TagClass cls = CONTEXT_SPECIFIC;
    TagStructType pc = PRIMITIVE;
    char chContinue = 0x0;    

    do 
    {
        cout<<"Plase input tagclass(0 = UNIVERSAL, 1 = APPLICATION, 2 = CONTEXT_SPECIFIC, 3 = PRIVATE):"<<endl;
        do
        {
            cout<<">";
            cin>>tag;
            if ((tag != UNIVERSAL) && (tag != APPLICATION) 
                && (tag != CONTEXT_SPECIFIC) && (tag != PRIVATE))
            {
                cout<<"Input error!"<<endl;
            }
            else
            {
                cls = (TagClass)tag;
                break;
            }
        }while (true);

        cout<<"Please input tag struct type(0 = PRIMITIVE, 1 = CONSTRUCTED):"<<endl;
        do 
        {
            cout<<">";
            cin>>tag;
            if ((tag != PRIMITIVE) && (tag != CONSTRUCTED))
            {
                cout<<"Input error!"<<endl;
            }
            else
            {
                pc = (TagStructType)tag;
                break;
            }
        }while(true);

        cout<<"Please input tag value:"<<endl;
        cout<<">";
        cin>>tag;

        ResultLow = CalTagValue(tag, cls, pc, &ResultHigh);

        cout<<"The Result is:0x"<<hex<<ResultHigh<<ResultLow<<endl;
        
        cout<<"Do you want continue?(y/n):"<<endl;
        cout<<">";
        cin>>chContinue;
    }while (tolower(chContinue) =='y');

    return 0;
}

unsigned int CalTagValue(int tag, TagClass cls, TagStructType pc, unsigned int *pResult)
{
    unsigned char chFirstByte = 0;
    
    switch (cls)
    {
    case UNIVERSAL:  // bit7 = 0 , bit6 = 0
        chFirstByte |= 0x0;
        break;
    case APPLICATION: // bit7 = 0, bit6 = 1
        chFirstByte |= 0x40;
        break;
    case CONTEXT_SPECIFIC: // bit7 = 1, bit6 = 0
        chFirstByte |= 0x80;
        break;
    case PRIVATE: // bit7 = 1, bit6 = 1
        chFirstByte |= 0xc0;
        break;
    default:
        return 0;   // error!
    }

    switch (pc)
    {
    case PRIMITIVE: //bi5 = 0
        chFirstByte |= 0x0;
        break;
    case CONSTRUCTED:
        chFirstByte |= 0x20;
        break;
    default:
        return 0; //error
    }
   
    if ((0 <= tag) && (tag <= 30))
    {
        chFirstByte |= (unsigned char)tag;
        return (unsigned int)(chFirstByte);    // Success!
    }
    else if (tag >= 31)
    {
        chFirstByte |= 0x1f;  // bit0 ~ bit4 all 1;
        int nTmp = tag;
        int nCount = 0;
        unsigned char szTmpBuf[7] = {0};  // 保存除第一个字节外的7个字节
        while ((nTmp > 0) && (nCount < sizeof(szTmpBuf)/sizeof(unsigned char)))
        {
            szTmpBuf[nCount++] = nTmp % 0x80;
            nTmp /= 0x80;
        }
		
		unsigned char chTmp = 0;
        unsigned int nTempTag = 0;
        nTempTag |= chFirstByte;
		
		if (nCount < 3)
		{
			for (int i=0; i < nCount; i++)
			{
				chTmp &= 0x0;  
				chTmp |= 0x80; // bit7 = 1
				chTmp |= szTmpBuf[nCount - 1 - i];
				nTempTag <<= 8;
				nTempTag |= chTmp;
			}
			nTempTag &= ~0x80;
		}
		else if ((nCount >= 3) && (nCount <= 7) && (NULL != pResult))
		{
			*pResult &= 0x0;
		    for (int i=0; i < nCount; i++)
			{
			    chTmp &= 0x0;  
				chTmp |= 0x80; // bit7 = 1
				chTmp |= szTmpBuf[nCount - 1 - i];
				*pResult <<= 8;
				*pResult |= ((nTempTag >> 24) & 0x0ff);
				nTempTag <<= 8;
				nTempTag |= chTmp;
			}
			nTempTag &= ~0x80;
		}
		else
		{
		    return 0; // Overflow!
		}
		return nTempTag; // Success!
    }
	else 
	{
	    // error!
	}

    return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值