167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'ext/yarp/api_pack.c', line 167
static VALUE
pack_parse(VALUE self, VALUE version_symbol, VALUE variant_symbol, VALUE format_string) {
if (version_symbol != v3_2_0_symbol) {
rb_raise(rb_eArgError, "invalid version");
}
yp_pack_variant variant;
if (variant_symbol == pack_symbol) {
variant = YP_PACK_VARIANT_PACK;
} else if (variant_symbol == unpack_symbol) {
variant = YP_PACK_VARIANT_UNPACK;
} else {
rb_raise(rb_eArgError, "invalid variant");
}
StringValue(format_string);
const char *format = RSTRING_PTR(format_string);
const char *format_end = format + RSTRING_LEN(format_string);
yp_pack_encoding encoding = YP_PACK_ENCODING_START;
VALUE directives_array = rb_ary_new();
while (format < format_end) {
yp_pack_type type;
yp_pack_signed signed_type;
yp_pack_endian endian;
yp_pack_size size;
yp_pack_length_type length_type;
uint64_t length;
const char *directive_start = format;
yp_pack_result parse_result = yp_pack_parse(variant, &format, format_end, &type, &signed_type, &endian,
&size, &length_type, &length, &encoding);
const char *directive_end = format;
switch (parse_result) {
case YP_PACK_OK:
break;
case YP_PACK_ERROR_UNSUPPORTED_DIRECTIVE:
rb_raise(rb_eArgError, "unsupported directive");
case YP_PACK_ERROR_UNKNOWN_DIRECTIVE:
rb_raise(rb_eArgError, "unsupported directive");
case YP_PACK_ERROR_LENGTH_TOO_BIG:
rb_raise(rb_eRangeError, "pack length too big");
case YP_PACK_ERROR_BANG_NOT_ALLOWED:
rb_raise(rb_eRangeError, "bang not allowed");
case YP_PACK_ERROR_DOUBLE_ENDIAN:
rb_raise(rb_eRangeError, "double endian");
default:
rb_bug("parse result");
}
if (type == YP_PACK_END) {
break;
}
VALUE directive_args[9] = { version_symbol,
variant_symbol,
rb_usascii_str_new(directive_start, directive_end - directive_start),
pack_type_to_symbol(type),
pack_signed_to_symbol(signed_type),
pack_endian_to_symbol(endian),
pack_size_to_symbol(size),
pack_length_type_to_symbol(length_type),
UINT64T2NUM(length) };
rb_ary_push(directives_array, rb_class_new_instance(9, directive_args, rb_cYARPPackDirective));
}
VALUE format_args[2];
format_args[0] = directives_array;
format_args[1] = pack_encoding_to_ruby(encoding);
return rb_class_new_instance(2, format_args, rb_cYARPPackFormat);
}
|