bytestreamjs-wildcards
v2.1.0
Published
ByteStream is a library making possible to manipulates single bytes and bits on pure JavaScript
Downloads
7
Maintainers
Readme
ByteStream.js
ByteStream class
ByteStream class is the major class for all others. It provides many useful function for making search for patterns and data transformation. Optimized for fast as possible data processing.
Method | Description -------|-------------- clear|Clear existing stream fromArrayBuffer|Initialize "Stream" object from existing "ArrayBuffer" fromUint8Array|Initialize "Stream" object from existing "Uint8Array" fromString|Initialize "Stream" object from existing string toString|Represent "Stream" object content as a string fromHexString|Initialize "Stream" object from existing hexdecimal string toHexString|Represent "Stream" object content as a hexdecimal string copy|Return copy of existing "Stream" slice|Return slice of existing "Stream" realloc|Change size of existing "Stream" append|Append a new "Stream" content to the current "Stream" insert|Insert "Stream" content to the current "Stream" at specific position isEqual|Check that two "Stream" objects has equal content isEqualView|Check that current "Stream" objects has equal content with input "Uint8Array" findPattern|Find any byte pattern in "Stream" findFirstIn|Find first position of any pattern from input array findAllIn|Find all positions of any pattern from input array findAllPatternIn|Find all positions of a pattern findFirstNotIn|Find first position of data, not included in patterns from input array findAllNotIn|Find all positions of data, not included in patterns from input array findFirstSequence|Find position of a sequence of any patterns from input array findAllSequences|Find all positions of a sequence of any patterns from input array findPairedPatterns|Find all paired patterns in the stream findPairedArrays|Find all paired patterns in the stream replacePattern|Replace one patter with other skipPatterns|Skip any pattern from input array skipNotPatterns|Skip any pattern not from input array
SeqStream class
SeqStream class is the aux class for sequential reading/writing data from/to ByteStream underline class.
Method | Description -------|-------------- resetPosition|Reset current position of the "SeqStream" findPattern|Find any byte pattern in "ByteStream" findFirstIn|Find first position of any pattern from input array findAllIn|Find all positions of any pattern from input array findFirstNotIn|Find first position of data, not included in patterns from input array findAllNotIn|Find all positions of data, not included in patterns from input array findFirstSequence|Find position of a sequence of any patterns from input array findAllSequences|Find position of a sequence of any patterns from input array findPairedPatterns|Find all paired patterns in the stream findPairedArrays|Find all paired patterns in the stream replacePattern|Replace one patter with other skipPatterns|Skip of any pattern from input array skipNotPatterns|Skip of any pattern from input array append|Append a new "Stream" content to the current "Stream" appendView|Append a "view" content to the current "Stream" appendChar|Append a new char to the current "Stream" getBlock|Get a block of data getUint32|Get 4-byte unsigned integer value
BitStream class
Main purpose of the BitStream is manipulating of each bit inside any ByteStream data.
Method | Description -------|-------------- clear|Clear existing stream fromByteStream|Initialize "BitStream" by data from existing "ByteStream" fromArrayBuffer|Initialize "BitStream" object from existing "ArrayBuffer" fromUint8Array|Initialize "BitStream" object from existing "Uint8Array" fromString|Initialize "BitStream" object from existing bit string toString|Represent "BitStream" object content as a string shiftRight|Shift entire "BitStream" value right to number of bits shiftLeft|Shift entire "BitStream" value left to number of bits slice|Return slice of existing "BitStream" copy|Return copy of existing "BitStream" shrink|Shrink unnecessary bytes in current stream accordingly to "bitsCount" value reverseBytes|Reverse bits order in each byte in the stream reverseValue|Reverse all bits in entire "BitStream" getNumberValue|Trying to represent entire "BitStream" as an unsigned integer findPattern|Find any bit pattern in "BitStream" findFirstIn|Find first position of any pattern from input array findAllIn|Find all positions of any pattern from input array findAllPatternIn|Find all positions of a pattern findFirstNotIn|Find first position of data, not included in patterns from input array findAllNotIn|Find all positions of data, not included in patterns from input array findFirstSequence|Find position of a sequence of any patterns from input array findAllSequences|Find position of a sequence of any patterns from input array findPairedPatterns|Find all paired patterns in the stream findPairedArrays|Find all paired patterns in the stream replacePattern|Replace one pattern with other skipPatterns|Skip any pattern from input array skipNotPatterns|Skip any pattern not from input array append|Append a new "BitStream" content to the current "BitStream"
SeqBitStream class
SeqBitStream class is the aux class for sequential reading/writing data from/to BitStream underline class.
Method | Description -------|-------------- getBits|Get next "length" bits from the stream getBitsString|Get string representation for the next "length" bits from the stream getBitsReversedValue|Get number value representation of the next "length" bits from the stream, preliminary reversed toString|Represent remaining bits in "BitStream" as a string
Wildcard byte patterns
The ByteStream class provides an option to use wildcard byte patterns. These are useful in searching for magic bytes in a file format.
Example of using wildcard byte patterns:
let wildcard = "?".charCodeAt(0);
let searchPattern = new ByteStream({
// set wildcard byte value
wildcard: wildcard,
// set search pattern with wildcard bytes ("s??e ")
view: new Uint8Array([0x73, wildcard, wildcard, 0x65, 0x20])
});
let dataPattern = new ByteStream({ data: 'some random data 0x00 0x00 0x01 0x45' });
dataPattern.findPattern(searchPattern); // find search pattern in data pattern
parseByteMap functionality
The parseByteMap
function is intended to parse and check byte streams with determined structure.
Example of map used as a kind of template. Exactly this map is using for parsing PDF xref table:
let map = [
{
type: "string",
name: "type",
minlength: 1,
maxlength: 1,
func: function(array){
let result = {
status: (-1),
length: 1
};
switch(array[0])
{
case 0x6E: // "n"
result.value = "n";
break;
case 0x66: // "f"
result.value = "f";
break;
default:
return result;
}
result.status = 1;
return result;
}
},
{
type: "check",
minlength: 1,
maxlength: 2,
func: function(array){
let position = (-1);
if(array[0] == 0x0A)
position = 1;
if(array[1] == 0x0A)
position = 2;
return {
status: (position > 0) ? 1 : (-1),
length: position
};
}
}
];
License
Copyright (c) 2016-2022, Peculiar Ventures All rights reserved.
Author 2016-2018 Yury Strozhevsky.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.