Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
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.

byte.h 2.5 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* this header file comes from libowfat, http://www.fefe.de/libowfat/ */
  2. #ifndef BYTE_H
  3. #define BYTE_H
  4. /* for size_t: */
  5. #include <stddef.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include <libowfat/compiler.h>
  10. /* byte_chr returns the smallest integer i between 0 and len-1
  11. * inclusive such that one[i] equals needle, or len if not found. */
  12. att_readn(1,2)
  13. size_t byte_chr(const void* haystack, size_t len, char needle) noexcept;
  14. /* byte_rchr returns the largest integer i between 0 and len-1 inclusive
  15. * such that one[i] equals needle, or len if not found. */
  16. att_pure
  17. att_readn(1,2)
  18. size_t byte_rchr(const void* haystack,size_t len,char needle) noexcept;
  19. /* byte_copy copies in[0] to out[0], in[1] to out[1], ... and in[len-1]
  20. * to out[len-1]. */
  21. att_writen(1,2)
  22. att_readn(3,2)
  23. void byte_copy(void* out, size_t len, const void* in) noexcept;
  24. /* byte_copyr copies in[len-1] to out[len-1], in[len-2] to out[len-2],
  25. * ... and in[0] to out[0] */
  26. att_writen(1,2)
  27. att_readn(3,2)
  28. void byte_copyr(void* out, size_t len, const void* in) noexcept;
  29. /* byte_diff returns negative, 0, or positive, depending on whether the
  30. * string a[0], a[1], ..., a[len-1] is lexicographically smaller
  31. * than, equal to, or greater than the string b[0], b[1], ...,
  32. * b[len-1]. When the strings are different, byte_diff does not read
  33. * bytes past the first difference. */
  34. att_pure
  35. att_readn(1,2)
  36. att_readn(3,2)
  37. int byte_diff(const void* a, size_t len, const void* b) noexcept;
  38. /* byte_zero sets the bytes out[0], out[1], ..., out[len-1] to 0 */
  39. att_writen(1,2)
  40. void byte_zero(void* out, size_t len) noexcept;
  41. #define byte_equal(s,n,t) (!byte_diff((s),(n),(t)))
  42. /* Return 1 iff (b,blen) is a prefix of (a,alen), 0 otherwise.
  43. * Will abort early on mismatch */
  44. att_pure
  45. att_readn(1,2)
  46. att_readn(3,4)
  47. int byte_start(const void* a,size_t alen,const void* b,size_t blen) noexcept;
  48. /* equivalent to byte_start(a,alen,str,strlen(str)) */
  49. att_pure
  50. att_readn(1,2)
  51. att_read(3)
  52. int byte_starts(const void* a,size_t alen,const char* str) noexcept;
  53. #if defined(__GNUC__) && !defined(__LIBOWFAT_INTERNAL)
  54. /* If str is a string constant, strlen will be done at compile time */
  55. #define byte_starts(a,alen,str) (__builtin_constant_p(str) ? byte_start(a,alen,str,strlen(str)) : byte_starts(a,alen,str))
  56. #endif
  57. att_pure
  58. att_readn(1,2)
  59. att_readn(3,2)
  60. int byte_equal_notimingattack(const void* a, size_t len,const void* b) noexcept;
  61. #if defined(__i386__) || defined(__x86_64__)
  62. #define UNALIGNED_ACCESS_OK
  63. #endif
  64. #ifdef __cplusplus
  65. }
  66. #endif
  67. #endif