Module: LZString::Custom

Included in:
LZString
Defined in:
lib/lzstring/custom.rb

Overview

Module for custom encoding/decoding

Class Method Summary collapse

Class Method Details

.compress_to_custom(input, key_str) ⇒ String

Compress a string to custom encoding

Parameters:

  • input (String)

    String to compress

  • key_str (String)

    Custom key string for compression

Returns:

  • (String)

    Compressed string with custom encoding



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lzstring/custom.rb', line 8

def self.compress_to_custom(input, key_str)
  return "" if input.nil? || input.empty? || key_str.nil? || key_str.empty?

  begin
    # Force input to UTF-8 encoding
    input = input.to_s.dup.force_encoding(Encoding::UTF_8)
    key_str = key_str.to_s.dup.force_encoding(Encoding::UTF_8)

    # Validate key string length
    raise ArgumentError, "Custom key string must have at least 2 characters" if key_str.length < 2

    res_str = ""
    val = 0
    c = 0

    # Use the _compress function with custom parameters
    LZString._compress(input, 6) do |a|
      val = (val << 6) | a
      c += 6

      while c >= 6
        c -= 6
        index = (val >> c) & 63
        res_str += begin
          key_str[index].chr
        rescue
          "?"
        end
      end

      a
    end

    # Handle remaining bits
    if c.positive?
      res_str += begin
        key_str[((val << 6) >> c) & 63].chr
      rescue
        "?"
      end
    end

    res_str
  rescue
    # Log error and return empty string on failure
    # STDERR.puts "Custom compression error: #{e.message}"
    ""
  end
end

.decompress_from_custom(input, key_str) ⇒ String?

Decompress a string from custom encoding

Parameters:

  • input (String)

    Compressed string with custom encoding

  • key_str (String)

    Custom key string used for compression

Returns:

  • (String, nil)

    Decompressed string or nil if decompression fails



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lzstring/custom.rb', line 62

def self.decompress_from_custom(input, key_str)
  return "" if input.nil? || input.empty? || key_str.nil? || key_str.empty?

  begin
    # Force input to UTF-8 encoding
    input = input.to_s.dup.force_encoding(Encoding::UTF_8)
    key_str = key_str.to_s.dup.force_encoding(Encoding::UTF_8)

    # Validate key string length
    raise ArgumentError, "Custom key string must have at least 2 characters" if key_str.length < 2

    # Create a reverse mapping for the key string
    reverse_dict = {}
    key_str.chars.each_with_index do |char, index|
      reverse_dict[char] = index
    end

    # Use the _decompress function with custom parameters
    result = LZString._decompress(input.length, 32) do |index|
      if index >= input.length
        0
      else
        char = input[index]
        if reverse_dict.key?(char)
          reverse_dict[char]
        else
          0 # Use 0 for characters not in the key string
        end
      end
    end

    # Ensure proper UTF-8 encoding of the result
    if result.is_a?(String)
      # Force UTF-8 encoding and replace invalid bytes
      result.force_encoding(Encoding::UTF_8)

      # Check if the result is valid UTF-8, if not try to repair
      unless result.valid_encoding?
        # Replace invalid sequences with a replacement character
        result = result.encode(Encoding::UTF_8,
                               Encoding::UTF_8,
                               invalid: :replace,
                               undef: :replace,
                               replace: "?")
      end

    end
    result
  rescue
    # Log error and return nil on failure
    # STDERR.puts "Custom decompression error: #{e.message}"
    nil
  end
end