util.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright (c) 2003-2006 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. /* These are Polipo's error codes. They need to be positive integers,
  20. and must not collide with possible errno values.
  21. Starting at 2^16 should be safe enough. */
  22. #define E0 (1 << 16)
  23. #define E1 (2 << 16)
  24. #define E2 (3 << 16)
  25. #define E3 (4 << 16)
  26. #define EUNKNOWN (E0)
  27. #define EDOSHUTDOWN (E0 + 1)
  28. #define EDOGRACEFUL (E0 + 2)
  29. #define EDOTIMEOUT (E0 + 3)
  30. #define ECLIENTRESET (E0 + 4)
  31. #define ESYNTAX (E0 + 5)
  32. #define EREDIRECTOR (E0 + 6)
  33. #define EDNS_HOST_NOT_FOUND (E1)
  34. #define EDNS_NO_ADDRESS (E1 + 1)
  35. #define EDNS_NO_RECOVERY (E1 + 2)
  36. #define EDNS_TRY_AGAIN (E1 + 3)
  37. #define EDNS_INVALID (E1 + 4)
  38. #define EDNS_UNSUPPORTED (E1 + 5)
  39. #define EDNS_FORMAT (E1 + 6)
  40. #define EDNS_REFUSED (E1 + 7)
  41. #define EDNS_CNAME_LOOP (E1 + 8)
  42. #define ESOCKS_PROTOCOL (E2)
  43. /* These correspond to SOCKS status codes 91 through 93 */
  44. #define ESOCKS_REJECT_FAIL (E2 + 1)
  45. #define ESOCKS_REJECT_IDENTD (E2 + 2)
  46. #define ESOCKS_REJECT_UID_MISMATCH (E2 + 3)
  47. /* (ESOCKS5_BASE + n) corresponds to SOCKS5 status code n (0 to 8) */
  48. #define ESOCKS5_BASE (E3)
  49. typedef struct _IntRange {
  50. int from;
  51. int to;
  52. } IntRangeRec, *IntRangePtr;
  53. typedef struct _IntList {
  54. int length;
  55. int size;
  56. IntRangePtr ranges;
  57. } IntListRec, *IntListPtr;
  58. char *strdup_n(const char *restrict buf, int n) ATTRIBUTE ((malloc));
  59. int snnprintf(char *restrict buf, int n, int len, const char *format, ...)
  60. ATTRIBUTE ((format (printf, 4, 5)));
  61. int snnvprintf(char *restrict buf, int n, int len, const char *format, va_list args)
  62. ATTRIBUTE ((format (printf, 4, 0)));
  63. int snnprint_n(char *restrict buf, int n, int len, const char *s, int slen);
  64. int digit(char) ATTRIBUTE ((const));
  65. int letter(char) ATTRIBUTE ((const));
  66. char lwr(char) ATTRIBUTE ((const));
  67. char* lwrcpy(char *restrict dst, const char *restrict src, int n);
  68. int lwrcmp(const char *as, const char *bs, int n) ATTRIBUTE ((pure));
  69. int strcasecmp_n(const char *string, const char *buf, int n)
  70. ATTRIBUTE ((pure));
  71. int atoi_n(const char *restrict string, int n, int len, int *value_return);
  72. int isWhitespace(const char *string) ATTRIBUTE((pure));
  73. #ifndef HAVE_MEMRCHR
  74. void *memrchr(const void *s, int c, size_t n) ATTRIBUTE ((pure));
  75. #endif
  76. int h2i(char h) ATTRIBUTE ((const));
  77. char i2h(int i) ATTRIBUTE ((const));
  78. int log2_floor(int x) ATTRIBUTE ((const));
  79. int log2_ceil(int x) ATTRIBUTE ((const));
  80. char* vsprintf_a(const char *f, va_list args)
  81. ATTRIBUTE ((malloc, format (printf, 1, 0)));
  82. char* sprintf_a(const char *f, ...)
  83. ATTRIBUTE ((malloc, format (printf, 1, 2)));
  84. unsigned int hash(unsigned seed, const void *restrict key, int key_size,
  85. unsigned int hash_size)
  86. ATTRIBUTE ((pure));
  87. char *pstrerror(int e);
  88. time_t mktime_gmt(struct tm *tm) ATTRIBUTE ((pure));
  89. AtomPtr expandTilde(AtomPtr filename);
  90. void do_daemonise(int noclose);
  91. void writePid(char *pidfile);
  92. int b64cpy(char *restrict dst, const char *restrict src, int n, int fss);
  93. int b64cmp(const char *restrict a, int an, const char *restrict b, int bn)
  94. ATTRIBUTE ((pure));
  95. IntListPtr makeIntList(int size) ATTRIBUTE ((malloc));
  96. void destroyIntList(IntListPtr list);
  97. int intListMember(int n, IntListPtr list) ATTRIBUTE ((pure));
  98. int intListCons(int from, int to, IntListPtr list);
  99. int physicalMemory(void);