You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

80 rivejä
1.7 KiB

  1. /*
  2. ** $Id: lzio.c,v 1.35 2012/05/14 13:34:18 roberto Exp $
  3. ** Buffered streams
  4. ** See Copyright Notice in lua.h
  5. */
  6. #if defined HAVE_CONFIG_H // LOL BEGIN
  7. # include "config.h"
  8. #endif // LOL END
  9. #include <string.h>
  10. #define lzio_c
  11. #define LUA_CORE
  12. #include "lua.h"
  13. #include "llimits.h"
  14. #include "lmem.h"
  15. #include "lstate.h"
  16. #include "lzio.h"
  17. int luaZ_fill (ZIO *z) {
  18. size_t size;
  19. lua_State *L = z->L;
  20. const char *buff;
  21. lua_unlock(L);
  22. buff = z->reader(L, z->data, &size);
  23. lua_lock(L);
  24. if (buff == NULL || size == 0)
  25. return EOZ;
  26. z->n = size - 1; /* discount char being returned */
  27. z->p = buff;
  28. return cast_uchar(*(z->p++));
  29. }
  30. void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
  31. z->L = L;
  32. z->reader = reader;
  33. z->data = data;
  34. z->n = 0;
  35. z->p = NULL;
  36. }
  37. /* --------------------------------------------------------------- read --- */
  38. size_t luaZ_read (ZIO *z, void *b, size_t n) {
  39. while (n) {
  40. size_t m;
  41. if (z->n == 0) { /* no bytes in buffer? */
  42. if (luaZ_fill(z) == EOZ) /* try to read more */
  43. return n; /* no more input; return number of missing bytes */
  44. else {
  45. z->n++; /* luaZ_fill consumed first byte; put it back */
  46. z->p--;
  47. }
  48. }
  49. m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
  50. memcpy(b, z->p, m);
  51. z->n -= m;
  52. z->p += m;
  53. b = (char *)b + m;
  54. n -= m;
  55. }
  56. return 0;
  57. }
  58. /* ------------------------------------------------------------------------ */
  59. char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
  60. if (n > buff->buffsize) {
  61. if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
  62. luaZ_resizebuffer(L, buff, n);
  63. }
  64. return buff->buffer;
  65. }