注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

神魔破杜梓的叨叨堂

Programming every day!

 
 
 

日志

 
 
 
 

BitsReader:从ByteArry中读取一定个数的二进制位  

2008-07-15 23:33:37|  分类: My Tech |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
  原文来自ntt.cc

System-Binary I am working on SWF file decompiling according to the swf_file_format_spec_v9.pdf provided by adobe these days.  After I decompiled the first 8 bytes data of a sample SWF file. I found that the FrameSize is defined as a RECT structure below:

  1. RECT
  2. Field Type Comment
  3. Nbits UB[5] Bits in each rect value field
  4. Xmin SB[Nbits] x minimum position for rect
  5. Xmax SB[Nbits] x maximum position for rect
  6. Ymin SB[Nbits] y minimum position for rect
  7. Ymax SB[Nbits] y maximum position for rect

I have to read the first 5 bits from a ByteArray contains 9 bytes as the Nbits, then read 15 bits each time to get the positions for the rect.

That’s really a hard work for me to get bits from the binary data, I have never do such thing before.

I wrote a lot of codes, and tried many ways to get what I want . But I found all of them are not “beautiful” enough. At last, I got this simple class to catch what I want.

This class is used to read any amount,less than 53, bits from a given ByteArray.

Download: BitsReader.as
  1. package com.edison.utils
  2. {
  3. import flash.utils.ByteArray;
  4.  
  5. public class BitsReader
  6. {
  7. private var _source:ByteArray;
  8.  
  9. private var maxBitsLength:uint;
  10.  
  11. private const BYTE_LEGNTH:uint = 8;
  12.  
  13. private const NUMBER_LENGTH:uint = 53;
  14.  
  15. private const INT_LENGTH:uint = 32;
  16.  
  17. public function get source():ByteArray
  18. {
  19. return _source;
  20. }
  21.  
  22. private var _position:uint = 0;
  23.  
  24. public function get position():uint
  25. {
  26. return _position;
  27. }
  28.  
  29. public function set position(value:uint):void
  30. {
  31. if(value > maxBitsLength)
  32. {
  33. _position = maxBitsLength;
  34. }
  35. else
  36. {
  37. _position = value;
  38. }
  39. }
  40.  
  41. public function BitsReader(source:ByteArray)
  42. {
  43. maxBitsLength = source.length*BYTE_LEGNTH;
  44. _source = source;
  45. }
  46.  
  47. public function read(length:uint):Number
  48. {
  49. //make sure the length of the return value is less or equal than the length of Number;
  50. if(length> NUMBER_LENGTH)
  51. {
  52. length = NUMBER_LENGTH;
  53. }
  54.  
  55. //make sure the length of the return value if less or equal the amount of all the bits.
  56. if(length>maxBitsLength)
  57. {
  58. length = maxBitsLength;
  59. }
  60.  
  61. //if there is not enought bits to return, return all the left.
  62. if(length>maxBitsLength - position)
  63. {
  64. length = maxBitsLength - position;
  65. }
  66.  
  67. var result:Number = 0;
  68.  
  69. var byteLength:uint = (position+length)/BYTE_LEGNTH;
  70.  
  71. var leftBitsLength:uint = (position+length)%BYTE_LEGNTH;
  72.  
  73. var startByteIndex:uint = 0;
  74. var extractFromFirstByte:Boolean = false;
  75.  
  76. if(position!=0)
  77. {
  78. startByteIndex = Math.ceil(position/BYTE_LEGNTH)-1;
  79. extractFromFirstByte = true;
  80. }
  81. source.position = startByteIndex;
  82.  
  83. var byte:uint = 0;
  84. for(var i:uint = startByteIndex ;i<byteLength;i++)
  85. {
  86. result <<= BYTE_LEGNTH;
  87. byte = source.readUnsignedByte();
  88.  
  89. if(extractFromFirstByte)
  90. {
  91. result ^= (byte<>> (position%BYTE_LEGNTH+INT_LENGTH-BYTE_LEGNTH) ;
  92. position += (BYTE_LEGNTH - position%BYTE_LEGNTH);
  93. extractFromFirstByte = false;
  94. }
  95. else
  96. {
  97. result ^= byte;
  98. position += BYTE_LEGNTH;
  99. }
  100. }
  101. result <>(BYTE_LEGNTH-leftBitsLength);
  102.  
  103. position += leftBitsLength;
  104.  
  105. return result;
  106. }
  107.  
  108. }
  109. }

This is a sample of BitsReader, quite easy.

  1. var rect:ByteArray = new ByteArray();
  2. uncompressedData.readBytes(rect,0,9);
  3.  
  4. var bits:BitsReader = new BitsReader(rect);
  5. bits.read(5);
  6. bits.read(15);
  7. trace(file width: “,bits.read(15)/20,”pixel”);
  8. bits.read(15);
  9. trace(”file height: “,bits.read(15)/20,”pixel);

Now, you can have a try with this class :).

  评论这张
 
阅读(512)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017