/ src / common / mac / super_fat_arch.h
super_fat_arch.h
 1  // Copyright 2015 Google LLC
 2  //
 3  // Redistribution and use in source and binary forms, with or without
 4  // modification, are permitted provided that the following conditions are
 5  // met:
 6  //
 7  //     * Redistributions of source code must retain the above copyright
 8  // notice, this list of conditions and the following disclaimer.
 9  //     * Redistributions in binary form must reproduce the above
10  // copyright notice, this list of conditions and the following disclaimer
11  // in the documentation and/or other materials provided with the
12  // distribution.
13  //     * Neither the name of Google LLC nor the names of its
14  // contributors may be used to endorse or promote products derived from
15  // this software without specific prior written permission.
16  //
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  
29  // Original author: Erik Chen <erikchen@chromium.org>
30  
31  // super_fat_arch.h: A class to handle 64-bit object files. Has conversions to
32  // and from struct fat_arch.
33  
34  #ifndef BREAKPAD_COMMON_MAC_SUPER_FAT_ARCH_H_
35  #define BREAKPAD_COMMON_MAC_SUPER_FAT_ARCH_H_
36  
37  #include <limits>
38  #include <mach-o/fat.h>
39  #include <stdint.h>
40  
41  // Similar to struct fat_arch, except size-related parameters support
42  // 64-bits.
43  class SuperFatArch {
44   public:
45    uint32_t cputype;
46    uint32_t cpusubtype;
47    uint64_t offset;
48    uint64_t size;
49    uint64_t align;
50  
51    SuperFatArch() :
52        cputype(0),
53        cpusubtype(0),
54        offset(0),
55        size(0),
56        align(0) {
57    }
58  
59    explicit SuperFatArch(const struct fat_arch& arch) :
60        cputype(arch.cputype),
61        cpusubtype(arch.cpusubtype),
62        offset(arch.offset),
63        size(arch.size),
64        align(arch.align) {
65    }
66  
67    // Returns false if the conversion cannot be made.
68    // If the conversion succeeds, the result is placed in |output_arch|.
69    bool ConvertToFatArch(struct fat_arch* output_arch) const {
70      if (offset > std::numeric_limits<uint32_t>::max())
71        return false;
72      if (size > std::numeric_limits<uint32_t>::max())
73        return false;
74      if (align > std::numeric_limits<uint32_t>::max())
75        return false;
76      struct fat_arch arch;
77      arch.cputype = cputype;
78      arch.cpusubtype = cpusubtype;
79      arch.offset = offset;
80      arch.size = size;
81      arch.align = align;
82      *output_arch = arch;
83      return true;
84    }
85  };
86  
87  #endif  // BREAKPAD_COMMON_MAC_SUPER_FAT_ARCH_H_