Sha256: fe3f4552cca7131a08d5a017997216b689e27d8d6c6e6b686521135ce583bbbf
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
# coding: utf-8 # frozen_string_literal: true # This file is part of PacketGen # See https://github.com/sdaubert/packetgen for more informations # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net> # This program is published under MIT license. module PacketGen module Types # This class handles null-terminated strings (aka C strings). # @author Sylvain Daubert class CString < ::String # @param [Hash] options # @option options [Integer] :static_length set a static length for this string def initialize(options={}) super() @static_length = options[:static_length] end # @param [::String] str # @return [String] self def read(str) s = str.to_s s = s[0, @static_length] if @static_length.is_a? Integer idx = s.index(0.chr) s = s[0, idx] unless idx.nil? self.replace s self end # get null-terminated string # @return [String] def to_s if defined?(@static_length) && @static_length.is_a?(Integer) if self.size >= @static_length s = self[0, @static_length] s[-1] = "\x00".encode(s.encoding) PacketGen.force_binary s else PacketGen.force_binary(self + "\0" * (@static_length - self.length)) end else PacketGen.force_binary(self + +"\x00".encode(self.encoding)) end end # @return [Integer] def sz if @static_length.is_a? Integer @static_length else to_s.size end end # Populate CString from a human readable string # @param [String] str # @return [self] def from_human(str) read str end # @return [String] def to_human idx = self.index(+"\x00".encode(self.encoding)) || self.sz self[0, idx] end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
packetgen-3.1.4 | lib/packetgen/types/cstring.rb |