| 1 | // Copyright (c) 2006, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | // file_id.cc: Return a unique identifier for a file |
| 31 | // |
| 32 | // See file_id.h for documentation |
| 33 | // |
| 34 | // Author: Dan Waylonis |
| 35 | |
| 36 | #include "common/mac/file_id.h" |
| 37 | |
| 38 | #include <fcntl.h> |
| 39 | #include <stdio.h> |
| 40 | #include <string.h> |
| 41 | |
| 42 | #include "common/mac/macho_id.h" |
| 43 | #include "common/scoped_ptr.h" |
| 44 | |
| 45 | using MacFileUtilities::MachoID; |
| 46 | |
| 47 | namespace google_breakpad { |
| 48 | namespace mach_o { |
| 49 | // Constructs a FileID given a path to a file |
| 50 | FileID::FileID(const char* path) : memory_(nullptr), size_(0) { |
| 51 | snprintf(path_, sizeof(path_), "%s" , path); |
| 52 | } |
| 53 | |
| 54 | // Constructs a FileID given the contents of a file and its size |
| 55 | FileID::FileID(void* memory, size_t size) |
| 56 | : path_(), memory_(memory), size_(size) {} |
| 57 | |
| 58 | bool FileID::MachoIdentifier(cpu_type_t cpu_type, |
| 59 | cpu_subtype_t cpu_subtype, |
| 60 | unsigned char identifier[16]) { |
| 61 | scoped_ptr<MachoID> macho; |
| 62 | if (memory_) { |
| 63 | macho.reset(new MachoID(memory_, size_)); |
| 64 | } else { |
| 65 | macho.reset(new MachoID(path_)); |
| 66 | } |
| 67 | if (macho->UUIDCommand(cpu_type, cpu_subtype, identifier)) |
| 68 | return true; |
| 69 | |
| 70 | return macho->MD5(cpu_type, cpu_subtype, identifier); |
| 71 | } |
| 72 | |
| 73 | // static |
| 74 | void FileID::ConvertIdentifierToString(const unsigned char identifier[16], |
| 75 | char *buffer, int buffer_length) { |
| 76 | int buffer_idx = 0; |
| 77 | for (int idx = 0; (buffer_idx < buffer_length) && (idx < 16); ++idx) { |
| 78 | int hi = (identifier[idx] >> 4) & 0x0F; |
| 79 | int lo = (identifier[idx]) & 0x0F; |
| 80 | |
| 81 | if (idx == 4 || idx == 6 || idx == 8 || idx == 10) |
| 82 | buffer[buffer_idx++] = '-'; |
| 83 | |
| 84 | buffer[buffer_idx++] = |
| 85 | static_cast<char>((hi >= 10) ? ('A' + hi - 10) : ('0' + hi)); |
| 86 | buffer[buffer_idx++] = |
| 87 | static_cast<char>((lo >= 10) ? ('A' + lo - 10) : ('0' + lo)); |
| 88 | } |
| 89 | |
| 90 | // NULL terminate |
| 91 | buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0; |
| 92 | } |
| 93 | |
| 94 | } // namespace mach_o |
| 95 | } // namespace google_breakpad |
| 96 | |