bson 4.2.2 → 4.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/README.md +25 -7
  5. data/Rakefile +16 -9
  6. data/ext/bson/{native-endian.h → bson-endian.h} +5 -99
  7. data/ext/bson/bson-native.h +125 -0
  8. data/ext/bson/bytebuf.c +133 -0
  9. data/ext/bson/endian.c +117 -0
  10. data/ext/bson/init.c +355 -0
  11. data/ext/bson/libbson-utf8.c +230 -0
  12. data/ext/bson/read.c +411 -0
  13. data/ext/bson/util.c +95 -0
  14. data/ext/bson/write.c +680 -0
  15. data/lib/bson.rb +6 -3
  16. data/lib/bson/active_support.rb +17 -0
  17. data/lib/bson/array.rb +57 -17
  18. data/lib/bson/binary.rb +185 -13
  19. data/lib/bson/boolean.rb +12 -3
  20. data/lib/bson/code.rb +16 -2
  21. data/lib/bson/code_with_scope.rb +32 -5
  22. data/lib/bson/config.rb +1 -1
  23. data/lib/bson/date.rb +12 -2
  24. data/lib/bson/date_time.rb +2 -2
  25. data/lib/bson/db_pointer.rb +110 -0
  26. data/lib/bson/decimal128.rb +17 -3
  27. data/lib/bson/decimal128/builder.rb +1 -1
  28. data/lib/bson/document.rb +152 -5
  29. data/lib/bson/environment.rb +2 -1
  30. data/lib/bson/error.rb +27 -0
  31. data/lib/bson/ext_json.rb +383 -0
  32. data/lib/bson/false_class.rb +1 -1
  33. data/lib/bson/float.rb +48 -2
  34. data/lib/bson/hash.rb +68 -17
  35. data/lib/bson/int32.rb +52 -13
  36. data/lib/bson/int64.rb +59 -15
  37. data/lib/bson/integer.rb +36 -2
  38. data/lib/bson/json.rb +1 -1
  39. data/lib/bson/max_key.rb +13 -1
  40. data/lib/bson/min_key.rb +13 -1
  41. data/lib/bson/nil_class.rb +4 -2
  42. data/lib/bson/object.rb +28 -1
  43. data/lib/bson/object_id.rb +16 -2
  44. data/lib/bson/open_struct.rb +1 -1
  45. data/lib/bson/regexp.rb +27 -4
  46. data/lib/bson/registry.rb +3 -3
  47. data/lib/bson/specialized.rb +4 -2
  48. data/lib/bson/string.rb +5 -3
  49. data/lib/bson/symbol.rb +99 -7
  50. data/lib/bson/time.rb +63 -4
  51. data/lib/bson/time_with_zone.rb +54 -0
  52. data/lib/bson/timestamp.rb +44 -6
  53. data/lib/bson/true_class.rb +1 -1
  54. data/lib/bson/undefined.rb +12 -1
  55. data/lib/bson/version.rb +2 -2
  56. data/spec/bson/array_spec.rb +18 -1
  57. data/spec/bson/binary_spec.rb +100 -3
  58. data/spec/bson/binary_uuid_spec.rb +189 -0
  59. data/spec/bson/boolean_spec.rb +1 -1
  60. data/spec/bson/byte_buffer_read_spec.rb +197 -0
  61. data/spec/bson/byte_buffer_spec.rb +121 -381
  62. data/spec/bson/byte_buffer_write_spec.rb +854 -0
  63. data/spec/bson/code_spec.rb +1 -1
  64. data/spec/bson/code_with_scope_spec.rb +1 -1
  65. data/spec/bson/date_spec.rb +1 -1
  66. data/spec/bson/date_time_spec.rb +54 -1
  67. data/spec/bson/decimal128_spec.rb +35 -35
  68. data/spec/bson/document_as_spec.rb +46 -0
  69. data/spec/bson/document_spec.rb +197 -30
  70. data/spec/bson/ext_json_parse_spec.rb +308 -0
  71. data/spec/bson/false_class_spec.rb +1 -1
  72. data/spec/bson/float_spec.rb +37 -1
  73. data/spec/bson/hash_as_spec.rb +57 -0
  74. data/spec/bson/hash_spec.rb +209 -1
  75. data/spec/bson/int32_spec.rb +180 -6
  76. data/spec/bson/int64_spec.rb +199 -6
  77. data/spec/bson/integer_spec.rb +29 -3
  78. data/spec/bson/json_spec.rb +1 -1
  79. data/spec/bson/max_key_spec.rb +1 -1
  80. data/spec/bson/min_key_spec.rb +1 -1
  81. data/spec/bson/nil_class_spec.rb +1 -1
  82. data/spec/bson/object_id_spec.rb +1 -1
  83. data/spec/bson/object_spec.rb +1 -1
  84. data/spec/bson/open_struct_spec.rb +1 -1
  85. data/spec/bson/raw_spec.rb +34 -2
  86. data/spec/bson/regexp_spec.rb +1 -1
  87. data/spec/bson/registry_spec.rb +1 -1
  88. data/spec/bson/string_spec.rb +19 -1
  89. data/spec/bson/symbol_raw_spec.rb +45 -0
  90. data/spec/bson/symbol_spec.rb +63 -3
  91. data/spec/bson/time_spec.rb +205 -2
  92. data/spec/bson/time_with_zone_spec.rb +68 -0
  93. data/spec/bson/timestamp_spec.rb +56 -1
  94. data/spec/bson/true_class_spec.rb +1 -1
  95. data/spec/bson/undefined_spec.rb +1 -1
  96. data/spec/bson_spec.rb +1 -1
  97. data/spec/{support → runners}/common_driver.rb +1 -1
  98. data/spec/runners/corpus.rb +185 -0
  99. data/spec/{support/corpus.rb → runners/corpus_legacy.rb} +41 -59
  100. data/spec/spec_helper.rb +40 -3
  101. data/spec/{bson/driver_bson_spec.rb → spec_tests/common_driver_spec.rb} +1 -0
  102. data/spec/{bson/corpus_spec.rb → spec_tests/corpus_legacy_spec.rb} +10 -7
  103. data/spec/spec_tests/corpus_spec.rb +124 -0
  104. data/spec/spec_tests/data/corpus/README.md +15 -0
  105. data/spec/spec_tests/data/corpus/array.json +49 -0
  106. data/spec/spec_tests/data/corpus/binary.json +113 -0
  107. data/spec/spec_tests/data/corpus/boolean.json +27 -0
  108. data/spec/spec_tests/data/corpus/code.json +67 -0
  109. data/spec/spec_tests/data/corpus/code_w_scope.json +78 -0
  110. data/spec/spec_tests/data/corpus/datetime.json +42 -0
  111. data/spec/spec_tests/data/corpus/dbpointer.json +56 -0
  112. data/spec/spec_tests/data/corpus/dbref.json +31 -0
  113. data/spec/spec_tests/data/corpus/decimal128-1.json +317 -0
  114. data/spec/spec_tests/data/corpus/decimal128-2.json +793 -0
  115. data/spec/spec_tests/data/corpus/decimal128-3.json +1771 -0
  116. data/spec/spec_tests/data/corpus/decimal128-4.json +117 -0
  117. data/spec/spec_tests/data/corpus/decimal128-5.json +402 -0
  118. data/spec/spec_tests/data/corpus/decimal128-6.json +119 -0
  119. data/spec/spec_tests/data/corpus/decimal128-7.json +323 -0
  120. data/spec/spec_tests/data/corpus/document.json +36 -0
  121. data/spec/spec_tests/data/corpus/double.json +87 -0
  122. data/spec/spec_tests/data/corpus/int32.json +43 -0
  123. data/spec/spec_tests/data/corpus/int64.json +43 -0
  124. data/spec/spec_tests/data/corpus/maxkey.json +12 -0
  125. data/spec/spec_tests/data/corpus/minkey.json +12 -0
  126. data/spec/spec_tests/data/corpus/multi-type-deprecated.json +15 -0
  127. data/spec/spec_tests/data/corpus/multi-type.json +11 -0
  128. data/spec/spec_tests/data/corpus/null.json +12 -0
  129. data/spec/spec_tests/data/corpus/oid.json +28 -0
  130. data/spec/spec_tests/data/corpus/regex.json +65 -0
  131. data/spec/spec_tests/data/corpus/string.json +72 -0
  132. data/spec/spec_tests/data/corpus/symbol.json +80 -0
  133. data/spec/spec_tests/data/corpus/timestamp.json +34 -0
  134. data/spec/spec_tests/data/corpus/top.json +236 -0
  135. data/spec/spec_tests/data/corpus/undefined.json +15 -0
  136. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/array.json +8 -2
  137. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/binary.json +0 -0
  138. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/boolean.json +0 -0
  139. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code.json +1 -1
  140. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/code_w_scope.json +1 -1
  141. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/document.json +1 -1
  142. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/double.json +1 -1
  143. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/datetime.json +0 -0
  144. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/dbpointer.json +0 -0
  145. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/int64.json +0 -0
  146. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/failures/symbol.json +0 -0
  147. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/int32.json +1 -1
  148. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/maxkey.json +1 -1
  149. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/minkey.json +1 -1
  150. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/null.json +1 -1
  151. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/oid.json +0 -0
  152. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/regex.json +1 -1
  153. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/string.json +0 -0
  154. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/timestamp.json +1 -1
  155. data/spec/{support/corpus-tests → spec_tests/data/corpus_legacy}/top.json +0 -0
  156. data/spec/{support/corpus-tests/failures → spec_tests/data/corpus_legacy}/undefined.json +0 -0
  157. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-1.json +0 -0
  158. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-2.json +0 -0
  159. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-3.json +0 -0
  160. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-4.json +0 -0
  161. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-5.json +0 -0
  162. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-6.json +0 -0
  163. data/spec/{support/driver-spec-tests → spec_tests/data}/decimal128/decimal128-7.json +0 -0
  164. data/spec/support/shared_examples.rb +3 -5
  165. data/spec/support/spec_config.rb +16 -0
  166. data/spec/support/utils.rb +10 -0
  167. metadata +227 -124
  168. metadata.gz.sig +0 -0
  169. data/ext/bson/bson_native.c +0 -762
data/ext/bson/util.c ADDED
@@ -0,0 +1,95 @@
1
+ /*
2
+ * Copyright (C) 2009-2020 MongoDB Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include "bson-native.h"
18
+
19
+ /**
20
+ * Holds the machine id hash for object id generation.
21
+ */
22
+ static char rb_bson_machine_id_hash[HOST_NAME_HASH_MAX];
23
+
24
+ void rb_bson_generate_machine_id(VALUE rb_md5_class, char *rb_bson_machine_id)
25
+ {
26
+ VALUE digest = rb_funcall(rb_md5_class, rb_intern("digest"), 1, rb_str_new2(rb_bson_machine_id));
27
+ memcpy(rb_bson_machine_id_hash, RSTRING_PTR(digest), RSTRING_LEN(digest));
28
+ }
29
+
30
+ /**
31
+ * Generate the next object id.
32
+ */
33
+ VALUE rb_bson_object_id_generator_next(int argc, VALUE* args, VALUE self)
34
+ {
35
+ char bytes[12];
36
+ uint32_t t;
37
+ uint32_t c;
38
+ uint16_t pid = BSON_UINT16_TO_BE(getpid());
39
+
40
+ if (argc == 0 || (argc == 1 && *args == Qnil)) {
41
+ t = BSON_UINT32_TO_BE((int) time(NULL));
42
+ }
43
+ else {
44
+ t = BSON_UINT32_TO_BE(NUM2ULONG(rb_funcall(*args, rb_intern("to_i"), 0)));
45
+ }
46
+
47
+ c = BSON_UINT32_TO_BE(rb_bson_object_id_counter << 8);
48
+
49
+ memcpy(&bytes, &t, 4);
50
+ memcpy(&bytes[4], rb_bson_machine_id_hash, 3);
51
+ memcpy(&bytes[7], &pid, 2);
52
+ memcpy(&bytes[9], &c, 3);
53
+ rb_bson_object_id_counter++;
54
+ return rb_str_new(bytes, 12);
55
+ }
56
+
57
+ /**
58
+ * Returns a Ruby constant nested one level, e.g. BSON::Document.
59
+ */
60
+ VALUE pvt_const_get_2(const char *c1, const char *c2) {
61
+ return rb_const_get(rb_const_get(rb_cObject, rb_intern(c1)), rb_intern(c2));
62
+ }
63
+
64
+ /**
65
+ * Returns a Ruby constant nested two levels, e.g. BSON::Regexp::Raw.
66
+ */
67
+ VALUE pvt_const_get_3(const char *c1, const char *c2, const char *c3) {
68
+ return rb_const_get(pvt_const_get_2(c1, c2), rb_intern(c3));
69
+ }
70
+
71
+ /**
72
+ * Returns the value of the :mode option, or the default if the option is not
73
+ * specified. Raises ArgumentError if the value is not one of nil or :bson.
74
+ * A future version of bson-ruby is expected to also support :ruby and :ruby!
75
+ * values. Returns one of the BSON_MODE_* values.
76
+ */
77
+ int pvt_get_mode_option(int argc, VALUE *argv) {
78
+ VALUE opts;
79
+ VALUE mode;
80
+
81
+ rb_scan_args(argc, argv, ":", &opts);
82
+ if (NIL_P(opts)) {
83
+ return BSON_MODE_DEFAULT;
84
+ } else {
85
+ mode = rb_hash_lookup(opts, ID2SYM(rb_intern("mode")));
86
+ if (mode == Qnil) {
87
+ return BSON_MODE_DEFAULT;
88
+ } else if (mode == ID2SYM(rb_intern("bson"))) {
89
+ return BSON_MODE_BSON;
90
+ } else {
91
+ rb_raise(rb_eArgError, "Invalid value for :mode option: %s",
92
+ RSTRING_PTR(rb_funcall(mode, rb_intern("inspect"), 0)));
93
+ }
94
+ }
95
+ }
data/ext/bson/write.c ADDED
@@ -0,0 +1,680 @@
1
+ /*
2
+ * Copyright (C) 2009-2020 MongoDB Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include "bson-native.h"
18
+ #include <ruby/encoding.h>
19
+
20
+ typedef struct{
21
+ byte_buffer_t *b;
22
+ VALUE buffer;
23
+ VALUE validating_keys;
24
+ } put_hash_context;
25
+
26
+ static void pvt_replace_int32(byte_buffer_t *b, int32_t position, int32_t newval);
27
+ static void pvt_put_field(byte_buffer_t *b, VALUE rb_buffer, VALUE val, VALUE validating_keys);
28
+ static void pvt_put_byte(byte_buffer_t *b, const char byte);
29
+ static void pvt_put_int32(byte_buffer_t *b, const int32_t i32);
30
+ static void pvt_put_uint32(byte_buffer_t *b, const uint32_t i32);
31
+ static void pvt_put_int64(byte_buffer_t *b, const int64_t i);
32
+ static void pvt_put_double(byte_buffer_t *b, double f);
33
+ static void pvt_put_cstring(byte_buffer_t *b, const char *str, int32_t length, const char *data_type);
34
+ static void pvt_put_bson_key(byte_buffer_t *b, VALUE string, VALUE validating_keys);
35
+ static VALUE pvt_bson_byte_buffer_put_bson_partial_string(VALUE self, const char *str, int32_t length);
36
+ static VALUE pvt_bson_byte_buffer_put_binary_string(VALUE self, const char *str, int32_t length);
37
+
38
+ static int fits_int32(int64_t i64){
39
+ return i64 >= INT32_MIN && i64 <= INT32_MAX;
40
+ }
41
+
42
+ void pvt_put_field(byte_buffer_t *b, VALUE rb_buffer, VALUE val, VALUE validating_keys){
43
+ switch(TYPE(val)){
44
+ case T_BIGNUM:
45
+ case T_FIXNUM:{
46
+ int64_t i64= NUM2LL(val);
47
+ if(fits_int32(i64)){
48
+ pvt_put_int32(b, (int32_t)i64);
49
+ }else{
50
+ pvt_put_int64(b, i64);
51
+ }
52
+ break;
53
+ }
54
+ case T_FLOAT:
55
+ pvt_put_double(b, NUM2DBL(val));
56
+ break;
57
+ case T_ARRAY:
58
+ rb_bson_byte_buffer_put_array(rb_buffer, val, validating_keys);
59
+ break;
60
+ case T_TRUE:
61
+ pvt_put_byte(b, 1);
62
+ break;
63
+ case T_FALSE:
64
+ pvt_put_byte(b, 0);
65
+ break;
66
+ case T_HASH:
67
+ rb_bson_byte_buffer_put_hash(rb_buffer, val, validating_keys);
68
+ break;
69
+ default:{
70
+ rb_funcall(val, rb_intern("to_bson"), 2, rb_buffer, validating_keys);
71
+ break;
72
+ }
73
+ }
74
+ }
75
+
76
+ void pvt_put_byte(byte_buffer_t *b, const char byte)
77
+ {
78
+ ENSURE_BSON_WRITE(b, 1);
79
+ *WRITE_PTR(b) = byte;
80
+ b->write_position += 1;
81
+
82
+ }
83
+
84
+ /* The docstring is in init.c. */
85
+ VALUE rb_bson_byte_buffer_put_byte(VALUE self, VALUE byte)
86
+ {
87
+ byte_buffer_t *b;
88
+ const char *str;
89
+ size_t length;
90
+
91
+ if (!RB_TYPE_P(byte, T_STRING))
92
+ rb_raise(rb_eArgError, "A string argument is required for put_byte");
93
+
94
+ str = RSTRING_PTR(byte);
95
+ length = RSTRING_LEN(byte);
96
+
97
+ if (length != 1) {
98
+ rb_raise(rb_eArgError, "put_byte requires a string of length 1");
99
+ }
100
+
101
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
102
+ ENSURE_BSON_WRITE(b, 1);
103
+ memcpy(WRITE_PTR(b), str, 1);
104
+ b->write_position += 1;
105
+
106
+ return self;
107
+ }
108
+
109
+ /* The docstring is in init.c. */
110
+ VALUE rb_bson_byte_buffer_put_bytes(VALUE self, VALUE bytes)
111
+ {
112
+ byte_buffer_t *b;
113
+ const char *str;
114
+ size_t length;
115
+
116
+ if (!RB_TYPE_P(bytes, T_STRING) && !RB_TYPE_P(bytes, RUBY_T_DATA))
117
+ rb_raise(rb_eArgError, "Invalid input");
118
+
119
+ str = RSTRING_PTR(bytes);
120
+ length = RSTRING_LEN(bytes);
121
+
122
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
123
+ ENSURE_BSON_WRITE(b, length);
124
+ memcpy(WRITE_PTR(b), str, length);
125
+ b->write_position += length;
126
+ return self;
127
+ }
128
+
129
+ /* write the byte denoting the BSON type for the passed object */
130
+ void pvt_put_type_byte(byte_buffer_t *b, VALUE val){
131
+ char type_byte;
132
+
133
+ switch (TYPE(val)){
134
+ case T_BIGNUM:
135
+ case T_FIXNUM:
136
+ if (fits_int32(NUM2LL(val))) {
137
+ type_byte = BSON_TYPE_INT32;
138
+ } else {
139
+ type_byte = BSON_TYPE_INT64;
140
+ }
141
+ break;
142
+ case T_STRING:
143
+ type_byte = BSON_TYPE_STRING;
144
+ break;
145
+ case T_ARRAY:
146
+ type_byte = BSON_TYPE_ARRAY;
147
+ break;
148
+ case T_TRUE:
149
+ case T_FALSE:
150
+ type_byte = BSON_TYPE_BOOLEAN;
151
+ break;
152
+ case T_HASH:
153
+ type_byte = BSON_TYPE_DOCUMENT;
154
+ break;
155
+ case T_FLOAT:
156
+ type_byte = BSON_TYPE_DOUBLE;
157
+ break;
158
+ default: {
159
+ VALUE type;
160
+ VALUE responds = rb_funcall(val, rb_intern("respond_to?"), 1, ID2SYM(rb_intern("bson_type")));
161
+ if (!RTEST(responds)) {
162
+ VALUE klass = pvt_const_get_3("BSON", "Error", "UnserializableClass");
163
+ VALUE val_str = rb_funcall(val, rb_intern("to_s"), 0);
164
+ rb_raise(klass, "Value does not define its BSON serialized type: %s", RSTRING_PTR(val_str));
165
+ }
166
+ type = rb_funcall(val, rb_intern("bson_type"), 0);
167
+ type_byte = *RSTRING_PTR(type);
168
+ RB_GC_GUARD(type);
169
+ break;
170
+ }
171
+ }
172
+
173
+ pvt_put_byte(b, type_byte);
174
+ }
175
+
176
+ /**
177
+ * Write a binary string (i.e. one potentially including null bytes)
178
+ * to byte buffer. length is the number of bytes to write.
179
+ * If str is null terminated, length does not include the terminating null.
180
+ */
181
+ VALUE pvt_bson_byte_buffer_put_binary_string(VALUE self, const char *str, int32_t length)
182
+ {
183
+ byte_buffer_t *b;
184
+ int32_t length_le;
185
+
186
+ rb_bson_utf8_validate(str, length, true, "String");
187
+
188
+ /* Even though we are storing binary data, and including the length
189
+ * of it, the bson spec still demands the (useless) trailing null.
190
+ */
191
+ length_le = BSON_UINT32_TO_LE(length + 1);
192
+
193
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
194
+ ENSURE_BSON_WRITE(b, length + 5);
195
+ memcpy(WRITE_PTR(b), &length_le, 4);
196
+ b->write_position += 4;
197
+ memcpy(WRITE_PTR(b), str, length);
198
+ b->write_position += length;
199
+ pvt_put_byte(b, 0);
200
+
201
+ return self;
202
+ }
203
+
204
+ /**
205
+ * Encodes +string+ to UTF-8. If +string+ is already in UTF-8, validates that
206
+ * it contains only valid UTF-8 bytes/byte sequences.
207
+ *
208
+ * Raises EncodingError on failure.
209
+ */
210
+ static VALUE pvt_bson_encode_to_utf8(VALUE string) {
211
+ VALUE existing_encoding_name;
212
+ VALUE encoding;
213
+ VALUE utf8_string;
214
+ const char *str;
215
+ int32_t length;
216
+
217
+ existing_encoding_name = rb_funcall(
218
+ rb_funcall(string, rb_intern("encoding"), 0),
219
+ rb_intern("name"), 0);
220
+
221
+ if (strcmp(RSTRING_PTR(existing_encoding_name), "UTF-8") == 0) {
222
+ utf8_string = string;
223
+
224
+ str = RSTRING_PTR(utf8_string);
225
+ length = RSTRING_LEN(utf8_string);
226
+
227
+ rb_bson_utf8_validate(str, length, true, "String");
228
+ } else {
229
+ encoding = rb_enc_str_new_cstr("UTF-8", rb_utf8_encoding());
230
+ utf8_string = rb_funcall(string, rb_intern("encode"), 1, encoding);
231
+ RB_GC_GUARD(encoding);
232
+ }
233
+
234
+ return utf8_string;
235
+ }
236
+
237
+ /* The docstring is in init.c. */
238
+ VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string)
239
+ {
240
+ VALUE utf8_string;
241
+ const char *str;
242
+ int32_t length;
243
+
244
+ utf8_string = pvt_bson_encode_to_utf8(string);
245
+ /* At this point utf8_string contains valid utf-8 byte sequences only */
246
+
247
+ str = RSTRING_PTR(utf8_string);
248
+ length = RSTRING_LEN(utf8_string);
249
+
250
+ RB_GC_GUARD(utf8_string);
251
+
252
+ return pvt_bson_byte_buffer_put_binary_string(self, str, length);
253
+ }
254
+
255
+ /**
256
+ * Writes a string (which may form part of a BSON object) to the byte buffer.
257
+ *
258
+ * Note: the string may not contain null bytes, and must be null-terminated.
259
+ * length is the number of bytes to write and does not include the null
260
+ * terminator.
261
+ */
262
+ VALUE pvt_bson_byte_buffer_put_bson_partial_string(VALUE self, const char *str, int32_t length)
263
+ {
264
+ byte_buffer_t *b;
265
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
266
+ pvt_put_cstring(b, str, length, "String");
267
+ return self;
268
+ }
269
+
270
+ /* The docstring is in init.c. */
271
+ VALUE rb_bson_byte_buffer_put_cstring(VALUE self, VALUE obj)
272
+ {
273
+ VALUE string;
274
+ const char *str;
275
+ int32_t length;
276
+
277
+ switch (TYPE(obj)) {
278
+ case T_STRING:
279
+ string = pvt_bson_encode_to_utf8(obj);
280
+ break;
281
+ case T_SYMBOL:
282
+ string = rb_sym2str(obj);
283
+ break;
284
+ case T_FIXNUM:
285
+ string = rb_fix2str(obj, 10);
286
+ break;
287
+ default:
288
+ rb_raise(rb_eTypeError, "Invalid type for put_cstring");
289
+ }
290
+
291
+ str = RSTRING_PTR(string);
292
+ length = RSTRING_LEN(string);
293
+ RB_GC_GUARD(string);
294
+ return pvt_bson_byte_buffer_put_bson_partial_string(self, str, length);
295
+ }
296
+
297
+ /**
298
+ * Writes a string (which may form part of a BSON object) to the byte buffer.
299
+ *
300
+ * Note: the string may not contain null bytes, and must be null-terminated.
301
+ * length is the number of bytes to write and does not include the null
302
+ * terminator.
303
+ *
304
+ * data_type is the type of data being written, e.g. "String" or "Key".
305
+ */
306
+ void pvt_put_cstring(byte_buffer_t *b, const char *str, int32_t length, const char *data_type)
307
+ {
308
+ int bytes_to_write;
309
+ rb_bson_utf8_validate(str, length, false, data_type);
310
+ bytes_to_write = length + 1;
311
+ ENSURE_BSON_WRITE(b, bytes_to_write);
312
+ memcpy(WRITE_PTR(b), str, bytes_to_write);
313
+ b->write_position += bytes_to_write;
314
+ }
315
+
316
+ /* The docstring is in init.c. */
317
+ VALUE rb_bson_byte_buffer_put_symbol(VALUE self, VALUE symbol)
318
+ {
319
+ VALUE symbol_str = rb_sym_to_s(symbol);
320
+ const char *str = RSTRING_PTR(symbol_str);
321
+ const int32_t length = RSTRING_LEN(symbol_str);
322
+
323
+ RB_GC_GUARD(symbol_str);
324
+ return pvt_bson_byte_buffer_put_binary_string(self, str, length);
325
+ }
326
+
327
+ /**
328
+ * Write a hash key to the byte buffer, validating it if requested
329
+ */
330
+ void pvt_put_bson_key(byte_buffer_t *b, VALUE string, VALUE validating_keys){
331
+ char *c_str = RSTRING_PTR(string);
332
+ size_t length = RSTRING_LEN(string);
333
+
334
+ if (RTEST(validating_keys)) {
335
+ if (length > 0 && (c_str[0] == '$' || memchr(c_str, '.', length))) {
336
+ rb_exc_raise(rb_funcall(rb_bson_illegal_key, rb_intern("new"), 1, string));
337
+ }
338
+ }
339
+
340
+ pvt_put_cstring(b, c_str, length, "Key");
341
+ }
342
+
343
+ void pvt_replace_int32(byte_buffer_t *b, int32_t position, int32_t newval)
344
+ {
345
+ const int32_t i32 = BSON_UINT32_TO_LE(newval);
346
+ memcpy(READ_PTR(b) + position, &i32, 4);
347
+ }
348
+
349
+ /* The docstring is in init.c. */
350
+ VALUE rb_bson_byte_buffer_replace_int32(VALUE self, VALUE position, VALUE newval)
351
+ {
352
+ byte_buffer_t *b;
353
+ long _position;
354
+
355
+ _position = NUM2LONG(position);
356
+ if (_position < 0) {
357
+ rb_raise(rb_eArgError, "Position given to replace_int32 cannot be negative: %ld", _position);
358
+ }
359
+
360
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
361
+
362
+ if (b->write_position < 4) {
363
+ rb_raise(rb_eArgError, "Buffer does not have enough data to use replace_int32");
364
+ }
365
+
366
+ if ((size_t) _position > b->write_position - 4) {
367
+ rb_raise(rb_eArgError, "Position given to replace_int32 is out of bounds: %ld", _position);
368
+ }
369
+
370
+ pvt_replace_int32(b, _position, NUM2LONG(newval));
371
+
372
+ return self;
373
+ }
374
+
375
+ /* The docstring is in init.c. */
376
+ VALUE rb_bson_byte_buffer_put_int32(VALUE self, VALUE i)
377
+ {
378
+ byte_buffer_t *b;
379
+ const int32_t i32 = NUM2INT(i);
380
+
381
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
382
+ pvt_put_int32(b, i32);
383
+ return self;
384
+ }
385
+
386
+ void pvt_put_int32(byte_buffer_t *b, const int32_t i)
387
+ {
388
+ const int32_t i32 = BSON_UINT32_TO_LE(i);
389
+ ENSURE_BSON_WRITE(b, 4);
390
+ memcpy(WRITE_PTR(b), &i32, 4);
391
+ b->write_position += 4;
392
+ }
393
+
394
+ /* The docstring is in init.c. */
395
+ VALUE rb_bson_byte_buffer_put_uint32(VALUE self, VALUE i)
396
+ {
397
+ byte_buffer_t *b;
398
+ int64_t temp;
399
+ uint32_t i32;
400
+
401
+ if (RB_TYPE_P(i, T_FLOAT)) {
402
+ rb_raise(rb_eArgError, "put_uint32: incorrect type: float, expected: integer");
403
+ }
404
+
405
+ temp = NUM2LL(i);
406
+ if (temp < 0 || temp > UINT32_MAX) {
407
+ rb_raise(rb_eRangeError, "Number %lld is out of range [0, 2^32)", (long long)temp);
408
+ }
409
+
410
+ i32 = NUM2UINT(i);
411
+
412
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
413
+ pvt_put_uint32(b, i32);
414
+ return self;
415
+ }
416
+
417
+ void pvt_put_uint32(byte_buffer_t *b, const uint32_t i)
418
+ {
419
+ const uint32_t i32 = BSON_UINT32_TO_LE(i);
420
+ ENSURE_BSON_WRITE(b, 4);
421
+ memcpy(WRITE_PTR(b), &i32, 4);
422
+ b->write_position += 4;
423
+ }
424
+
425
+ /* The docstring is in init.c. */
426
+ VALUE rb_bson_byte_buffer_put_int64(VALUE self, VALUE i)
427
+ {
428
+ byte_buffer_t *b;
429
+ const int64_t i64 = NUM2LL(i);
430
+
431
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
432
+ pvt_put_int64(b, i64);
433
+
434
+ return self;
435
+ }
436
+
437
+ void pvt_put_int64(byte_buffer_t *b, const int64_t i)
438
+ {
439
+ const int64_t i64 = BSON_UINT64_TO_LE(i);
440
+
441
+ ENSURE_BSON_WRITE(b, 8);
442
+ memcpy(WRITE_PTR(b), &i64, 8);
443
+ b->write_position += 8;
444
+ }
445
+
446
+ /* The docstring is in init.c. */
447
+ VALUE rb_bson_byte_buffer_put_double(VALUE self, VALUE f)
448
+ {
449
+ byte_buffer_t *b;
450
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
451
+ pvt_put_double(b, NUM2DBL(f));
452
+
453
+ return self;
454
+ }
455
+
456
+ void pvt_put_double(byte_buffer_t *b, double f)
457
+ {
458
+ const double d = BSON_DOUBLE_TO_LE(f);
459
+ ENSURE_BSON_WRITE(b, 8);
460
+ memcpy(WRITE_PTR(b), &d, 8);
461
+ b->write_position += 8;
462
+ }
463
+
464
+ /* The docstring is in init.c. */
465
+ VALUE rb_bson_byte_buffer_put_decimal128(VALUE self, VALUE low, VALUE high)
466
+ {
467
+ byte_buffer_t *b;
468
+ const int64_t low64 = BSON_UINT64_TO_LE(NUM2ULL(low));
469
+ const int64_t high64 = BSON_UINT64_TO_LE(NUM2ULL(high));
470
+
471
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
472
+ ENSURE_BSON_WRITE(b, 16);
473
+ memcpy(WRITE_PTR(b), &low64, 8);
474
+ b->write_position += 8;
475
+
476
+ memcpy(WRITE_PTR(b), &high64, 8);
477
+ b->write_position += 8;
478
+
479
+ return self;
480
+ }
481
+
482
+ static int put_hash_callback(VALUE key, VALUE val, VALUE context){
483
+ VALUE buffer = ((put_hash_context*)context)->buffer;
484
+ VALUE validating_keys = ((put_hash_context*)context)->validating_keys;
485
+ byte_buffer_t *b = ((put_hash_context*)context)->b;
486
+ VALUE key_str;
487
+
488
+ pvt_put_type_byte(b, val);
489
+
490
+ switch(TYPE(key)){
491
+ case T_STRING:
492
+ pvt_put_bson_key(b, key, validating_keys);
493
+ break;
494
+ case T_SYMBOL:
495
+ key_str = rb_sym_to_s(key);
496
+ RB_GC_GUARD(key_str);
497
+ pvt_put_bson_key(b, key_str, validating_keys);
498
+ break;
499
+ default:
500
+ rb_bson_byte_buffer_put_cstring(buffer, rb_funcall(key, rb_intern("to_bson_key"), 1, validating_keys));
501
+ }
502
+
503
+ pvt_put_field(b, buffer, val, validating_keys);
504
+ return ST_CONTINUE;
505
+ }
506
+
507
+ /* The docstring is in init.c. */
508
+ VALUE rb_bson_byte_buffer_put_hash(VALUE self, VALUE hash, VALUE validating_keys){
509
+ byte_buffer_t *b = NULL;
510
+ put_hash_context context = { NULL };
511
+ size_t position = 0;
512
+ size_t new_position = 0;
513
+ int32_t new_length = 0;
514
+
515
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
516
+ Check_Type(hash, T_HASH);
517
+
518
+ position = READ_SIZE(b);
519
+
520
+ /* insert length placeholder */
521
+ pvt_put_int32(b, 0);
522
+ context.buffer = self;
523
+ context.validating_keys = validating_keys;
524
+ context.b = b;
525
+
526
+ rb_hash_foreach(hash, put_hash_callback, (VALUE)&context);
527
+ pvt_put_byte(b, 0);
528
+
529
+ /* update length placeholder with actual value */
530
+ new_position = READ_SIZE(b);
531
+ new_length = new_position - position;
532
+ pvt_replace_int32(b, position, new_length);
533
+
534
+ return self;
535
+ }
536
+
537
+ static const char *index_strings[] = {
538
+ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
539
+ "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
540
+ "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
541
+ "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43",
542
+ "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54",
543
+ "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65",
544
+ "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76",
545
+ "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87",
546
+ "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98",
547
+ "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109",
548
+ "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120",
549
+ "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131",
550
+ "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142",
551
+ "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153",
552
+ "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164",
553
+ "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175",
554
+ "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186",
555
+ "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197",
556
+ "198", "199", "200", "201", "202", "203", "204", "205", "206", "207", "208",
557
+ "209", "210", "211", "212", "213", "214", "215", "216", "217", "218", "219",
558
+ "220", "221", "222", "223", "224", "225", "226", "227", "228", "229", "230",
559
+ "231", "232", "233", "234", "235", "236", "237", "238", "239", "240", "241",
560
+ "242", "243", "244", "245", "246", "247", "248", "249", "250", "251", "252",
561
+ "253", "254", "255", "256", "257", "258", "259", "260", "261", "262", "263",
562
+ "264", "265", "266", "267", "268", "269", "270", "271", "272", "273", "274",
563
+ "275", "276", "277", "278", "279", "280", "281", "282", "283", "284", "285",
564
+ "286", "287", "288", "289", "290", "291", "292", "293", "294", "295", "296",
565
+ "297", "298", "299", "300", "301", "302", "303", "304", "305", "306", "307",
566
+ "308", "309", "310", "311", "312", "313", "314", "315", "316", "317", "318",
567
+ "319", "320", "321", "322", "323", "324", "325", "326", "327", "328", "329",
568
+ "330", "331", "332", "333", "334", "335", "336", "337", "338", "339", "340",
569
+ "341", "342", "343", "344", "345", "346", "347", "348", "349", "350", "351",
570
+ "352", "353", "354", "355", "356", "357", "358", "359", "360", "361", "362",
571
+ "363", "364", "365", "366", "367", "368", "369", "370", "371", "372", "373",
572
+ "374", "375", "376", "377", "378", "379", "380", "381", "382", "383", "384",
573
+ "385", "386", "387", "388", "389", "390", "391", "392", "393", "394", "395",
574
+ "396", "397", "398", "399", "400", "401", "402", "403", "404", "405", "406",
575
+ "407", "408", "409", "410", "411", "412", "413", "414", "415", "416", "417",
576
+ "418", "419", "420", "421", "422", "423", "424", "425", "426", "427", "428",
577
+ "429", "430", "431", "432", "433", "434", "435", "436", "437", "438", "439",
578
+ "440", "441", "442", "443", "444", "445", "446", "447", "448", "449", "450",
579
+ "451", "452", "453", "454", "455", "456", "457", "458", "459", "460", "461",
580
+ "462", "463", "464", "465", "466", "467", "468", "469", "470", "471", "472",
581
+ "473", "474", "475", "476", "477", "478", "479", "480", "481", "482", "483",
582
+ "484", "485", "486", "487", "488", "489", "490", "491", "492", "493", "494",
583
+ "495", "496", "497", "498", "499", "500", "501", "502", "503", "504", "505",
584
+ "506", "507", "508", "509", "510", "511", "512", "513", "514", "515", "516",
585
+ "517", "518", "519", "520", "521", "522", "523", "524", "525", "526", "527",
586
+ "528", "529", "530", "531", "532", "533", "534", "535", "536", "537", "538",
587
+ "539", "540", "541", "542", "543", "544", "545", "546", "547", "548", "549",
588
+ "550", "551", "552", "553", "554", "555", "556", "557", "558", "559", "560",
589
+ "561", "562", "563", "564", "565", "566", "567", "568", "569", "570", "571",
590
+ "572", "573", "574", "575", "576", "577", "578", "579", "580", "581", "582",
591
+ "583", "584", "585", "586", "587", "588", "589", "590", "591", "592", "593",
592
+ "594", "595", "596", "597", "598", "599", "600", "601", "602", "603", "604",
593
+ "605", "606", "607", "608", "609", "610", "611", "612", "613", "614", "615",
594
+ "616", "617", "618", "619", "620", "621", "622", "623", "624", "625", "626",
595
+ "627", "628", "629", "630", "631", "632", "633", "634", "635", "636", "637",
596
+ "638", "639", "640", "641", "642", "643", "644", "645", "646", "647", "648",
597
+ "649", "650", "651", "652", "653", "654", "655", "656", "657", "658", "659",
598
+ "660", "661", "662", "663", "664", "665", "666", "667", "668", "669", "670",
599
+ "671", "672", "673", "674", "675", "676", "677", "678", "679", "680", "681",
600
+ "682", "683", "684", "685", "686", "687", "688", "689", "690", "691", "692",
601
+ "693", "694", "695", "696", "697", "698", "699", "700", "701", "702", "703",
602
+ "704", "705", "706", "707", "708", "709", "710", "711", "712", "713", "714",
603
+ "715", "716", "717", "718", "719", "720", "721", "722", "723", "724", "725",
604
+ "726", "727", "728", "729", "730", "731", "732", "733", "734", "735", "736",
605
+ "737", "738", "739", "740", "741", "742", "743", "744", "745", "746", "747",
606
+ "748", "749", "750", "751", "752", "753", "754", "755", "756", "757", "758",
607
+ "759", "760", "761", "762", "763", "764", "765", "766", "767", "768", "769",
608
+ "770", "771", "772", "773", "774", "775", "776", "777", "778", "779", "780",
609
+ "781", "782", "783", "784", "785", "786", "787", "788", "789", "790", "791",
610
+ "792", "793", "794", "795", "796", "797", "798", "799", "800", "801", "802",
611
+ "803", "804", "805", "806", "807", "808", "809", "810", "811", "812", "813",
612
+ "814", "815", "816", "817", "818", "819", "820", "821", "822", "823", "824",
613
+ "825", "826", "827", "828", "829", "830", "831", "832", "833", "834", "835",
614
+ "836", "837", "838", "839", "840", "841", "842", "843", "844", "845", "846",
615
+ "847", "848", "849", "850", "851", "852", "853", "854", "855", "856", "857",
616
+ "858", "859", "860", "861", "862", "863", "864", "865", "866", "867", "868",
617
+ "869", "870", "871", "872", "873", "874", "875", "876", "877", "878", "879",
618
+ "880", "881", "882", "883", "884", "885", "886", "887", "888", "889", "890",
619
+ "891", "892", "893", "894", "895", "896", "897", "898", "899", "900", "901",
620
+ "902", "903", "904", "905", "906", "907", "908", "909", "910", "911", "912",
621
+ "913", "914", "915", "916", "917", "918", "919", "920", "921", "922", "923",
622
+ "924", "925", "926", "927", "928", "929", "930", "931", "932", "933", "934",
623
+ "935", "936", "937", "938", "939", "940", "941", "942", "943", "944", "945",
624
+ "946", "947", "948", "949", "950", "951", "952", "953", "954", "955", "956",
625
+ "957", "958", "959", "960", "961", "962", "963", "964", "965", "966", "967",
626
+ "968", "969", "970", "971", "972", "973", "974", "975", "976", "977", "978",
627
+ "979", "980", "981", "982", "983", "984", "985", "986", "987", "988", "989",
628
+ "990", "991", "992", "993", "994", "995", "996", "997", "998", "999"};
629
+
630
+ /**
631
+ * Writes an array index to the byte buffer.
632
+ */
633
+ void pvt_put_array_index(byte_buffer_t *b, int32_t index)
634
+ {
635
+ char buffer[16];
636
+ const char *c_str = NULL;
637
+ size_t length;
638
+
639
+ if (index < 1000) {
640
+ c_str = index_strings[index];
641
+ } else {
642
+ c_str = buffer;
643
+ snprintf(buffer, sizeof(buffer), "%d", index);
644
+ }
645
+ length = strlen(c_str) + 1;
646
+ ENSURE_BSON_WRITE(b, length);
647
+ memcpy(WRITE_PTR(b), c_str, length);
648
+ b->write_position += length;
649
+ }
650
+
651
+ /* The docstring is in init.c. */
652
+ VALUE rb_bson_byte_buffer_put_array(VALUE self, VALUE array, VALUE validating_keys){
653
+ byte_buffer_t *b = NULL;
654
+ size_t new_position = 0;
655
+ int32_t new_length = 0;
656
+ size_t position = 0;
657
+ VALUE *array_element = NULL;
658
+ TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
659
+ Check_Type(array, T_ARRAY);
660
+
661
+ position = READ_SIZE(b);
662
+ /* insert length placeholder */
663
+ pvt_put_int32(b, 0);
664
+
665
+ array_element = RARRAY_PTR(array);
666
+
667
+ for(int32_t index=0; index < RARRAY_LEN(array); index++, array_element++){
668
+ pvt_put_type_byte(b, *array_element);
669
+ pvt_put_array_index(b, index);
670
+ pvt_put_field(b, self, *array_element, validating_keys);
671
+ }
672
+ pvt_put_byte(b, 0);
673
+
674
+ /* update length placeholder */
675
+ new_position = READ_SIZE(b);
676
+ new_length = new_position - position;
677
+ pvt_replace_int32(b, position, new_length);
678
+
679
+ return self;
680
+ }