bit-struct 0.13.5 → 0.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/History.txt +22 -1
  3. data/LICENSE +56 -0
  4. data/{README.txt → README.md} +94 -92
  5. data/Rakefile +56 -30
  6. data/examples/field-ripper.rb +1 -1
  7. data/examples/ip.rb +6 -6
  8. data/examples/longlong.rb +2 -2
  9. data/examples/md.rb +1 -1
  10. data/examples/modular-def.rb +2 -2
  11. data/examples/native.rb +3 -3
  12. data/examples/ping.rb +1 -1
  13. data/examples/raw.rb +1 -1
  14. data/examples/switch-endian.rb +2 -2
  15. data/examples/vector.rb +2 -2
  16. data/lib/bit-struct.rb +0 -11
  17. data/lib/bit-struct/bit-struct.rb +61 -143
  18. data/lib/bit-struct/char-field.rb +3 -3
  19. data/lib/bit-struct/field.rb +94 -0
  20. data/lib/bit-struct/fields.rb +13 -13
  21. data/lib/bit-struct/float-field.rb +4 -4
  22. data/lib/bit-struct/hex-octet-field.rb +3 -3
  23. data/lib/bit-struct/nested-field.rb +8 -8
  24. data/lib/bit-struct/octet-field.rb +4 -4
  25. data/lib/bit-struct/signed-field.rb +23 -28
  26. data/lib/bit-struct/text-field.rb +2 -2
  27. data/lib/bit-struct/unsigned-field.rb +24 -27
  28. data/lib/bit-struct/vector-field.rb +7 -7
  29. data/lib/bit-struct/vector.rb +24 -22
  30. data/lib/bit-struct/yaml.rb +22 -3
  31. data/test/test-endian.rb +5 -5
  32. data/test/test-vector.rb +6 -6
  33. data/test/test.rb +71 -46
  34. metadata +43 -69
  35. data/.gitignore +0 -3
  36. data/TODO +0 -20
  37. data/tasks/ann.rake +0 -80
  38. data/tasks/bones.rake +0 -20
  39. data/tasks/gem.rake +0 -201
  40. data/tasks/git.rake +0 -40
  41. data/tasks/notes.rake +0 -27
  42. data/tasks/post_load.rake +0 -34
  43. data/tasks/rdoc.rake +0 -51
  44. data/tasks/rubyforge.rake +0 -55
  45. data/tasks/setup.rb +0 -292
  46. data/tasks/spec.rake +0 -54
  47. data/tasks/svn.rake +0 -47
  48. data/tasks/test.rake +0 -40
  49. data/tasks/zentest.rake +0 -36
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a1cf35ff5b015befc041abc2494b386401d3c01e8a9c5712e3f61a428d182328
4
+ data.tar.gz: afbaee46ef4571da92e49ab6b428d60a6babfb846cf7309f274e4c05a2597d0c
5
+ SHA512:
6
+ metadata.gz: d484839de3555a0389591c8a33727bed2f032b64161e4056d61263da319e6b3ca7e3e98bd5c1c8da3cd5e5773fb2a83475834530c756569cdbce5ac44584a068
7
+ data.tar.gz: dd2ba2763c4ceda28774b75d0fd52b7022700e64707adf6e3788c13164b5c77e85bb123d1085a64d2c4a4465cc0274807d92385dbee14cc988461fc23bfb4531
data/History.txt CHANGED
@@ -1,3 +1,24 @@
1
+ bit-struct 0.17
2
+
3
+ - updated for ruby 3.0
4
+
5
+ bit-struct 0.16
6
+
7
+ - updated for ruby 2.4
8
+
9
+ bit-struct 0.15
10
+
11
+ - modernize rakefile and gemspec
12
+ - switch to minitest
13
+
14
+ bit-struct 0.14
15
+
16
+ - updated for ruby 2.0
17
+
18
+ bit-struct 0.13.6
19
+
20
+ - Fixed bug in #to_a. Thanks Michael Edgar.
21
+
1
22
  bit-struct 0.13
2
23
 
3
24
  - Packaged as a gem using bones.
@@ -22,7 +43,7 @@ bit-struct 0.10
22
43
  - Fixed a bug when calling #to_yaml on a BitStruct with a pad field. Thanks
23
44
  to Jay Reitz for reporting it and providing the solution.
24
45
 
25
- - Added BitStruct.default_options. Particularly useful for default endian
46
+ - Added BitStruct.default_options. Particularly useful for default endian
26
47
  settings--see examples/native.rb.
27
48
 
28
49
  - Fixed a bug that prevented warning about field name conflicts with
data/LICENSE ADDED
@@ -0,0 +1,56 @@
1
+ bit-struct is copyrighted free software by Joel VanderWerf <vjoel@users.sourceforge.net>
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a) place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b) use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c) give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d) make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a) distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b) accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c) give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d) make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
@@ -1,4 +1,4 @@
1
- = BitStruct
1
+ # BitStruct #
2
2
 
3
3
  Class for packed binary data stored in ruby Strings. BitStruct accessors, generated from user declared fields, use pack/unpack to treat substrings as fields with a specified portable format.
4
4
 
@@ -37,25 +37,25 @@ Field options (specifiable as :foo => val or "foo" => val) include:
37
37
  * *fixed*: float stored as fixed-point integer, with specified scale factor
38
38
 
39
39
 
40
- == Installation
40
+ ## Installation ##
41
41
 
42
42
  For .gem:
43
43
 
44
- gem install bit-struct
44
+ gem install bit-struct
45
45
 
46
46
  For .tgz, unpack and then:
47
47
 
48
- ruby install.rb config
49
- ruby install.rb setup
50
- ruby install.rb install
48
+ ruby install.rb config
49
+ ruby install.rb setup
50
+ ruby install.rb install
51
51
 
52
- == Uses
52
+ ## Uses ##
53
53
 
54
54
  BitStruct is useful for defining packets used in network protocols. This is especially useful for raw IP--see examples/ping-recv.rb. All multibyte numeric fields are stored by default in network order.
55
55
 
56
56
  BitStruct is most efficient when your data is primarily treated as a binary string, and only secondarily treated as a data structure. (For instance, you are routing packets from one socket to another, possibly looking at one or two fields as it passes through or munging some headers.) If accessor operations are a bottleneck, a better approach is to define a class that wraps an array and uses pack/unpack when the object needs to behave like a binary string.
57
57
 
58
- == Features
58
+ ## Features ##
59
59
 
60
60
  * Extensible with user-defined field classes.
61
61
 
@@ -77,13 +77,13 @@ BitStruct is most efficient when your data is primarily treated as a binary stri
77
77
 
78
78
  * Includes tests, examples, and rdoc API documentation.
79
79
 
80
- == Limitations
80
+ ## Limitations ##
81
81
 
82
82
  * Fields that are not aligned on byte boundaries may cross no more than two bytes boundaries. (See examples/byte-bdy.rb.)
83
83
 
84
84
  * No variable length fields (except the #rest field).
85
-
86
- == Future plans
85
+
86
+ ## Future plans ##
87
87
 
88
88
  * Currently, the library is written in pure ruby. The implementation uses Array#pack and String#unpack calls, as well as shifting and masking in pure ruby. Future versions will optionally generate a customized C extension for better efficiency.
89
89
 
@@ -91,97 +91,99 @@ BitStruct is most efficient when your data is primarily treated as a binary stri
91
91
 
92
92
  * Remove field size and alignment limitations.
93
93
 
94
- == Example
94
+ ## Example ##
95
95
 
96
96
  An IP packet can be defined and used like this:
97
97
 
98
- require 'bit-struct'
99
-
100
- class IP < BitStruct
101
- unsigned :ip_v, 4, "Version"
102
- unsigned :ip_hl, 4, "Header length"
103
- unsigned :ip_tos, 8, "TOS"
104
- unsigned :ip_len, 16, "Length"
105
- unsigned :ip_id, 16, "ID"
106
- unsigned :ip_off, 16, "Frag offset"
107
- unsigned :ip_ttl, 8, "TTL"
108
- unsigned :ip_p, 8, "Protocol"
109
- unsigned :ip_sum, 16, "Checksum"
110
- octets :ip_src, 32, "Source addr"
111
- octets :ip_dst, 32, "Dest addr"
112
- rest :body, "Body of message"
113
-
114
- note " rest is application defined message body"
115
-
116
- initial_value.ip_v = 4
117
- initial_value.ip_hl = 5
118
- end
119
-
120
- ip = IP.new
121
- ip.ip_tos = 0
122
- ip.ip_len = 0
123
- ip.ip_id = 0
124
- ip.ip_off = 0
125
- ip.ip_ttl = 255
126
- ip.ip_p = 255
127
- ip.ip_sum = 0
128
- ip.ip_src = "192.168.1.4"
129
- ip.ip_dst = "192.168.1.255"
130
- ip.body = "This is the payload text."
131
- ip.ip_len = ip.length
132
-
133
- puts ip.inspect
134
- puts "-"*50
135
- puts ip.inspect_detailed
136
- puts "-"*50
137
- puts IP.describe
98
+ ```ruby
99
+ require 'bit-struct'
100
+
101
+ class IP < BitStruct
102
+ unsigned :ip_v, 4, "Version"
103
+ unsigned :ip_hl, 4, "Header length"
104
+ unsigned :ip_tos, 8, "TOS"
105
+ unsigned :ip_len, 16, "Length"
106
+ unsigned :ip_id, 16, "ID"
107
+ unsigned :ip_off, 16, "Frag offset"
108
+ unsigned :ip_ttl, 8, "TTL"
109
+ unsigned :ip_p, 8, "Protocol"
110
+ unsigned :ip_sum, 16, "Checksum"
111
+ octets :ip_src, 32, "Source addr"
112
+ octets :ip_dst, 32, "Dest addr"
113
+ rest :body, "Body of message"
114
+
115
+ note " rest is application defined message body"
116
+
117
+ initial_value.ip_v = 4
118
+ initial_value.ip_hl = 5
119
+ end
120
+
121
+ ip = IP.new
122
+ ip.ip_tos = 0
123
+ ip.ip_len = 0
124
+ ip.ip_id = 0
125
+ ip.ip_off = 0
126
+ ip.ip_ttl = 255
127
+ ip.ip_p = 255
128
+ ip.ip_sum = 0
129
+ ip.ip_src = "192.168.1.4"
130
+ ip.ip_dst = "192.168.1.255"
131
+ ip.body = "This is the payload text."
132
+ ip.ip_len = ip.length
133
+
134
+ puts ip.inspect
135
+ puts "-"*50
136
+ puts ip.inspect_detailed
137
+ puts "-"*50
138
+ puts IP.describe
139
+ ```
138
140
 
139
141
  (Note that you can also construct an IP packet by passing a string to new, or by passing a hash of <tt>field,value</tt> pairs, or by providing a block that is yielded the new BitStruct.)
140
142
 
141
143
  The output of this fragment is:
142
144
 
143
- #<IP ip_v=4, ip_hl=5, ip_tos=0, ip_len=45, ip_id=0, ip_off=0, ip_ttl=255, ip_p=255, ip_sum=0, ip_src="192.168.1.4", ip_dst="192.168.1.255", body="This is the payload text.">
144
- --------------------------------------------------
145
- IP:
146
- Version = 4
147
- Header length = 5
148
- TOS = 0
149
- Length = 45
150
- ID = 0
151
- Frag offset = 0
152
- TTL = 255
153
- Protocol = 255
154
- Checksum = 0
155
- Source addr = "192.168.1.4"
156
- Dest addr = "192.168.1.255"
157
- Body of message = "This is the payload text."
158
- --------------------------------------------------
159
-
160
- Description of IP Packet:
161
- byte: type name [size] description
162
- ----------------------------------------------------------------------
163
- @0: unsigned ip_v [ 4b] Version
164
- @0: unsigned ip_hl [ 4b] Header length
165
- @1: unsigned ip_tos [ 8b] TOS
166
- @2: unsigned ip_len [ 16b] Length
167
- @4: unsigned ip_id [ 16b] ID
168
- @6: unsigned ip_off [ 16b] Frag offset
169
- @8: unsigned ip_ttl [ 8b] TTL
170
- @9: unsigned ip_p [ 8b] Protocol
171
- @10: unsigned ip_sum [ 16b] Checksum
172
- @12: octets ip_src [ 32b] Source addr
173
- @16: octets ip_dst [ 32b] Dest addr
174
- rest is application defined message body
175
-
176
- == Web site
177
-
178
- The current version of this software can be found at http://redshift.sourceforge.net/bit-struct.
179
-
180
- == License
145
+ #<IP ip_v=4, ip_hl=5, ip_tos=0, ip_len=45, ip_id=0, ip_off=0, ip_ttl=255, ip_p=255, ip_sum=0, ip_src="192.168.1.4", ip_dst="192.168.1.255", body="This is the payload text.">
146
+ --------------------------------------------------
147
+ IP:
148
+ Version = 4
149
+ Header length = 5
150
+ TOS = 0
151
+ Length = 45
152
+ ID = 0
153
+ Frag offset = 0
154
+ TTL = 255
155
+ Protocol = 255
156
+ Checksum = 0
157
+ Source addr = "192.168.1.4"
158
+ Dest addr = "192.168.1.255"
159
+ Body of message = "This is the payload text."
160
+ --------------------------------------------------
161
+
162
+ Description of IP Packet:
163
+ byte: type name [size] description
164
+ ----------------------------------------------------------------------
165
+ @0: unsigned ip_v [ 4b] Version
166
+ @0: unsigned ip_hl [ 4b] Header length
167
+ @1: unsigned ip_tos [ 8b] TOS
168
+ @2: unsigned ip_len [ 16b] Length
169
+ @4: unsigned ip_id [ 16b] ID
170
+ @6: unsigned ip_off [ 16b] Frag offset
171
+ @8: unsigned ip_ttl [ 8b] TTL
172
+ @9: unsigned ip_p [ 8b] Protocol
173
+ @10: unsigned ip_sum [ 16b] Checksum
174
+ @12: octets ip_src [ 32b] Source addr
175
+ @16: octets ip_dst [ 32b] Dest addr
176
+ rest is application defined message body
177
+
178
+ ## Web site ##
179
+
180
+ The current version of this software can be found at https://github.com/vjoel/bit-struct.
181
+
182
+ ## License ##
181
183
 
182
184
  This software is distributed under the Ruby license. See http://www.ruby-lang.org.
183
185
 
184
- == Author
186
+ ## Author ##
185
187
 
186
188
  Joel VanderWerf, mailto:vjoel@users.sourceforge.net
187
- Copyright (c) 2005-2009, Joel VanderWerf.
189
+ Copyright (c) 2005-2014, Joel VanderWerf.
data/Rakefile CHANGED
@@ -1,38 +1,64 @@
1
- # Look in the tasks/setup.rb file for the various options that can be
2
- # configured in this Rakefile. The .rake files in the tasks directory
3
- # are where the options are used.
4
-
5
- begin
6
- require 'bones'
7
- Bones.setup
8
- rescue LoadError
9
- begin
10
- load 'tasks/setup.rb'
11
- rescue LoadError
12
- raise RuntimeError, '### please install the "bones" gem ###'
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ PRJ = "bit-struct"
5
+
6
+ def version
7
+ @version ||= begin
8
+ require 'bit-struct'
9
+ warn "BitStruct::VERSION not a string" unless
10
+ BitStruct::VERSION.kind_of? String
11
+ BitStruct::VERSION
13
12
  end
14
13
  end
15
14
 
16
- ensure_in_path 'lib'
17
- require 'bit-struct/bit-struct'
15
+ def tag
16
+ @tag ||= "#{PRJ}-#{version}"
17
+ end
18
+
19
+ desc "Run unit tests"
20
+ Rake::TestTask.new :test do |t|
21
+ t.libs << "lib"
22
+ t.test_files = FileList["test/*.rb"]
23
+ end
24
+
25
+ desc "Commit, tag, and push repo; build and push gem"
26
+ task :release => "release:is_new_version" do
27
+ require 'tempfile'
28
+
29
+ sh "gem build #{PRJ}.gemspec"
18
30
 
19
- #task :default => 'spec:run'
31
+ file = Tempfile.new "template"
32
+ begin
33
+ file.puts "release #{version}"
34
+ file.close
35
+ sh "git commit --allow-empty -a -v -t #{file.path}"
36
+ ensure
37
+ file.close unless file.closed?
38
+ file.unlink
39
+ end
20
40
 
21
- PROJ.name = 'bit-struct'
22
- PROJ.authors = 'Joel VanderWerf'
23
- PROJ.email = 'vjoel@users.sourceforge.net'
24
- PROJ.url = 'http://rubyforge.org/projects/bit-struct/'
25
- PROJ.version = BitStruct::VERSION
26
- PROJ.rubyforge.name = 'bit-struct'
27
- PROJ.summary = "Library for packed binary data stored in ruby Strings"
28
- PROJ.description = <<END
29
- Library for packed binary data stored in ruby Strings. Useful for accessing fields in network packets and binary files.
30
- END
31
- PROJ.changes = File.read(PROJ.history_file)[/^\w.*?(?=^\w)/m]
41
+ sh "git tag #{tag}"
42
+ sh "git push"
43
+ sh "git push --tags"
44
+
45
+ sh "gem push #{tag}.gem"
46
+ end
32
47
 
33
- PROJ.spec.opts << '--color'
34
- PROJ.test.files = Dir["test/*.rb"]
48
+ namespace :release do
49
+ desc "Diff to latest release"
50
+ task :diff do
51
+ latest = `git describe --abbrev=0 --tags --match '#{PRJ}-*'`.chomp
52
+ sh "git diff #{latest}"
53
+ end
35
54
 
36
- task :release => ["gem:release", "doc:release"]
55
+ desc "Log to latest release"
56
+ task :log do
57
+ latest = `git describe --abbrev=0 --tags --match '#{PRJ}-*'`.chomp
58
+ sh "git log #{latest}.."
59
+ end
37
60
 
38
- # EOF
61
+ task :is_new_version do
62
+ abort "#{tag} exists; update version!" unless `git tag -l #{tag}`.empty?
63
+ end
64
+ end
@@ -13,7 +13,7 @@ end
13
13
 
14
14
  class BS2 < BitStruct
15
15
  fields_to_add = BS1.fields.select {|f| f.name.to_s =~ /[234]/}
16
-
16
+
17
17
  fields_to_add.each do |field|
18
18
  add_field(field.name, field.length, field.options)
19
19
  end
data/examples/ip.rb CHANGED
@@ -15,7 +15,7 @@ class IP < BitStruct
15
15
  rest :body, "Body of message"
16
16
 
17
17
  note " rest is application defined message body"
18
-
18
+
19
19
  initial_value.ip_v = 4
20
20
  initial_value.ip_hl = 5
21
21
  end
@@ -33,7 +33,7 @@ if __FILE__ == $0
33
33
  ip1.ip_dst = "192.168.1.255"
34
34
  ip1.body = "This is the payload text."
35
35
  ip1.ip_len = ip1.length
36
-
36
+
37
37
  ip2 = IP.new do |ip|
38
38
  ip.ip_tos = 0
39
39
  ip.ip_len = 0
@@ -47,7 +47,7 @@ if __FILE__ == $0
47
47
  ip.body = "This is the payload text."
48
48
  ip.ip_len = ip.length
49
49
  end
50
-
50
+
51
51
  ip3 = IP.new(
52
52
  :ip_tos => 0,
53
53
  :ip_len => 0,
@@ -62,15 +62,15 @@ if __FILE__ == $0
62
62
  ) do |ip|
63
63
  ip.ip_len = ip.length
64
64
  end
65
-
65
+
66
66
  ip4 = IP.new(ip1) # Construct from a BitStruct (or String)
67
-
67
+
68
68
  raise unless ip1 == ip2
69
69
  raise unless ip1 == ip3
70
70
  raise unless ip1 == ip4
71
71
 
72
72
  ip = ip1
73
-
73
+
74
74
  puts ip.inspect
75
75
  puts "-"*50
76
76
  puts ip.inspect_detailed