15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file. 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This implementation doesn't use ICU. The ICU macros are oriented towards 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// character-at-a-time processing, whereas byte-at-a-time processing is easier 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// with streaming input. 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/i18n/streaming_utf8_validator.h" 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/i18n/utf8_validator_tables.h" 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/logging.h" 135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace base { 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace { 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)uint8 StateTableLookup(uint8 offset) { 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) DCHECK_LT(offset, internal::kUtf8ValidatorTablesSize); 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return internal::kUtf8ValidatorTables[offset]; 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)StreamingUtf8Validator::State StreamingUtf8Validator::AddBytes(const char* data, 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) size_t size) { 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Copy |state_| into a local variable so that the compiler doesn't have to be 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // careful of aliasing. 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) uint8 state = state_; 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) for (const char* p = data; p != data + size; ++p) { 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if ((*p & 0x80) == 0) { 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (state == 0) 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) continue; 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) state = internal::I18N_UTF8_VALIDATOR_INVALID_INDEX; 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) break; 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const uint8 shift_amount = StateTableLookup(state); 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const uint8 shifted_char = (*p & 0x7F) >> shift_amount; 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) state = StateTableLookup(state + shifted_char + 1); 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // State may be INVALID here, but this code is optimised for the case of 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // valid UTF-8 and it is more efficient (by about 2%) to not attempt an 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // early loop exit unless we hit an ASCII character. 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) state_ = state; 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return state == 0 ? VALID_ENDPOINT 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) : state == internal::I18N_UTF8_VALIDATOR_INVALID_INDEX 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ? INVALID 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) : VALID_MIDPOINT; 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void StreamingUtf8Validator::Reset() { 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) state_ = 0u; 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool StreamingUtf8Validator::Validate(const std::string& string) { 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return StreamingUtf8Validator().AddBytes(string.data(), string.size()) == 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) VALID_ENDPOINT; 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace base 60