<commit_before>module ComicWalker
  class ItemDecoder
    module Unknown
      module_function

      # @param [String] Base64-encoded encrypted data
      # @param [String] key3 key?
      # @return [Array<Array<Fixnum>>] Chunked encrypted data
      CHUNK_SIZE = 65536 / 4 * 3
      def split_encrypted_data(a, key3)
        bytes = Base64.decode64(a).unpack('C*')
        func1(key3, bytes).each_slice(CHUNK_SIZE).to_a
      end

      # @param [Array<Array<Fixnum>>] Chunked encrypted data
      # @param [String] key1 key?
      # @param [String] key2 key?
      # @param [Integer] bsize block size?
      # @param [Integer] hsize size?
      # @return [Array<Array<Fixnum>>] Chunked decrypted data
      def decrypt(chunks, key1, key2, bsize, hsize)
        s = []
        i = 0
        chunks.each do |chunk|
          while i < chunk.size
            s << chunk[i]
            i += bsize
          end
          i -= chunk.size
        end

        c = func2(s, key2)
        i = 0
        j = 0
        chunks.each do |chunk|
          while i < chunk.size
            chunk[i] = c[j]
            j += 1
            i += bsize
          end
          i -= chunk.size
        end
        func3(key1, chunks[0], hsize)
        chunks
      end

      # @param [String] key
      # @param [Array<Fixnum>] chunk Encrypted data slice
      # @return [nil] Modify the given chunk
      def func3(key, chunk, hsize)
        hsize = [hsize, chunk.size].min
        ary1 = Array.new(hsize)
        hsize.times do |i|
          ary1[i] = chunk[i]
        end
        ary2 = func2(ary1, key)
        hsize.times do |i|
          chunk[i] = ary2[i]
        end
      end

      # @param [Array<Fixnum>] ary binary data?
      # @param [String] key key?
      # @return [Array<Fixnum>]
      def func2(ary, key)
        ary1 = Array.new(ary.size)
        tbl = gen_table(key)
        d = 0
        e = 0
        ary.size.times do |i|
          e = (e + 1) % 256
          d = (d + tbl[e]) % 256
          tbl[e], tbl[d] = tbl[d], tbl[e]
          c = (tbl[e] + tbl[d]) % 256
          ary1[i] = ary[i] ^ tbl[c]
        end
        ary1
      end

      # @param [String] key key?
      # @param [Array<Fixnum>] bytes Encrypted data
      # @return [Array<Fixnum>]
      def func1(key, bytes)
        d = gen_table(key)
        bytes.map.with_index do |b, i|
          b ^ d[i % 256]
        end
      end

      # @param [String] key key?
      # @return [Array<Fixnum>] Some table
      def gen_table(key)
        key = key.unpack('U*')
        e = []
        d = []
        256.times do |b|
          e[b] = b
        end
        256.times do |b|
          d[b] = key[b % key.size]
        end
        a = 0
        256.times do |b|
          a = (a + e[b] + d[b]) % 256
          e[b], e[a] = e[a], e[b]
        end<commit_msg>Ruby way<commit_after>module ComicWalker
  class ItemDecoder
    module Unknown
      module_function

      # @param [String] Base64-encoded encrypted data
      # @param [String] key3 key?
      # @return [Array<Array<Fixnum>>] Chunked encrypted data
      CHUNK_SIZE = 65536 / 4 * 3
      def split_encrypted_data(a, key3)
        bytes = Base64.decode64(a).unpack('C*')
        func1(key3, bytes).each_slice(CHUNK_SIZE).to_a
      end

      # @param [Array<Array<Fixnum>>] Chunked encrypted data
      # @param [String] key1 key?
      # @param [String] key2 key?
      # @param [Integer] bsize block size?
      # @param [Integer] hsize header size?
      # @return [Array<Array<Fixnum>>] Chunked decrypted data
      def decrypt(chunks, key1, key2, bsize, hsize)
        s = []
        chunks.each do |chunk|
          0.step(chunk.size-1, bsize) do |i|
            s << chunk[i]
          end
        end

        c = func2(s, key2)
        # s.size == c.size

        chunks.each do |chunk|
          0.step(chunk.size-1, bsize) do |i|
            chunk[i] = c.shift
          end
        end
        func3(key1, chunks[0], hsize)
        chunks
      end

      # @param [String] key
      # @param [Array<Fixnum>] chunk Encrypted data slice
      # @return [nil] Modify the given chunk
      def func3(key, chunk, hsize)
        hsize = [hsize, chunk.size].min
        func2(chunk.slice(0, hsize), key).each.with_index do |x, i|
          chunk[i] = x
        end
      end

      # @param [Array<Fixnum>] ary binary data?
      # @param [String] key key?
      # @return [Array<Fixnum>]
      def func2(ary, key)
        # RC4?
        tbl = gen_table(key)
        i = 0
        j = 0
        ary.map do |x|
          i = (i + 1) % 256
          j = (j + tbl[i]) % 256
          tbl[i], tbl[j] = tbl[j], tbl[i]
          c = (tbl[i] + tbl[j]) % 256
          x ^ tbl[c]
        end
      end

      # @param [String] key key?
      # @param [Array<Fixnum>] bytes Encrypted data
      # @return [Array<Fixnum>]
      def func1(key, bytes)
        d = gen_table(key)
        bytes.map.with_index do |b, i|
          b ^ d[i % 256]
        end
      end

      # @param [String] key key?
      # @return [Array<Fixnum>] Some table
      def gen_table(key)
        key = key.unpack('C*')
        e = []
        d = []
        256.times do |b|
          e[b] = b
        end
        256.times do |b|
          d[b] = key[b % key.size]
        end
        a = 0
        256.times do |b|
          a = (a + e[b] + d[b]) % 256
          e[b], e[a] = e[a], e[b]
        end
{'fraction_non_alphanumeric': 0.128285820066642, 'fraction_numerical': 0.02480562754535357, 'mean_word_length': 1.5354293758798685, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3211700', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1868, 'n_words': 508}