streaming_utf8_validator.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // A streaming validator for UTF-8. Validation is based on the definition in
  5. // RFC-3629. In particular, it does not reject the invalid characters rejected
  6. // by base::IsStringUTF8().
  7. //
  8. // The implementation detects errors on the first possible byte.
  9. #ifndef BASE_I18N_STREAMING_UTF8_VALIDATOR_H_
  10. #define BASE_I18N_STREAMING_UTF8_VALIDATOR_H_
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include <string>
  14. #include "base/i18n/base_i18n_export.h"
  15. namespace base {
  16. class BASE_I18N_EXPORT StreamingUtf8Validator {
  17. public:
  18. // The validator exposes 3 states. It starts in state VALID_ENDPOINT. As it
  19. // processes characters it alternates between VALID_ENDPOINT and
  20. // VALID_MIDPOINT. If it encounters an invalid byte or UTF-8 sequence the
  21. // state changes permanently to INVALID.
  22. enum State {
  23. VALID_ENDPOINT,
  24. VALID_MIDPOINT,
  25. INVALID
  26. };
  27. StreamingUtf8Validator() : state_(0u) {}
  28. // This type could be made copyable but there is currently no use-case for
  29. // it.
  30. StreamingUtf8Validator(const StreamingUtf8Validator&) = delete;
  31. StreamingUtf8Validator& operator=(const StreamingUtf8Validator&) = delete;
  32. // Trivial destructor intentionally omitted.
  33. // Validate |size| bytes starting at |data|. If the concatenation of all calls
  34. // to AddBytes() since this object was constructed or reset is a valid UTF-8
  35. // string, returns VALID_ENDPOINT. If it could be the prefix of a valid UTF-8
  36. // string, returns VALID_MIDPOINT. If an invalid byte or UTF-8 sequence was
  37. // present, returns INVALID.
  38. State AddBytes(const char* data, size_t size);
  39. // Return the object to a freshly-constructed state so that it can be re-used.
  40. void Reset();
  41. // Validate a complete string using the same criteria. Returns true if the
  42. // string only contains complete, valid UTF-8 codepoints.
  43. static bool Validate(const std::string& string);
  44. private:
  45. // The current state of the validator. Value 0 is the initial/valid state.
  46. // The state is stored as an offset into |kUtf8ValidatorTables|. The special
  47. // state |kUtf8InvalidState| is invalid.
  48. uint8_t state_;
  49. };
  50. } // namespace base
  51. #endif // BASE_I18N_STREAMING_UTF8_VALIDATOR_H_