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