bin_struct 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is part of BinStruct
4
+ # see https://github.com/lemontree55/bin_struct for more informations
5
+ # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
6
+ # Copyright (C) 2024 LemonTree55 <lenontree@proton.me>
7
+ # This program is published under MIT license.
8
+
9
+ require 'forwardable'
10
+
11
+ module BinStruct
12
+ # This class mimics regular String, but it is {Fieldable}.
13
+ # @author Sylvain Daubert
14
+ # @since 3.1.6 no more a subclass or regular String
15
+ class String
16
+ extend Forwardable
17
+ include Fieldable
18
+ include LengthFrom
19
+
20
+ def_delegators :@string, :[], :to_s, :length, :size, :inspect, :==,
21
+ :unpack, :force_encoding, :encoding, :index, :empty?,
22
+ :encode, :slice, :slice!, :[]=
23
+
24
+ # @return [::String]
25
+ attr_reader :string
26
+ # @return [Integer]
27
+ attr_reader :static_length
28
+
29
+ # @param [Hash] options
30
+ # @option options [Types::Int,Proc] :length_from object or proc from which
31
+ # takes length when reading
32
+ # @option options [Integer] :static_length set a static length for this string
33
+ def initialize(options = {})
34
+ register_internal_string(+'')
35
+ initialize_length_from(options)
36
+ @static_length = options[:static_length]
37
+ end
38
+
39
+ def initialize_copy(_orig)
40
+ @string = @string.dup
41
+ end
42
+
43
+ # @param [::String] str
44
+ # @return [String] self
45
+ def read(str)
46
+ s = read_with_length_from(str)
47
+ register_internal_string s
48
+ self
49
+ end
50
+
51
+ alias old_sz_to_read sz_to_read
52
+ private :old_sz_to_read
53
+
54
+ # Size to read.
55
+ # Computed from static_length or length_from, if defined.
56
+ # @return [Integer]
57
+ # @since 3.1.6
58
+ def sz_to_read
59
+ return static_length if static_length?
60
+
61
+ old_sz_to_read
62
+ end
63
+
64
+ # Say if a static length is defined
65
+ # @return [Boolean]
66
+ # @since 3.1.6
67
+ def static_length?
68
+ !static_length.nil?
69
+ end
70
+
71
+ def format_inspect
72
+ inspect
73
+ end
74
+
75
+ # Append the given string to String
76
+ # @param [#to_s] str
77
+ # @return [self]
78
+ def <<(str)
79
+ @string << str.to_s
80
+ self
81
+ end
82
+
83
+ alias sz length
84
+ alias to_human to_s
85
+ alias from_human read
86
+
87
+ private
88
+
89
+ def register_internal_string(str)
90
+ @string = str
91
+ BinStruct.force_binary(@string)
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is part of BinStruct
4
+ # see https://github.com/lemontree55/bin_struct for more informations
5
+ # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
6
+ # Copyright (C) 2024 LemonTree55 <lenontree@proton.me>
7
+ # This program is published under MIT license.
8
+
9
+ module BinStruct
10
+ VERSION = '0.1.0'
11
+ end
data/lib/bin_struct.rb ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is part of BinStruct
4
+ # see https://github.com/lemontree55/bin_struct for more informations
5
+ # Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
6
+ # Copyright (C) 2024 LemonTree55 <lenontree@proton.me>
7
+ # This program is published under MIT license.
8
+
9
+ require_relative 'bin_struct/version'
10
+
11
+ # BinStruct module
12
+ # @author LemonTree55
13
+ module BinStruct
14
+ # BinStruct error class
15
+ class Error < StandardError; end
16
+
17
+ # Force binary encoding for +str+
18
+ # @param [String] str
19
+ # @return [String] binary encoded string
20
+ def self.force_binary(str)
21
+ str.dup.force_encoding(Encoding::BINARY)
22
+ end
23
+ end
24
+
25
+ require_relative 'bin_struct/fieldable'
26
+ require_relative 'bin_struct/int'
27
+ require_relative 'bin_struct/enum'
28
+ require_relative 'bin_struct/fields'
29
+ require_relative 'bin_struct/length_from'
30
+ require_relative 'bin_struct/abstract_tlv'
31
+ require_relative 'bin_struct/array'
32
+ require_relative 'bin_struct/string'
33
+ require_relative 'bin_struct/cstring'
34
+ require_relative 'bin_struct/int_string'
35
+ require_relative 'bin_struct/oui'
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bin_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - LemonTree55
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-07-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'BinStruct is a binary dissector and generator. It eases manipulating
14
+ complex binary data.
15
+
16
+ '
17
+ email:
18
+ - lenontree@proton.me
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files:
22
+ - README.md
23
+ files:
24
+ - CHANGELOG.md
25
+ - README.md
26
+ - lib/bin_struct.rb
27
+ - lib/bin_struct/abstract_tlv.rb
28
+ - lib/bin_struct/array.rb
29
+ - lib/bin_struct/cstring.rb
30
+ - lib/bin_struct/enum.rb
31
+ - lib/bin_struct/fieldable.rb
32
+ - lib/bin_struct/fields.rb
33
+ - lib/bin_struct/int.rb
34
+ - lib/bin_struct/int_string.rb
35
+ - lib/bin_struct/length_from.rb
36
+ - lib/bin_struct/oui.rb
37
+ - lib/bin_struct/string.rb
38
+ - lib/bin_struct/version.rb
39
+ homepage: https://github.com/lemontree55/bin_struct
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/lemontree55/bin_struct
44
+ source_code_uri: https://github.com/lemontree55/bin_struct
45
+ bug_tracker_uri: https://github.com/lemontree55/bin_struct/issues
46
+ post_install_message:
47
+ rdoc_options:
48
+ - "--title"
49
+ - BinStruct
50
+ - "--main"
51
+ - README.md
52
+ - "--inline-source"
53
+ - "--quiet"
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.7.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.3.15
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Binary dissector and generator
71
+ test_files: []