/src/freeimage-svn/FreeImage/trunk/Source/OpenEXR/IlmImf/ImfCheckedArithmetic.h
Line  | Count  | Source  | 
1  |  | ///////////////////////////////////////////////////////////////////////////  | 
2  |  | //  | 
3  |  | // Copyright (c) 2009, Industrial Light & Magic, a division of Lucas  | 
4  |  | // Digital Ltd. LLC  | 
5  |  | //   | 
6  |  | // All rights reserved.  | 
7  |  | //   | 
8  |  | // Redistribution and use in source and binary forms, with or without  | 
9  |  | // modification, are permitted provided that the following conditions are  | 
10  |  | // met:  | 
11  |  | // *       Redistributions of source code must retain the above copyright  | 
12  |  | // notice, this list of conditions and the following disclaimer.  | 
13  |  | // *       Redistributions in binary form must reproduce the above  | 
14  |  | // copyright notice, this list of conditions and the following disclaimer  | 
15  |  | // in the documentation and/or other materials provided with the  | 
16  |  | // distribution.  | 
17  |  | // *       Neither the name of Industrial Light & Magic nor the names of  | 
18  |  | // its contributors may be used to endorse or promote products derived  | 
19  |  | // from this software without specific prior written permission.   | 
20  |  | //   | 
21  |  | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  | 
22  |  | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  | 
23  |  | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  | 
24  |  | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  | 
25  |  | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  | 
26  |  | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  | 
27  |  | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  | 
28  |  | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  | 
29  |  | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  | 
30  |  | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  | 
31  |  | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  | 
32  |  | //  | 
33  |  | ///////////////////////////////////////////////////////////////////////////  | 
34  |  |  | 
35  |  | #ifndef INCLUDED_IMF_CHECKED_ARITHMETIC_H  | 
36  |  | #define INCLUDED_IMF_CHECKED_ARITHMETIC_H  | 
37  |  |  | 
38  |  | //-----------------------------------------------------------------------------  | 
39  |  | //  | 
40  |  | //  Integer arithmetic operations that throw exceptions  | 
41  |  | //      on overflow, underflow or division by zero.  | 
42  |  | //  | 
43  |  | //-----------------------------------------------------------------------------  | 
44  |  |  | 
45  |  | #include <limits>  | 
46  |  | #include "IexMathExc.h"  | 
47  |  | #include "ImfNamespace.h"  | 
48  |  |  | 
49  |  | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER  | 
50  |  |  | 
51  |  | template <bool b> struct StaticAssertionFailed;  | 
52  |  | template <> struct StaticAssertionFailed <true> {}; | 
53  |  |  | 
54  |  | #define IMF_STATIC_ASSERT(x) \  | 
55  | 0  |     do {StaticAssertionFailed <x> staticAssertionFailed; ((void) staticAssertionFailed);} while (false) | 
56  |  |  | 
57  |  |  | 
58  |  | template <class T>  | 
59  |  | T  | 
60  |  | uiMult (T a, T b)  | 
61  | 0  | { | 
62  |  |     //  | 
63  |  |     // Unsigned integer multiplication  | 
64  |  |     //  | 
65  |  | 
  | 
66  | 0  |     IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&  | 
67  | 0  |                         std::numeric_limits<T>::is_integer);  | 
68  |  | 
  | 
69  | 0  |     if (a > 0 && b > std::numeric_limits<T>::max() / a)  | 
70  | 0  |         throw IEX_NAMESPACE::OverflowExc ("Integer multiplication overflow."); | 
71  |  |  | 
72  | 0  |     return a * b;  | 
73  | 0  | } Unexecuted instantiation: unsigned int Imf_2_2::uiMult<unsigned int>(unsigned int, unsigned int) Unexecuted instantiation: unsigned long Imf_2_2::uiMult<unsigned long>(unsigned long, unsigned long)  | 
74  |  |  | 
75  |  |  | 
76  |  | template <class T>  | 
77  |  | T  | 
78  |  | uiDiv (T a, T b)  | 
79  |  | { | 
80  |  |     //  | 
81  |  |     // Unsigned integer division  | 
82  |  |     //  | 
83  |  |  | 
84  |  |     IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&  | 
85  |  |                         std::numeric_limits<T>::is_integer);  | 
86  |  |  | 
87  |  |     if (b == 0)  | 
88  |  |         throw IEX_NAMESPACE::DivzeroExc ("Integer division by zero."); | 
89  |  |  | 
90  |  |     return a / b;  | 
91  |  | }  | 
92  |  |  | 
93  |  |  | 
94  |  | template <class T>  | 
95  |  | T  | 
96  |  | uiAdd (T a, T b)  | 
97  | 0  | { | 
98  |  |     //  | 
99  |  |     // Unsigned integer addition  | 
100  |  |     //  | 
101  |  | 
  | 
102  | 0  |     IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&  | 
103  | 0  |                         std::numeric_limits<T>::is_integer);  | 
104  |  | 
  | 
105  | 0  |     if (a > std::numeric_limits<T>::max() - b)  | 
106  | 0  |         throw IEX_NAMESPACE::OverflowExc ("Integer addition overflow."); | 
107  |  |  | 
108  | 0  |     return a + b;  | 
109  | 0  | }  | 
110  |  |  | 
111  |  |  | 
112  |  | template <class T>  | 
113  |  | T  | 
114  |  | uiSub (T a, T b)  | 
115  |  | { | 
116  |  |     //  | 
117  |  |     // Unsigned integer subtraction  | 
118  |  |     //  | 
119  |  |  | 
120  |  |     IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&  | 
121  |  |                         std::numeric_limits<T>::is_integer);  | 
122  |  |  | 
123  |  |     if (a < b)  | 
124  |  |         throw IEX_NAMESPACE::UnderflowExc ("Integer subtraction underflow."); | 
125  |  |  | 
126  |  |     return a - b;  | 
127  |  | }  | 
128  |  |  | 
129  |  |  | 
130  |  | template <class T>  | 
131  |  | size_t  | 
132  |  | checkArraySize (T n, size_t s)  | 
133  | 0  | { | 
134  |  |     //  | 
135  |  |     // Verify that the size, in bytes, of an array with n elements  | 
136  |  |     // of size s can be computed without overflowing:  | 
137  |  |     //  | 
138  |  |     // If computing  | 
139  |  |     //  | 
140  |  |     //      size_t (n) * s  | 
141  |  |     //  | 
142  |  |     // would overflow, then throw an IEX_NAMESPACE::OverflowExc exception.  | 
143  |  |     // Otherwise return  | 
144  |  |     //  | 
145  |  |     //      size_t (n).  | 
146  |  |     //  | 
147  |  | 
  | 
148  | 0  |     IMF_STATIC_ASSERT (!std::numeric_limits<T>::is_signed &&  | 
149  | 0  |                         std::numeric_limits<T>::is_integer);  | 
150  |  | 
  | 
151  | 0  |     IMF_STATIC_ASSERT (sizeof (T) <= sizeof (size_t));  | 
152  |  | 
  | 
153  | 0  |     if (size_t (n) > std::numeric_limits<size_t>::max() / s)  | 
154  | 0  |         throw IEX_NAMESPACE::OverflowExc ("Integer multiplication overflow."); | 
155  |  |  | 
156  | 0  |     return size_t (n);  | 
157  | 0  | } Unexecuted instantiation: unsigned long Imf_2_2::checkArraySize<unsigned int>(unsigned int, unsigned long) Unexecuted instantiation: unsigned long Imf_2_2::checkArraySize<unsigned long>(unsigned long, unsigned long)  | 
158  |  |  | 
159  |  |  | 
160  |  | OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT  | 
161  |  |  | 
162  |  |  | 
163  |  | #endif  |