/* returns a pointer to a free area with at least 'sz' bytes  */
LUALIB_API char* luaL_prepbuffsize(luaL_Buffer *B, size_t sz)
/* returns a pointer to a free area with at least 'sz' bytes  */
LUALIB_API char* luaL_prepbuffsize(luaL_Buffer *B, size_t sz)
{
    char *newbuff;
    size_t newsize = B->size * 2;
    if (newsize - B->n < sz)
      newsize = B->n + sz;
    if (newsize < B->n || newsize - B->n < sz)
      luaL_error(L, "buffer too large");
    if (buffonstack(B))
      newbuff = (char *)resizebox(L, -1, newsize);
    else {
      newbuff = (char *)newbox(L, newsize);
      memcpy(newbuff, B->b, B->n * sizeof(char));
    }
    B->b = newbuff;
    B->size = newsize;
  }
  return &B->b[B->n];
}