Sha256: aeabd875b5885233a4c58cf578a5e181098237215c483f3bc1b9f5f3d73bcfd0

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'stringio'

module Aws
  module Crt
    # module for CRT Blob utility methods
    # CRT encodes lists of strings as [length, str*]
    # using null padded, unsigned long
    module StringBlob
      # Encode an array of strings into
      # a buffer (blob)
      # @param strings [Array<String>]
      # @return buffer (Array<char>)
      def self.encode(strings)
        buffer = StringIO.new
        strings.each do |s|
          e = s.to_s.unpack('c*')
          buffer << [e.length].pack('N')
          buffer << (e).pack('c*')
        end
        buffer.string.unpack('c*')
      end

      # Decode a blob (StringBlob)/Buffer into
      # an array of strings
      # @param buffer - array of chars (buffer)
      # @return strings
      def self.decode(buffer)
        strings = []
        i = 0
        while i < buffer.size
          len = buffer[i, 4].pack('c*').unpack1('N')
          strings << (buffer[i + 4, len].pack('c*'))
                     .force_encoding(Encoding::UTF_8)
          i += len + 4
        end
        strings
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aws-crt-0.1.5-x64-mingw32 lib/aws-crt/string_blob.rb
aws-crt-0.1.4-x64-mingw32 lib/aws-crt/string_blob.rb
aws-crt-0.1.2-x86_64-mingw32 lib/aws-crt/string_blob.rb