Module: IdPack::LZString

Defined in:
lib/id_pack/lz_string.rb

Constant Summary collapse

KEY_STR_BASE64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
KEY_STR_URI_SAFE =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$"

Class Method Summary collapse

Class Method Details

._compress(uncompressed, bits_per_char, &get_char_from_int) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/id_pack/lz_string.rb', line 121

def _compress uncompressed, bits_per_char, &get_char_from_int
  return "" if uncompressed.nil?

  context_dictionary = {}
  context_dictionary_to_create = {}
  context_c = ""
  context_wc = ""
  context_w = ""
  context_enlarge_in = 2
  context_dict_size = 3
  context_num_bits = 2
  context_data = []
  context_data_val = 0
  context_data_position = 0

  uncompressed.length.times do |ii|
    context_c = uncompressed[ii]

    if !context_dictionary[context_c]
      context_dictionary[context_c] = context_dict_size
      context_dict_size += 1
      context_dictionary_to_create[context_c] = true
    end

    context_wc = context_w + context_c

    if context_dictionary[context_wc]
      context_w = context_wc
    else
      if context_dictionary_to_create[context_w]
        if context_w[0].ord < 256
          context_num_bits.times do |i|
            context_data_val = (context_data_val << 1)

            if context_data_position == bits_per_char - 1
              context_data_position = 0
              context_data.push(get_char_from_int.call(context_data_val))
              context_data_val = 0
            else
              context_data_position += 1
            end
          end

          value = context_w[0].ord

          8.times do |i|
            context_data_val = (context_data_val << 1) | (value & 1)

            if context_data_position == bits_per_char - 1
              context_data_position = 0
              context_data.push(get_char_from_int.call(context_data_val))
              context_data_val = 0
            else
              context_data_position += 1
            end

            value = value >> 1
          end
        else
          value = 1

          context_num_bits.times do |i|
            context_data_val = (context_data_val << 1) | value

            if context_data_position == bits_per_char - 1
              context_data_position = 0
              context_data.push(get_char_from_int.call(context_data_val))
              context_data_val = 0
            else
              context_data_position += 1
            end

            value = 0
          end

          value = context_w[0].ord

          16.times do |i|
            context_data_val = (context_data_val << 1) | (value & 1)

            if context_data_position == bits_per_char - 1
              context_data_position = 0
              context_data.push(get_char_from_int.call(context_data_val))
              context_data_val = 0
            else
              context_data_position += 1
            end

            value = value >> 1
          end
        end

        context_enlarge_in -= 1

        if context_enlarge_in == 0
          context_enlarge_in = 2 ** context_num_bits
          context_num_bits += 1
        end

        context_dictionary_to_create.delete(context_w)
      else
        value = context_dictionary[context_w]

        context_num_bits.times do |i|
          context_data_val = (context_data_val << 1) | (value & 1)

          if context_data_position == bits_per_char - 1
            context_data_position = 0
            context_data.push(get_char_from_int.call(context_data_val))
            context_data_val = 0
          else
            context_data_position += 1
          end

          value = value >> 1
        end
      end

      context_enlarge_in -= 1

      if context_enlarge_in == 0
        context_enlarge_in = 2 ** context_num_bits
        context_num_bits += 1
      end

      context_dictionary[context_wc] = context_dict_size
      context_dict_size += 1
      context_w = context_c.to_s
    end
  end

  if context_w != ""
    if context_dictionary_to_create[context_w]
      if context_w[0].ord < 256
        context_num_bits.times do |i|
          context_data_val = context_data_val << 1

          if context_data_position == bits_per_char - 1
            context_data_position = 0
            context_data.push(get_char_from_int.call(context_data_val))
            context_data_val = 0
          else
            context_data_position += 1
          end
        end

        value = context_w[0].ord

        8.times do |i|
          context_data_val = (context_data_val << 1) | (value & 1)

          if context_data_position == bits_per_char - 1
            context_data_position = 0
            context_data.push(get_char_from_int.call(context_data_val))
            context_data_val = 0
          else
            context_data_position += 1
          end

          value = value >> 1
        end
      else
        value = 1

        context_num_bits.times do |i|
          context_data_val = (context_data_val << 1) | value

          if context_data_position == bits_per_char - 1
            context_data_position = 0
            context_data.push(get_char_from_int.call(context_data_val))
            context_data_val = 0
          else
            context_data_position += 1
          end

          value = 0
        end

        value = context_w[0].ord

        16.times do |i|
          context_data_val = (context_data_val << 1) | (value & 1)

          if context_data_position == bits_per_char - 1
            context_data_position = 0
            context_data.push(get_char_from_int.call(context_data_val))
            context_data_val = 0
          else
            context_data_position += 1
          end

          value = value >> 1
        end
      end

      context_enlarge_in -= 1

      if context_enlarge_in == 0
        context_enlarge_in = 2 ** context_num_bits
        context_num_bits += 1
      end

      context_dictionary_to_create.delete(context_w)
    else
      value = context_dictionary[context_w]

      context_num_bits.times do |i|
        context_data_val = (context_data_val << 1) | (value & 1)

        if context_data_position == bits_per_char - 1
          context_data_position = 0
          context_data.push(get_char_from_int.call(context_data_val))
          context_data_val = 0
        else
          context_data_position += 1
        end

        value = value >> 1
      end
    end

    context_enlarge_in -= 1

    if context_enlarge_in == 0
      context_enlarge_in = 2 ** context_num_bits
      context_num_bits += 1
    end
  end

  value = 2

  context_num_bits.times do |i|
    context_data_val = (context_data_val << 1) | (value & 1)

    if context_data_position == bits_per_char - 1
      context_data_position = 0
      context_data.push(get_char_from_int.call(context_data_val))
      context_data_val = 0
    else
      context_data_position += 1
    end

    value = value >> 1
  end

  while true do
    context_data_val = (context_data_val << 1)

    if context_data_position == bits_per_char - 1
      context_data.push(get_char_from_int.call(context_data_val))
      break
    else
      context_data_position += 1
    end
  end

  context_data.join('')
end

._decompress(length, reset_value, &get_next_value) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/id_pack/lz_string.rb', line 389

def _decompress length, reset_value, &get_next_value
  dictionary = []
  enlarge_in = 4
  dict_size = 4
  num_bits = 3
  entry = ""
  result = []
  data = {
    val: get_next_value.call(0),
    position: reset_value,
    index: 1
  }

  3.times do |i|
    dictionary[i] = i
  end

  bits = 0
  maxpower = 2 ** 2
  power = 1

  while power != maxpower
    resb = data[:val] & data[:position]
    data[:position] = data[:position] >> 1

    if data[:position] == 0
      data[:position] = reset_value
      data[:val] = get_next_value.call(data[:index])
      data[:index] += 1
    end

    bits |= (resb > 0 ? 1 : 0) * power
    power = power << 1
  end

  case bits
  when 0
    bits = 0
    maxpower = 2 ** 8
    power = 1

    while power != maxpower
      resb = data[:val] & data[:position]
      data[:position] = data[:position] >> 1

      if data[:position] == 0
        data[:position] = reset_value
        data[:val] = get_next_value.call(data[:index])
        data[:index] += 1
      end

      bits |= (resb > 0 ? 1 : 0) * power
      power <<= 1
    end

    c = [bits].pack 'U'
  when 1
    bits = 0
    maxpower = 2 ** 16
    power = 1

    while power != maxpower
      resb = data[:val] & data[:position]
      data[:position] = data[:position] >> 1

      if data[:position] == 0
        data[:position] = reset_value
        data[:val] = get_next_value.call(data[:index])
        data[:index] += 1
      end

      bits |= (resb > 0 ? 1 : 0) * power
      power <<= 1
    end

    c = [bits].pack 'U'
  when 2
    return ""
  end

  dictionary[3] = c
  w = c
  result.push(c)

  while true do
    return "" if data[:index] > length

    bits = 0
    maxpower = 2 ** num_bits
    power = 1

    while power != maxpower
      resb = data[:val] & data[:position]
      data[:position] = data[:position] >> 1

      if data[:position] == 0
        data[:position] = reset_value
        data[:val] = get_next_value.call(data[:index])
        data[:index] += 1
      end

      bits |= (resb > 0 ? 1 : 0) * power
      power <<= 1
    end

    c = bits

    case bits
    when 0
      bits = 0
      maxpower = 2 ** 8
      power = 1

      while power != maxpower
        resb = data[:val] & data[:position]
        data[:position] >>= 1

        if data[:position] == 0
          data[:position] = reset_value
          data[:val] = get_next_value.call(data[:index])
          data[:index] += 1
        end

        bits |= (resb > 0 ? 1 : 0) * power
        power <<= 1
      end

      dictionary[dict_size] = [bits].pack 'U'
      dict_size += 1
      c = dict_size - 1
      enlarge_in -= 1
    when 1
      bits = 0
      maxpower = 2 ** 16
      power = 1

      while power != maxpower
        resb = data[:val] & data[:position]
        data[:position] >>= 1

        if data[:position] == 0
          data[:position] = reset_value
          data[:val] = get_next_value.call(data[:index])
          data[:index] += 1
        end

        bits |= (resb > 0 ? 1 : 0) * power
        power <<= 1
      end

      dictionary[dict_size] = [bits].pack 'U'
      dict_size += 1
      c = dict_size - 1
      enlarge_in -= 1
    when 2
      return result.join("")
    end

    if enlarge_in == 0
      enlarge_in = 2 ** num_bits
      num_bits += 1
    end

    if dictionary[c]
      entry = dictionary[c]
    else
      return nil if c != dict_size

      entry = w + w[0]
    end

    result.push(entry)

    dictionary[dict_size] = w + entry[0]
    dict_size += 1
    enlarge_in -= 1

    w = entry

    if enlarge_in == 0
      enlarge_in = 2 ** num_bits
      num_bits += 1
    end
  end
end

.compress(uncompressed) ⇒ Object



115
116
117
118
119
# File 'lib/id_pack/lz_string.rb', line 115

def compress uncompressed
  _compress(uncompressed, 16) do |a|
    [a].pack 'U'
  end
end

.compress_to_base64(input) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/id_pack/lz_string.rb', line 22

def compress_to_base64 input
  return "" if input.nil?

  res = _compress(input, 6) do |a|
    KEY_STR_BASE64[a]
  end

  case res.length % 4
  when 0 then res
  when 1 then res + "==="
  when 2 then res + "=="
  when 3 then res + "="
  end
end

.compress_to_encoded_uri_component(input) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/id_pack/lz_string.rb', line 96

def compress_to_encoded_uri_component input
  return "" if input.nil?

  _compress(input, 6) do |a|
    KEY_STR_URI_SAFE[a]
  end
end

.compress_to_uint8_array(uncompressed) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/id_pack/lz_string.rb', line 63

def compress_to_uint8_array uncompressed
  compressed = compress(uncompressed)
  buf = []

  compressed.length.times do |i|
    current_value = compressed[i].ord
    buf[i * 2] = current_value >> 8 # TODO: >>> 8 how to do it in ruby?
    buf[i * 2 + 1] = current_value % 256
  end

  buf
end

.compress_to_utf16(input) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/id_pack/lz_string.rb', line 46

def compress_to_utf16 input
  return "" if input.nil?

  _compress(input, 15) do |a|
    [a + 32].pack 'U'
  end + " "
end

.decompress(compressed) ⇒ Object



380
381
382
383
384
385
386
387
# File 'lib/id_pack/lz_string.rb', line 380

def decompress compressed
  return "" if compressed.nil?
  return null if compressed == ""

  _decompress(compressed.length, 32768) do |index|
    compressed[index].ord
  end
end

.decompress_from_base64(input) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/id_pack/lz_string.rb', line 37

def decompress_from_base64 input
  return "" if input.nil?
  return nil if input == ""

  _decompress(input.length, 32) do |index|
    get_base_value(KEY_STR_BASE64, input[index])
  end
end

.decompress_from_encoded_uri_component(input) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/id_pack/lz_string.rb', line 104

def decompress_from_encoded_uri_component input
  return "" if input.nil?
  return nil if input == ""

  input.gsub!(/ /, "+")

  _decompress(input.length, 32) do |index|
    get_base_value(KEY_STR_URI_SAFE, input[index])
  end
end

.decompress_from_uint8_array(compressed) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/id_pack/lz_string.rb', line 76

def decompress_from_uint8_array compressed
  return decompress(compressed) if compressed.nil?

  buf = []

  (compressed.length / 2).times do |i|
    buf[i] = compressed[i * 2] * 256 + compressed[i * 2 + 1]
  end

  result = []

  buf.each do |c|
    result.push(
      [a + 32].pack 'U'
    )
  end

  decompress(result.join(''))
end

.decompress_from_utf16(compressed) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/id_pack/lz_string.rb', line 54

def decompress_from_utf16 compressed
  return "" if compressed.nil?
  return nil if compressed == ""

  _decompress(compressed.length, 16384) do |index|
    compressed[index].ord - 32
  end
end

.get_base_value(alphabet, character) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/id_pack/lz_string.rb', line 8

def get_base_value alphabet, character
  @base_reverse_dic ||= {}

  if !@base_reverse_dic[alphabet]
    @base_reverse_dic[alphabet] = {}

    alphabet.length.times do |i|
      @base_reverse_dic[alphabet][alphabet[i]] = i
    end
  end

  @base_reverse_dic[alphabet][character]
end