findchar.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // SPDX-License-Identifier: GPL-2.0 or GPL-3.0
  2. // Copyright © 2019 Ariadne Devos
  3. #ifndef _sHT_LEX_FINDCHAR
  4. #define _sHT_LEX_FINDCHAR
  5. #include <sHT/bitvec.h>
  6. #include <sHT/compiler.h>
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. /* TODO: is uint16_t, uint32_t, uint64_t more interesting?
  10. (e.g., more noise for attackers) */
  11. typedef uint8_t sHT_charclass_limb;
  12. #define sHT_charclass_n (256u)
  13. #define sHT_charclass_limbs (sHT_charclass_n / 8)
  14. /* Instances are generated by <tools/gen-character-bitvec.py>
  15. (currently in 'webdav' branch). */
  16. typedef const sHT_charclass_limb sHT_charclass[sHT_charclass_limbs];
  17. /* TODO: and \initialized */
  18. /*@ predicate valid_charclass(sHT_charclass_limb *v)
  19. = \valid_read(&v[0 .. sHT_charclass_limbs - 1]); */
  20. /*@ axiomatic Charclass {
  21. predicate in_charclass{L1}(sHT_charclass_limb *v, ℤ i)
  22. reads v[0 .. sHT_charclass_limbs - 1];
  23. } */
  24. /** Test a byte is in a set */
  25. /*@ requires valid: valid_charclass(s);
  26. terminates \true;
  27. assigns \result \from s[0 .. sHT_charclass_limbs - 1], c;
  28. ensures rNS: \result <==> in_charclass{Pre}(s, c); */
  29. __attribute__((always_inline))
  30. __attribute__((pure))
  31. static inline _Bool
  32. sHT_charclass_test(sHT_charclass s, uint8_t c)
  33. {
  34. /* (* precondition satisfied by typing of c
  35. (max(c) = 256 = 32 * 8) combined with (a) *) */
  36. return sHT_bit_test(s, c);
  37. }
  38. /** Find the first byte in a string belonging to a set
  39. (Starting at `b`, returning `e` on EOF). */
  40. /*@ requires from_r: \valid_read(&from[b .. e - 1]);
  41. requires from_s: \initialized(&from[b .. e - 1]);
  42. requires s_valid: valid_charclass(s);
  43. requires ra: b <= e;
  44. requires rb: 0 < e;
  45. assigns \result;
  46. ensures before_NS: \forall ℤ j; b <= j < \result ==> !in_charclass(s, from[j]);
  47. ensures after_NS: \result != e ==> in_charclass(s, from[\result]);
  48. ensures result_rangeNS: b <= \result <= e;
  49. ensures result_rangeS: \result <= e; */
  50. __attribute__((pure))
  51. size_t
  52. sHT_findchar(size_t e, const uint8_t from[e], size_t b, sHT_charclass s);
  53. #endif