names.c
Go to the documentation of this file.
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 "names.h"
32 
33 #include <stdlib.h>
34 
35 #include "protobuf.h"
36 
37 /* stringsink *****************************************************************/
38 
39 typedef struct {
40  char *ptr;
41  size_t len, size;
42 } stringsink;
43 
44 static size_t stringsink_string(stringsink *sink, const char *ptr, size_t len) {
45  size_t new_size = sink->size;
46 
47  while (sink->len + len > new_size) {
48  new_size *= 2;
49  }
50 
51  if (new_size != sink->size) {
52  sink->ptr = realloc(sink->ptr, new_size);
53  sink->size = new_size;
54  }
55 
56  memcpy(sink->ptr + sink->len, ptr, len);
57  sink->len += len;
58 
59  return len;
60 }
61 
63  sink->size = 32;
64  sink->ptr = malloc(sink->size);
65  PBPHP_ASSERT(sink->ptr != NULL);
66  sink->len = 0;
67 }
68 
69 static void stringsink_uninit(stringsink *sink) { free(sink->ptr); }
70 
71 /* def name -> classname ******************************************************/
72 
73 const char *const kReservedNames[] = {
74  "abstract", "and", "array", "as", "break",
75  "callable", "case", "catch", "class", "clone",
76  "const", "continue", "declare", "default", "die",
77  "do", "echo", "else", "elseif", "empty",
78  "enddeclare", "endfor", "endforeach", "endif", "endswitch",
79  "endwhile", "eval", "exit", "extends", "final",
80  "finally", "fn", "for", "foreach", "function",
81  "if", "implements", "include", "include_once", "instanceof",
82  "global", "goto", "insteadof", "interface", "isset",
83  "list", "match", "namespace", "new", "object",
84  "or", "print", "private", "protected", "public",
85  "require", "require_once", "return", "static", "switch",
86  "throw", "trait", "try", "unset", "use",
87  "var", "while", "xor", "yield", "int",
88  "float", "bool", "string", "true", "false",
89  "null", "void", "iterable", NULL};
90 
91 bool is_reserved_name(const char* name) {
92  int i;
93  for (i = 0; kReservedNames[i]; i++) {
94  if (strcmp(kReservedNames[i], name) == 0) {
95  return true;
96  }
97  }
98  return false;
99 }
100 
101 static char nolocale_tolower(char ch) {
102  if (ch >= 'A' && ch <= 'Z') {
103  return ch - ('A' - 'a');
104  } else {
105  return ch;
106  }
107 }
108 
109 static char nolocale_toupper(char ch) {
110  if (ch >= 'a' && ch <= 'z') {
111  return ch - ('a' - 'A');
112  } else {
113  return ch;
114  }
115 }
116 
117 static bool is_reserved(const char *segment, int length) {
118  bool result;
119  char* lower = calloc(1, length + 1);
120  memcpy(lower, segment, length);
121  int i = 0;
122  while(lower[i]) {
123  lower[i] = nolocale_tolower(lower[i]);
124  i++;
125  }
126  lower[length] = 0;
127  result = is_reserved_name(lower);
128  free(lower);
129  return result;
130 }
131 
132 static void fill_prefix(const char *segment, int length,
133  const char *prefix_given,
134  const char *package_name,
135  stringsink *classname) {
136  if (prefix_given != NULL && strcmp(prefix_given, "") != 0) {
137  stringsink_string(classname, prefix_given, strlen(prefix_given));
138  } else {
139  if (is_reserved(segment, length)) {
140  if (package_name != NULL &&
141  strcmp("google.protobuf", package_name) == 0) {
142  stringsink_string(classname, "GPB", 3);
143  } else {
144  stringsink_string(classname, "PB", 2);
145  }
146  }
147  }
148 }
149 
150 static void fill_segment(const char *segment, int length,
151  stringsink *classname, bool use_camel) {
152  if (use_camel && (segment[0] < 'A' || segment[0] > 'Z')) {
153  char first = nolocale_toupper(segment[0]);
154  stringsink_string(classname, &first, 1);
155  stringsink_string(classname, segment + 1, length - 1);
156  } else {
157  stringsink_string(classname, segment, length);
158  }
159 }
160 
161 static void fill_namespace(const char *package, const char *php_namespace,
162  stringsink *classname) {
163  if (php_namespace != NULL) {
164  if (strlen(php_namespace) != 0) {
165  stringsink_string(classname, php_namespace, strlen(php_namespace));
166  stringsink_string(classname, "\\", 1);
167  }
168  } else if (package != NULL) {
169  int i = 0, j = 0;
170  size_t package_len = strlen(package);
171  while (i < package_len) {
172  j = i;
173  while (j < package_len && package[j] != '.') {
174  j++;
175  }
176  fill_prefix(package + i, j - i, "", package, classname);
177  fill_segment(package + i, j - i, classname, true);
178  stringsink_string(classname, "\\", 1);
179  i = j + 1;
180  }
181  }
182 }
183 
184 static void fill_classname(const char *fullname,
185  const char *package,
186  const char *prefix,
187  stringsink *classname) {
188  int classname_start = 0;
189  if (package != NULL) {
190  size_t package_len = strlen(package);
191  classname_start = package_len == 0 ? 0 : package_len + 1;
192  }
193  size_t fullname_len = strlen(fullname);
194 
195  int i = classname_start, j;
196  while (i < fullname_len) {
197  j = i;
198  while (j < fullname_len && fullname[j] != '.') {
199  j++;
200  }
201  fill_prefix(fullname + i, j - i, prefix, package, classname);
202  fill_segment(fullname + i, j - i, classname, false);
203  if (j != fullname_len) {
204  stringsink_string(classname, "\\", 1);
205  }
206  i = j + 1;
207  }
208 }
209 
210 char *GetPhpClassname(const upb_filedef *file, const char *fullname) {
211  // Prepend '.' to package name to make it absolute. In the 5 additional
212  // bytes allocated, one for '.', one for trailing 0, and 3 for 'GPB' if
213  // given message is google.protobuf.Empty.
214  const char *package = upb_filedef_package(file);
215  const char *php_namespace = upb_filedef_phpnamespace(file);
216  const char *prefix = upb_filedef_phpprefix(file);
217  char *ret;
218  stringsink namesink;
219  stringsink_init(&namesink);
220 
221  fill_namespace(package, php_namespace, &namesink);
222  fill_classname(fullname, package, prefix, &namesink);
223  stringsink_string(&namesink, "\0", 1);
224  ret = strdup(namesink.ptr);
225  stringsink_uninit(&namesink);
226  return ret;
227 }
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
PBPHP_ASSERT
#define PBPHP_ASSERT(x)
Definition: protobuf/php/ext/google/protobuf/protobuf.h:139
upb_filedef_phpprefix
const char * upb_filedef_phpprefix(const upb_filedef *f)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:4504
binary_size.new_size
def new_size
Definition: binary_size.py:124
stringsink_string
static size_t stringsink_string(stringsink *sink, const char *ptr, size_t len)
Definition: names.c:44
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
setup.name
name
Definition: setup.py:542
protobuf.h
nolocale_tolower
static char nolocale_tolower(char ch)
Definition: names.c:101
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
kReservedNames
const char *const kReservedNames[]
Definition: names.c:73
fill_prefix
static void fill_prefix(const char *segment, int length, const char *prefix_given, const char *package_name, stringsink *classname)
Definition: names.c:132
upb_filedef_phpnamespace
const char * upb_filedef_phpnamespace(const upb_filedef *f)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:4508
stringsink_init
static void stringsink_init(stringsink *sink)
Definition: names.c:62
fill_namespace
static void fill_namespace(const char *package, const char *php_namespace, stringsink *classname)
Definition: names.c:161
is_reserved_name
bool is_reserved_name(const char *name)
Definition: names.c:91
upb_filedef
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:3000
stringsink::ptr
char * ptr
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1472
sink
FormatSinkImpl * sink
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:450
is_reserved
static bool is_reserved(const char *segment, int length)
Definition: names.c:117
stringsink_uninit
static void stringsink_uninit(stringsink *sink)
Definition: names.c:69
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
first
StrT first
Definition: cxa_demangle.cpp:4884
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
fill_segment
static void fill_segment(const char *segment, int length, stringsink *classname, bool use_camel)
Definition: names.c:150
names.h
ch
char ch
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3621
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
GetPhpClassname
char * GetPhpClassname(const upb_filedef *file, const char *fullname)
Definition: names.c:210
nolocale_toupper
static char nolocale_toupper(char ch)
Definition: names.c:109
fill_classname
static void fill_classname(const char *fullname, const char *package, const char *prefix, stringsink *classname)
Definition: names.c:184
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
stringsink
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1469
strdup
#define strdup(ptr)
Definition: acountry.c:55


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:42