/src/zziplib/zzip/err.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 168 lines · 110 code · 17 blank · 41 comment · 13 complexity · 5c970cc48d1b72496fc31ad212aaa386 MD5 · raw file

  1. /*
  2. * Author:
  3. * Guido Draheim <guidod@gmx.de>
  4. * Tomi Ollila <Tomi.Ollila@iki.fi>
  5. *
  6. * Copyright (c) 1999,2000,2001,2002,2003 Guido Draheim
  7. * All rights reserved,
  8. * use under the restrictions of the
  9. * Lesser GNU General Public License
  10. * or alternatively the restrictions
  11. * of the Mozilla Public License 1.1
  12. */
  13. #include <zzip/lib.h> /* exported... */
  14. #include <zlib.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <zzip/file.h>
  18. /* *INDENT-OFF* */
  19. static struct errlistentry { int code; const char* mesg; }
  20. errlist[] =
  21. {
  22. { ZZIP_NO_ERROR, "No error" },
  23. { ZZIP_OUTOFMEM,
  24. "could not get temporary memory for internal structures" },
  25. { ZZIP_DIR_OPEN, "Failed to open zip-file %s" },
  26. { ZZIP_DIR_STAT, "Failed to fstat zip-file %s" },
  27. { ZZIP_DIR_SEEK, "Failed to lseek zip-file %s" },
  28. { ZZIP_DIR_READ, "Failed to read zip-file %s"},
  29. { ZZIP_DIR_TOO_SHORT, "zip-file %s too short" },
  30. { ZZIP_DIR_EDH_MISSING, "zip-file central directory not found" },
  31. { ZZIP_DIRSIZE, "Directory size too big..." },
  32. { ZZIP_ENOENT, "No such file found in zip-file %s" },
  33. { ZZIP_UNSUPP_COMPR, "Unsupported compression format" },
  34. { ZZIP_CORRUPTED, "Zipfile corrupted" },
  35. { ZZIP_UNDEF, "Some undefined error occurred" },
  36. { ZZIP_DIR_LARGEFILE, "Directory is largefile variant" },
  37. { 0, 0 },
  38. };
  39. /* *INDENT-ON* */
  40. #define errlistSIZE (sizeof(errlist)/sizeof(*errlist))
  41. /**
  42. * returns the static string for the given error code. The
  43. * error code can be either a normal system error (a
  44. * positive error code will flag this), it can be => libz
  45. * error code (a small negative error code will flag this)
  46. * or it can be an error code from => libzzip, which is an
  47. * negative value lower than => ZZIP_ERROR
  48. */
  49. zzip_char_t *
  50. zzip_strerror(int errcode)
  51. {
  52. if (errcode < ZZIP_ERROR && errcode > ZZIP_ERROR - 32)
  53. {
  54. struct errlistentry *err = errlist;
  55. for (; err->mesg; err++)
  56. {
  57. if (err->code == errcode)
  58. return err->mesg;
  59. }
  60. errcode = EINVAL;
  61. }
  62. if (errcode < 0)
  63. {
  64. if (errcode == -1)
  65. return strerror(errcode);
  66. else
  67. return zError(errcode);
  68. }
  69. return strerror(errcode);
  70. }
  71. /** => zzip_strerror
  72. * This function fetches the errorcode from the => DIR-handle and
  73. * runs it through => zzip_strerror to obtain the static string
  74. * describing the error.
  75. */
  76. zzip_char_t *
  77. zzip_strerror_of(ZZIP_DIR * dir)
  78. {
  79. if (! dir)
  80. return strerror(errno);
  81. return zzip_strerror(dir->errcode);
  82. }
  83. /* *INDENT-OFF* */
  84. static struct errnolistentry { int code; int e_no; }
  85. errnolist[] =
  86. {
  87. { Z_STREAM_ERROR, EPIPE },
  88. { Z_DATA_ERROR, ESPIPE },
  89. { Z_MEM_ERROR, ENOMEM },
  90. { Z_BUF_ERROR, EMFILE },
  91. { Z_VERSION_ERROR, ENOEXEC },
  92. { ZZIP_DIR_OPEN, ENOTDIR },
  93. { ZZIP_DIR_STAT, EREMOTE },
  94. { ZZIP_DIR_SEEK, ESPIPE },
  95. # ifdef ESTRPIPE
  96. { ZZIP_DIR_READ, ESTRPIPE},
  97. # else
  98. { ZZIP_DIR_READ, EPIPE},
  99. # endif
  100. { ZZIP_DIR_TOO_SHORT, ENOEXEC },
  101. # ifdef ENOMEDIUM
  102. { ZZIP_DIR_EDH_MISSING, ENOMEDIUM },
  103. # else
  104. { ZZIP_DIR_EDH_MISSING, EIO },
  105. # endif
  106. { ZZIP_DIRSIZE, EFBIG },
  107. { ZZIP_OUTOFMEM, ENOMEM },
  108. { ZZIP_ENOENT, ENOENT },
  109. # ifdef EPFNOSUPPORT
  110. { ZZIP_UNSUPP_COMPR, EPFNOSUPPORT },
  111. # else
  112. { ZZIP_UNSUPP_COMPR, EACCES },
  113. # endif
  114. # ifdef EILSEQ
  115. { ZZIP_CORRUPTED, EILSEQ },
  116. # else
  117. { ZZIP_CORRUPTED, ELOOP },
  118. # endif
  119. { ZZIP_UNDEF, EINVAL },
  120. { 0, 0 },
  121. };
  122. /* *INDENT-ON* */
  123. /**
  124. * map the error code to a system error code. This is used
  125. * for the drop-in replacement functions to return a value
  126. * that can be interpreted correctly by code sections that
  127. * are unaware of the fact they their => open(2) call had been
  128. * diverted to a file inside a zip-archive.
  129. */
  130. int
  131. zzip_errno(int errcode)
  132. {
  133. if (errcode >= -1)
  134. {
  135. return errno;
  136. } else
  137. {
  138. struct errnolistentry *err = errnolist;
  139. for (; err->code; err++)
  140. {
  141. if (err->code == errcode)
  142. return err->e_no;
  143. }
  144. return EINVAL;
  145. }
  146. }
  147. /*
  148. * Local variables:
  149. * c-file-style: "stroustrup"
  150. * End:
  151. */