Sha256: 11aea83b6ba2f8f7663f03736e49088ff67daca6a997b690d4068065c1e531ad

Contents?: true

Size: 1.1 KB

Versions: 48

Compression:

Stored size: 1.1 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

48 entries across 48 versions & 1 rubygems

Version Path
aws-crt-0.2.0-universal-java lib/aws-crt/string_blob.rb
aws-crt-0.2.0-arm64-darwin lib/aws-crt/string_blob.rb
aws-crt-0.2.0-aarch64-linux lib/aws-crt/string_blob.rb
aws-crt-0.2.0 lib/aws-crt/string_blob.rb
aws-crt-0.1.9-x86_64-linux lib/aws-crt/string_blob.rb
aws-crt-0.1.9-x86_64-darwin lib/aws-crt/string_blob.rb
aws-crt-0.1.9-universal-java lib/aws-crt/string_blob.rb
aws-crt-0.1.9-arm64-darwin lib/aws-crt/string_blob.rb
aws-crt-0.1.9-aarch64-linux lib/aws-crt/string_blob.rb
aws-crt-0.1.9 lib/aws-crt/string_blob.rb
aws-crt-0.1.8-x86_64-linux lib/aws-crt/string_blob.rb
aws-crt-0.1.8-x86_64-darwin lib/aws-crt/string_blob.rb
aws-crt-0.1.8-universal-java lib/aws-crt/string_blob.rb
aws-crt-0.1.8-arm64-darwin lib/aws-crt/string_blob.rb
aws-crt-0.1.8-aarch64-linux lib/aws-crt/string_blob.rb
aws-crt-0.1.8 lib/aws-crt/string_blob.rb
aws-crt-0.1.7 lib/aws-crt/string_blob.rb
aws-crt-0.1.7-x86_64-linux lib/aws-crt/string_blob.rb
aws-crt-0.1.7-x86_64-darwin lib/aws-crt/string_blob.rb
aws-crt-0.1.7-universal-java lib/aws-crt/string_blob.rb