1 /* 2 * Copyright (C) 2024 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 MEDIAPROVIDER_PDF_JNI_PDFCLIENT_UTF8_H 18 #define MEDIAPROVIDER_PDF_JNI_PDFCLIENT_UTF8_H 19 20 #include <iterator> 21 22 namespace pdfClient { 23 namespace utf8 { 24 25 const uint16_t LEAD_SURROGATE_MIN = 0xd800u; 26 const uint16_t LEAD_SURROGATE_MAX = 0xdbffu; 27 const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u; 28 const uint16_t LEAD_OFFSET = LEAD_SURROGATE_MIN - (0x10000 >> 10); 29 const uint32_t SURROGATE_OFFSET = 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN; 30 31 template <typename octet_type> mask8(octet_type oc)32inline uint8_t mask8(octet_type oc) { 33 return static_cast<uint8_t>(0xff & oc); 34 } 35 36 template <typename u16_type> mask16(u16_type oc)37inline uint16_t mask16(u16_type oc) { 38 return static_cast<uint16_t>(0xffff & oc); 39 } 40 41 template <typename u16> is_lead_surrogate(u16 cp)42inline bool is_lead_surrogate(u16 cp) { 43 return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX); 44 } 45 46 template <typename octet_iterator> sequence_length(octet_iterator lead_it)47inline typename std::iterator_traits<octet_iterator>::difference_type sequence_length( 48 octet_iterator lead_it) { 49 uint8_t lead = mask8(*lead_it); 50 if (lead < 0x80) 51 return 1; 52 else if ((lead >> 5) == 0x6) 53 return 2; 54 else if ((lead >> 4) == 0xe) 55 return 3; 56 else if ((lead >> 3) == 0x1e) 57 return 4; 58 else 59 return 0; 60 } 61 62 } // namespace utf8 63 } // namespace pdfClient 64 65 #endif // header guard