** 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. */
| 1744 | ** overflow. |
| 1745 | */ |
| 1746 | static lua_Integer unpackint (lua_State *L, const char *str, |
| 1747 | int islittle, int size, int issigned) { |
| 1748 | lua_Unsigned res = 0; |
| 1749 | int i; |
| 1750 | int limit = (size <= SZINT) ? size : SZINT; |
| 1751 | for (i = limit - 1; i >= 0; i--) { |
| 1752 | res <<= NB; |
| 1753 | res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i]; |
| 1754 | } |
| 1755 | if (size < SZINT) { /* real size smaller than lua_Integer? */ |
| 1756 | if (issigned) { /* needs sign extension? */ |
| 1757 | lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1); |
| 1758 | res = ((res ^ mask) - mask); /* do sign extension */ |
| 1759 | } |
| 1760 | } |
| 1761 | else if (size > SZINT) { /* must check unread bytes */ |
| 1762 | int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC; |
| 1763 | for (i = limit; i < size; i++) { |
| 1764 | if (l_unlikely((unsigned char)str[islittle ? i : size - 1 - i] != mask)) |
| 1765 | luaL_error(L, "%d-byte integer does not fit into Lua Integer", size); |
| 1766 | } |
| 1767 | } |
| 1768 | return (lua_Integer)res; |
| 1769 | } |
| 1770 | |
| 1771 | |
| 1772 | static int str_unpack (lua_State *L) { |
no test coverage detected