Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lua_struct.c File Reference
#include <ctype.h>
#include <limits.h>
#include <stddef.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"

Go to the source code of this file.

Data Structures

struct  cD
 
struct  Header
 

Macros

#define STRUCT_INT   long
 
#define MAXINTSIZE   32
 
#define isp2(x)   ((x) > 0 && ((x) & ((x) - 1)) == 0)
 
#define PADDING   (sizeof(struct cD) - sizeof(double))
 
#define MAXALIGN   (PADDING > sizeof(int) ? PADDING : sizeof(int))
 
#define BIG   0
 
#define LITTLE   1
 
#define defaultoptions(h)   ((h)->endian = native.endian, (h)->align = 1)
 

Typedefs

typedef STRUCT_INT Inttype
 
typedef unsigned STRUCT_INT Uinttype
 
typedef struct Header Header
 

Functions

static int getnum (const char **fmt, int df)
 
static size_t optsize (lua_State *L, char opt, const char **fmt)
 
static int gettoalign (size_t len, Header *h, int opt, size_t size)
 
static void controloptions (lua_State *L, int opt, const char **fmt, Header *h)
 
static void putinteger (lua_State *L, luaL_Buffer *b, int arg, int endian, int size)
 
static void correctbytes (char *b, int size, int endian)
 
static int b_pack (lua_State *L)
 
static lua_Number getinteger (const char *buff, int endian, int issigned, int size)
 
static int b_unpack (lua_State *L)
 
static int b_size (lua_State *L)
 
LUALIB_API int luaopen_struct (lua_State *L)
 

Variables

union { 
 
   int   dummy 
 
   char   endian 
 
native = {1} 
 
static const struct luaL_Reg thislib []
 

Macro Definition Documentation

◆ BIG

#define BIG   0

Definition at line 75 of file lua_struct.c.

Referenced by controloptions(), and getinteger().

◆ defaultoptions

#define defaultoptions ( h)    ((h)->endian = native.endian, (h)->align = 1)

Definition at line 104 of file lua_struct.c.

Referenced by b_pack(), b_size(), and b_unpack().

◆ isp2

#define isp2 ( x)    ((x) > 0 && ((x) & ((x) - 1)) == 0)

Definition at line 61 of file lua_struct.c.

Referenced by controloptions().

◆ LITTLE

#define LITTLE   1

Definition at line 76 of file lua_struct.c.

Referenced by controloptions(), and putinteger().

◆ MAXALIGN

#define MAXALIGN   (PADDING > sizeof(int) ? PADDING : sizeof(int))

Definition at line 71 of file lua_struct.c.

Referenced by controloptions().

◆ MAXINTSIZE

#define MAXINTSIZE   32

Definition at line 58 of file lua_struct.c.

Referenced by optsize(), and putinteger().

◆ PADDING

#define PADDING   (sizeof(struct cD) - sizeof(double))

Definition at line 70 of file lua_struct.c.

◆ STRUCT_INT

#define STRUCT_INT   long

Definition at line 48 of file lua_struct.c.

Typedef Documentation

◆ Header

typedef struct Header Header

◆ Inttype

Definition at line 51 of file lua_struct.c.

◆ Uinttype

typedef unsigned STRUCT_INT Uinttype

Definition at line 54 of file lua_struct.c.

Function Documentation

◆ b_pack()

static int b_pack ( lua_State * L)
static

Definition at line 205 of file lua_struct.c.

205 {
206 luaL_Buffer b;
207 const char *fmt = luaL_checkstring(L, 1);
208 Header h;
209 int arg = 2;
210 size_t totalsize = 0;
211 defaultoptions(&h);
212 lua_pushnil(L); /* mark to separate arguments from string buffer */
213 luaL_buffinit(L, &b);
214 while (*fmt != '\0') {
215 int opt = *fmt++;
216 size_t size = optsize(L, opt, &fmt);
217 int toalign = gettoalign(totalsize, &h, opt, size);
218 totalsize += toalign;
219 while (toalign-- > 0) luaL_addchar(&b, '\0');
220 switch (opt) {
221 case 'b': case 'B': case 'h': case 'H':
222 case 'l': case 'L': case 'T': case 'i': case 'I': { /* integer types */
223 putinteger(L, &b, arg++, h.endian, size);
224 break;
225 }
226 case 'x': {
227 luaL_addchar(&b, '\0');
228 break;
229 }
230 case 'f': {
231 float f = (float)luaL_checknumber(L, arg++);
232 correctbytes((char *)&f, size, h.endian);
233 luaL_addlstring(&b, (char *)&f, size);
234 break;
235 }
236 case 'd': {
237 double d = luaL_checknumber(L, arg++);
238 correctbytes((char *)&d, size, h.endian);
239 luaL_addlstring(&b, (char *)&d, size);
240 break;
241 }
242 case 'c': case 's': {
243 size_t l;
244 const char *s = luaL_checklstring(L, arg++, &l);
245 if (size == 0) size = l;
246 luaL_argcheck(L, l >= (size_t)size, arg, "string too short");
247 luaL_addlstring(&b, s, size);
248 if (opt == 's') {
249 luaL_addchar(&b, '\0'); /* add zero at the end */
250 size++;
251 }
252 break;
253 }
254 default: controloptions(L, opt, &fmt, &h);
255 }
256 totalsize += size;
257 }
258 luaL_pushresult(&b);
259 return 1;
260}
CURL_EXTERN int void * arg
Definition curl.h:2622
LUA_API void lua_pushnil(lua_State *L)
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
LUALIB_API const char * luaL_checklstring(lua_State *L, int narg, size_t *len)
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
#define luaL_addchar(B, c)
#define luaL_checkstring(L, n)
#define luaL_argcheck(L, cond, numarg, extramsg)
static void controloptions(lua_State *L, int opt, const char **fmt, Header *h)
Definition lua_struct.c:145
static void putinteger(lua_State *L, luaL_Buffer *b, int arg, int endian, int size)
Definition lua_struct.c:166
#define defaultoptions(h)
Definition lua_struct.c:104
static size_t optsize(lua_State *L, char opt, const char **fmt)
Definition lua_struct.c:108
static int gettoalign(size_t len, Header *h, int opt, size_t size)
Definition lua_struct.c:134
static void correctbytes(char *b, int size, int endian)
Definition lua_struct.c:193
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318
int endian
Definition lua_struct.c:86

References arg, controloptions(), correctbytes(), defaultoptions, Header::endian, gettoalign(), lua_pushnil(), luaL_addchar, luaL_addlstring(), luaL_argcheck, luaL_buffinit(), luaL_checklstring(), luaL_checknumber(), luaL_checkstring, luaL_pushresult(), optsize(), putinteger(), and s.

◆ b_size()

static int b_size ( lua_State * L)
static

Definition at line 359 of file lua_struct.c.

359 {
360 Header h;
361 const char *fmt = luaL_checkstring(L, 1);
362 size_t pos = 0;
363 defaultoptions(&h);
364 while (*fmt) {
365 int opt = *fmt++;
366 size_t size = optsize(L, opt, &fmt);
367 pos += gettoalign(pos, &h, opt, size);
368 if (opt == 's')
369 luaL_argerror(L, 1, "option 's' has no fixed size");
370 else if (opt == 'c' && size == 0)
371 luaL_argerror(L, 1, "option 'c0' has no fixed size");
372 if (!isalnum(opt))
373 controloptions(L, opt, &fmt, &h);
374 pos += size;
375 }
376 lua_pushinteger(L, pos);
377 return 1;
378}
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)

References controloptions(), defaultoptions, gettoalign(), lua_pushinteger(), luaL_argerror(), luaL_checkstring, and optsize().

◆ b_unpack()

static int b_unpack ( lua_State * L)
static

Definition at line 290 of file lua_struct.c.

290 {
291 Header h;
292 const char *fmt = luaL_checkstring(L, 1);
293 size_t ld;
294 const char *data = luaL_checklstring(L, 2, &ld);
295 size_t pos = (size_t)luaL_optinteger(L, 3, 1) - 1;
296 int n = 0; /* number of results */
297 luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
298 defaultoptions(&h);
299 while (*fmt) {
300 int opt = *fmt++;
301 size_t size = optsize(L, opt, &fmt);
302 pos += gettoalign(pos, &h, opt, size);
303 luaL_argcheck(L, size <= ld - pos, 2, "data string too short");
304 /* stack space for item + next position */
305 luaL_checkstack(L, 2, "too many results");
306 switch (opt) {
307 case 'b': case 'B': case 'h': case 'H':
308 case 'l': case 'L': case 'T': case 'i': case 'I': { /* integer types */
309 int issigned = islower(opt);
310 lua_Number res = getinteger(data+pos, h.endian, issigned, size);
311 lua_pushnumber(L, res); n++;
312 break;
313 }
314 case 'x': {
315 break;
316 }
317 case 'f': {
318 float f;
319 memcpy(&f, data+pos, size);
320 correctbytes((char *)&f, sizeof(f), h.endian);
321 lua_pushnumber(L, f); n++;
322 break;
323 }
324 case 'd': {
325 double d;
326 memcpy(&d, data+pos, size);
327 correctbytes((char *)&d, sizeof(d), h.endian);
328 lua_pushnumber(L, d); n++;
329 break;
330 }
331 case 'c': {
332 if (size == 0) {
333 if (n == 0 || !lua_isnumber(L, -1))
334 luaL_error(L, "format 'c0' needs a previous size");
335 size = lua_tonumber(L, -1);
336 lua_pop(L, 1); n--;
337 luaL_argcheck(L, size <= ld - pos, 2, "data string too short");
338 }
339 lua_pushlstring(L, data+pos, size); n++;
340 break;
341 }
342 case 's': {
343 const char *e = (const char *)memchr(data+pos, '\0', ld - pos);
344 if (e == NULL)
345 luaL_error(L, "unfinished string in data");
346 size = (e - (data+pos)) + 1;
347 lua_pushlstring(L, data+pos, size - 1); n++;
348 break;
349 }
350 default: controloptions(L, opt, &fmt, &h);
351 }
352 pos += size;
353 }
354 lua_pushinteger(L, pos + 1); /* next position */
355 return n + 1;
356}
#define NULL
Definition gmacros.h:924
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
LUA_API int lua_isnumber(lua_State *L, int idx)
LUA_API void lua_pushlstring(lua_State *L, const char *s, size_t len)
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg, lua_Integer def)
#define lua_pop(L, n)
LUA_NUMBER lua_Number
#define lua_tonumber(L, i)
static lua_Number getinteger(const char *buff, int endian, int issigned, int size)
Definition lua_struct.c:263

References controloptions(), correctbytes(), defaultoptions, Header::endian, getinteger(), gettoalign(), lua_isnumber(), lua_pop, lua_pushinteger(), lua_pushlstring(), lua_pushnumber(), lua_tonumber, luaL_argcheck, luaL_checklstring(), luaL_checkstack(), luaL_checkstring, luaL_error(), luaL_optinteger(), NULL, and optsize().

◆ controloptions()

static void controloptions ( lua_State * L,
int opt,
const char ** fmt,
Header * h )
static

Definition at line 145 of file lua_struct.c.

146 {
147 switch (opt) {
148 case ' ': return; /* ignore white spaces */
149 case '>': h->endian = BIG; return;
150 case '<': h->endian = LITTLE; return;
151 case '!': {
152 int a = getnum(fmt, MAXALIGN);
153 if (!isp2(a))
154 luaL_error(L, "alignment %d is not a power of 2", a);
155 h->align = a;
156 return;
157 }
158 default: {
159 const char *msg = lua_pushfstring(L, "invalid format option '%c'", opt);
160 luaL_argerror(L, 1, msg);
161 }
162 }
163}
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
#define MAXALIGN
Definition lua_struct.c:71
static int getnum(const char **fmt, int df)
Definition lua_struct.c:91
#define BIG
Definition lua_struct.c:75
#define LITTLE
Definition lua_struct.c:76
#define isp2(x)
Definition lua_struct.c:61
int align
Definition lua_struct.c:87

References Header::align, BIG, Header::endian, getnum(), isp2, LITTLE, lua_pushfstring(), luaL_argerror(), luaL_error(), and MAXALIGN.

Referenced by b_pack(), b_size(), and b_unpack().

◆ correctbytes()

static void correctbytes ( char * b,
int size,
int endian )
static

Definition at line 193 of file lua_struct.c.

193 {
194 if (endian != native.endian) {
195 int i = 0;
196 while (i < --size) {
197 char temp = b[i];
198 b[i++] = b[size];
199 b[size] = temp;
200 }
201 }
202}
static union @102 native
char endian
Definition lua_struct.c:81

References endian, and native.

Referenced by b_pack(), and b_unpack().

◆ getinteger()

static lua_Number getinteger ( const char * buff,
int endian,
int issigned,
int size )
static

Definition at line 263 of file lua_struct.c.

264 {
265 Uinttype l = 0;
266 int i;
267 if (endian == BIG) {
268 for (i = 0; i < size; i++) {
269 l <<= 8;
270 l |= (Uinttype)(unsigned char)buff[i];
271 }
272 }
273 else {
274 for (i = size - 1; i >= 0; i--) {
275 l <<= 8;
276 l |= (Uinttype)(unsigned char)buff[i];
277 }
278 }
279 if (!issigned)
280 return (lua_Number)l;
281 else { /* signed format */
282 Uinttype mask = (Uinttype)(~((Uinttype)0)) << (size*8 - 1);
283 if (l & mask) /* negative value? */
284 l |= mask; /* signal extension */
285 return (lua_Number)(Inttype)l;
286 }
287}
#define mask(n)
unsigned STRUCT_INT Uinttype
Definition lua_struct.c:54
STRUCT_INT Inttype
Definition lua_struct.c:51

References BIG, endian, and mask.

Referenced by b_unpack().

◆ getnum()

static int getnum ( const char ** fmt,
int df )
static

Definition at line 91 of file lua_struct.c.

91 {
92 if (!isdigit(**fmt)) /* no number? */
93 return df; /* return default value */
94 else {
95 int a = 0;
96 do {
97 a = a*10 + *((*fmt)++) - '0';
98 } while (isdigit(**fmt));
99 return a;
100 }
101}

Referenced by controloptions(), and optsize().

◆ gettoalign()

static int gettoalign ( size_t len,
Header * h,
int opt,
size_t size )
static

Definition at line 134 of file lua_struct.c.

134 {
135 if (size == 0 || opt == 'c') return 0;
136 if (size > (size_t)h->align)
137 size = h->align; /* respect max. alignment */
138 return (size - (len & (size - 1))) & (size - 1);
139}

References Header::align.

Referenced by b_pack(), b_size(), and b_unpack().

◆ luaopen_struct()

LUALIB_API int luaopen_struct ( lua_State * L)

Definition at line 394 of file lua_struct.c.

394 {
395
396 /* added to global environment */
398 lua_pushvalue(L, -1);
399 lua_setglobal(L, "struct");
400
401 return 1;
402}
LUA_API void lua_pushvalue(lua_State *L, int idx)
#define lua_setglobal(L, s)
#define luaL_newlib(L, l)
static const struct luaL_Reg thislib[]
Definition lua_struct.c:384

References lua_pushvalue(), lua_setglobal, luaL_newlib, and thislib.

◆ optsize()

static size_t optsize ( lua_State * L,
char opt,
const char ** fmt )
static

Definition at line 108 of file lua_struct.c.

108 {
109 switch (opt) {
110 case 'B': case 'b': return sizeof(char);
111 case 'H': case 'h': return sizeof(short);
112 case 'L': case 'l': return sizeof(long);
113 case 'T': return sizeof(size_t);
114 case 'f': return sizeof(float);
115 case 'd': return sizeof(double);
116 case 'x': return 1;
117 case 'c': return getnum(fmt, 1);
118 case 'i': case 'I': {
119 int sz = getnum(fmt, sizeof(int));
120 if (sz > MAXINTSIZE)
121 luaL_error(L, "integral size %d is larger than limit of %d",
122 sz, MAXINTSIZE);
123 return sz;
124 }
125 default: return 0; /* other cases do not need alignment */
126 }
127}
#define MAXINTSIZE
Definition lua_struct.c:58

References getnum(), luaL_error(), and MAXINTSIZE.

Referenced by b_pack(), b_size(), and b_unpack().

◆ putinteger()

static void putinteger ( lua_State * L,
luaL_Buffer * b,
int arg,
int endian,
int size )
static

Definition at line 166 of file lua_struct.c.

167 {
170 char buff[MAXINTSIZE];
171 if (n < 0)
172 value = (Uinttype)(Inttype)n;
173 else
174 value = (Uinttype)n;
175 if (endian == LITTLE) {
176 int i;
177 for (i = 0; i < size; i++) {
178 buff[i] = (value & 0xff);
179 value >>= 8;
180 }
181 }
182 else {
183 int i;
184 for (i = size - 1; i >= 0; i--) {
185 buff[i] = (value & 0xff);
186 value >>= 8;
187 }
188 }
189 luaL_addlstring(b, buff, size);
190}
int value
Definition lsqlite3.c:2155

References arg, endian, LITTLE, luaL_addlstring(), luaL_checknumber(), MAXINTSIZE, and value.

Referenced by b_pack().

Variable Documentation

◆ dummy

int dummy

Definition at line 80 of file lua_struct.c.

◆ endian

char endian

Definition at line 81 of file lua_struct.c.

Referenced by correctbytes(), getinteger(), and putinteger().

◆ [union]

union { ... } native

Referenced by correctbytes().

◆ thislib

const struct luaL_Reg thislib[]
static
Initial value:
= {
{"pack", b_pack},
{"unpack", b_unpack},
{"size", b_size},
}
static int b_unpack(lua_State *L)
Definition lua_struct.c:290
static int b_size(lua_State *L)
Definition lua_struct.c:359
static int b_pack(lua_State *L)
Definition lua_struct.c:205

Definition at line 384 of file lua_struct.c.

384 {
385 {"pack", b_pack},
386 {"unpack", b_unpack},
387 {"size", b_size},
388 {NULL, NULL}
389};

Referenced by luaopen_struct().