MCPcopy Create free account
hub / github.com/EmmyLua/EmmyLuaDebugger / unpackint

Function unpackint

third-party/lua-5.3.5/src/lstrlib.c:1450–1473  ·  view source on GitHub ↗

** Unpack an integer with 'size' bytes and 'islittle' endianness. ** If size is smaller than the size of a Lua integer and integer ** is signed, must do sign extension (propagating the sign to the ** higher bits); if size is larger than the size of a Lua integer, ** it must check the unread bytes to see whether they do not cause an ** overflow. */

Source from the content-addressed store, hash-verified

1448** overflow.
1449*/
1450static lua_Integer unpackint (lua_State *L, const char *str,
1451 int islittle, int size, int issigned) {
1452 lua_Unsigned res = 0;
1453 int i;
1454 int limit = (size <= SZINT) ? size : SZINT;
1455 for (i = limit - 1; i >= 0; i--) {
1456 res <<= NB;
1457 res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i];
1458 }
1459 if (size < SZINT) { /* real size smaller than lua_Integer? */
1460 if (issigned) { /* needs sign extension? */
1461 lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
1462 res = ((res ^ mask) - mask); /* do sign extension */
1463 }
1464 }
1465 else if (size > SZINT) { /* must check unread bytes */
1466 int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC;
1467 for (i = limit; i < size; i++) {
1468 if ((unsigned char)str[islittle ? i : size - 1 - i] != mask)
1469 luaL_error(L, "%d-byte integer does not fit into Lua Integer", size);
1470 }
1471 }
1472 return (lua_Integer)res;
1473}
1474
1475
1476static int str_unpack (lua_State *L) {

Callers 1

str_unpackFunction · 0.70

Calls 1

luaL_errorFunction · 0.70

Tested by

no test coverage detected