Skip to content

Latest commit

History

29 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Buffer PHP

Buffer for PHP strings (ByteArray)

Prerequisites

  • PHP 8.0+
  • GMP extension enabled

Installation

composer require comely-io/buffer-php

Buffer

Constructors

MethodDescription
fromBase16Initialize buffer using Base16/Hexadecimal encoded string
fromBase64Initialize buffer from Base64 encoded string
fromByteArrayInitialize buffer from byte array
fromBinaryInitialize buffer from array of binary encoded bytes
__constructCreates a new buffer instance from given argument

Fixed-length Buffers

Methods

MethodDescription
lenGet length/size of buffer in bytes
rawGet existing bytes in buffer as string
byteArrayReturns an indexed Array comprised of every byte's position on ASCII table
copyCreates a copy of buffer from given start and/or up to length
equalsCompares buffer bytes with another buffer or string
readCreates a ByteReader instance
hashCreates a ByteDigest instance
switchEndianessConverts endianess of entire buffer between big and little endians
dumpBuffer information as an Array
applyFnApplies a function/callback to existing buffer creating a new buffer instance as result
toBase16Encodes the buffer as Base16/Hexadecimal
toBase64Encodes the buffer in Base64
toBinaryEncodes the buffer into an indexed Array where each index is binary representation of byte (1s and 0s)
checkSizeChecks if buffer is comprised of exactly same number of bytes as in argument, throws LengthException otherwise

Writeable Buffers

All the methods from fixed-length buffers above, and also:

Methods

MethodDescription
cleanFlush any existing data in buffer
readOnlyPuts buffer in Read-only state; Nothing further can be appended to it
writablePuts buffer in writable state
isWritableReturn boolean
appendAppend bytes to end of buffer
prependPrepend bytes to start of buffer
prependUInt8Prepends a single byte integer
appendUInt8Appends a single byte integer
appendUInt16LEAppends 2 byte integer in little endian byte order
appendUInt16BEAppends 2 byte integer in big endian byte order
appendUInt32LEAppends 4 byte integer in little endian byte order
appendUInt32BEAppends 4 byte integer in big endian byte order
appendUInt64LEAppends 8 byte integer in little endian byte order
appendUInt64BEAppends 8 byte integer in big endian byte order

ByteReader

For systematically reading bytes from buffer in order. Useful for serialization and un-serialization of data (i.e. Bitcoin blocks and transactions)

Methods

MethodDescription
ignoreUnderflowIf invoked, ByteReaderUnderflowException will not be thrown if required number of bytes are not present
isEndreturn boolean if end of buffer has been reached
lenLength of buffer in bytes
posCurrent position/index at byte in buffer
resetResets position index to 0
firstResets position index to 0, then reads N number of bytes from start of buffer
lookBehindReads last N bytes previously read (does NOT update internal pointer)
lookAheadReads next N bytes but does NOT updates internal pointer
nextReads next N bytes while updating the pointer
readUInt8Reads and converts next 1 byte as UInt8
readUInt16LEReads and converts next 2 bytes as UInt16 from little endian byte order
readUInt16BEReads and converts next 2 bytes as UInt16 from big endian byte order
readUInt32LEReads and converts next 4 bytes as UInt32 from little endian byte order
readUInt32BEReads and converts next 4 bytes as UInt32 from big endian byte order
readUInt64LEReads and converts next 8 bytes as UInt64 from little endian byte order
readUInt64BEReads and converts next 8 bytes as UInt64 from big endian byte order
setPointerSets internal reading pointer to specified byte
remainingRetrieved all remaining bytes in buffer (does NOT update internal pointer)

ByteDigest

Applies hash function to bytes in buffer and returns digest bytes.

Check PHP.net hash_algos() and hash_hmac_algos() for list of available/supported algorithms.

Methods

MethodDescription
toStringIf invoked before any other method, all hash functions will return string instead of new buffer instance
hashApplies specified hash algorithm times N iterations and returns X numbers of bytes from digest
hmacApplies HMAC hash function
pbkdf2Applies PBKDF2 hash function
md5Applies md5 hash function
sha1Applies SHA1 hash function
sha256Applies SHA256 hash function
sha512Applies SHA512 hash function
ripeMd160Applies ripeMd160 hash function

About

Buffer for PHP byte arrays

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages