| 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | #include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
| 32 | #include <google/protobuf/stubs/strutil.h> |
| 33 | #include <google/protobuf/any.h> |
| 34 | #include <google/protobuf/arenastring.h> |
| 35 | #include <google/protobuf/generated_message_util.h> |
| 36 | |
| 37 | namespace google { |
| 38 | namespace protobuf { |
| 39 | namespace internal { |
| 40 | |
| 41 | std::string GetTypeUrl(StringPiece message_name, |
| 42 | StringPiece type_url_prefix) { |
| 43 | if (!type_url_prefix.empty() && |
| 44 | type_url_prefix[type_url_prefix.size() - 1] == '/') { |
| 45 | return StrCat(a: type_url_prefix, b: message_name); |
| 46 | } else { |
| 47 | return StrCat(a: type_url_prefix, b: "/" , c: message_name); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | const char kAnyFullTypeName[] = "google.protobuf.Any" ; |
| 52 | const char kTypeGoogleApisComPrefix[] = "type.googleapis.com/" ; |
| 53 | const char kTypeGoogleProdComPrefix[] = "type.googleprod.com/" ; |
| 54 | |
| 55 | bool AnyMetadata::InternalPackFrom(Arena* arena, const MessageLite& message, |
| 56 | StringPiece type_url_prefix, |
| 57 | StringPiece type_name) { |
| 58 | type_url_->Set(value: GetTypeUrl(message_name: type_name, type_url_prefix), arena); |
| 59 | return message.SerializeToString(output: value_->Mutable(arena)); |
| 60 | } |
| 61 | |
| 62 | bool AnyMetadata::InternalUnpackTo(StringPiece type_name, |
| 63 | MessageLite* message) const { |
| 64 | if (!InternalIs(type_name)) { |
| 65 | return false; |
| 66 | } |
| 67 | return message->ParseFromString(data: value_->Get()); |
| 68 | } |
| 69 | |
| 70 | bool AnyMetadata::InternalIs(StringPiece type_name) const { |
| 71 | StringPiece type_url = type_url_->Get(); |
| 72 | return type_url.size() >= type_name.size() + 1 && |
| 73 | type_url[type_url.size() - type_name.size() - 1] == '/' && |
| 74 | HasSuffixString(str: type_url, suffix: type_name); |
| 75 | } |
| 76 | |
| 77 | bool ParseAnyTypeUrl(StringPiece type_url, std::string* url_prefix, |
| 78 | std::string* full_type_name) { |
| 79 | size_t pos = type_url.find_last_of(c: '/'); |
| 80 | if (pos == std::string::npos || pos + 1 == type_url.size()) { |
| 81 | return false; |
| 82 | } |
| 83 | if (url_prefix) { |
| 84 | *url_prefix = std::string(type_url.substr(pos: 0, n: pos + 1)); |
| 85 | } |
| 86 | *full_type_name = std::string(type_url.substr(pos: pos + 1)); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool ParseAnyTypeUrl(StringPiece type_url, std::string* full_type_name) { |
| 91 | return ParseAnyTypeUrl(type_url, url_prefix: nullptr, full_type_name); |
| 92 | } |
| 93 | |
| 94 | } // namespace internal |
| 95 | } // namespace protobuf |
| 96 | } // namespace google |
| 97 | |