protobuf/ruby/ext/google/protobuf_c/convert.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 // -----------------------------------------------------------------------------
32 // Ruby <-> upb data conversion functions.
33 //
34 // This file Also contains a few other assorted algorithms on upb_msgval.
35 //
36 // None of the algorithms in this file require any access to the internal
37 // representation of Ruby or upb objects.
38 // -----------------------------------------------------------------------------
39 
40 #include "convert.h"
41 
42 #include "message.h"
43 #include "protobuf.h"
44 
47  if (arena) {
48  char *ptr = upb_arena_malloc(arena, RSTRING_LEN(str));
49  memcpy(ptr, RSTRING_PTR(str), RSTRING_LEN(str));
50  ret.data = ptr;
51  } else {
52  // Data is only needed temporarily (within map lookup).
53  ret.data = RSTRING_PTR(str);
54  }
55  ret.size = RSTRING_LEN(str);
56  return ret;
57 }
58 
59 static bool is_ruby_num(VALUE value) {
60  return (TYPE(value) == T_FLOAT ||
61  TYPE(value) == T_FIXNUM ||
62  TYPE(value) == T_BIGNUM);
63 }
64 
65 static void Convert_CheckInt(const char* name, upb_fieldtype_t type,
66  VALUE val) {
67  if (!is_ruby_num(val)) {
68  rb_raise(cTypeError,
69  "Expected number type for integral field '%s' (given %s).", name,
70  rb_class2name(CLASS_OF(val)));
71  }
72 
73  // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
74  // bound; we just need to do precision checks (i.e., disallow rounding) and
75  // check for < 0 on unsigned types.
76  if (TYPE(val) == T_FLOAT) {
77  double dbl_val = NUM2DBL(val);
78  if (floor(dbl_val) != dbl_val) {
79  rb_raise(rb_eRangeError,
80  "Non-integral floating point value assigned to integer field "
81  "'%s' (given %s).",
82  name, rb_class2name(CLASS_OF(val)));
83  }
84  }
86  if (NUM2DBL(val) < 0) {
87  rb_raise(
88  rb_eRangeError,
89  "Assigning negative value to unsigned integer field '%s' (given %s).",
90  name, rb_class2name(CLASS_OF(val)));
91  }
92  }
93 }
94 
95 static int32_t Convert_ToEnum(VALUE value, const char* name,
96  const upb_enumdef* e) {
97  int32_t val;
98 
99  switch (TYPE(value)) {
100  case T_FLOAT:
101  case T_FIXNUM:
102  case T_BIGNUM:
104  val = NUM2INT(value);
105  break;
106  case T_STRING:
107  if (!upb_enumdef_ntoi(e, RSTRING_PTR(value), RSTRING_LEN(value), &val)) {
108  goto unknownval;
109  }
110  break;
111  case T_SYMBOL:
112  if (!upb_enumdef_ntoiz(e, rb_id2name(SYM2ID(value)), &val)) {
113  goto unknownval;
114  }
115  break;
116  default:
117  rb_raise(cTypeError,
118  "Expected number or symbol type for enum field '%s'.", name);
119  }
120 
121  return val;
122 
123 unknownval:
124  rb_raise(rb_eRangeError, "Unknown symbol value for enum field '%s'.", name);
125 }
126 
127 upb_msgval Convert_RubyToUpb(VALUE value, const char* name, TypeInfo type_info,
128  upb_arena* arena) {
129  upb_msgval ret;
130 
131  switch (type_info.type) {
132  case UPB_TYPE_FLOAT:
133  if (!is_ruby_num(value)) {
134  rb_raise(cTypeError, "Expected number type for float field '%s' (given %s).",
135  name, rb_class2name(CLASS_OF(value)));
136  }
137  ret.float_val = NUM2DBL(value);
138  break;
139  case UPB_TYPE_DOUBLE:
140  if (!is_ruby_num(value)) {
141  rb_raise(cTypeError, "Expected number type for double field '%s' (given %s).",
142  name, rb_class2name(CLASS_OF(value)));
143  }
144  ret.double_val = NUM2DBL(value);
145  break;
146  case UPB_TYPE_BOOL: {
147  if (value == Qtrue) {
148  ret.bool_val = 1;
149  } else if (value == Qfalse) {
150  ret.bool_val = 0;
151  } else {
152  rb_raise(cTypeError, "Invalid argument for boolean field '%s' (given %s).",
153  name, rb_class2name(CLASS_OF(value)));
154  }
155  break;
156  }
157  case UPB_TYPE_STRING: {
158  VALUE utf8 = rb_enc_from_encoding(rb_utf8_encoding());
159  if (CLASS_OF(value) == rb_cSymbol) {
160  value = rb_funcall(value, rb_intern("to_s"), 0);
161  } else if (CLASS_OF(value) != rb_cString) {
162  rb_raise(cTypeError, "Invalid argument for string field '%s' (given %s).",
163  name, rb_class2name(CLASS_OF(value)));
164  }
165 
166  if (rb_obj_encoding(value) != utf8) {
167  // Note: this will not duplicate underlying string data unless necessary.
168  value = rb_str_encode(value, utf8, 0, Qnil);
169 
170  if (rb_enc_str_coderange(value) == ENC_CODERANGE_BROKEN) {
171  rb_raise(rb_eEncodingError, "String is invalid UTF-8");
172  }
173  }
174 
175  ret.str_val = Convert_StringData(value, arena);
176  break;
177  }
178  case UPB_TYPE_BYTES: {
179  VALUE bytes = rb_enc_from_encoding(rb_ascii8bit_encoding());
180  if (CLASS_OF(value) != rb_cString) {
181  rb_raise(cTypeError, "Invalid argument for bytes field '%s' (given %s).",
182  name, rb_class2name(CLASS_OF(value)));
183  }
184 
185  if (rb_obj_encoding(value) != bytes) {
186  // Note: this will not duplicate underlying string data unless necessary.
187  // TODO(haberman): is this really necessary to get raw bytes?
188  value = rb_str_encode(value, bytes, 0, Qnil);
189  }
190 
191  ret.str_val = Convert_StringData(value, arena);
192  break;
193  }
194  case UPB_TYPE_MESSAGE:
195  ret.msg_val =
197  break;
198  case UPB_TYPE_ENUM:
199  ret.int32_val = Convert_ToEnum(value, name, type_info.def.enumdef);
200  break;
201  case UPB_TYPE_INT32:
202  case UPB_TYPE_INT64:
203  case UPB_TYPE_UINT32:
204  case UPB_TYPE_UINT64:
205  Convert_CheckInt(name, type_info.type, value);
206  switch (type_info.type) {
207  case UPB_TYPE_INT32:
208  ret.int32_val = NUM2INT(value);
209  break;
210  case UPB_TYPE_INT64:
211  ret.int64_val = NUM2LL(value);
212  break;
213  case UPB_TYPE_UINT32:
214  ret.uint32_val = NUM2UINT(value);
215  break;
216  case UPB_TYPE_UINT64:
217  ret.uint64_val = NUM2ULL(value);
218  break;
219  default:
220  break;
221  }
222  break;
223  default:
224  break;
225  }
226 
227  return ret;
228 }
229 
230 VALUE Convert_UpbToRuby(upb_msgval upb_val, TypeInfo type_info, VALUE arena) {
231  switch (type_info.type) {
232  case UPB_TYPE_FLOAT:
233  return DBL2NUM(upb_val.float_val);
234  case UPB_TYPE_DOUBLE:
235  return DBL2NUM(upb_val.double_val);
236  case UPB_TYPE_BOOL:
237  return upb_val.bool_val ? Qtrue : Qfalse;
238  case UPB_TYPE_INT32:
239  return INT2NUM(upb_val.int32_val);
240  case UPB_TYPE_INT64:
241  return LL2NUM(upb_val.int64_val);
242  case UPB_TYPE_UINT32:
243  return UINT2NUM(upb_val.uint32_val);
244  case UPB_TYPE_UINT64:
245  return ULL2NUM(upb_val.int64_val);
246  case UPB_TYPE_ENUM: {
247  const char* name =
248  upb_enumdef_iton(type_info.def.enumdef, upb_val.int32_val);
249  if (name) {
250  return ID2SYM(rb_intern(name));
251  } else {
252  return INT2NUM(upb_val.int32_val);
253  }
254  }
255  case UPB_TYPE_STRING: {
256  VALUE str_rb = rb_str_new(upb_val.str_val.data, upb_val.str_val.size);
257  rb_enc_associate(str_rb, rb_utf8_encoding());
258  rb_obj_freeze(str_rb);
259  return str_rb;
260  }
261  case UPB_TYPE_BYTES: {
262  VALUE str_rb = rb_str_new(upb_val.str_val.data, upb_val.str_val.size);
263  rb_enc_associate(str_rb, rb_ascii8bit_encoding());
264  rb_obj_freeze(str_rb);
265  return str_rb;
266  }
267  case UPB_TYPE_MESSAGE:
268  return Message_GetRubyWrapper((upb_msg*)upb_val.msg_val,
269  type_info.def.msgdef, arena);
270  default:
271  rb_raise(rb_eRuntimeError, "Convert_UpbToRuby(): Unexpected type %d",
272  (int)type_info.type);
273  }
274 }
275 
277  upb_arena* arena) {
278  upb_msgval new_msgval;
279 
280  switch (type_info.type) {
281  default:
282  memcpy(&new_msgval, &msgval, sizeof(msgval));
283  break;
284  case UPB_TYPE_STRING:
285  case UPB_TYPE_BYTES: {
286  size_t n = msgval.str_val.size;
287  char *mem = upb_arena_malloc(arena, n);
288  new_msgval.str_val.data = mem;
289  new_msgval.str_val.size = n;
290  memcpy(mem, msgval.str_val.data, n);
291  break;
292  }
293  case UPB_TYPE_MESSAGE:
294  new_msgval.msg_val =
295  Message_deep_copy(msgval.msg_val, type_info.def.msgdef, arena);
296  break;
297  }
298 
299  return new_msgval;
300 }
301 
303  switch (type_info.type) {
304  case UPB_TYPE_BOOL:
305  return memcmp(&val1, &val2, 1) == 0;
306  case UPB_TYPE_FLOAT:
307  case UPB_TYPE_INT32:
308  case UPB_TYPE_UINT32:
309  case UPB_TYPE_ENUM:
310  return memcmp(&val1, &val2, 4) == 0;
311  case UPB_TYPE_DOUBLE:
312  case UPB_TYPE_INT64:
313  case UPB_TYPE_UINT64:
314  return memcmp(&val1, &val2, 8) == 0;
315  case UPB_TYPE_STRING:
316  case UPB_TYPE_BYTES:
317  return val1.str_val.size == val2.str_val.size &&
318  memcmp(val1.str_val.data, val2.str_val.data,
319  val1.str_val.size) == 0;
320  case UPB_TYPE_MESSAGE:
321  return Message_Equal(val1.msg_val, val2.msg_val, type_info.def.msgdef);
322  default:
323  rb_raise(rb_eRuntimeError, "Internal error, unexpected type");
324  }
325 }
326 
328  switch (type_info.type) {
329  case UPB_TYPE_BOOL:
330  return Wyhash(&val, 1, seed, kWyhashSalt);
331  case UPB_TYPE_FLOAT:
332  case UPB_TYPE_INT32:
333  case UPB_TYPE_UINT32:
334  case UPB_TYPE_ENUM:
335  return Wyhash(&val, 4, seed, kWyhashSalt);
336  case UPB_TYPE_DOUBLE:
337  case UPB_TYPE_INT64:
338  case UPB_TYPE_UINT64:
339  return Wyhash(&val, 8, seed, kWyhashSalt);
340  case UPB_TYPE_STRING:
341  case UPB_TYPE_BYTES:
342  return Wyhash(val.str_val.data, val.str_val.size, seed, kWyhashSalt);
343  case UPB_TYPE_MESSAGE:
344  return Message_Hash(val.msg_val, type_info.def.msgdef, seed);
345  default:
346  rb_raise(rb_eRuntimeError, "Internal error, unexpected type");
347  }
348 }
xds_interop_client.str
str
Definition: xds_interop_client.py:487
Convert_CheckInt
static void Convert_CheckInt(const char *name, upb_fieldtype_t type, VALUE val)
Definition: protobuf/ruby/ext/google/protobuf_c/convert.c:65
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
UPB_TYPE_UINT64
@ UPB_TYPE_UINT64
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:487
grpc::testing::val1
const char val1[]
Definition: client_context_test_peer_test.cc:34
upb_arena_malloc
UPB_INLINE void * upb_arena_malloc(upb_arena *a, size_t size)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:383
upb_arena
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:2252
Convert_RubyToUpb
upb_msgval Convert_RubyToUpb(VALUE value, const char *name, TypeInfo type_info, upb_arena *arena)
Definition: protobuf/ruby/ext/google/protobuf_c/convert.c:127
Convert_UpbToRuby
VALUE Convert_UpbToRuby(upb_msgval upb_val, TypeInfo type_info, VALUE arena)
Definition: protobuf/ruby/ext/google/protobuf_c/convert.c:230
UPB_TYPE_FLOAT
@ UPB_TYPE_FLOAT
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:476
Message_GetRubyWrapper
VALUE Message_GetRubyWrapper(upb_msg *msg, const upb_msgdef *m, VALUE arena)
Definition: protobuf/ruby/ext/google/protobuf_c/message.c:122
upb_fieldtype_t
upb_fieldtype_t
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:472
seed
static const uint8_t seed[20]
Definition: dsa_test.cc:79
Message_GetUpbMessage
bool Message_GetUpbMessage(zval *val, const Descriptor *desc, upb_arena *arena, upb_msg **msg)
Definition: protobuf/php/ext/google/protobuf/message.c:456
Convert_StringData
static upb_strview Convert_StringData(VALUE str, upb_arena *arena)
Definition: protobuf/ruby/ext/google/protobuf_c/convert.c:45
TypeInfo
Definition: protobuf/php/ext/google/protobuf/def.h:69
Message_Hash
uint64_t Message_Hash(const upb_msg *msg, const upb_msgdef *m, uint64_t seed)
Definition: protobuf/ruby/ext/google/protobuf_c/message.c:708
UPB_TYPE_UINT32
@ UPB_TYPE_UINT32
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:478
setup.name
name
Definition: setup.py:542
upb_msgval::str_val
upb_strview str_val
Definition: php-upb.h:4623
upb_strview::data
const char * data
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:258
upb_msg
void upb_msg
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:548
upb_enumdef_ntoiz
UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e, const char *name, int32_t *num)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:3607
Message_Equal
bool Message_Equal(const upb_msg *m1, const upb_msg *m2, const upb_msgdef *m)
Definition: protobuf/ruby/ext/google/protobuf_c/message.c:667
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
Msgval_IsEqual
bool Msgval_IsEqual(upb_msgval val1, upb_msgval val2, TypeInfo type_info)
Definition: protobuf/ruby/ext/google/protobuf_c/convert.c:302
UPB_TYPE_BYTES
@ UPB_TYPE_BYTES
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:482
UPB_TYPE_ENUM
@ UPB_TYPE_ENUM
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:479
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
upb_enumdef_ntoi
bool upb_enumdef_ntoi(const upb_enumdef *def, const char *name, size_t len, int32_t *num)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:3273
protobuf.h
Message_deep_copy
VALUE Message_deep_copy(VALUE _self)
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/message.c:513
grpc::testing::val2
const char val2[]
Definition: client_context_test_peer_test.cc:35
TypeInfo::msgdef
const upb_msgdef * msgdef
Definition: defs.h:56
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
upb_msgval::float_val
float float_val
Definition: php-upb.h:4614
upb_enumdef_iton
const char * upb_enumdef_iton(const upb_enumdef *def, int32_t num)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:3283
Msgval_GetHash
uint64_t Msgval_GetHash(upb_msgval val, TypeInfo type_info, uint64_t seed)
Definition: protobuf/ruby/ext/google/protobuf_c/convert.c:327
upb_msgval::double_val
double double_val
Definition: php-upb.h:4615
cTypeError
VALUE cTypeError
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.c:35
UPB_TYPE_INT32
@ UPB_TYPE_INT32
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:477
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
absl::hash_internal::kWyhashSalt
constexpr uint64_t kWyhashSalt[5]
Definition: bloaty/third_party/abseil-cpp/absl/hash/internal/hash.cc:58
upb_msgval::bool_val
bool bool_val
Definition: php-upb.h:4613
mem
void * mem
Definition: libc.cpp:91
value
const char * value
Definition: hpack_parser_table.cc:165
TypeInfo::enumdef
const upb_enumdef * enumdef
Definition: defs.h:57
upb_msgval
Definition: php-upb.h:4612
UPB_TYPE_STRING
@ UPB_TYPE_STRING
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:481
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
upb_enumdef
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:2984
absl::hash_internal::Wyhash
uint64_t Wyhash(const void *data, size_t len, uint64_t seed, const uint64_t salt[])
Definition: wyhash.cc:30
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
Msgval_DeepCopy
upb_msgval Msgval_DeepCopy(upb_msgval msgval, TypeInfo type_info, upb_arena *arena)
Definition: protobuf/ruby/ext/google/protobuf_c/convert.c:276
upb_msgval::msg_val
const upb_msg * msg_val
Definition: php-upb.h:4621
is_ruby_num
static bool is_ruby_num(VALUE value)
Definition: protobuf/ruby/ext/google/protobuf_c/convert.c:59
UPB_TYPE_MESSAGE
@ UPB_TYPE_MESSAGE
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:483
Convert_ToEnum
static int32_t Convert_ToEnum(VALUE value, const char *name, const upb_enumdef *e)
Definition: protobuf/ruby/ext/google/protobuf_c/convert.c:95
TYPE
#define TYPE(u, l)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:8202
upb_msgval::int32_val
int32_t int32_val
Definition: php-upb.h:4616
upb_strview
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:257
TypeInfo::type
upb_fieldtype_t type
Definition: protobuf/php/ext/google/protobuf/def.h:70
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
convert.h
UPB_TYPE_DOUBLE
@ UPB_TYPE_DOUBLE
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:485
upb_strview::size
size_t size
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:259
TypeInfo::def
union TypeInfo::@436 def
UPB_TYPE_BOOL
@ UPB_TYPE_BOOL
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:474
UPB_TYPE_INT64
@ UPB_TYPE_INT64
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:486
upb_msgval::int64_val
int64_t int64_val
Definition: php-upb.h:4617
upb_msgval::uint32_val
uint32_t uint32_val
Definition: php-upb.h:4618
message.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:02