bireader

1.0.56 • Public • Published

bireader

A feature rich binary reader and writer that keeps track of your position to quickly create file structures. Includes shared naming conventions, programmable inputs and advanced math for easy data conversions to do low level parsing. Accepts Uint8Array or Buffer.

Supported data types are:

  • Bitfields ([u]bit{1-32}{le|be}) 1-32 bit signed or unsigned value in big or little endian order
  • Bytes ([u]int8, byte) 8 bit signed or unsigned value
  • Shorts ([u]int16, word, short{le|be}) 16 bit signed or unsigned value in big or little endian order
  • Half Floats (halffloat, half{le|be}) 16 bit decimal value in big or little endian order
  • Integers ([u]int32, long, int, double{le|be}) 32 bit signed or unsigned value in big or little endian order
  • Floats (float{le|be}) 32 bit decimal value in big or little endian
  • Quadwords ([u]int64, quad, bigint{le|be}) 64 bit signed or unsigned in big or little endian
  • Double Floats (doublefloat, dfloat{le|be}) 64 bit decimal value in big or little endian
  • Strings (string) Fixed and non-fixed length, UTF, pascal, wide pascal. Includes all TextEncoder types

Installation

npm install bireader

Provides both CommonJS and ES modules.

Example

Import the reader or writer. Create a new parser with the data and start parsing.

Includes presents for quick parsing or programmable functions (examples below).

import {bireader, biwriter} from 'bireader';

//read example - parse a webp file
function parse_webp(data){
  const br = new bireader(data)
  br.hexdump({supressUnicode:true}) //console.log data as hex

  //         0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F  0123456789ABCDEF
  // 00000  52 49 46 46 98 3a 00 00 57 45 42 50 56 50 38 58  RIFF.:..WEBPVP8X
  // 00010  0a 00 00 00 10 00 00 00 ff 00 00 ff 00 00 41 4c  ..............AL
  // 00020  50 48 26 10 00 00 01 19 45 6d 1b 49 4a 3b cf 0c  PH&.....Em.IJ;..
  // 00030  7f c0 7b 60 88 e8 ff 04 80 a2 82 65 56 d2 d2 86  ..{`.......eV...
  // 00040  24 54 61 d0 83 8f 7f 0e 82 b6 6d e3 f0 a7 bd ed  $Ta.......m.....
  // 00050  87 10 11 13 40 3b 86 8f 26 4b d6 2a b7 6d 24 39  ....@;..&K.*.m$9
  // 00060  52 4f fe 39 7f 3b 62 4e cc ec 9b 17 31 01 0c 24  RO.9.;bN....1..$
  // 00070  49 89 23 e0 01 ab 52 64 e3 23 fc 61 db 76 cc 91  I.#...Rd.#.a.v..
  // 00080  b6 7d fb 51 48 c5 69 db 4c 1b 63 db b6 ed b9 6d  .}.QH.i.L.c....m
  // 00090  db be 87 8d b1 6d db 9e b6 cd a4 d3 ee 24 95 54  .....m.......$.T
  // 000a0  52 b8 8e 65 a9 eb 38 ce ab 52 75 9d 67 ff 75 2f  R..e..8..Ru.g.u/
  // 000b0  77 44 40 94 6d 25 6c 74 91 a8 88 86 58 9b da 6e  wD@.m%lt....X..n

  const header = {}
  header.magic = br.string({length:4})    //RIFF
  header.size = br.uint32le()             //15000
  header.fileSize = header.size + 8       //15008
  header.payload = br.string({length:4})  //WEBP
  header.format = br.string({length:4})   //VP8X
  header.formatChunkSize = br.uint32le()  //10
  switch (header.format){
    case "VP8 ":
        header.formatType = "Lossy"
        var read_size = 0
        header.frame_tag = br.ubit24()
        read_size += 3;
        header.key_frame = header.frame_tag & 0x1;
        header.version = (header.frame_tag >> 1) & 0x7;
        header.show_frame = (header.frame_tag >> 4) & 0x1;
        header.first_part_size = (header.frame_tag >> 5) & 0x7FFFF;
        header.start_code = br.ubit24() //should be 2752925
        header.horizontal_size_code = br.ubit16();
        header.width = header.horizontal_size_code & 0x3FFF;
        header.horizontal_scale = header.horizontal_size_code >> 14;
        header.vertical_size_code = br.ubit16();
        header.height = header.vertical_size_code & 0x3FFF;
        header.vertical_scale = header.vertical_size_code >> 14;
        read_size += 7;
        header.VP8data = br.extract(header.formatChunkSize - read_size, true)
        break;
    case "VP8L":
        header.formatType = "Lossless"
        var read_size = 0
        header.signature = br.ubyte() //should be 47
        read_size += 1;
        header.readWidth =  br.ubit14()
        header.width = header.readWidth+1;
        header.readHeight =  br.ubit14()
        header.height = header.readHeight+1;
        header.alpha_is_used =  br.bit1() 
        header.version_number =  br.ubit3() 
        read_size += 4;
        header.VP8Ldata = br.extract(header.formatChunkSize - read_size, true)
        break;
    case "VP8X":
        header.formatType = "Extended"
        br.big() //switch to Big Endian bit read
        header.rsv = br.bit2()  //Reserved
        header.I = br.bit1()    //ICC profile
        header.L = br.bit1()    //Alpha
        header.E = br.bit1()    //Exif
        header.X = br.bit1()    //XMP
        header.A = br.bit1()    //Animation
        header.R = br.bit1()    //Reserved
        br.little() //return to little
        header.rsv2 = br.ubit24()
        header.widthMinus1 = br.ubit24()
        header.width = header.widthMinus1 + 1
        header.heightMinus1 = br.ubit24()
        header.height = header.heightMinus1 + 1
        if(header.I)
        {
          header.ICCP = br.string({length:4})  // Should be ICCP
          header.ICCPChunkSize = br.uint32()
          header.ICCPData = br.extract(header.ICCPChunkSize, true)
        }
        if(header.L)
        {
          header.ALPH = br.string({length:4})  // Should be ALPH
          header.ALPHChunkSize = br.uint32() //4134
          header.ALPHData = br.extract(header.ALPHChunkSize, true)
        }
        if(header.A)
        {
          header.ANI = br.string({length:4})  // Should be ANIM or ANIF
          header.ANIChunkSize = br.uint32()
          if(header.ANI == "ANIM")
          {
            header.BGColor = br.uint32()
            header.loopCount = br.ushort()
            header.ANIMData = br.extract(header.ANIChunkSize, true)
          } else
          if (header.ANI == "ANIF")
          {
            header.FrameX = br.ubit24()
            header.FrameY = br.ubit24()
            header.readFrameWidth = br.ubit24()
            header.readFrameHeight = br.ubit24()
            header.frameWidth = readFrameWidth + 1
            header.frameHeight = readFrameHeight + 1
            header.duration = br.ubit24()
            header.rsv3 = br.ubit6()
            header.byte.B = br.bit1() //Blending
            header.byte.D = br.bit1() //Disposal
            header.frameData = br.extract(16, true)
            header.ANIFData = br.extract(header.ANIChunkSize, true)
          }
        }
        header.extFormatStr = br.string({length:4})
        header.extChunkSize = br.uint32()
        header.extData = br.extract(header.extChunkSize, true)
        if(header.E)
        {
          header.EXIF = br.string({length:4})  // Should be EXIF
          header.EXIFChunkSize = br.uint32()
          header.EXIFData = br.extract(header.EXIFChunkSize, true)
        }
        if(header.X)
        {
          header.XMP = br.string({length:4})  // Should be XMP
          header.XMPChunkSize = br.uint32()
          header.XMPMetaData = br.extract(header.XMPChunkSize, true)
        }
        break;
    default:
        header.data = br.extract(header.formatChunkSize, true)
        break;
  }
  br.finished()
  return header
}

//write example - write a webp file from read data
function write_webp(data){
  const bw = new biwriter(new Uint8Arry(1)) //extends array as we write by default
  bw.string("RIFF",{length:4})
  bw.uint32le(0) //dummy for now, will be final size - 8
  bw.string("WEBP",{length:4})
  switch(data.format){
    case "VP8 ":
      bw.string("VP8 ",{length:4})
      bw.uint32le(data.VP8data.length)
      bw.ubit24(data.key_frame)
      bw.ubit24(data.start_code)
      bw.ubit16(data.horizontal_size_code)
      bw.ubit16(data.vertical_size_code)
      bw.overwrite(data.VP8data ,true)
      break;
    case "VP8L":
      bw.string("VP8L",{length:4})
      bw.uint32le(data.VP8Ldata.length - 4)
      bw.ubyte(47)
      bw.ubit14(data.width - 1)
      bw.ubit14(data.heigth - 1)
      bw.ubit1(data.alpha_is_used)
      bw.bit3(data.version_number)
      bw.overwrite(data.VP8Ldata,true)
      break;
    case "VP8X":
      bw.string("VP8X",{length:4})
      bw.uint32le(10)
      bw.big()
      bw.bit2(0)
      bw.bit1(data.I)
      bw.bit1(data.L)
      bw.bit1(data.E)
      bw.bit1(data.X)
      bw.bit1(data.A)
      bw.bit1(0)
      bw.little()
      bw.ubit24(data.rsv2)
      bw.ubit24(data.width - 1)
      bw.ubit24(data.height - 1)
      if(data.I)
      {
        bw.string(data.ICCP, {length:4})
        bw.uint32(data.ICCPData.length)
        bw.replace(data.ICCPData, true)
      }
      if(data.L)
      {
        bw.string(data.ALPH, {length:4})
        bw.uint32(data.ALPHData.length) 
        bw.replace(data.ALPHData)
      }
      if(data.A)
      {
        bw.string(data.ANI, {length:4})
        bw.uint32(data.ANIChunkSize)
        if(data.ANI == "ANIM")
        {
          bw.uint32(data.BGColor)
          bw.ushort(data.loopCount)
          bw.replace(data.ANIMData)
        } else
        if (data.ANI == "ANIF")
        {
          bw.ubit24(data.FrameX)
          bw.ubit24(data.FrameY)
          bw.ubit24(data.frameWidth - 1)
          bw.ubit24(data.frameHeigh - 1)
          bw.ubit24(data.duration)
          bw.ubit6(data.rsv3)
          bw.bit1(data.byte.B)
          bw.bit1(data.byte.D) 
          bw.replace(data.frameData, true)
          bw.replace(data.ANIFData, true)
        }
      }
      bw.string(data.extFormatStr, {length:4})
      bw.uint32(data.extData.length)
      bw.replace(data.extData, true)
      if(data.E)
      {
        bw.string(data.EXIF, {length:4})
        bw.uint32(data.EXIFData.length)
        bw.replace( data.EXIFData, true)
      }
      if(data.X)
      {
        bw.string(data.XMP, {length:4})
        bw.uint32(data.XMPMetaData.length)
        bw.replace(data.XMPMetaData, true)
      }
      break;
    default:
      break;
  }
  bw.trim() //remove any remaining bytes
  bw.goto(4)
  bw.uint32le(bw.size - 8) //write file size
  return bw.return()
}

Common Functions

Common functions for setup, movement, manipulation and math shared by both.

Function Params (bold requires) Desc
Setup
Name new bireader(data, byteOffset, bitOffset, endianess, strict) Buffer or Uint8Array, byte offset (default 0), bit offset (default 0), endian big or little (default little), strict mode true to restrict extending initially supplied data (default true for reader, false for writer) Start with new Constructor.

Note: Data can always be found with .data. Also biwrite can created without data and will default to a supplied offset as a Buffer (if available) or Uint8Array.
Name new biwriter(data, byteOffset, bitOffset, endianess, strict)
Name endianness(bigOrLittle) big or little (default little) Set or change Endian. Can be changed at any time.
Presets bigEndian(), big(), be()
littleEndian(), little(), le()
Name length() none Gets the current buffer size in bytes.
Aliases size, FileSize()
Name lengthB() none Gets the current buffer size in bits.
Aliases sizeB, FileSizeB()
Name getOffset() none Gets current byte position.
Aliases FTell(), tell(), saveOffset()
Name getOffsetBit() none Gets current byte's bit position (0-7).
Aliases FTellB(), tellB(), saveOffsetBit()
Name getOffsetAbsBit() none Gets current absolute bit position from start of data.
Aliases tellAbsB(), saveOffsetAbsBit()
Name remain() none Size in bytes of current read position to the end.
Aliases FEoF()
Name remainB() none Size in bits of current read position to the end.
Aliases FEoFB()
Name getLine() none Row line of the file (16 bytes per row).
Aliases row()
Name get() none Returns supplied data.
Aliases return()
Name hexdump({length, startByte, supressUnicode}) Length of dump in bytes (default 192), byte position to start the dump (default current byte position), supress unicode character preview for cleaner columns (default false) Console logs data. Will trigger on error unless turned off (see below)
Name errorDumpOff() None Does not hexdump on error (default true)
Name errorDumpOn() None Will hexdump on error (default true)
Name unrestrict() none Sets strict mode to false, will extend array if data is outside of max size (default true for reader, false for writer)
Name restrict() none Sets strict mode to true, won't extend array if data is outside of max size (default true for reader, false for writer)
Name end() none Removes supplied data.
Aliases close(), done(), finished()
Search
Name findString(value, unsigned, endian) Searches for byte position of string from current read position. Note: Does not change current read position.
Name findByte(value, unsigned, endian) Searches for byte value (can be signed or unsigned) position from current read position. Note: Does not change current read position.
Name findShort(value, unsigned, endian) Searches for short value (can be signed or unsigned) position from current read position. Note: Does not change current read position.
Name findInt(value, unsigned, endian) Searches for integer value (can be signed or unsigned) position from current read position. Note: Does not change current read position.
Name findInt64(value, unsigned, endian) Searches for 64 bit position from current read position. Note: Does not change current read position.
Name findHalfFloat(value, endian) Searches for half float value position from current read position. Note: Does not change current read position.
Name findFloat(value, endian) Searches for float value position from current read position. Note: Does not change current read position.
Name findDoubleFloat(value, endian) Searches for double float value position from current read position. Note: Does not change current read position.
Movement
Name align(number) Aligns byte position to number. Note: Errors in strict mode when change is outside of data size.
Name alignRev(number) Reverse aligns byte position to number. Note: Errors in strict mode when change is outside of data size.
Name skip(bytes, bits) Bytes to skip from current byte position, bits to skip (default 0) Use negative to go back.
Note: Remaining bits are dropped when returning to a byte function.
Alias FSeek(byte, bit)
seek(byte, bit)
jump(bytes, bits)
Name goto(byte, bit) Byte offset from start, bit offset from byte offset Note: Remaining bits are drop when returning to byte function.
Aliases pointer(byte, bit)
warp(byte, bit)
Name rewind() none Moves current byte position to start of data.
Alias gotoStart()
Name last() none Moves current byte position to end of data.
Alias gotoEnd(), EoF()
Manipulation
Name delete(startOffset, endOffset, consume) Start byte of data (default 0), end byte of data (default current byte position), move byte position to after data read (default false) Removes and returns data.
Note: Errors on strict mode
Preset clip()
trim()
None Removes data after the current byte position and returns data.
Note: Errors on strict mode
Name crop(length, consume) Number of bytes to read and remove from current byte position, move byte position to after data read (default false) Removes and returns data from current byte position for length of data.
Note: Errors on strict mode
Alias drop(length, consume)
Name replace(data, consume, offset) Data to replace in supplied data, move byte position to after data read (default false), byte position to start replace (default current byte position) Replaces data at current byte or supplied offset.
Note: Errors on strict mode
Alias overwrite(data, consume, offset)
Name lift(startByte, endByte, consume, fillValue) Start of byte read (default current byte position), end of byte read (default end of data), move current byte position to end of byte read (default false), value to fill bytes (will NOT fill on default) Returns data from supplied byte positions.
Note: Only moves current byte position if consume is true. Only fills data if value is supplied
Aliases fill(startByte, endByte, consume, fillValue)
Name extract(length, consume) Number of bytes to read, move byte position to after data read (default false) Returns data from current byte position for length of data.
Aliases slice(length, consume)
wrap(length, consume)
Name insert(data, consume, offset) New data to insert, move byte position to after data read (default false), byte position to insert (default current byte position) Inserts new data into supplied data. Note: Data type must match supplied data. Errors on strict mode
Aliases place(data, consume, offset)
Name unshift(data, consume) New data to insert, move byte position to after data read (default false) Adds new data to start of supplied data
Note: Data type must match supplied data. Errors on strict mode
Aliases prepend(data, consume)
Name push(length, consume) Number, move byte position to after data read (default false) Adds new data to end of supplied data
Note: Data type must match supplied data. Errors on strict mode
Aliases append(data, consume)
Math
Name xor(xorKey, startOffset, endOffset, consume) Byte value, string, Uint8Array or Buffer, byte position to start (default current position), byte position to end (default end of data), move byte position to after operation (default false) XOR data. Note: Will loop if operation length is longer than supplied key.
Name xorThis(xorKey, length, consume) Byte value, string, Uint8Array or Buffer, length of bytes starting at current byte (repeats when longer, default 1 byte for byte value, string length or end of data for string, array length or end of data for array or Buffer), byte position to end (default end of data), move byte position to after operation (default false) XOR data Note: Will loop if operation length is longer than supplied key.
Name or(orKey, startOffset, endOffset, consume) Byte value, string, Uint8Array or Buffer, byte position to start (default current position), byte position to end (default end of data), move byte position to after operation (default false) OR data Note: Will loop if operation length is longer than supplied key.
Name orThis(orKey, length, consume) Byte value, string, Uint8Array or Buffer, length of bytes starting at current byte (repeats when longer, default 1 byte for byte value, string length or end of data for string, array length or end of data for array or Buffer), byte position to end (default end of data), move byte position to after operation (default false) OR data Note: Will loop if operation length is longer than supplied key.
Name and(andKey, startOffset, endOffset, consume) Byte value, string, number array or Buffer, byte position to start (default current position), byte position to end (default end of data), move byte position to after operation (default false) AND data Note: Will loop if operation length is longer than supplied key.
Name andThis(andKey, length, consume) Byte value, string, number array or Buffer, length of bytes starting at current byte (repeats when longer, default 1 byte for byte value, string length or end of data for string, array length or end of data for array or Buffer), byte position to end (default end of data), move byte position to after operation (default false) AND data Note: Will loop if operation length is longer than supplied key.
Name add(addKey, startOffset, endOffset, consume) Byte value, string, number array or Buffer, byte position to start (default current position), byte position to end (default end of data), move byte position to after operation (default false) Add value to data (per byte). Note: Will loop if operation length is longer than supplied key.
Name addThis(addKey, length, consume) Byte value, string, number array or Buffer, length of bytes starting at current byte (repeats when longer, default 1 byte for byte value, string length or end of data for string, array length or end of data for array or Buffer), byte position to end (default end of data), move byte position to after operation (default false) Add value to data (per byte)
Name not(startOffset, endOffset, consume) Byte position to start (default current position), byte position to end (default end of data), move byte position to after operation (default false) NOT data (per byte)
Name notThis(length, consume) Length of bytes starting at current byte position (default 1), byte position to end (default end of data), move byte position to after operation (default false) NOT data (per byte)
Name lShift(shiftKey, startOffset, endOffset, consume) Byte value, string, number array or Buffer, byte position to start (default current position), byte position to end (default end of data), move byte position to after operation (default false) Left shift data (per byte). Note: Will loop if operation length is longer than supplied key.
Name lShiftThis(shiftKey, length, consume) Byte value, string, number array or Buffer, length of bytes starting at current byte (repeats when longer, default 1 byte for byte value, string length or end of data for string, array length or end of data for array or Buffer), byte position to end (default end of data), move byte position to after operation (default false) Left shift data (per byte)
Name rShift(shiftKey, startOffset, endOffset, consume) Byte value, string, number array or Buffer, byte position to start (default current position), byte position to end (default end of data), move byte position to after operation (default false) Right shift data (per byte). Note: Will loop if operation length is longer than supplied key.
Name rShiftThis(shiftKey, length, consume) Byte value, string, number array or Buffer, length of bytes starting at current byte (repeats when longer, default 1 byte for byte value, string length or end of data for string, array length or end of data for array or Buffer), byte position to end (default end of data), move byte position to after operation (default false) Right shift data (per byte)

Bit field

Parse value as a bit field. There are 32 functions from bit1 to bit32 and can be signed or unsigned (with a u at the start) and in little or big endian order (be or le at the end).

Note: Remaining bits are dropped when returning to a byte read. Example, after using bit4() then ubyte(), the read locations drops the remaining 4 bits after bit4() when reading ubyte(). Any bit reading under 8 will always be unsigned.

Functions Params (bold requires)
Name
(master)
readBit(bits, unsigned, endian) number of bits, if the value is returned unsigned, big or little endian
writeBit(value, bits, unsigned, endian) value to write, number of bits, if the value is written unsigned, big or little endian
Note: Will throw error if value is outside of size of data
Presets (reader) [u]bit{1-32}{le|be}(*unsigned) If value is signed or not.
*Note: functions without the starting letter u can still be called unsigned when true is the first augment
Presets (writer) [u]bit{1-32}{le|be}(value, *unsigned) value to write, if value is unsigned or not.
*Note: functions without the starting letter u can still be called unsigned when true is the fourth augment

Byte

Parse value as a byte (aka int8). Can be signed or unsigned (with a u at the start).

Functions Params (bold requires)
Name
(master)
readByte(unsigned) if the value is returned unsigned
writeByte(value, offsetBytes, unsigned) value to write, byte offset from current position, if the value is written unsigned
Note: Will throw error if value is outside of size of data
Presets (reader) [u]{byte|int8}(*unsigned) If value is signed or not.
*Note: functions without the starting letter u can still be called unsigned when true is the first augment
Presets (writer) [u]{byte|int8}(value, *unsigned) value to write, if value is signed or not.
*Note: functions without the starting letter u can still be called unsigned when true is the third augment

Short

Parse value as a int16 (aka short or word). Can be signed or unsigned (with a u at the start) and in little or big endian order (be or le at the end).

Functions Params (bold requires)
Name
(master)
readInt16(unsigned, endian) if the value is returned unsigned, big or little endian
writeInt16(value, unsigned, endian) value to write, if the value is written unsigned, big or little endian
Note: Will throw error if value is outside of size of data
Presets (reader) [u]{int16|word|short}{be|le}(*unsigned, *endian) If value is unsigned, big or little endian.
*Note: functions without the starting letter u can still be called unsigned when true is the first augment, and functions without ending letters be or le can still be called the endian in the second augment (does not overwite set endian).
Presets (writer) [u]{int16|word|short}{be|le}(value, *unsigned, *endian) value to write, if value is unsigned, big or little endian.
*Note: functions without the starting letter u can still be called unsigned when true is the third augment, and functions without ending letters be or le can still be called the endian in the fourth augment (does not overwite set endian).

Half Float

Parse value as a half float (aka half). Can be in little or big endian order (be or le at the end).

Functions Params (bold requires)
Name
(master)
readHalfFloat(endian) big or little endian
writeHalfFloat(value, endian) value to write, big or little endian
Note: Will throw error if value is outside of size of data
Presets (reader) {halffloat|half}{be|le}(*endian) Big or little endian.
*Note: functions without ending letters be or le can still be called the endian in the first augment (does not overwite set endian).
Presets (writer) {halffloat|half}{be|le}(value, *endian) value to write, big or little endian.
*Note: and functions without ending letters be or le can still be called the endian in the second augment (does not overwite set endian).

Integer

Parse value as a int32 (aka int, long or double). Can be signed or unsigned (with a u at the start) and in little or big endian order (be or le at the end).

Functions Params (bold requires)
Name
(master)
readInt32(unsigned, endian) if the value is returned unsigned, big or little endian
writeInt32(value, unsigned, endian) value to write, if the value is written unsigned, big or little endian
Note: Will throw error if value is outside of size of data
Presets (reader) [u]{int32|long|int|double}{be|le}(*unsigned, *endian) If value is unsigned, little or big endian.
*Note: functions without the starting letter u can still be called unsigned when true is the first augment, and functions without ending letters be or le can still be called the endian in the second augment (does not overwite set endian).
Presets (writer) [u]{int32|long|int|double}{be|le}(value, *unsigned, *endian) value to write, if value is unsigned, little or big endian
*Note: functions without the starting letter u can still be called unsigned when true is the third augment, and functions without ending letters be or le can still be called the endian in the fourth augment (does not overwite set endian).

Float

Parse value as a float. Can be in little or big endian order (be or le at the end).

Functions Params (bold requires)
Name
(master)
readFloat(endian) big or little endian
writeInt64(value, endian) value to write, big or little endian
Note: Will throw error if value is outside of size of data
Presets (reader) float{be|le}(*endian) Big or little endian.
*Note: functions without ending letters be or le can still be called the endian in the first augment (does not overwite set endian).
Presets (writer) float{be|le}(value, *endian) value to write, big or little endian.
*Note: functions without ending letters be or le can still be called the endian in the third augment (does not overwite set endian).

Quadword

Parse value as a int64 (aka quad or bigint). Can be signed or unsigned (with a u at the start) and in little or big endian order (be or le at the end).

Functions Params (bold requires)
Name
(master)
readInt64(unsigned, endian) if the value is returned unsigned, big or little endian
writeInt64(value, unsigned, endian) value to write, if the value is written unsigned, big or little endian
Note: Will throw error if value is outside of size of data
Presets (reader) [u]{int64|quad|bigint}{be|le}(*unsigned, *endian) If value is unsigned, if value is unsigned, big or little endian.
*Note: functions without the starting letter u can still be called unsigned when true is the first augment, and functions without ending letters be or le can still be called the endian in the second augment (does not overwite set endian).
Presets (writer) [u]{int64|quad|bigint}{be|le}(value, *unsigned, *endian) value to write, if value is unsigned, big or little endian.
*Note: functions without the starting letter u can still be called unsigned when true is the third augment, and functions without ending letters be or le can still be called the endian in the fourth augment (does not overwite set endian).

Double Float

Parse value as a double float (aka dfloat). Can be in little or big endian order (be or le at the end).

Functions Params (bold requires)
Name
(master)
readDoubleFloat(endian) big or little endian
writeDoubleFloat(value, endian) value to write, big or little endian.
Note: Will throw error if value is outside of size of data
Presets (reader) {doublefloat|dfloat}{be|le}(*endian) Big or little endian.
*Note: functions without ending letters be or le can still be called the endian in the first augment (does not overwite set endian).
Presets (writer) {doublefloat|dfloat}{be|le}(value, *endian) Value to write, big or little endian.
*Note: functions without ending letters be or le can still be called the endian in the third augment (does not overwite set endian).

Strings

Parse a string in any format. Be sure to use options object for formatting unless using a preset. Strings with larger than 1 byte character reads can use be or le at the end for little or big endian.

Presents include C or Unicode, Ansi and multiple pascals.

Functions Params (bold requires)
Name
(master)
readString({
length,
stringType,
terminateValue,
lengthReadSize,
stripNull,
encoding,
endian
})
  • Length for non-fixed UTF strings. If not supplied, reads until 0 or supplied terminate value (for fixed length UTF strings only)
  • String type. Defaults to utf-8 (utf-8, utf-16, pascal, wide-pascal accepted)
  • Terminate value. Default is 0x00 (for non-fixed length utf strings)
  • Size of the first value that defines the length of the string. Defaults to 1 as uint8, respects supplied endian (for Pascal strings only, accepts 1, 2 or 4 bytes)
  • Removes 0x00 characters on read (default true)
  • Encoding. Defaults to utf-8 on utf-8 or pascal and utf-16 on utf-16 or wide-pascal (accepts all TextDecoder options)
  • Endian (for wide-pascal and utf-16 character order, does not overwite set endian)
writeString(string, {
length,
stringType,
terminateValue,
lengthReadSize,
encoding,
endian
})
  • String to write
  • Length for non-fixed UTF strings. If not supplied, defaults to encoded string length (for fixed length UTF strings only, will trucate if supplied value is smaller than string length)
  • String type. Defaults to utf-8 (utf-8, utf-16, pascal, wide-pascal accepted)
  • Terminate value. Default is 0x00 (for non-fixed length utf strings)
  • Size of the first value that defines the length of the string. Defaults to 1 as uint8, respects supplied endian (for Pascal strings only, accepts 1, 2 or 4 bytes)
  • Encoding. Defaults to utf-8 on utf-8 or pascal and utf-16 on utf-16 or wide-pascal (accepts all TextDecoder options)
  • Endian (for wide-pascal and utf-16 character order, does not overwite set endian)
Presets (reader) {c|utf8}string(length, terminateValue, stripNull)

ansistring(length, terminateValue, stripNull)

{utf16|uni}string(length, terminateValue, stripNull, *endian)

pstring(lengthReadSize, stripNull, *endian)

pstring{1|2|4}{be|le}(stripNull, *endian)

wpstring{be|le}(lengthReadSize, stripNull, *endian)

wpstring{1|2|4}{be|le}(stripNull, *endian)
Based on above.
Note: Presets use augments not a single object. Endian only needed when not part of function name. Does not override set endian.
Presets (writer) {c|utf8}string(string, length, terminateValue)

ansistring(string, length, terminateValue)

{utf16|uni}string{be|le}(string,length, terminateValue, *endian)

pstring(string, lengthWriteSize, *endian)

pstring{1|2|4}{be|le}(string, *endian)

wpstring{be|le}(string, lengthWriteSize, *endian)

wpstring{1|2|4}{be|le}(string, *endian)
Based on above.
Note: Presets use augments not a single object. Endian only needed when not part of function name. Does not override set endian.

Acknowledgements

This project was born from the desire to have a single library that could both read and write in binary with common named functions. Having been using tools like Binary-parser, QuickBMS and 010 Editor in the past, I wanted something I could translate quickly to a Node app and then use in a web site without having to redo work.

I'm happy to connect and grow this library if others find it useful. Pull requests or bug reports are welcome!

License

MIT

Package Sidebar

Install

npm i bireader

Weekly Downloads

1

Version

1.0.56

License

MIT

Unpacked Size

1.64 MB

Total Files

11

Last publish

Collaborators

  • hearhellacopters