misc.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* libanode: the Anode C reference implementation
  2. * Copyright (C) 2009-2010 Adam Ierymenko <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. /* This contains miscellaneous functions, including some re-implementations
  17. * of some functions from string.h. This is to help us port to some platforms
  18. * (cough Windows Mobile cough) that lack a lot of the basic C library. */
  19. #ifndef _ANODE_MISC_H
  20. #define _ANODE_MISC_H
  21. #include <time.h>
  22. #include <sys/time.h>
  23. #include "types.h"
  24. #ifndef ANODE_NO_STRING_H
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #endif
  28. /* Table mapping ASCII characters to themselves or their lower case */
  29. extern const unsigned char Anode_ascii_tolower_table[256];
  30. /* Get the lower case version of an ASCII char */
  31. #define Anode_tolower(c) ((char)Anode_ascii_tolower_table[((unsigned long)((unsigned char)(c)))])
  32. /* Test strings for equality, return nonzero if equal */
  33. static inline unsigned int Anode_streq(const char *restrict a,const char *restrict b)
  34. {
  35. if ((!a)||(!b))
  36. return 0;
  37. while (*a == *(b++)) {
  38. if (!*(a++))
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. /* Equality test ignoring (ASCII) case */
  44. static inline unsigned int Anode_strcaseeq(const char *restrict a,const char *restrict b)
  45. {
  46. if ((!a)||(!b))
  47. return 0;
  48. while (Anode_tolower(*a) == Anode_tolower(*(b++))) {
  49. if (!*(a++))
  50. return 1;
  51. }
  52. return 0;
  53. }
  54. /* Safe c-string copy, ensuring that dest[] always ends with zero */
  55. static inline void Anode_str_copy(char *restrict dest,const char *restrict src,unsigned int dest_size)
  56. {
  57. char *restrict dest_end = dest + (dest_size - 1);
  58. while ((*src)&&(dest != dest_end))
  59. *(dest++) = *(src++);
  60. *dest = (char)0;
  61. }
  62. /* Simple memcpy() */
  63. #ifdef ANODE_NO_STRING_H
  64. static inline void Anode_memcpy(void *restrict dest,const void *restrict src,unsigned int len)
  65. {
  66. unsigned int i;
  67. for(i=0;i<len;++i)
  68. ((unsigned char *restrict)dest)[i] = ((const unsigned char *restrict)src)[i];
  69. }
  70. #else
  71. #define Anode_memcpy(d,s,l) memcpy((d),(s),(l))
  72. #endif
  73. /* Memory test for equality */
  74. #ifdef ANODE_NO_STRING_H
  75. static inline unsigned int Anode_mem_eq(const void *restrict a,const void *restrict b,unsigned int len)
  76. {
  77. unsigned int i;
  78. for(i=0;i<len;++i) {
  79. if (((const unsigned char *restrict)a)[i] != ((const unsigned char *restrict)b)[i])
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. #else
  85. #define Anode_mem_eq(a,b,l) (!memcmp((a),(b),(l)))
  86. #endif
  87. /* Zero memory */
  88. #ifdef ANODE_NO_STRING_H
  89. static inline void Anode_zero(void *restrict ptr,unsigned int len)
  90. {
  91. unsigned int i;
  92. for(i=0;i<len;++i)
  93. ((unsigned char *restrict)ptr)[i] = (unsigned char)0;
  94. }
  95. #else
  96. #define Anode_zero(p,l) memset((p),0,(l))
  97. #endif
  98. /* Get a pointer to the first occurrance of a character in a string */
  99. #ifdef ANODE_NO_STRING_H
  100. static inline const char *Anode_strchr(const char *s,char c)
  101. {
  102. while (*s) {
  103. if (*s == c)
  104. return s;
  105. ++s;
  106. }
  107. return (char *)0;
  108. }
  109. #else
  110. #define Anode_strchr(s,c) strchr((s),(c))
  111. #endif
  112. static inline unsigned int Anode_count_char(const char *s,char c)
  113. {
  114. unsigned int cnt = 0;
  115. while (s) {
  116. if (*s == c)
  117. ++cnt;
  118. ++s;
  119. }
  120. return cnt;
  121. }
  122. /* Strip all of a given set of characters from a string */
  123. static inline void Anode_strip_all(char *s,const char *restrict schars)
  124. {
  125. char *d = s;
  126. while (*s) {
  127. if (!Anode_strchr(schars,*s))
  128. *(d++) = *s;
  129. ++s;
  130. }
  131. *d = (char)0;
  132. }
  133. /* Trim whitespace from beginning and end of string */
  134. void Anode_trim(char *s);
  135. /* Get the length of a string */
  136. #ifdef ANODE_NO_STRING_H
  137. static inline unsigned int Anode_strlen(const char *s)
  138. {
  139. const char *ptr = s;
  140. while (*ptr) ++ptr;
  141. return (unsigned int)(ptr - s);
  142. }
  143. #else
  144. #define Anode_strlen(s) strlen((s))
  145. #endif
  146. /* Returns number of milliseconds since the epoch (Java-style) */
  147. static inline uint64_t Anode_time64()
  148. {
  149. struct timeval tv;
  150. gettimeofday(&tv,(void *)0);
  151. return ( (((uint64_t)tv.tv_sec) / 1000ULL) + ((uint64_t)(tv.tv_usec / 1000ULL)) );
  152. }
  153. /* Returns number of seconds since the epoch (*nix style) */
  154. static inline unsigned long Anode_time()
  155. {
  156. struct timeval tv;
  157. gettimeofday(&tv,(void *)0);
  158. return (unsigned long)tv.tv_sec;
  159. }
  160. /* Simple random function, not cryptographically safe */
  161. unsigned int Anode_rand();
  162. /* Fast hex/ascii conversion */
  163. void Anode_to_hex(const unsigned char *b,unsigned int len,char *h,unsigned int hlen);
  164. void Anode_from_hex(const char *h,unsigned char *b,unsigned int blen);
  165. /* Convert back and forth from base32 encoding */
  166. /* 5 bytes -> 8 base32 characters and vice versa */
  167. void Anode_base32_5_to_8(const unsigned char *in,char *out);
  168. void Anode_base32_8_to_5(const char *in,unsigned char *out);
  169. #endif