bsize.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. bsize_t bsize_fromsize (size_t v)
  50. {
  51. bsize_t s;
  52. s.is_overflow = 0;
  53. s.value = v;
  54. return s;
  55. }
  56. bsize_t bsize_fromint (int v)
  57. {
  58. bsize_t s;
  59. if (v < 0 || v > SIZE_MAX) {
  60. s.is_overflow = 1;
  61. } else {
  62. s.is_overflow = 0;
  63. s.value = v;
  64. }
  65. return s;
  66. }
  67. static bsize_t bsize_overflow (void)
  68. {
  69. bsize_t s;
  70. s.is_overflow = 1;
  71. return s;
  72. }
  73. int bsize_tosize (bsize_t s, size_t *out)
  74. {
  75. if (s.is_overflow) {
  76. return 0;
  77. }
  78. if (out) {
  79. *out = s.value;
  80. }
  81. return 1;
  82. }
  83. int bsize_toint (bsize_t s, int *out)
  84. {
  85. if (s.is_overflow || s.value > INT_MAX) {
  86. return 0;
  87. }
  88. if (out) {
  89. *out = s.value;
  90. }
  91. return 1;
  92. }
  93. bsize_t bsize_add (bsize_t s1, bsize_t s2)
  94. {
  95. bsize_t s;
  96. if (s1.is_overflow || s2.is_overflow || s2.value > SIZE_MAX - s1.value) {
  97. s.is_overflow = 1;
  98. } else {
  99. s.is_overflow = 0;
  100. s.value = s1.value + s2.value;
  101. }
  102. return s;
  103. }
  104. bsize_t bsize_max (bsize_t s1, bsize_t s2)
  105. {
  106. bsize_t s;
  107. if (s1.is_overflow || s2.is_overflow) {
  108. s.is_overflow = 1;
  109. } else {
  110. s.is_overflow = 0;
  111. s.value = s1.value;
  112. if (s.value < s2.value) {
  113. s.value = s2.value;
  114. }
  115. }
  116. return s;
  117. }
  118. #endif