OLD | NEW |
1 // Copyright 2005-2008 Google Inc. All Rights Reserved. | 1 // Copyright 2005-2008 Google Inc. All Rights Reserved. |
2 // Author: jrm@google.com (Jim Meehan) | 2 // Author: jrm@google.com (Jim Meehan) |
3 | 3 |
4 #include <google/protobuf/stubs/common.h> | 4 #include <google/protobuf/stubs/common.h> |
5 | 5 |
6 #include <google/protobuf/stubs/stringpiece.h> | 6 #include <google/protobuf/stubs/stringpiece.h> |
7 | 7 |
8 namespace google { | 8 namespace google { |
9 namespace protobuf { | 9 namespace protobuf { |
10 namespace internal { | 10 namespace internal { |
(...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 *bytes_consumed = src - isrc; | 504 *bytes_consumed = src - isrc; |
505 return exit_reason; | 505 return exit_reason; |
506 } | 506 } |
507 | 507 |
508 // Hack: On some compilers the static tables are initialized at startup. | 508 // Hack: On some compilers the static tables are initialized at startup. |
509 // We can't use them until they are initialized. However, some Protocol | 509 // We can't use them until they are initialized. However, some Protocol |
510 // Buffer parsing happens at static init time and may try to validate | 510 // Buffer parsing happens at static init time and may try to validate |
511 // UTF-8 strings. Since UTF-8 validation is only used for debugging | 511 // UTF-8 strings. Since UTF-8 validation is only used for debugging |
512 // anyway, we simply always return success if initialization hasn't | 512 // anyway, we simply always return success if initialization hasn't |
513 // occurred yet. | 513 // occurred yet. |
514 extern bool cr_module_initialized_; | 514 namespace { |
| 515 |
| 516 bool module_initialized_ = false; |
| 517 |
| 518 struct InitDetector { |
| 519 InitDetector() { |
| 520 module_initialized_ = true; |
| 521 } |
| 522 }; |
| 523 InitDetector init_detector; |
| 524 |
| 525 } // namespace |
515 | 526 |
516 bool IsStructurallyValidUTF8(const char* buf, int len) { | 527 bool IsStructurallyValidUTF8(const char* buf, int len) { |
517 if (!cr_module_initialized_) return true; | 528 if (!module_initialized_) return true; |
518 | 529 |
519 int bytes_consumed = 0; | 530 int bytes_consumed = 0; |
520 UTF8GenericScanFastAscii(&utf8acceptnonsurrogates_obj, | 531 UTF8GenericScanFastAscii(&utf8acceptnonsurrogates_obj, |
521 buf, len, &bytes_consumed); | 532 buf, len, &bytes_consumed); |
522 return (bytes_consumed == len); | 533 return (bytes_consumed == len); |
523 } | 534 } |
524 | 535 |
525 int UTF8SpnStructurallyValid(const StringPiece& str) { | 536 int UTF8SpnStructurallyValid(const StringPiece& str) { |
526 if (!cr_module_initialized_) return str.size(); | 537 if (!module_initialized_) return str.size(); |
527 | 538 |
528 int bytes_consumed = 0; | 539 int bytes_consumed = 0; |
529 UTF8GenericScanFastAscii(&utf8acceptnonsurrogates_obj, | 540 UTF8GenericScanFastAscii(&utf8acceptnonsurrogates_obj, |
530 str.data(), str.size(), &bytes_consumed); | 541 str.data(), str.size(), &bytes_consumed); |
531 return bytes_consumed; | 542 return bytes_consumed; |
532 } | 543 } |
533 | 544 |
534 // Coerce UTF-8 byte string in src_str to be | 545 // Coerce UTF-8 byte string in src_str to be |
535 // a structurally-valid equal-length string by selectively | 546 // a structurally-valid equal-length string by selectively |
536 // overwriting illegal bytes with replace_char (typically blank). | 547 // overwriting illegal bytes with replace_char (typically blank). |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 src += n; | 579 src += n; |
569 dst += n; | 580 dst += n; |
570 } | 581 } |
571 } | 582 } |
572 return idst; | 583 return idst; |
573 } | 584 } |
574 | 585 |
575 } // namespace internal | 586 } // namespace internal |
576 } // namespace protobuf | 587 } // namespace protobuf |
577 } // namespace google | 588 } // namespace google |
OLD | NEW |