** 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. */
| 1669 | ** overflow. |
| 1670 | */ |
| 1671 | static lua_Integer unpackint (lua_State *L, const char *str, |
| 1672 | int islittle, int size, int issigned) { |
| 1673 | lua_Unsigned res = 0; |
| 1674 | int i; |
| 1675 | int limit = (size <= SZINT) ? size : SZINT; |
| 1676 | for (i = limit - 1; i >= 0; i--) { |
| 1677 | res <<= NB; |
| 1678 | res |= (lua_Unsigned)(unsigned char)str[islittle ? i : size - 1 - i]; |
| 1679 | } |
| 1680 | if (size < SZINT) { /* real size smaller than lua_Integer? */ |
| 1681 | if (issigned) { /* needs sign extension? */ |
| 1682 | lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1); |
| 1683 | res = ((res ^ mask) - mask); /* do sign extension */ |
| 1684 | } |
| 1685 | } |
| 1686 | else if (size > SZINT) { /* must check unread bytes */ |
| 1687 | int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC; |
| 1688 | for (i = limit; i < size; i++) { |
| 1689 | if (l_unlikely((unsigned char)str[islittle ? i : size - 1 - i] != mask)) |
| 1690 | luaL_error(L, "%d-byte integer does not fit into Lua Integer", size); |
| 1691 | } |
| 1692 | } |
| 1693 | return (lua_Integer)res; |
| 1694 | } |
| 1695 | |
| 1696 | |
| 1697 | static int str_unpack (lua_State *L) { |
no test coverage detected