** 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). */
| 1142 | ** bytes if necessary (by default they would be zeros). |
| 1143 | */ |
| 1144 | static void packint (luaL_Buffer *b, lua_Unsigned n, |
| 1145 | int islittle, int size, int neg) { |
| 1146 | char *buff = luaL_prepbuffsize(b, size); |
| 1147 | int i; |
| 1148 | buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */ |
| 1149 | for (i = 1; i < size; i++) { |
| 1150 | n >>= NB; |
| 1151 | buff[islittle ? i : size - 1 - i] = (char)(n & MC); |
| 1152 | } |
| 1153 | if (neg && size > SZINT) { /* negative number need sign extension? */ |
| 1154 | for (i = SZINT; i < size; i++) /* correct extra bytes */ |
| 1155 | buff[islittle ? i : size - 1 - i] = (char)MC; |
| 1156 | } |
| 1157 | luaL_addsize(b, size); /* add result to buffer */ |
| 1158 | } |
| 1159 | |
| 1160 | |
| 1161 | /* |
no test coverage detected