• 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/stubs/strutil.h>
34 #include <google/protobuf/stubs/status_macros.h>
35 
36 namespace google {
37 namespace protobuf {
38 namespace util {
39 namespace converter {
40 
41 namespace {
CallPathSink(PathSinkCallback path_sink,StringPiece arg)42 inline util::Status CallPathSink(PathSinkCallback path_sink,
43                                    StringPiece arg) {
44   return path_sink->Run(arg);
45 }
46 
CreatePublicError(util::error::Code code,const string & message)47 util::Status CreatePublicError(util::error::Code code,
48                                  const string& message) {
49   return util::Status(code, message);
50 }
51 
52 // Appends a FieldMask path segment to a prefix.
AppendPathSegmentToPrefix(StringPiece prefix,StringPiece segment)53 string AppendPathSegmentToPrefix(StringPiece prefix, StringPiece segment) {
54   if (prefix.empty()) {
55     return segment.ToString();
56   }
57   if (segment.empty()) {
58     return prefix.ToString();
59   }
60   // If the segment is a map key, appends it to the prefix without the ".".
61   if (segment.starts_with("[\"")) {
62     return StrCat(prefix, segment);
63   }
64   return StrCat(prefix, ".", segment);
65 }
66 
67 }  // namespace
68 
ConvertFieldMaskPath(const StringPiece path,ConverterCallback converter)69 string ConvertFieldMaskPath(const StringPiece path,
70                             ConverterCallback converter) {
71   string result;
72   result.reserve(path.size() << 1);
73 
74   bool is_quoted = false;
75   bool is_escaping = false;
76   int current_segment_start = 0;
77 
78   // Loops until 1 passed the end of the input to make handling the last
79   // segment easier.
80   for (size_t i = 0; i <= path.size(); ++i) {
81     // Outputs quoted string as-is.
82     if (is_quoted) {
83       if (i == path.size()) {
84         break;
85       }
86       result.push_back(path[i]);
87       if (is_escaping) {
88         is_escaping = false;
89       } else if (path[i] == '\\') {
90         is_escaping = true;
91       } else if (path[i] == '\"') {
92         current_segment_start = i + 1;
93         is_quoted = false;
94       }
95       continue;
96     }
97     if (i == path.size() || path[i] == '.' || path[i] == '(' ||
98         path[i] == ')' || path[i] == '\"') {
99       result += converter(
100           path.substr(current_segment_start, i - current_segment_start));
101       if (i < path.size()) {
102         result.push_back(path[i]);
103       }
104       current_segment_start = i + 1;
105     }
106     if (i < path.size() && path[i] == '\"') {
107       is_quoted = true;
108     }
109   }
110   return result;
111 }
112 
DecodeCompactFieldMaskPaths(StringPiece paths,PathSinkCallback path_sink)113 util::Status DecodeCompactFieldMaskPaths(StringPiece paths,
114                                            PathSinkCallback path_sink) {
115   stack<string> prefix;
116   int length = paths.length();
117   int previous_position = 0;
118   bool in_map_key = false;
119   bool is_escaping = false;
120   // Loops until 1 passed the end of the input to make the handle of the last
121   // segment easier.
122   for (int i = 0; i <= length; ++i) {
123     if (i != length) {
124       // Skips everything in a map key until we hit the end of it, which is
125       // marked by an un-escaped '"' immediately followed by a ']'.
126       if (in_map_key) {
127         if (is_escaping) {
128           is_escaping = false;
129           continue;
130         }
131         if (paths[i] == '\\') {
132           is_escaping = true;
133           continue;
134         }
135         if (paths[i] != '\"') {
136           continue;
137         }
138         // Un-escaped '"' must be followed with a ']'.
139         if (i >= length - 1 || paths[i + 1] != ']') {
140           return util::Status(
141               util::error::INVALID_ARGUMENT,
142               StrCat("Invalid FieldMask '", paths,
143                      "'. Map keys should be represented as [\"some_key\"]."));
144         }
145         // The end of the map key ("\"]") has been found.
146         in_map_key = false;
147         // Skips ']'.
148         i++;
149         // Checks whether the key ends at the end of a path segment.
150         if (i < length - 1 && paths[i + 1] != '.' && paths[i + 1] != ',' &&
151             paths[i + 1] != ')' && paths[i + 1] != '(') {
152           return util::Status(
153               util::error::INVALID_ARGUMENT,
154               StrCat("Invalid FieldMask '", paths,
155                      "'. Map keys should be at the end of a path segment."));
156         }
157         is_escaping = false;
158         continue;
159       }
160 
161       // We are not in a map key, look for the start of one.
162       if (paths[i] == '[') {
163         if (i >= length - 1 || paths[i + 1] != '\"') {
164           return util::Status(
165               util::error::INVALID_ARGUMENT,
166               StrCat("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     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 charactor 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(CallPathSink(
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(util::error::INVALID_ARGUMENT,
211                           StrCat("Invalid FieldMask '", paths,
212                                  "'. Cannot find matching ']' for all '['."));
213   }
214   if (!prefix.empty()) {
215     return util::Status(util::error::INVALID_ARGUMENT,
216                           StrCat("Invalid FieldMask '", paths,
217                                  "'. Cannot find matching ')' for all '('."));
218   }
219   return util::Status::OK;
220 }
221 
222 }  // namespace converter
223 }  // namespace util
224 }  // namespace protobuf
225 }  // namespace google
226