• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/util/internal/field_mask_utility.h>
32 
33 #include <google/protobuf/util/internal/utility.h>
34 #include <google/protobuf/stubs/status.h>
35 #include <google/protobuf/stubs/strutil.h>
36 #include <google/protobuf/stubs/status_macros.h>
37 
38 // Must be included last.
39 #include <google/protobuf/port_def.inc>
40 
41 namespace google {
42 namespace protobuf {
43 namespace util {
44 namespace converter {
45 
46 namespace {
47 
48 // Appends a FieldMask path segment to a prefix.
AppendPathSegmentToPrefix(StringPiece prefix,StringPiece segment)49 std::string AppendPathSegmentToPrefix(StringPiece prefix,
50                                       StringPiece segment) {
51   if (prefix.empty()) {
52     return std::string(segment);
53   }
54   if (segment.empty()) {
55     return std::string(prefix);
56   }
57   // If the segment is a map key, appends it to the prefix without the ".".
58   if (HasPrefixString(segment, "[\"")) {
59     return StrCat(prefix, segment);
60   }
61   return StrCat(prefix, ".", segment);
62 }
63 
64 }  // namespace
65 
ConvertFieldMaskPath(const StringPiece path,ConverterCallback converter)66 std::string ConvertFieldMaskPath(const StringPiece path,
67                                  ConverterCallback converter) {
68   std::string result;
69   result.reserve(path.size() << 1);
70 
71   bool is_quoted = false;
72   bool is_escaping = false;
73   int current_segment_start = 0;
74 
75   // Loops until 1 passed the end of the input to make handling the last
76   // segment easier.
77   for (size_t i = 0; i <= path.size(); ++i) {
78     // Outputs quoted string as-is.
79     if (is_quoted) {
80       if (i == path.size()) {
81         break;
82       }
83       result.push_back(path[i]);
84       if (is_escaping) {
85         is_escaping = false;
86       } else if (path[i] == '\\') {
87         is_escaping = true;
88       } else if (path[i] == '\"') {
89         current_segment_start = i + 1;
90         is_quoted = false;
91       }
92       continue;
93     }
94     if (i == path.size() || path[i] == '.' || path[i] == '(' ||
95         path[i] == ')' || path[i] == '\"') {
96       result += converter(
97           path.substr(current_segment_start, i - current_segment_start));
98       if (i < path.size()) {
99         result.push_back(path[i]);
100       }
101       current_segment_start = i + 1;
102     }
103     if (i < path.size() && path[i] == '\"') {
104       is_quoted = true;
105     }
106   }
107   return result;
108 }
109 
DecodeCompactFieldMaskPaths(StringPiece paths,PathSinkCallback path_sink)110 util::Status DecodeCompactFieldMaskPaths(StringPiece paths,
111                                          PathSinkCallback path_sink) {
112   std::stack<std::string> prefix;
113   int length = paths.length();
114   int previous_position = 0;
115   bool in_map_key = false;
116   bool is_escaping = false;
117   // Loops until 1 passed the end of the input to make the handle of the last
118   // segment easier.
119   for (int i = 0; i <= length; ++i) {
120     if (i != length) {
121       // Skips everything in a map key until we hit the end of it, which is
122       // marked by an un-escaped '"' immediately followed by a ']'.
123       if (in_map_key) {
124         if (is_escaping) {
125           is_escaping = false;
126           continue;
127         }
128         if (paths[i] == '\\') {
129           is_escaping = true;
130           continue;
131         }
132         if (paths[i] != '\"') {
133           continue;
134         }
135         // Un-escaped '"' must be followed with a ']'.
136         if (i >= length - 1 || paths[i + 1] != ']') {
137           return util::Status(
138               util::error::INVALID_ARGUMENT,
139               StrCat(
140                   "Invalid FieldMask '", paths,
141                   "'. Map keys should be represented as [\"some_key\"]."));
142         }
143         // The end of the map key ("\"]") has been found.
144         in_map_key = false;
145         // Skips ']'.
146         i++;
147         // Checks whether the key ends at the end of a path segment.
148         if (i < length - 1 && paths[i + 1] != '.' && paths[i + 1] != ',' &&
149             paths[i + 1] != ')' && paths[i + 1] != '(') {
150           return util::Status(
151               util::error::INVALID_ARGUMENT,
152               StrCat(
153                   "Invalid FieldMask '", paths,
154                   "'. Map keys should be at the end of a path segment."));
155         }
156         is_escaping = false;
157         continue;
158       }
159 
160       // We are not in a map key, look for the start of one.
161       if (paths[i] == '[') {
162         if (i >= length - 1 || paths[i + 1] != '\"') {
163           return util::Status(
164               util::error::INVALID_ARGUMENT,
165               StrCat(
166                   "Invalid FieldMask '", paths,
167                   "'. Map keys should be represented as [\"some_key\"]."));
168         }
169         // "[\"" starts a map key.
170         in_map_key = true;
171         i++;  // Skips the '\"'.
172         continue;
173       }
174       // If the current character is not a special character (',', '(' or ')'),
175       // continue to the next.
176       if (paths[i] != ',' && paths[i] != ')' && paths[i] != '(') {
177         continue;
178       }
179     }
180     // Gets the current segment - sub-string between previous position (after
181     // '(', ')', ',', or the beginning of the input) and the current position.
182     StringPiece segment =
183         paths.substr(previous_position, i - previous_position);
184     std::string current_prefix = prefix.empty() ? "" : prefix.top();
185 
186     if (i < length && paths[i] == '(') {
187       // Builds a prefix and save it into the stack.
188       prefix.push(AppendPathSegmentToPrefix(current_prefix, segment));
189     } else if (!segment.empty()) {
190       // When the current character is ')', ',' or the current position has
191       // passed the end of the input, builds and outputs a new paths by
192       // concatenating the last prefix with the current segment.
193       RETURN_IF_ERROR(
194           path_sink(AppendPathSegmentToPrefix(current_prefix, segment)));
195     }
196 
197     // Removes the last prefix after seeing a ')'.
198     if (i < length && paths[i] == ')') {
199       if (prefix.empty()) {
200         return util::Status(
201             util::error::INVALID_ARGUMENT,
202             StrCat("Invalid FieldMask '", paths,
203                          "'. Cannot find matching '(' for all ')'."));
204       }
205       prefix.pop();
206     }
207     previous_position = i + 1;
208   }
209   if (in_map_key) {
210     return util::Status(
211         util::error::INVALID_ARGUMENT,
212         StrCat("Invalid FieldMask '", paths,
213                      "'. Cannot find matching ']' for all '['."));
214   }
215   if (!prefix.empty()) {
216     return util::Status(
217         util::error::INVALID_ARGUMENT,
218         StrCat("Invalid FieldMask '", paths,
219                      "'. Cannot find matching ')' for all '('."));
220   }
221   return util::Status();
222 }
223 
224 }  // namespace converter
225 }  // namespace util
226 }  // namespace protobuf
227 }  // namespace google
228