Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return module #3

Merged
merged 5 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions Makefile

This file was deleted.

36 changes: 19 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
This library is extended from `lpack` library on Luarocks to support Hexdecimal data too.

This is a simple Lua library for packing and unpacking binary data.
The library adds two functions to the string library: pack and unpack.
The library provides two functions: `pack` and `unpack`.

pack is called as follows: pack(F,x1,x2,...), where F is a string describing
how the values x1, x2, ... are to be interpreted and formatted. Each letter
in the format string F consumes one of the given values. Only values of type
number or string are accepted. pack returns a (binary) string containing the
values packed as described in F. The letter codes understood by pack are listed
`pack` is called as follows: `pack(F,x1,x2,...)`, where `F` is a string describing
how the values `x1`, `x2`, ... are to be interpreted and formatted. Each letter
in the format string `F` consumes one of the given values. Only values of type
number or string are accepted. `pack` returns a (binary) string containing the
values packed as described in `F`. The letter codes understood by pack are listed
in lpack.c (they are inspired by Perl's codes but are not the same). Numbers
following letter codes in F indicate repetitions.
following letter codes in `F` indicate repetitions.

unpack is called as follows: unpack(s,F,[init]), where s is a (binary) string
containing data packed as if by pack, F is a format string describing what is
to be read from s, and the optional init marks where in s to begin reading the
values. unpack returns one value per letter in F until F or s is exhausted
(the letters codes are the same as for pack, except that numbers following `A'
`unpack` is called as follows: `unpack(s,F,[init])`, where `s` is a (binary) string
containing data packed as if by `pack`, `F` is a format string describing what is
to be read from `s`, and the optional `init` marks where in `s` to begin reading the
values. `unpack` returns one value per letter in `F` until `F` or `s` is exhausted
(the letters codes are the same as for `pack`, except that numbers following ``A'`
are interpreted as the number of characters to read into the string, not as
repetitions).

The first value returned by unpack is the next unread position in s, which can
be used as the init position in a subsequent call to unpack. This allows you to
unpack values in a loop or in several steps. If the position returned by unpack
is beyond the end of s, then s has been exhausted; any calls to unpack starting
beyond the end of s will always return nil values.
The first value returned by `unpack` is the next unread position in `s`, which can
be used as the init position in a subsequent call to `unpack`. This allows you to
unpack values in a loop or in several steps. If the position returned by `unpack`
is beyond the end of `s`, then `s` has been exhausted; any calls to `unpack` starting
beyond the end of `s` will always return `nil` values.

-------------------------------------------------------------------------------

```
pack library:
pack(f,...) unpack(s,f,[init])
```

-------------------------------------------------------------------------------
4 changes: 2 additions & 2 deletions lua_pack-1.0.5-0.rockspec → lua_pack-2.0.0-0.rockspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package="lua_pack"
version="1.0.5-0"
version="2.0.0-0"
source = {
url = "git://github.com/mashape/lua-pack",
tag = "1.0.5"
tag = "2.0.0"
}
description = {
summary = "This is a simple Lua library for packing and unpacking binary data",
Expand Down
16 changes: 9 additions & 7 deletions lua_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,13 @@ static const luaL_Reg R[] =

int luaopen_lua_pack(lua_State *L)
{
#ifdef USE_GLOBALS
lua_register(L,"bpack",l_pack);
lua_register(L,"bunpack",l_unpack);
#else
luaL_openlib(L, LUA_STRLIBNAME, R, 0);
#endif
return 0;
lua_newtable(L);

lua_pushcfunction(L, &l_pack);
lua_setfield(L, -2, "pack");

lua_pushcfunction(L, &l_unpack);
lua_setfield(L, -2, "unpack");

return 1;
}
10 changes: 5 additions & 5 deletions test.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
require"pack"
local pack = require"lua_pack"

bpack=string.pack
bunpack=string.unpack
local bpack = pack.pack
local bunpack = pack.unpack

function hex(s)
s=string.gsub(s,"(.)",function (x) return string.format("%02X",string.byte(x)) end)
return s
end

a=bpack("Ab8","\027Lua",5*16+1,0,1,4,4,4,8,0)
a=bpack("AC8","\027Lua",5*16+1,0,1,4,4,4,8,0)
print(hex(a),string.len(a))

b=string.dump(hex)
b=string.sub(b,1,string.len(a))
print(a==b,string.len(b))
print(bunpack(b,"bA3b8"))
print(bunpack(b,"CA3C8"))

i=314159265 f="<I>I=I"
a=bpack(f,i,i,i)
Expand Down