qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          TTCN-3 編碼解碼相關(guān)模塊引入2

           在編解碼例子一中提到了系統(tǒng)例子中提供的 binary_string.c,binary_string.h 兩個操作二進制串的文件,看過代碼的童鞋會發(fā)現(xiàn),實現(xiàn)是使用一個char數(shù)組來保存數(shù)據(jù)的,也就是說,每次存入提取都需要以8比特為單位。
            在網(wǎng)絡(luò)數(shù)據(jù)傳輸中一般是保證整字節(jié)的,但是網(wǎng)絡(luò)協(xié)議的定義往往對每一比特位都“物盡其用”,因此在編解碼的過程中往往會出現(xiàn)添加不是整字節(jié)單位數(shù)據(jù)的情況。
            考慮下面代碼,使用到TTCN-3中hexstring的類型
          module Codec_B {
          //1.端口定義
          type port common_port message {
          inout all
          }
          //2.成分定義
          type component MyMTC{
          port common_port mtc_port;
          }
          type component MySUT{
          port common_port sut_port;
          }
          // 定義測試
          testcase Basic_TC () runs on MyMTC system MySUT
          {
          mtc_port.clear;
          //不能操作sut_port,這個端口在SUT,并不在MTC的控制下
          //sut_port.clear;
          map(mtc:mtc_port, system:sut_port);
          mtc_port.start;
          //不能操作sut_port,這個端口在SUT,并不在MTC的控制下
          //sut_port.start;
          mtc_port.send('ABCD'H);
          mtc_port.receive('ABCD'H);
          mtc_port.send('ABC'H);
          mtc_port.receive('ABC0'H);
          setverdict(pass);
          stop;
          }
          //控制部分
          control {
          execute(Basic_TC());
          }
          }



           編碼函數(shù)中,碰到hexstring我們需要做出如下處理:
            1.碰到偶數(shù)情況與octetstring處理方式相同
            2.如果是奇數(shù)情況,需要額外多申請一個字節(jié),該字節(jié)的高4位是數(shù)據(jù)、低4位用0填充;同時記錄數(shù)據(jù)位數(shù),繼續(xù)編碼時從低4位開始
            在binary_string.c 中添加處理半字節(jié)的方法
          /* Appends half byte to the end of binary string
          * It's assumed that binary string has enough free space
          */
          static void append_half_byte (MyBinaryString *string, unsigned char data)
          {
          unsigned long free_pos;
          free_pos = string->string.bits/8;
          string->string.data[free_pos] = data;
          string->string.bits += 4;
          }
            在tci_codec.c中添加hexstring編碼函數(shù)
          //對hexstring類型的數(shù)據(jù)進行編碼
          void encode_hexstring(MyBinaryString *msg, String str)
          {
          unsigned char * binstr;
          unsigned char binvalue;
          unsigned long len;
          int i,k;
          printf("\n In function encode_hexstring() \n");
          //輸入的字符串中含有前后雙引號和H,將它們?nèi)サ?/div>
          len = str?strlen(str):0;
          for(i=0;i<len-3;i++){
          str[i]=str[i+1];
          }
          str[i]='\0';
          len=strlen(str);//the new length
          if(len%2==0){//偶數(shù)的話與處理octetstring相同
          binstr =(unsigned char *)malloc(len);
          char2hex_encode(msg,str,binstr);
          binary_string_append_bytes(msg, binstr, len/2);
          }else{
          //基數(shù)的話先處理前面的偶數(shù)個字符
          binstr =(unsigned char *)malloc(len);
          //先把最后一個字符保存起來
          binvalue = str[i-1];
          str[i-1] = '\0';
          char2hex_encode(msg,str,binstr);
          binary_string_append_bytes(msg, binstr, len/2);
          //處理最后一個字節(jié),不夠的用0填充
          len += 1;
          if(((binvalue-'0')>=0)&&((binvalue-'9')<=0)) {
          binvalue = (binvalue-'0')*16;
          } else if(((binvalue-'A')>=0)&&((binvalue-'F')<=0)) {
          binvalue = (binvalue-'A'+10)*16;
          } else if(((binvalue-'a')>=0)&&((binvalue-'f')<=0)) {
          binvalue = (binvalue-'a'+10)*16;
          } else {
          tci_assert(0, "Wrong hex string: the value should be between 0~9, a~f");
          }
          binary_string_append_char(msg,binvalue);
          }
          printf("\n Leave function encode_hexstring() \n");
          }
          相關(guān)文章:
           TTCN-3 編碼解碼相關(guān)模塊引入

          posted on 2013-12-04 11:11 順其自然EVO 閱讀(210) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2013年12月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 葵青区| 米林县| 壶关县| 罗甸县| 乳山市| 萝北县| 屯昌县| 和龙市| 吴堡县| 沙田区| 襄垣县| 繁峙县| 奉新县| 缙云县| 贵港市| 儋州市| 江川县| 永川市| 会同县| 遂溪县| 潜山县| 奉贤区| 龙口市| 隆化县| 孟村| 萨嘎县| 屏山县| 凉城县| 揭东县| 连云港市| 当雄县| 新泰市| 阿巴嘎旗| 清丰县| 亚东县| 桦川县| 德化县| 同仁县| 清徐县| 扬州市| 金溪县|