bin_utils 0.0.4-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1035 @@
1
+ class String
2
+ unless method_defined?(:byteslice)
3
+ alias :byteslice :[]
4
+ end
5
+ end
6
+
7
+ module BinUtils
8
+ # Pure Ruby implementation of bin utils.
9
+ # It is provided just reference implementation, so that I could write tests before writting
10
+ # native code. You should not expect performance from it.
11
+ module PureRuby
12
+ extend self
13
+ INT32_INT32_LE = 'VV'.freeze
14
+ INT32_INT32_BE = 'NN'.freeze
15
+ MIN_INT = 0
16
+ MAX_INT64 = 2**64 - 1
17
+ MAX_INT32 = 2**32 - 1
18
+ MAX_INT16 = 2**16 - 1
19
+ MAX_INT8 = 2**8 - 1
20
+ MAX_SINT64 = 2**63 - 1
21
+ MAX_SINT32 = 2**31 - 1
22
+ MAX_SINT16 = 2**15 - 1
23
+ MAX_SINT8 = 2**7 - 1
24
+ MIN_SINT64 = -(2**63)
25
+ MIN_SINT32 = -(2**31)
26
+ MIN_SINT16 = -(2**15)
27
+ MIN_SINT8 = -(2**7)
28
+ EMPTY = ''.freeze
29
+ ONE = "\x01".freeze
30
+
31
+ def get_int8(data, i=0)
32
+ data.getbyte(i)
33
+ end
34
+
35
+ def slice_int8!(data)
36
+ int = data.getbyte(0)
37
+ data[0, 1] = EMPTY
38
+ int
39
+ end
40
+
41
+ def get_int16_le(data, i=0)
42
+ data.getbyte(i) + data.getbyte(i+1) * 256
43
+ end
44
+
45
+ def slice_int16_le!(data)
46
+ int = data.getbyte(0) + data.getbyte(1) * 256
47
+ data[0, 2] = EMPTY
48
+ int
49
+ end
50
+
51
+ def get_int16_be(data, i=0)
52
+ data.getbyte(i+1) + data.getbyte(i) * 256
53
+ end
54
+
55
+ def slice_int16_be!(data)
56
+ int = data.getbyte(1) + data.getbyte(0) * 256
57
+ data[0, 2] = EMPTY
58
+ int
59
+ end
60
+
61
+ def get_int24_le(data, i=0)
62
+ data.getbyte(i) + data.getbyte(i+1) * 256 + data.getbyte(i+2) * 65536
63
+ end
64
+
65
+ def slice_int24_le!(data)
66
+ int = data.getbyte(0) + data.getbyte(1) * 256 + data.getbyte(2) * 65536
67
+ data[0, 3] = EMPTY
68
+ int
69
+ end
70
+
71
+ def get_int24_be(data, i=0)
72
+ data.getbyte(i+2) + data.getbyte(i+1) * 256 + data.getbyte(i) * 65536
73
+ end
74
+
75
+ def slice_int24_be!(data)
76
+ int = data.getbyte(2) + data.getbyte(1) * 256 + data.getbyte(0) * 65536
77
+ data[0, 3] = EMPTY
78
+ int
79
+ end
80
+
81
+ def get_int32_le(data, i=0)
82
+ (data.getbyte(i) + data.getbyte(i+1) * 256 +
83
+ data.getbyte(i+2) * 65536 + data.getbyte(i+3) * 16777216)
84
+ end
85
+
86
+ def slice_int32_le!(data)
87
+ int = (data.getbyte(0) + data.getbyte(1) * 256 +
88
+ data.getbyte(2) * 65536 + data.getbyte(3) * 16777216)
89
+ data[0, 4] = EMPTY
90
+ int
91
+ end
92
+
93
+ def get_int32_be(data, i=0)
94
+ (data.getbyte(i+3) + data.getbyte(i+2) * 256 +
95
+ data.getbyte(i+1) * 65536 + data.getbyte(i) * 16777216)
96
+ end
97
+
98
+ def slice_int32_be!(data)
99
+ int = (data.getbyte(3) + data.getbyte(2) * 256 +
100
+ data.getbyte(1) * 65536 + data.getbyte(0) * 16777216)
101
+ data[0, 4] = EMPTY
102
+ int
103
+ end
104
+
105
+ def get_int40_le(data, i=0)
106
+ (data.getbyte(i) + data.getbyte(i+1) * 256 +
107
+ data.getbyte(i+2) * 65536 + data.getbyte(i+3) * 16777216 +
108
+ (data.getbyte(i+4) << 32))
109
+ end
110
+
111
+ def slice_int40_le!(data)
112
+ int = (data.getbyte(0) + data.getbyte(1) * 256 +
113
+ data.getbyte(2) * 65536 + data.getbyte(3) * 16777216 +
114
+ (data.getbyte(4) << 32))
115
+ data[0, 5] = EMPTY
116
+ int
117
+ end
118
+
119
+ def get_int40_be(data, i=0)
120
+ (data.getbyte(i+4) + data.getbyte(i+3) * 256 +
121
+ data.getbyte(i+2) * 65536 + data.getbyte(i+1) * 16777216 +
122
+ (data.getbyte(i) << 32))
123
+ end
124
+
125
+ def slice_int40_be!(data)
126
+ int = (data.getbyte(4) + data.getbyte(3) * 256 +
127
+ data.getbyte(2) * 65536 + data.getbyte(1) * 16777216 +
128
+ (data.getbyte(0) << 32))
129
+ data[0, 5] = EMPTY
130
+ int
131
+ end
132
+
133
+ INT32_INT16_LE = 'Vv'.freeze
134
+ def slice_int48_le!(data)
135
+ int0, int1 = data.unpack(INT32_INT16_LE)
136
+ data[0, 6] = EMPTY
137
+ int0 + (int1 << 32)
138
+ end
139
+
140
+ def get_int48_le(data, i=0)
141
+ data = data.byteslice(i, 6) if i > 0
142
+ int0, int1 = data.unpack(INT32_INT16_LE)
143
+ int0 + (int1 << 32)
144
+ end
145
+
146
+ INT16_INT32_BE = 'nN'.freeze
147
+ def slice_int48_be!(data)
148
+ int1, int0 = data.unpack(INT16_INT32_BE)
149
+ data[0, 6] = EMPTY
150
+ int0 + (int1 << 32)
151
+ end
152
+
153
+ def get_int48_be(data, i=0)
154
+ data = data.byteslice(i, 6) if i > 0
155
+ int1, int0 = data.unpack(INT16_INT32_BE)
156
+ int0 + (int1 << 32)
157
+ end
158
+
159
+ INT32_INT16_INT8_LE = 'VvC'.freeze
160
+ def slice_int56_le!(data)
161
+ int0, int1, int2 = data.unpack(INT32_INT16_INT8_LE)
162
+ data[0, 7] = EMPTY
163
+ int0 + (int1 << 32) + (int2 << 48)
164
+ end
165
+
166
+ def get_int56_le(data, i=0)
167
+ data = data.byteslice(i, 7) if i > 0
168
+ int0, int1, int2 = data.unpack(INT32_INT16_INT8_LE)
169
+ int0 + (int1 << 32) + (int2 << 48)
170
+ end
171
+
172
+ INT8_INT16_INT32_BE = 'CnN'.freeze
173
+ def slice_int56_be!(data)
174
+ int2, int1, int0 = data.unpack(INT8_INT16_INT32_BE)
175
+ data[0, 7] = EMPTY
176
+ int0 + (int1 << 32) + (int2 << 48)
177
+ end
178
+
179
+ def get_int56_be(data, i=0)
180
+ data = data.byteslice(i, 7) if i > 0
181
+ int2, int1, int0 = data.unpack(INT8_INT16_INT32_BE)
182
+ int0 + (int1 << 32) + (int2 << 48)
183
+ end
184
+
185
+ def slice_int64_le!(data)
186
+ int0, int1 = data.unpack(INT32_INT32_LE)
187
+ data[0, 8] = EMPTY
188
+ int0 + (int1 << 32)
189
+ end
190
+
191
+ def get_int64_le(data, i=0)
192
+ data = data.byteslice(i, 8) if i > 0
193
+ int0, int1 = data.unpack(INT32_INT32_LE)
194
+ int0 + (int1 << 32)
195
+ end
196
+
197
+ def slice_int64_be!(data)
198
+ int1, int0 = data.unpack(INT32_INT32_BE)
199
+ data[0, 8] = EMPTY
200
+ int0 + (int1 << 32)
201
+ end
202
+
203
+ def get_int64_be(data, i=0)
204
+ data = data.byteslice(i, 8) if i > 0
205
+ int1, int0 = data.unpack(INT32_INT32_BE)
206
+ int0 + (int1 << 32)
207
+ end
208
+
209
+ def get_sint8(data, i=0)
210
+ i = get_int8(data, i)
211
+ i - ((i & 128) << 1)
212
+ end
213
+
214
+ def slice_sint8!(data)
215
+ i = slice_int8!(data)
216
+ i - ((i & 128) << 1)
217
+ end
218
+
219
+ for size in [16, 24, 32, 40, 48, 56, 64]
220
+ for endian in %w{le be}
221
+ class_eval <<-EOF, __FILE__, __LINE__
222
+ def get_sint#{size}_#{endian}(data, i=0)
223
+ i = get_int#{size}_#{endian}(data, i)
224
+ i - ((i >> #{size - 1}) << #{size})
225
+ end
226
+
227
+ def slice_sint#{size}_#{endian}!(data)
228
+ i = slice_int#{size}_#{endian}!(data)
229
+ i - ((i >> #{size - 1}) << #{size})
230
+ end
231
+ EOF
232
+ end
233
+ end
234
+
235
+ BER_5 = 128 ** 5
236
+ BER_6 = 128 ** 6
237
+ BER_7 = 128 ** 7
238
+ BER_8 = 128 ** 8
239
+ BER_9 = 128 ** 9
240
+ def ber_size(int)
241
+ int < 128 ? 1 :
242
+ int < 16384 ? 2 :
243
+ int < 2097153 ? 3 :
244
+ int < 268435456 ? 4 :
245
+ int < BER_5 ? 5 :
246
+ int < BER_6 ? 6 :
247
+ int < BER_7 ? 7 :
248
+ int < BER_8 ? 8 :
249
+ int < BER_9 ? 9 :
250
+ 10
251
+ end
252
+
253
+ def get_ber(data, i=0)
254
+ res = 0
255
+ while true
256
+ if (byte = data.getbyte(i)) <= 127
257
+ res += byte
258
+ break
259
+ else
260
+ res = (res + (byte - 128)) * 128
261
+ i += 1
262
+ end
263
+ end
264
+ res
265
+ end
266
+
267
+ def slice_ber!(data)
268
+ res = 0
269
+ pos = 0
270
+ while true
271
+ if (byte = data.getbyte(pos)) <= 127
272
+ res += byte
273
+ break
274
+ else
275
+ res = (res + (byte - 128)) * 128
276
+ pos += 1
277
+ end
278
+ end
279
+ data[0, pos+1] = EMPTY
280
+ res
281
+ end
282
+
283
+ BINARY = ::Encoding::BINARY
284
+ def append_int8!(str, *ints)
285
+ str ||= ''.force_encoding(BINARY)
286
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
287
+ i = 0
288
+ sz = ints.size
289
+ while i < sz
290
+ str << (ints[i] & 255)
291
+ i += 1
292
+ end
293
+ str
294
+ end
295
+
296
+ def append_int16_le!(str, *ints)
297
+ str ||= ''.force_encoding(BINARY)
298
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
299
+ i = 0
300
+ sz = ints.size
301
+ while i < sz
302
+ int = ints[i]
303
+ str << (int & 255) << ((int>>8) & 255)
304
+ i += 1
305
+ end
306
+ str
307
+ end
308
+
309
+ def append_int24_le!(str, *ints)
310
+ str ||= ''.force_encoding(BINARY)
311
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
312
+ i = 0
313
+ sz = ints.size
314
+ while i < sz
315
+ int = ints[i]
316
+ str << (int & 255) << ((int>>8) & 255) <<
317
+ ((int>>16) & 255)
318
+ i += 1
319
+ end
320
+ str
321
+ end
322
+
323
+ def append_int32_le!(str, *ints)
324
+ str ||= ''.force_encoding(BINARY)
325
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
326
+ i = 0
327
+ sz = ints.size
328
+ while i < sz
329
+ int = ints[i]
330
+ str << (int & 255) << ((int>>8) & 255) <<
331
+ ((int>>16) & 255) << ((int>>24) & 255)
332
+ i += 1
333
+ end
334
+ str
335
+ end
336
+
337
+ def append_int40_le!(str, *ints)
338
+ str ||= ''.force_encoding(BINARY)
339
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
340
+ i = 0
341
+ sz = ints.size
342
+ while i < sz
343
+ int = ints[i]
344
+ str << (int & 255) << ((int>>8) & 255) <<
345
+ ((int>>16) & 255) << ((int>>24) & 255) <<
346
+ ((int>>32) & 255)
347
+ i += 1
348
+ end
349
+ str
350
+ end
351
+
352
+ def append_int48_le!(str, *ints)
353
+ str ||= ''.force_encoding(BINARY)
354
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
355
+ i = 0
356
+ sz = ints.size
357
+ while i < sz
358
+ int = ints[i]
359
+ str << [int & MAX_INT32, int >> 32].pack(INT32_INT16_LE)
360
+ i += 1
361
+ end
362
+ str
363
+ end
364
+
365
+ def append_int56_le!(str, *ints)
366
+ str ||= ''.force_encoding(BINARY)
367
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
368
+ i = 0
369
+ sz = ints.size
370
+ while i < sz
371
+ int = ints[i]
372
+ str << [int & MAX_INT32, (int >> 32) & MAX_INT16, (int >> 48)].pack(INT32_INT16_INT8_LE)
373
+ i += 1
374
+ end
375
+ str
376
+ end
377
+
378
+ def append_int64_le!(str, *ints)
379
+ str ||= ''.force_encoding(BINARY)
380
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
381
+ i = 0
382
+ sz = ints.size
383
+ while i < sz
384
+ int = ints[i]
385
+ str << [int & MAX_INT32, int >> 32].pack(INT32_INT32_LE)
386
+ i += 1
387
+ end
388
+ str
389
+ end
390
+
391
+ def append_int16_be!(str, *ints)
392
+ str ||= ''.force_encoding(BINARY)
393
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
394
+ i = 0
395
+ sz = ints.size
396
+ while i < sz
397
+ int = ints[i]
398
+ str << ((int>>8) & 255) << (int & 255)
399
+ i += 1
400
+ end
401
+ str
402
+ end
403
+
404
+ def append_int24_be!(str, *ints)
405
+ str ||= ''.force_encoding(BINARY)
406
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
407
+ i = 0
408
+ sz = ints.size
409
+ while i < sz
410
+ int = ints[i]
411
+ str << ((int>>16) & 255) << ((int>>8) & 255) << (int & 255)
412
+ i += 1
413
+ end
414
+ str
415
+ end
416
+
417
+ def append_int32_be!(str, *ints)
418
+ str ||= ''.force_encoding(BINARY)
419
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
420
+ i = 0
421
+ sz = ints.size
422
+ while i < sz
423
+ int = ints[i]
424
+ str << ((int>>24) & 255) << ((int>>16) & 255) <<
425
+ ((int>>8) & 255) << (int & 255)
426
+ i += 1
427
+ end
428
+ str
429
+ end
430
+
431
+ def append_int40_be!(str, *ints)
432
+ str ||= ''.force_encoding(BINARY)
433
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
434
+ i = 0
435
+ sz = ints.size
436
+ while i < sz
437
+ int = ints[i]
438
+ str << ((int>>32) & 255) << ((int>>24) & 255) <<
439
+ ((int>>16) & 255) << ((int>>8) & 255) <<
440
+ (int & 255)
441
+ i += 1
442
+ end
443
+ str
444
+ end
445
+
446
+ def append_int48_be!(str, *ints)
447
+ str ||= ''.force_encoding(BINARY)
448
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
449
+ i = 0
450
+ sz = ints.size
451
+ while i < sz
452
+ int = ints[i]
453
+ str << [int >> 32, int & MAX_INT32].pack(INT16_INT32_BE)
454
+ i += 1
455
+ end
456
+ str
457
+ end
458
+
459
+ def append_int56_be!(str, *ints)
460
+ str ||= ''.force_encoding(BINARY)
461
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
462
+ i = 0
463
+ sz = ints.size
464
+ while i < sz
465
+ int = ints[i]
466
+ str << [(int >> 48), (int >> 32) & MAX_INT16, int & MAX_INT32].pack(INT8_INT16_INT32_BE)
467
+ i += 1
468
+ end
469
+ str
470
+ end
471
+
472
+ def append_int64_be!(str, *ints)
473
+ str ||= ''.force_encoding(BINARY)
474
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
475
+ i = 0
476
+ sz = ints.size
477
+ while i < sz
478
+ int = ints[i]
479
+ str << [int >> 32, int & MAX_INT32].pack(INT32_INT32_BE)
480
+ i += 1
481
+ end
482
+ str
483
+ end
484
+
485
+ def append_bersize_int8!(str, *ints)
486
+ str ||= ''.force_encoding(BINARY)
487
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
488
+ append_ber!(str, ints.size)
489
+ append_int8!(str, *ints)
490
+ end
491
+
492
+ def append_bersize_int16_le!(str, *ints)
493
+ str ||= ''.force_encoding(BINARY)
494
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
495
+ append_ber!(str, ints.size * 2)
496
+ append_int16_le!(str, *ints)
497
+ end
498
+
499
+ def append_bersize_int24_le!(str, *ints)
500
+ str ||= ''.force_encoding(BINARY)
501
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
502
+ append_ber!(str, ints.size * 3)
503
+ append_int24_le!(str, *ints)
504
+ end
505
+
506
+ def append_bersize_int32_le!(str, *ints)
507
+ str ||= ''.force_encoding(BINARY)
508
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
509
+ append_ber!(str, ints.size * 4)
510
+ append_int32_le!(str, *ints)
511
+ end
512
+
513
+ def append_bersize_int40_le!(str, *ints)
514
+ str ||= ''.force_encoding(BINARY)
515
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
516
+ append_ber!(str, ints.size * 5)
517
+ append_int40_le!(str, *ints)
518
+ end
519
+
520
+ def append_bersize_int48_le!(str, *ints)
521
+ str ||= ''.force_encoding(BINARY)
522
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
523
+ append_ber!(str, ints.size * 6)
524
+ append_int48_le!(str, *ints)
525
+ end
526
+
527
+ def append_bersize_int56_le!(str, *ints)
528
+ str ||= ''.force_encoding(BINARY)
529
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
530
+ append_ber!(str, ints.size * 7)
531
+ append_int56_le!(str, *ints)
532
+ end
533
+
534
+ def append_bersize_int64_le!(str, *ints)
535
+ str ||= ''.force_encoding(BINARY)
536
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
537
+ append_ber!(str, ints.size * 8)
538
+ append_int64_le!(str, *ints)
539
+ end
540
+
541
+ def append_bersize_int16_be!(str, *ints)
542
+ str ||= ''.force_encoding(BINARY)
543
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
544
+ append_ber!(str, ints.size * 2)
545
+ append_int16_be!(str, *ints)
546
+ end
547
+
548
+ def append_bersize_int24_be!(str, *ints)
549
+ str ||= ''.force_encoding(BINARY)
550
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
551
+ append_ber!(str, ints.size * 3)
552
+ append_int24_be!(str, *ints)
553
+ end
554
+
555
+ def append_bersize_int32_be!(str, *ints)
556
+ str ||= ''.force_encoding(BINARY)
557
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
558
+ append_ber!(str, ints.size * 4)
559
+ append_int32_be!(str, *ints)
560
+ end
561
+
562
+ def append_bersize_int40_be!(str, *ints)
563
+ str ||= ''.force_encoding(BINARY)
564
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
565
+ append_ber!(str, ints.size * 5)
566
+ append_int40_be!(str, *ints)
567
+ end
568
+
569
+ def append_bersize_int48_be!(str, *ints)
570
+ str ||= ''.force_encoding(BINARY)
571
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
572
+ append_ber!(str, ints.size * 6)
573
+ append_int48_be!(str, *ints)
574
+ end
575
+
576
+ def append_bersize_int56_be!(str, *ints)
577
+ str ||= ''.force_encoding(BINARY)
578
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
579
+ append_ber!(str, ints.size * 7)
580
+ append_int56_be!(str, *ints)
581
+ end
582
+
583
+ def append_bersize_int64_be!(str, *ints)
584
+ str ||= ''.force_encoding(BINARY)
585
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
586
+ append_ber!(str, ints.size * 8)
587
+ append_int64_be!(str, *ints)
588
+ end
589
+
590
+ def append_int32size_int8_le!(str, *ints)
591
+ str ||= ''.force_encoding(BINARY)
592
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
593
+ append_int32_le!(str, ints.size)
594
+ append_int8!(str, *ints)
595
+ end
596
+
597
+ def append_int32size_int16_le!(str, *ints)
598
+ str ||= ''.force_encoding(BINARY)
599
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
600
+ append_int32_le!(str, ints.size * 2)
601
+ append_int16_le!(str, *ints)
602
+ end
603
+
604
+ def append_int32size_int24_le!(str, *ints)
605
+ str ||= ''.force_encoding(BINARY)
606
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
607
+ append_int32_le!(str, ints.size * 3)
608
+ append_int24_le!(str, *ints)
609
+ end
610
+
611
+ def append_int32size_int32_le!(str, *ints)
612
+ str ||= ''.force_encoding(BINARY)
613
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
614
+ append_int32_le!(str, ints.size * 4)
615
+ append_int32_le!(str, *ints)
616
+ end
617
+
618
+ def append_int32size_int40_le!(str, *ints)
619
+ str ||= ''.force_encoding(BINARY)
620
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
621
+ append_int32_le!(str, ints.size * 5)
622
+ append_int40_le!(str, *ints)
623
+ end
624
+
625
+ def append_int32size_int48_le!(str, *ints)
626
+ str ||= ''.force_encoding(BINARY)
627
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
628
+ append_int32_le!(str, ints.size * 6)
629
+ append_int48_le!(str, *ints)
630
+ end
631
+
632
+ def append_int32size_int56_le!(str, *ints)
633
+ str ||= ''.force_encoding(BINARY)
634
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
635
+ append_int32_le!(str, ints.size * 7)
636
+ append_int56_le!(str, *ints)
637
+ end
638
+
639
+ def append_int32size_int64_le!(str, *ints)
640
+ str ||= ''.force_encoding(BINARY)
641
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
642
+ append_int32_le!(str, ints.size * 8)
643
+ append_int64_le!(str, *ints)
644
+ end
645
+
646
+ def append_int32size_int8_be!(str, *ints)
647
+ str ||= ''.force_encoding(BINARY)
648
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
649
+ append_int32_be!(str, ints.size)
650
+ append_int8!(str, *ints)
651
+ end
652
+
653
+ def append_int32size_int16_be!(str, *ints)
654
+ str ||= ''.force_encoding(BINARY)
655
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
656
+ append_int32_be!(str, ints.size * 2)
657
+ append_int16_be!(str, *ints)
658
+ end
659
+
660
+ def append_int32size_int24_be!(str, *ints)
661
+ str ||= ''.force_encoding(BINARY)
662
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
663
+ append_int32_be!(str, ints.size * 3)
664
+ append_int24_be!(str, *ints)
665
+ end
666
+
667
+ def append_int32size_int32_be!(str, *ints)
668
+ str ||= ''.force_encoding(BINARY)
669
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
670
+ append_int32_be!(str, ints.size * 4)
671
+ append_int32_be!(str, *ints)
672
+ end
673
+
674
+ def append_int32size_int40_be!(str, *ints)
675
+ str ||= ''.force_encoding(BINARY)
676
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
677
+ append_int32_be!(str, ints.size * 5)
678
+ append_int40_be!(str, *ints)
679
+ end
680
+
681
+ def append_int32size_int48_be!(str, *ints)
682
+ str ||= ''.force_encoding(BINARY)
683
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
684
+ append_int32_be!(str, ints.size * 6)
685
+ append_int48_be!(str, *ints)
686
+ end
687
+
688
+ def append_int32size_int56_be!(str, *ints)
689
+ str ||= ''.force_encoding(BINARY)
690
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
691
+ append_int32_be!(str, ints.size * 7)
692
+ append_int56_be!(str, *ints)
693
+ end
694
+
695
+ def append_int32size_int64_be!(str, *ints)
696
+ str ||= ''.force_encoding(BINARY)
697
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
698
+ append_int32_be!(str, ints.size * 8)
699
+ append_int64_be!(str, *ints)
700
+ end
701
+
702
+ def append_string!(data, str)
703
+ data ||= ''.force_encoding(BINARY)
704
+ data << str.dup.force_encoding(BINARY)
705
+ end
706
+
707
+ BER_STRING = "wa*".freeze
708
+ INT32LE_STRING = "Va*".freeze
709
+ INT32BE_STRING = "Na*".freeze
710
+ def append_bersize_string!(data, str)
711
+ data ||= ''.force_encoding(BINARY)
712
+ data << [str.bytesize, str].pack(BER_STRING)
713
+ end
714
+
715
+ def append_int32size_string_le!(data, str)
716
+ data ||= ''.force_encoding(BINARY)
717
+ data << [str.bytesize, str].pack(INT32LE_STRING)
718
+ end
719
+
720
+ def append_int32size_string_be!(data, str)
721
+ data ||= ''.force_encoding(BINARY)
722
+ data << [str.bytesize, str].pack(INT32BE_STRING)
723
+ end
724
+
725
+ BER = 'w*'
726
+ INT32LE_BER = 'Vw'
727
+ INT32BE_BER = 'Nw'
728
+ def append_ber!(data, *ints)
729
+ data ||= ''.force_encoding(BINARY)
730
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
731
+ if ints.size == 1 && ints[0] < 128
732
+ data << ints[0]
733
+ else
734
+ data << ints.pack(BER)
735
+ end
736
+ end
737
+
738
+ def append_bersize_ber!(data, *ints)
739
+ data ||= ''.force_encoding(BINARY)
740
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
741
+ bers = ints.pack(BER)
742
+ append_ber!(data, bers.size)
743
+ data << bers
744
+ end
745
+
746
+ def append_int32size_ber_le!(data, *ints)
747
+ data ||= ''.force_encoding(BINARY)
748
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
749
+ bers = ints.pack(BER)
750
+ append_int32_le!(data, bers.size)
751
+ data << bers
752
+ end
753
+
754
+ def append_int32size_ber_be!(data, *ints)
755
+ data ||= ''.force_encoding(BINARY)
756
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
757
+ bers = ints.pack(BER)
758
+ append_int32_be!(data, bers.size)
759
+ data << bers
760
+ end
761
+
762
+ # complex
763
+
764
+ def append_int8_ber!(data, int1, *ints)
765
+ data ||= ''.force_encoding(BINARY)
766
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
767
+ data << (int1 & 255)
768
+ append_ber!(data, *ints)
769
+ end
770
+
771
+ def append_int8_int16_le!(data, int1, *ints)
772
+ data ||= ''.force_encoding(BINARY)
773
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
774
+ data << (int1 & 255)
775
+ append_int16_le!(data, *ints)
776
+ end
777
+
778
+ def append_int8_int24_le!(data, int1, *ints)
779
+ data ||= ''.force_encoding(BINARY)
780
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
781
+ data << (int1 & 255)
782
+ append_int24_le!(data, *ints)
783
+ end
784
+
785
+ def append_int8_int32_le!(data, int1, *ints)
786
+ data ||= ''.force_encoding(BINARY)
787
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
788
+ data << (int1 & 255)
789
+ append_int32_le!(data, *ints)
790
+ end
791
+
792
+ def append_int16_ber_le!(data, int1, *ints)
793
+ data ||= ''.force_encoding(BINARY)
794
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
795
+ append_int16_le!(data, int1)
796
+ append_ber!(data, *ints)
797
+ end
798
+
799
+ def append_int16_int8_le!(data, int1, *ints)
800
+ data ||= ''.force_encoding(BINARY)
801
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
802
+ append_int16_le!(data, int1)
803
+ append_int8!(data, *ints)
804
+ end
805
+
806
+ def append_int16_int24_le!(data, int1, *ints)
807
+ data ||= ''.force_encoding(BINARY)
808
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
809
+ append_int16_le!(data, int1)
810
+ append_int24_le!(data, *ints)
811
+ end
812
+
813
+ def append_int16_int32_le!(data, int1, *ints)
814
+ data ||= ''.force_encoding(BINARY)
815
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
816
+ append_int16_le!(data, int1)
817
+ append_int32_le!(data, *ints)
818
+ end
819
+
820
+ def append_int24_ber_le!(data, int1, *ints)
821
+ append_int24_le!(data, int1)
822
+ append_ber!(data, *ints)
823
+ end
824
+
825
+ def append_int24_int8_le!(data, int1, *ints)
826
+ data ||= ''.force_encoding(BINARY)
827
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
828
+ append_int24_le!(data, int1)
829
+ append_int8!(data, *ints)
830
+ end
831
+
832
+ def append_int24_int16_le!(data, int1, *ints)
833
+ data ||= ''.force_encoding(BINARY)
834
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
835
+ append_int24_le!(data, int1)
836
+ append_int16_le!(data, *ints)
837
+ end
838
+
839
+ def append_int24_int32_le!(data, int1, *ints)
840
+ data ||= ''.force_encoding(BINARY)
841
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
842
+ append_int24_le!(data, int1)
843
+ append_int32_le!(data, *ints)
844
+ end
845
+
846
+ def append_int32_ber_le!(data, int1, *ints)
847
+ data ||= ''.force_encoding(BINARY)
848
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
849
+ append_int32_le!(data, int1)
850
+ append_ber!(data, *ints)
851
+ end
852
+
853
+ def append_int32_int8_le!(data, int1, *ints)
854
+ data ||= ''.force_encoding(BINARY)
855
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
856
+ append_int32_le!(data, int1)
857
+ append_int8!(data, *ints)
858
+ end
859
+
860
+ def append_int32_int16_le!(data, int1, *ints)
861
+ data ||= ''.force_encoding(BINARY)
862
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
863
+ append_int32_le!(data, int1)
864
+ append_int16_le!(data, *ints)
865
+ end
866
+
867
+ def append_int32_int24_le!(data, int1, *ints)
868
+ data ||= ''.force_encoding(BINARY)
869
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
870
+ append_int32_le!(data, int1)
871
+ append_int24_le!(data, *ints)
872
+ end
873
+
874
+ def append_int8_int16_be!(data, int1, *ints)
875
+ data ||= ''.force_encoding(BINARY)
876
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
877
+ data << (int1 & 255)
878
+ append_int16_be!(data, *ints)
879
+ end
880
+
881
+ def append_int8_int24_be!(data, int1, *ints)
882
+ data ||= ''.force_encoding(BINARY)
883
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
884
+ data << (int1 & 255)
885
+ append_int24_be!(data, *ints)
886
+ end
887
+
888
+ def append_int8_int32_be!(data, int1, *ints)
889
+ data ||= ''.force_encoding(BINARY)
890
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
891
+ data << (int1 & 255)
892
+ append_int32_be!(data, *ints)
893
+ end
894
+
895
+ def append_int16_ber_be!(data, int1, *ints)
896
+ data ||= ''.force_encoding(BINARY)
897
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
898
+ append_int16_be!(data, int1)
899
+ append_ber!(data, *ints)
900
+ end
901
+
902
+ def append_int16_int8_be!(data, int1, *ints)
903
+ data ||= ''.force_encoding(BINARY)
904
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
905
+ append_int16_be!(data, int1)
906
+ append_int8!(data, *ints)
907
+ end
908
+
909
+ def append_int16_int24_be!(data, int1, *ints)
910
+ data ||= ''.force_encoding(BINARY)
911
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
912
+ append_int16_be!(data, int1)
913
+ append_int24_be!(data, *ints)
914
+ end
915
+
916
+ def append_int16_int32_be!(data, int1, *ints)
917
+ data ||= ''.force_encoding(BINARY)
918
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
919
+ append_int16_be!(data, int1)
920
+ append_int32_be!(data, *ints)
921
+ end
922
+
923
+ def append_int24_ber_be!(data, int1, *ints)
924
+ data ||= ''.force_encoding(BINARY)
925
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
926
+ append_int24_be!(data, int1)
927
+ append_ber!(data, *ints)
928
+ end
929
+
930
+ def append_int24_int8_be!(data, int1, *ints)
931
+ data ||= ''.force_encoding(BINARY)
932
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
933
+ append_int24_be!(data, int1)
934
+ append_int8!(data, *ints)
935
+ end
936
+
937
+ def append_int24_int16_be!(data, int1, *ints)
938
+ data ||= ''.force_encoding(BINARY)
939
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
940
+ append_int24_be!(data, int1)
941
+ append_int16_be!(data, *ints)
942
+ end
943
+
944
+ def append_int24_int32_be!(data, int1, *ints)
945
+ data ||= ''.force_encoding(BINARY)
946
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
947
+ append_int24_be!(data, int1)
948
+ append_int32_be!(data, *ints)
949
+ end
950
+
951
+ def append_int32_ber_be!(data, int1, *ints)
952
+ data ||= ''.force_encoding(BINARY)
953
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
954
+ append_int32_be!(data, int1)
955
+ append_ber!(data, *ints)
956
+ end
957
+
958
+ def append_int32_int8_be!(data, int1, *ints)
959
+ data ||= ''.force_encoding(BINARY)
960
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
961
+ append_int32_be!(data, int1)
962
+ append_int8!(data, *ints)
963
+ end
964
+
965
+ def append_int32_int16_be!(data, int1, *ints)
966
+ data ||= ''.force_encoding(BINARY)
967
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
968
+ append_int32_be!(data, int1)
969
+ append_int16_be!(data, *ints)
970
+ end
971
+
972
+ def append_int32_int24_be!(data, int1, *ints)
973
+ data ||= ''.force_encoding(BINARY)
974
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
975
+ append_int32_be!(data, int1)
976
+ append_int24_be!(data, *ints)
977
+ end
978
+
979
+ def append_ber_int8!(data, int1, *ints)
980
+ data ||= ''.force_encoding(BINARY)
981
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
982
+ append_ber!(data, int1)
983
+ append_int8!(data, *ints)
984
+ end
985
+
986
+ def append_ber_int16_le!(data, int1, *ints)
987
+ data ||= ''.force_encoding(BINARY)
988
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
989
+ append_ber!(data, int1)
990
+ append_int16_le!(data, *ints)
991
+ end
992
+
993
+ def append_ber_int24_le!(data, int1, *ints)
994
+ data ||= ''.force_encoding(BINARY)
995
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
996
+ append_ber!(data, int1)
997
+ append_int24_le!(data, *ints)
998
+ end
999
+
1000
+ def append_ber_int32_le!(data, int1, *ints)
1001
+ data ||= ''.force_encoding(BINARY)
1002
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
1003
+ append_ber!(data, int1)
1004
+ append_int32_le!(data, *ints)
1005
+ end
1006
+
1007
+ def append_ber_int8!(data, int1, *ints)
1008
+ data ||= ''.force_encoding(BINARY)
1009
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
1010
+ append_ber!(data, int1)
1011
+ append_int8!(data, *ints)
1012
+ end
1013
+
1014
+ def append_ber_int16_be!(data, int1, *ints)
1015
+ data ||= ''.force_encoding(BINARY)
1016
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
1017
+ append_ber!(data, int1)
1018
+ append_int16_be!(data, *ints)
1019
+ end
1020
+
1021
+ def append_ber_int24_be!(data, int1, *ints)
1022
+ data ||= ''.force_encoding(BINARY)
1023
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
1024
+ append_ber!(data, int1)
1025
+ append_int24_be!(data, *ints)
1026
+ end
1027
+
1028
+ def append_ber_int32_be!(data, int1, *ints)
1029
+ data ||= ''.force_encoding(BINARY)
1030
+ ints = ints[0] if ints.size == 1 && Array === ints[0]
1031
+ append_ber!(data, int1)
1032
+ append_int32_be!(data, *ints)
1033
+ end
1034
+ end
1035
+ end