Upx_Doxygen
https://github.com/upx/upx
bptr.h
1 /* bptr.h --
2 
3  This file is part of the UPX executable compressor.
4 
5  Copyright (C) 1996-2016 Markus Franz Xaver Johannes Oberhumer
6  Copyright (C) 1996-2016 Laszlo Molnar
7  All Rights Reserved.
8 
9  UPX and the UCL library are free software; you can redistribute them
10  and/or modify them under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of
12  the License, or (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; see the file COPYING.
21  If not, write to the Free Software Foundation, Inc.,
22  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24  Markus F.X.J. Oberhumer Laszlo Molnar
25  <markus@oberhumer.com> <ezerotven+github@gmail.com>
26  */
27 
28 
29 #ifndef __UPX_BPTR_H
30 #define __UPX_BPTR_H 1
31 
32 
33 /*************************************************************************
34 // BoundedPtr
35 **************************************************************************/
36 
37 template <class T>
39 {
40 public:
41  //typedef T* StoredType;
42  //typedef T* PointerType;
43  //typedef T& ReferenceType;
44 
45  ~BoundedPtr() { }
46 
47  explicit BoundedPtr(void* base, size_t size_in_bytes, T* ptr=0)
48  : ptr_(ptr), base_(base), size_in_bytes_(0)
49  {
50  assert(base_ != NULL);
51  size_in_bytes_ = mem_size(1, size_in_bytes);
52  check();
53  }
54 
55  // assignment
56  BoundedPtr& operator= (const BoundedPtr& other) {
57  assert(base_ == other.base_);
58  assert(size_in_bytes_ == other.size_in_bytes_);
59  ptr_ = other.ptr_; check(); return *this;
60  }
61  BoundedPtr& operator= (T* other) {
62  ptr_ = other; check(); return *this;
63  }
64 
65  operator T* () { return ptr_; }
66  operator const T* () const { return ptr_; }
67 
68  BoundedPtr& operator += (size_t n) {
69  checkNULL(); ptr_ += n; checkRange(); return *this;
70  }
71  BoundedPtr& operator -= (size_t n) {
72  checkNULL(); ptr_ -= n; checkRange(); return *this;
73  }
74  BoundedPtr& operator ++ (void) {
75  checkNULL(); ptr_ += 1; checkRange(); return *this;
76  }
77 
78 private:
79  void checkNULL() const {
80  if __acc_very_unlikely(!ptr_)
81  throwCantUnpack("unexpected NULL pointer; take care!");
82  }
83  void checkRange() const {
84  size_t off = (const char *) ptr_ - (const char *) base_;
85  if __acc_very_unlikely(off > size_in_bytes_)
86  throwCantUnpack("pointer out of range; take care!");
87  }
88  void check() const { // check ptr_ invariant: either NULL or valid checkRange()
89  if (ptr_ != NULL)
90  checkRange();
91  }
92 
93  T* ptr_;
94  void* base_;
95  size_t size_in_bytes_;
96 
97  // disable copy
98  BoundedPtr(const BoundedPtr&); // {}
99  // disable dynamic allocation
100  DISABLE_NEW_DELETE
101 };
102 
103 
104 #endif /* already included */
105 
106 /* vim:set ts=4 sw=4 et: */
Definition: bptr.h:38