** Pack integer 'n' with 'size' bytes and 'islittle' endianness. ** The final 'if' handles the case when 'size' is larger than ** the size of a Lua integer, correcting the extra sign-extension ** bytes if necessary (by default they would be zeros). */
| 1509 | ** bytes if necessary (by default they would be zeros). |
| 1510 | */ |
| 1511 | static void packint (luaL_Buffer *b, lua_Unsigned n, |
| 1512 | int islittle, int size, int neg) { |
| 1513 | char *buff = luaL_prepbuffsize(b, size); |
| 1514 | int i; |
| 1515 | buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */ |
| 1516 | for (i = 1; i < size; i++) { |
| 1517 | n >>= NB; |
| 1518 | buff[islittle ? i : size - 1 - i] = (char)(n & MC); |
| 1519 | } |
| 1520 | if (neg && size > SZINT) { /* negative number need sign extension? */ |
| 1521 | for (i = SZINT; i < size; i++) /* correct extra bytes */ |
| 1522 | buff[islittle ? i : size - 1 - i] = (char)MC; |
| 1523 | } |
| 1524 | luaL_addsize(b, size); /* add result to buffer */ |
| 1525 | } |
| 1526 | |
| 1527 | |
| 1528 | /* |
no test coverage detected