1.0.2-release • Published 7 years ago

php-pack v1.0.2-release

Weekly downloads
28
License
-
Repository
github
Last release
7 years ago

PHP-Pack

This is a copy of PHP's pack/unpack function made into a nodejs module. Its a 100% clone with an additional options.

  • A new feature where we can populate an existing Buffer is now there, so the caller can worry about Buffer management. Simply make the first parameter a buffer

  • Also we can supply an array of values, rather than arguments, so the 2nd parameter is an array. ie. pack( "VV", 23,55 );

  • Added a Perl style format that was missing, which is format character "V" and "v"

Install

npm install php-pack

Packing Bytes Test

To test packing bytes, run:

node test/test.js
node test/speedtest.js

To use in code:

var pack = require('php-pack').pack,
  	unpack = require('php-pack').unpack;
  
var buffer = pack( "CCCV", [ 1,2,3, 12345679 ] );

Original Pack Documentation

pack - Pack data into binary Buffer object

Description

Buffer object = pack ( DestBuffer , string format [, mixed $args , mixed n... ] ) Pack given arguments into a binary Buffer according to format.

The idea for this function was taken from Perl and all formatting codes work the same as in Perl. However, there are some formatting codes that are missing such as Perl's "u" format code.

Note that the distinction between signed and unsigned values only affects the function unpack(), where as function pack() gives the same result for signed and unsigned format codes.

Parameters

format

The format string consists of format codes followed by an optional repeater argument. The repeater argument can be either an integer value or * for repeating to the end of the input data. For a, A, h, H the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are consumed and packed into the resulting binary string.

Currently implemented formats are:

pack() format characters

CodeDescription
aNUL-padded string
ASPACE-padded string
hHex string, low nibble first
HHex string, high nibble first
csigned char
Cunsigned char
ssigned short (always 16 bit, machine byte order)
Sunsigned short (always 16 bit, machine byte order)
nunsigned short (always 16 bit, big endian byte order)
vunsigned short (always 16 bit, little endian byte order)
isigned integer (machine dependent size and byte order)
Iunsigned integer (machine dependent size and byte order)
lsigned long (always 32 bit, machine byte order)
Lunsigned long (always 32 bit, machine byte order)
Nunsigned long (always 32 bit, big endian byte order)
Vunsigned long (always 32 bit, little endian byte order)
ffloat (machine dependent size and representation)
ddouble (machine dependent size and representation)
xNUL byte
XBack up one byte
ZNUL-padded string (new in PHP 5.5)
@NUL-fill to absolute position

Return Values

Returns a Buffer containing data.

unpack

unpack - Unpack data from a Buffer

Description

Object unpack ( String format , Buffer data, Boolean PerlFormat ) Unpacks from a binary string into an array according to the given format.

The unpacked data is stored in an associative array. To accomplish this you have to name the different format codes and separate them by a slash /. If a repeater argument is present, then each of the array keys will have a sequence number behind the given name.

Example: "c2chars/nints" will return an object with fields Object = { chars1: 0, chars2: 0, ints: 0 }.

Parameters

format See pack() for an explanation of the format codes.

data The packed data.

PerlFormat If this parameter exists and is true, we will ignore the PHP style names, and use just the variables index

Example: "c2n" will return an object with fields Object = { 1: 0, 2: 0, 3: 0 }

Return Values

Returns an associative array containing unpacked elements of binary string.

Note

The Buffer prototype has some very useful function like buf.writeUInt32BE.
So , if you know the detail of pack, you can use these functions to complete your work by node.

This C++ module I find it in raul's homepage.But it can't compile successfully, so I fixed it.However, it is not very useful,unless you have to pack a very big data.

1.0.2-release

7 years ago

1.0.1-release

7 years ago

1.0.0-release

7 years ago

1.0.0-beta

7 years ago

1.0.0-beat

7 years ago

1.0.5

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago