]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_filedb/lib/OverUnderflowCheck.h
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / libsecurity_filedb / lib / OverUnderflowCheck.h
1 #ifndef __OVERUNDERFLOWCHECK__
2 #define __OVERUNDERFLOWCHECK__
3
4 inline uint32 CheckUInt32Add(uint32 a, uint32 b)
5 {
6 uint32 c = a + b;
7 if (c < a)
8 {
9 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
10 }
11
12 return c;
13 }
14
15
16
17 inline uint32 CheckUInt32Subtract(uint32 a, uint32 b)
18 {
19 if (a < b)
20 {
21 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
22 }
23
24 return a - b;
25 }
26
27
28
29 inline uint32 CheckUInt32Multiply(uint32 a, uint32 b)
30 {
31 uint32 c = a * b;
32 uint64 cc = ((uint64) a) * ((uint64) b);
33 if (c != cc)
34 {
35 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
36 }
37
38 return c;
39 }
40
41
42
43 inline uint64 Check64BitAdd(uint64 a, uint64 b)
44 {
45 uint64 c = a + b;
46 if (c < a)
47 {
48 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
49 }
50
51 return c;
52 }
53
54
55
56 inline uint64 Check64BitSubtract(uint64 a, uint64 b)
57 {
58 if (a < b)
59 {
60 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
61 }
62
63 return a - b;
64 }
65
66
67
68 inline uint64 Check64BitMultiply(uint64 a, uint64 b)
69 {
70 if (a != 0)
71 {
72 uint64 max = (uint64) -1;
73 uint64 limit = max / a;
74 if (b > limit)
75 {
76 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
77 }
78 }
79
80 return a * b;
81 }
82
83
84
85 #endif