Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "linker_utils.h" |
| 18 | #include "linker_debug.h" |
| 19 | |
| 20 | bool normalize_path(const char* path, std::string* normalized_path) { |
| 21 | // Input should be an absolute path |
| 22 | if (path[0] != '/') { |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 23 | PRINT("normalize_path - invalid input: '%s', the input path should be absolute", path); |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 24 | return false; |
| 25 | } |
| 26 | |
| 27 | const size_t len = strlen(path) + 1; |
| 28 | char buf[len]; |
| 29 | |
| 30 | const char* in_ptr = path; |
| 31 | char* out_ptr = buf; |
| 32 | |
| 33 | while (*in_ptr != 0) { |
| 34 | if (*in_ptr == '/') { |
| 35 | char c1 = in_ptr[1]; |
| 36 | if (c1 == '.') { |
| 37 | char c2 = in_ptr[2]; |
| 38 | if (c2 == '/') { |
| 39 | in_ptr += 2; |
| 40 | continue; |
| 41 | } else if (c2 == '.' && (in_ptr[3] == '/' || in_ptr[3] == 0)) { |
| 42 | in_ptr += 3; |
| 43 | while (out_ptr > buf && *--out_ptr != '/') { |
| 44 | } |
| 45 | if (in_ptr[0] == 0) { |
| 46 | // retain '/' |
| 47 | out_ptr++; |
| 48 | } |
| 49 | continue; |
| 50 | } |
| 51 | } else if (c1 == '/') { |
| 52 | ++in_ptr; |
| 53 | continue; |
| 54 | } |
| 55 | } |
| 56 | *out_ptr++ = *in_ptr++; |
| 57 | } |
| 58 | |
| 59 | *out_ptr = 0; |
| 60 | *normalized_path = buf; |
| 61 | return true; |
| 62 | } |
| 63 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 64 | bool file_is_in_dir(const std::string& file, const std::string& dir) { |
| 65 | const char* needle = dir.c_str(); |
| 66 | const char* haystack = file.c_str(); |
| 67 | size_t needle_len = strlen(needle); |
| 68 | |
| 69 | return (strncmp(haystack, needle, needle_len) == 0 && |
| 70 | haystack[needle_len] == '/' && |
| 71 | strchr(haystack + needle_len + 1, '/') == nullptr); |
| 72 | } |
| 73 | |
| 74 | const char* const kZipFileSeparator = "!/"; |
| 75 | |
| 76 | bool parse_zip_path(const char* input_path, std::string* zip_path, std::string* entry_path) { |
| 77 | std::string normalized_path; |
| 78 | if (!normalize_path(input_path, &normalized_path)) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | const char* const path = normalized_path.c_str(); |
| 83 | TRACE("Trying zip file open from path '%s' -> normalized '%s'", input_path, path); |
| 84 | |
| 85 | // Treat an '!/' separator inside a path as the separator between the name |
| 86 | // of the zip file on disk and the subdirectory to search within it. |
| 87 | // For example, if path is "foo.zip!/bar/bas/x.so", then we search for |
| 88 | // "bar/bas/x.so" within "foo.zip". |
| 89 | const char* const separator = strstr(path, kZipFileSeparator); |
| 90 | if (separator == nullptr) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | char buf[512]; |
| 95 | if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) { |
| 96 | PRINT("Warning: ignoring very long library path: %s", path); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | buf[separator - path] = '\0'; |
| 101 | |
| 102 | *zip_path = buf; |
| 103 | *entry_path = &buf[separator - path + 2]; |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
Dmitriy Ivanov | 84bab5a | 2015-11-20 13:34:11 -0800 | [diff] [blame] | 108 | constexpr off64_t kPageMask = ~static_cast<off64_t>(PAGE_SIZE-1); |
| 109 | |
| 110 | off64_t page_start(off64_t offset) { |
| 111 | return offset & kPageMask; |
| 112 | } |
| 113 | |
| 114 | bool safe_add(off64_t* out, off64_t a, size_t b) { |
| 115 | CHECK(a >= 0); |
| 116 | if (static_cast<uint64_t>(INT64_MAX - a) < b) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | *out = a + b; |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | size_t page_offset(off64_t offset) { |
| 125 | return static_cast<size_t>(offset & (PAGE_SIZE-1)); |
| 126 | } |
| 127 | |