bsize.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @file bsize.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Arithmetic with overflow detection.
  32. */
  33. #ifndef BADVPN_MISC_BSIZE_H
  34. #define BADVPN_MISC_BSIZE_H
  35. #include <stddef.h>
  36. #include <limits.h>
  37. #include <stdint.h>
  38. typedef struct {
  39. int is_overflow;
  40. size_t value;
  41. } bsize_t;
  42. static bsize_t bsize_fromsize (size_t v);
  43. static bsize_t bsize_fromint (int v);
  44. static bsize_t bsize_overflow (void);
  45. static int bsize_tosize (bsize_t s, size_t *out);
  46. static int bsize_toint (bsize_t s, int *out);
  47. static bsize_t bsize_add (bsize_t s1, bsize_t s2);
  48. static bsize_t bsize_max (bsize_t s1, bsize_t s2);
  49. static bsize_t bsize_mul (bsize_t s1, bsize_t s2);
  50. bsize_t bsize_fromsize (size_t v)
  51. {
  52. bsize_t s = {0, v};
  53. return s;
  54. }
  55. bsize_t bsize_fromint (int v)
  56. {
  57. bsize_t s = {(v < 0 || v > SIZE_MAX), v};
  58. return s;
  59. }
  60. static bsize_t bsize_overflow (void)
  61. {
  62. bsize_t s = {1, 0};
  63. return s;
  64. }
  65. int bsize_tosize (bsize_t s, size_t *out)
  66. {
  67. if (s.is_overflow) {
  68. return 0;
  69. }
  70. if (out) {
  71. *out = s.value;
  72. }
  73. return 1;
  74. }
  75. int bsize_toint (bsize_t s, int *out)
  76. {
  77. if (s.is_overflow || s.value > INT_MAX) {
  78. return 0;
  79. }
  80. if (out) {
  81. *out = s.value;
  82. }
  83. return 1;
  84. }
  85. bsize_t bsize_add (bsize_t s1, bsize_t s2)
  86. {
  87. bsize_t s = {(s1.is_overflow || s2.is_overflow || s2.value > SIZE_MAX - s1.value), (s1.value + s2.value)};
  88. return s;
  89. }
  90. bsize_t bsize_max (bsize_t s1, bsize_t s2)
  91. {
  92. bsize_t s = {(s1.is_overflow || s2.is_overflow), ((s1.value > s2.value) * s1.value + (s1.value <= s2.value) * s2.value)};
  93. return s;
  94. }
  95. bsize_t bsize_mul (bsize_t s1, bsize_t s2)
  96. {
  97. bsize_t s = {(s1.is_overflow || s2.is_overflow || (s1.value != 0 && s2.value > SIZE_MAX / s1.value)), (s1.value * s2.value)};
  98. return s;
  99. }
  100. #endif