简体   繁体   English

Javascript二进制文件读取

[英]Javascript binary file reading

From here : 这里

_shl: function (a, b){
        for (++b; --b; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1);
        return a;
    },

_readByte: function (i, size) {
        return this._buffer.charCodeAt(this._pos + size - i - 1) & 0xff;
    },

_readBits: function (start, length, size) {
        var offsetLeft = (start + length) % 8;
        var offsetRight = start % 8;
        var curByte = size - (start >> 3) - 1;
        var lastByte = size + (-(start + length) >> 3);
        var diff = curByte - lastByte;

        var sum = (this._readByte(curByte, size) >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1);

        if (diff && offsetLeft) {
            sum += (this._readByte(lastByte++, size) & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight; 
        }

        while (diff) {
            sum += this._shl(this._readByte(lastByte++, size), (diff-- << 3) - offsetRight);
        }

        return sum;
    },

This code does binary file reading. 此代码执行二进制文件读取。 Unfortunately this code isn't documented. 不幸的是,该代码没有记录。 I would like to understand how it works. 我想了解它是如何工作的。 (especially _readBits and _shl methods) In _readBits what's offsetright for? (尤其是_readBits和_shl方法)在_readBits中,offsetright有什么用? also curByte and lastByte: i thought this way about it: 还curByte和lastByte:我这样想:

_readBits(0,16,2) curByte becomes 1. lastByte becomes 0. Why is lastByte less than curByte? _readBits(0,16,2)curByte变为1。lastByte变为0。为什么lastByte小于curByte? Or i made a mistake? 还是我弄错了? Where? 哪里? Please, help! 请帮忙!

我发现了这一点,并且有很好的记录: https : //github.com/vjeux/jParser

  • _readByte(i, size) : size is the buffer length in byte, i is the reversed index (index from the end of the buffer, not from the start) of the byte which you want to read. _readByte(i, size)size是缓冲区长度(以字节为单位), i是要读取的字节的反向索引 (从缓冲区末尾开始的索引,而不是从开头开始的索引)。
  • _readBits(start, length, size) : size is the buffer length in byte, length is the number of bits which you want to read, start is the the index of the first bit which you want to read. _readBits(start, length, size)size是缓冲区的长度(以字节为单位), length是要读取的位数, start是要读取的第一位的索引
  • _shl(a, b) : convert read byte a to actual value based on its index b . _shl(a, b) :根据其索引b将读取字节a转换为实际值。

Because _readByte use reversed index, so lastByte less than curByte . 因为_readByte使用反向索引,所以lastByte小于curByte

offsetLeft and offsetRight are used to remove unrelated bits (bits not in read range) from lastByte and curByte respectively. offsetLeftoffsetRight用于分别从lastBytecurByte删除不相关的位(不在读取范围内的位)。

I'm not sure how cross-browser the code is. 我不确定代码是如何跨浏览器的。 I've used the method below for reading binary data. 我已使用以下方法读取二进制数据。 IE has some issues that can't be resolved with only Javascript, you need a small helper function written in VB. IE有一些仅使用Javascript无法解决的问题,您需要一个用VB编写的小型辅助函数。 Here is a fully cross browser binary reader class. 是一个完全跨浏览器的二进制阅读器类。 If you just want the bits that matter without all the helper functionality, just add this to the end of your class that needs to read binary: 如果您只需要重要的位而没有所有辅助功能,只需将其添加到需要读取二进制文件的类的末尾即可:

// Add the helper functions in vbscript for IE browsers
// From https://gist.github.com/161492
if ($.browser.msie) {
    document.write(
        "<script type='text/vbscript'>\r\n"
        + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n"
        + " IEBinary_getByteAt = AscB(MidB(strBinary,iOffset+1,1))\r\n"
        + "End Function\r\n"
        + "Function IEBinary_getLength(strBinary)\r\n"
        + " IEBinary_getLength = LenB(strBinary)\r\n"
        + "End Function\r\n"
        + "</script>\r\n"
    );
}

Then you can pick whats right in your class add: 然后,您可以在类中选择合适的内容并添加:

// Define the binary accessor function
if ($.browser.msie) {
        this.getByteAt = function(binData, offset) {
        return IEBinary_getByteAt(binData, offset);
    }
} else {
    this.getByteAt = function(binData, offset) {
        return binData.charCodeAt(offset) & 0xFF;
    }
}

Now you can read the bytes all you want 现在您可以读取所有想要的字节

var byte = getByteAt.call(this, rawData, dataPos);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM