toolkit/crashreporter/google-breakpad/src/common/mac/file_id.cc

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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.
    30 // file_id.cc: Return a unique identifier for a file
    31 //
    32 // See file_id.h for documentation
    33 //
    34 // Author: Dan Waylonis
    36 #include <fcntl.h>
    37 #include <string.h>
    38 #include <unistd.h>
    40 #include "common/mac/file_id.h"
    41 #include "common/mac/macho_id.h"
    43 using MacFileUtilities::MachoID;
    45 namespace google_breakpad {
    47 FileID::FileID(const char *path) {
    48   strlcpy(path_, path, sizeof(path_));
    49 }
    51 bool FileID::FileIdentifier(unsigned char identifier[16]) {
    52   int fd = open(path_, O_RDONLY);
    53   if (fd == -1)
    54     return false;
    56   MD5Context md5;
    57   MD5Init(&md5);
    59   // Read 4k x 2 bytes at a time.  This is faster than just 4k bytes, but
    60   // doesn't seem to be an unreasonable size for the stack.
    61   unsigned char buffer[4096 * 2];
    62   size_t buffer_size = sizeof(buffer);
    63   while ((buffer_size = read(fd, buffer, buffer_size) > 0)) {
    64     MD5Update(&md5, buffer, buffer_size);
    65   }
    67   close(fd);
    68   MD5Final(identifier, &md5);
    70   return true;
    71 }
    73 bool FileID::MachoIdentifier(cpu_type_t cpu_type,
    74                              cpu_subtype_t cpu_subtype,
    75                              unsigned char identifier[16]) {
    76   MachoID macho(path_);
    78   if (macho.UUIDCommand(cpu_type, cpu_subtype, identifier))
    79     return true;
    81   return macho.MD5(cpu_type, cpu_subtype, identifier);
    82 }
    84 // static
    85 void FileID::ConvertIdentifierToString(const unsigned char identifier[16],
    86                                        char *buffer, int buffer_length) {
    87   int buffer_idx = 0;
    88   for (int idx = 0; (buffer_idx < buffer_length) && (idx < 16); ++idx) {
    89     int hi = (identifier[idx] >> 4) & 0x0F;
    90     int lo = (identifier[idx]) & 0x0F;
    92     if (idx == 4 || idx == 6 || idx == 8 || idx == 10)
    93       buffer[buffer_idx++] = '-';
    95     buffer[buffer_idx++] =
    96         static_cast<char>((hi >= 10) ? ('A' + hi - 10) : ('0' + hi));
    97     buffer[buffer_idx++] =
    98         static_cast<char>((lo >= 10) ? ('A' + lo - 10) : ('0' + lo));
    99   }
   101   // NULL terminate
   102   buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0;
   103 }
   105 }  // namespace google_breakpad

mercurial