Narayan Kamath | 044bc8e | 2014-12-03 18:22:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #ifndef LIBZIPARCHIVE_ENTRY_NAME_UTILS_INL_H_ |
| 18 | #define LIBZIPARCHIVE_ENTRY_NAME_UTILS_INL_H_ |
| 19 | |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | |
| 23 | // Check if |length| bytes at |entry_name| constitute a valid entry name. |
| 24 | // Entry names must be valid UTF-8 and must not contain '0'. |
| 25 | inline bool IsValidEntryName(const uint8_t* entry_name, const size_t length) { |
| 26 | for (size_t i = 0; i < length; ++i) { |
| 27 | const uint8_t byte = entry_name[i]; |
| 28 | if (byte == 0) { |
| 29 | return false; |
| 30 | } else if ((byte & 0x80) == 0) { |
| 31 | // Single byte sequence. |
| 32 | continue; |
| 33 | } else if ((byte & 0xc0) == 0x80 || (byte & 0xfe) == 0xfe) { |
| 34 | // Invalid sequence. |
| 35 | return false; |
| 36 | } else { |
| 37 | // 2-5 byte sequences. |
| 38 | for (uint8_t first = byte << 1; first & 0x80; first <<= 1) { |
| 39 | ++i; |
| 40 | |
| 41 | // Missing continuation byte.. |
| 42 | if (i == length) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | // Invalid continuation byte. |
| 47 | const uint8_t continuation_byte = entry_name[i]; |
| 48 | if ((continuation_byte & 0xc0) != 0x80) { |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
Narayan Kamath | 044bc8e | 2014-12-03 18:22:53 +0000 | [diff] [blame] | 58 | #endif // LIBZIPARCHIVE_ENTRY_NAME_UTILS_INL_H_ |