MCPcopy Create free account
hub / github.com/ObEngine/ObEngine / str_unpack

Function str_unpack

extlibs/lua/src/lstrlib.c:1693–1756  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1691
1692
1693static int str_unpack (lua_State *L) {
1694 Header h;
1695 const char *fmt = luaL_checkstring(L, 1);
1696 size_t ld;
1697 const char *data = luaL_checklstring(L, 2, &ld);
1698 size_t pos = posrelatI(luaL_optinteger(L, 3, 1), ld) - 1;
1699 int n = 0; /* number of results */
1700 luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
1701 initheader(L, &h);
1702 while (*fmt != '\0') {
1703 int size, ntoalign;
1704 KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
1705 luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2,
1706 "data string too short");
1707 pos += ntoalign; /* skip alignment */
1708 /* stack space for item + next position */
1709 luaL_checkstack(L, 2, "too many results");
1710 n++;
1711 switch (opt) {
1712 case Kint:
1713 case Kuint: {
1714 lua_Integer res = unpackint(L, data + pos, h.islittle, size,
1715 (opt == Kint));
1716 lua_pushinteger(L, res);
1717 break;
1718 }
1719 case Kfloat: {
1720 volatile Ftypes u;
1721 lua_Number num;
1722 copywithendian(u.buff, data + pos, size, h.islittle);
1723 if (size == sizeof(u.f)) num = (lua_Number)u.f;
1724 else if (size == sizeof(u.d)) num = (lua_Number)u.d;
1725 else num = u.n;
1726 lua_pushnumber(L, num);
1727 break;
1728 }
1729 case Kchar: {
1730 lua_pushlstring(L, data + pos, size);
1731 break;
1732 }
1733 case Kstring: {
1734 size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0);
1735 luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short");
1736 lua_pushlstring(L, data + pos + size, len);
1737 pos += len; /* skip string */
1738 break;
1739 }
1740 case Kzstr: {
1741 size_t len = (int)strlen(data + pos);
1742 luaL_argcheck(L, pos + len < ld, 2,
1743 "unfinished string for format 'z'");
1744 lua_pushlstring(L, data + pos, len);
1745 pos += len + 1; /* skip string plus final '\0' */
1746 break;
1747 }
1748 case Kpaddalign: case Kpadding: case Knop:
1749 n--; /* undo increment */
1750 break;

Callers

nothing calls this directly

Calls 11

luaL_checklstringFunction · 0.85
posrelatIFunction · 0.85
luaL_optintegerFunction · 0.85
initheaderFunction · 0.85
getdetailsFunction · 0.85
unpackintFunction · 0.85
lua_pushintegerFunction · 0.85
copywithendianFunction · 0.85
lua_pushnumberFunction · 0.85
lua_pushlstringFunction · 0.85
luaL_checkstackFunction · 0.70

Tested by

no test coverage detected