/* Returns a pointer to a free area with at least 'sz' bytes in buffer 'B'. 'boxidx' is the relative position in the stack where is the buffer's box or its placeholder.  */
static char* prepbuffsize(luaL_Buffer *B, size_t sz, int boxidx)
/* Returns a pointer to a free area with at least 'sz' bytes in buffer 'B'. 'boxidx' is the relative position in the stack where is the buffer's box or its placeholder.  */
static char* prepbuffsize(luaL_Buffer *B, size_t sz, int boxidx)
{
    lua_State *L = B->L;
    char *newbuff;
    size_t newsize = newbuffsize(B, sz);
    if (buffonstack(B))
      newbuff = (char *)resizebox(L, boxidx, newsize);
    else {
      lua_remove(L, boxidx);
      newbox(L);
      lua_insert(L, boxidx);
      lua_toclose(L, boxidx);
      newbuff = (char *)resizebox(L, boxidx, newsize);
      memcpy(newbuff, B->b, B->n * sizeof(char));
    }
    B->b = newbuff;
    B->size = newsize;
    return newbuff + B->n;
  }
}