• Stars
    star
    342
  • Rank 113,225 (Top 3 %)
  • Language
    C
  • Created almost 12 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A self contained Lua MessagePack C implementation.

README for lua-cmsgpack.c

Lua-cmsgpack is a MessagePack implementation and bindings for Lua 5.1/5.2/5.3 in a self contained C file without external dependencies.

This library is open source software licensed under the BSD two-clause license.

INSTALLATION

Using LuaRocks (http://luarocks.org):

  • Install current stable release:

    sudo luarocks install lua-cmsgpack

  • Install current Git master head from GitHub:

    sudo luarocks install lua-cmsgpack --from=rocks-cvs

  • Install from current working copy

    cd lua-cmsgpack/ sudo luarocks make rockspec/lua-cmsgpack-scm-1.rockspec

If you embed Lua and all modules into your C project, just add the lua_cmsgpack.c file and call the following function after creating the Lua interpreter:

luaopen_cmsgpack(L);

USAGE

The exported API is very simple, consisting of four functions:

Basic API:

msgpack = cmsgpack.pack(lua_object1, lua_object2, ..., lua_objectN)
lua_object1, lua_object2, ..., lua_objectN = cmsgpack.unpack(msgpack)

Detailed API giving you more control over unpacking multiple values:

resume_offset, lua_object1 = cmsgpack.unpack_one(msgpack)
resume_offset1, lua_object2 = cmsgpack.unpack_one(msgpack, resume_offset)
...
-1, lua_objectN = cmsgpack.unpack_one(msgpack, resume_offset_previous)

resume_offset, lua_object1, lua_object2 = cmsgpack.unpack_limit(msgpack, 2)
resume_offset2, lua_object3 = cmsgpack.unpack_limit(msgpack, 1, resume_offset1)

Functions:

  • pack(arg1, arg2, ..., argn) - pack any number of lua objects into one msgpack stream. returns: msgpack
  • unpack(msgpack) - unpack all objects in msgpack to individual return values. returns: object1, object2, ..., objectN
  • unpack_one(msgpack); unpack_one(msgpack, offset) - unpacks the first object after offset. returns: offset, object
  • unpack_limit(msgpack, limit); unpack_limit(msgpack, limit, offset) - unpacks the first limit objects and returns: offset, object1, objet2, ..., objectN (up to limit, but may return fewer than limit if not that many objects remain to be unpacked)

When you reach the end of your input stream with unpack_one or unpack_limit, an offset of -1 is returned.

You may require "msgpack" or you may require "msgpack.safe". The safe version returns errors as (nil, errstring).

However because of the nature of Lua numerical and table type a few behavior of the library must be well understood to avoid problems:

  • A table is converted into a MessagePack array type only if all the keys are composed of incrementing integers starting at 1 end ending at N, without holes, without additional non numerical keys. All the other tables are converted into maps.
  • An empty table is always converted into a MessagePack array, the rationale is that empty lists are much more common than empty maps (usually used to represent objects with fields).
  • A Lua number is converted into an integer type if floor(number) == number, otherwise it is converted into the MessagePack float or double value.
  • When a Lua number is converted to float or double, the former is preferred if there is no loss of precision compared to the double representation.
  • When a MessagePack big integer (64 bit) is converted to a Lua number it is possible that the resulting number will not represent the original number but just an approximation. This is unavoidable because the Lua numerical type is usually a double precision floating point type.

TESTING

Build and test:

mkdir build; cd build
cmake ..
make
lua ../test.lua

You can build a 32-bit module on a 64-bit platform with:

mkdir build; cd build
cmake -DBuild32Bit=ON ..
make
lua ../test.lua

NESTED TABLES

Nested tables are handled correctly up to LUACMSGPACK_MAX_NESTING levels of nesting (that is set to 16 by default). Every table that is nested at a greater level than the maxium is encoded as MessagePack nil value.

It is worth to note that in Lua it is possible to create tables that mutually refer to each other, creating a cycle. For example:

a = {x=nil,y=5}
b = {x=a}
a['x'] = b

This condition will simply make the encoder reach the max level of nesting, thus avoiding an infinite loop.

CREDITS

This library was written by Salvatore Sanfilippo for Redis, but is maintained as a separated project by the author.

Some of the test vectors in "test.lua" are obtained from the Javascript MessagePack-JS library.

More Repositories

1

disque

Disque is a distributed message broker
C
7,954
star
2

kilo

A text editor in less than 1000 LOC with syntax highlight and search.
C
6,638
star
3

sds

Simple Dynamic Strings library for C
C
4,601
star
4

linenoise

A small self-contained alternative to readline and libedit
C
3,348
star
5

neural-redis

Neural networks module for Redis
C
2,214
star
6

dump1090

Dump1090 is a simple Mode S decoder for RTLSDR devices
C
2,210
star
7

lamernews

Lamer News -- an HN style social news site written in Ruby/Sinatra/Redis/JQuery
Ruby
1,357
star
8

hping

hping network tool
C
1,314
star
9

smaz

Small strings compression library
C
1,096
star
10

rax

A radix tree implementation in ANSI C
C
957
star
11

load81

SDL based Lua programming environment for kids similar to Codea
C
577
star
12

disque-module

Disque ported as Redis module
C
477
star
13

shapeme

Evolve images using simulated annealing
C
381
star
14

protoview

Flipper Zero app to display known and unknown signals
C
379
star
15

retwis

A Twitter-toy clone written in PHP and Redis, used in the early days to introduce Redis data types.
PHP
355
star
16

aocla

A small stack based, written to bring Advent of Code 2022 Day 13 puzzle to the extreme consequences
C
324
star
17

otree

a simple btree implementation with automatic space reclaiming
C
268
star
18

redis-sampler

Small program to understand the composition of your Redis data set
Ruby
260
star
19

freakwan

A MicroPython driver for the SX1276 LoRa chip
C++
245
star
20

redis-rb-cluster

Redis Cluster Ruby client based on redis-rb
Ruby
245
star
21

RESP3

RESP protocol V3 repository. Contains the specification, and other related resource
219
star
22

stonky

Stock market Telegram bot
C
209
star
23

redlock-rb

Redlock Redis-based distributed locks implementation in Ruby
Ruby
196
star
24

lloogg

LLOOGG realtime web log analyzer
PHP
195
star
25

redis-timeseries

Ruby library for Redis backed time series.
Ruby
195
star
26

redis-tools

Abandoned project "Redis tools". What was relevant is now part of redis-cli & redis-benchmark
C
191
star
27

pngtostl

Turn PNG images into STL 3D models that will "develop" in front of a light source
C
157
star
28

iconping

Icon Ping - visual ping to 4.2.2.2
Objective-C
129
star
29

jsrt

Javascript ray tracing engine
118
star
30

redimension

Redis multi-dimensional query library
Ruby
98
star
31

adventofcode2022

A few Advent of Code puzzles (2022 edition) in C
C
91
star
32

gopher2redis

A Ruby script that translates a directory structure and its files into the Redis keys to be served via Redis Gopher protocol
Ruby
88
star
33

mc-benchmark

Memcache port of Redis benchmark
C
85
star
34

listpack

A serialization format and implementation for backward-traversable lists of strings.
C
79
star
35

Bigdis

Bigdis - a file-based KV store speaking the Redis protocol
Tcl
76
star
36

visitors

Visitors fast web log analyzer
C
76
star
37

book-examples

Redis The Definitive Guide book code examples
Ruby
51
star
38

Gitan

Gitan is a very basic web interface to create and inspect bare git repositories
Ruby
45
star
39

flipper-asteroids

Asteroids for Flipper Zero
C
44
star
40

aspark

ASCII sparklines for the Enterprise
C
41
star
41

nolate

NO LAme TEmplate System for Ruby
Ruby
40
star
42

Jim

Jim is a small footprint Tcl interpreter, with some changes to the original language but mostly compatible.
40
star
43

tclircd

An IRC server I wrote in 2004 for fun, using the Tcl language.
Tcl
39
star
44

recidiv

minimal continuous integration framework written in Tcl (used for Redis CI)
Tcl
38
star
45

partitions

Partitions.tcl is a Tcl program to simulate partitions between physical hosts
Tcl
36
star
46

connect4-montecarlo

Simple connect 4 AI using Monte Carlo method
C
31
star
47

iqmeteo

Meteo widget for the Garmin Vivoactive HR powered by Yahoo Weather API
Shell
21
star
48

siphash

A modification of SipHash reference implementation to make it more practical for Redis usage
C
20
star
49

talk32

C program to talk via serial to MicroPython powered ESP32 boards
C
19
star
50

redisdotphp

Legacy Redis PHP client lib. A best-effort support repository.
18
star
51

yaku-ns

a DNS server I wrote 10 years ago. Here for historical reasons
C
18
star
52

strabo

Turns HGT elevation maps into 2D images or 3D models
C
18
star
53

nn-2003

2003 Neural Networks experiments -- when it was not mainstream ;-)
C
16
star
54

hiredis

WARNING: hiredis repository moved to http://github.com/redis/hiredis. Just my private fork.
13
star
55

crack-checksum

Find checksum (crc8, xor, add) parameters in a set of messages.
C
11
star
56

dict-scan-fuzz-tester

Fuzz testing for the SCAN underlying algorithm
C
11
star
57

Siboom

A simple markup system for writing books drafts
Ruby
10
star
58

sbignum

Old code about C library for big numbers plus Tcl bindings
C
9
star
59

redis-cp-rewrite-sim

Redis/Raft snapshotting rewriting simulation
C
8
star
60

codakido

Redirects to Load81 project
8
star
61

cache-mem-tester

Test memory efficiency of Redis / Memcached against values with a given size distribution.
Ruby
7
star
62

rascan

A prototype for a multi processes port scanner I wrote in 1998. Here only to archive it for myself.
C
6
star
63

gif-pure-tcl

Pure TCL GIF generator
Tcl
4
star
64

botlib

C Telegram bot framework
C
2
star
65

failed-3d-prints-bot

A Telegram bot that detects failed prints and send you an image of your printer
C
1
star