rbstruct 0.0.6 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +79 -4
- data/examples/gpt.rb +11 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9bf4018d500ab91aade8cf73b92f763420ba4ff
|
4
|
+
data.tar.gz: 8b865a10504a8c52fd47e755dc0984b96bcfb801
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 470e4423d942659a83fbf07307ece5b57624c7eeff3793eb97bde1d3c660a6dc87ad504e63dd9a9fa59c9eaba5df1fe67291ce55f40bd1013758d49c5ebbaa8d
|
7
|
+
data.tar.gz: 34147337e1f8778faf3ad1971c7753fc59af09672dfcd51d7fab5212e69593850c5b611e1ba9ed5d15ab1e05ee073e9c0b7f374b3589bc2462f3a8b799020249
|
data/README.md
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
# Rbstruct
|
2
|
+
[](https://travis-ci.org/kaffepanna/rbtelldus)
|
2
3
|
|
3
|
-
|
4
|
+
## What is RbStruct
|
4
5
|
|
5
|
-
|
6
|
+
RbStruct is a ruby library providing a way of describing structured binary data much like structs in C are declared. RbStruct uses ruby arrays to hold data in memory and should be fairly fast.
|
7
|
+
|
8
|
+
## History
|
9
|
+
|
10
|
+
The first iteration of this library was created in late 2008 and put up on google code and forgotton. Initialy it was used for reading quake3
|
11
|
+
level and player model datafiles to be used in a game engine. The code was forgotten for about 6 years until I needed a way of modifing partitions and
|
12
|
+
I decided to make a gem out of it and put it on github.
|
6
13
|
|
7
14
|
## Installation
|
8
15
|
|
@@ -22,7 +29,75 @@ Or install it yourself as:
|
|
22
29
|
|
23
30
|
## Usage
|
24
31
|
|
25
|
-
|
32
|
+
RbStruct defines a kind of dsl to resembel how structs are declared in C.
|
33
|
+
|
34
|
+
Example of GPT partition header:
|
35
|
+
```ruby
|
36
|
+
GptHeader = RbStruct do
|
37
|
+
unsigned_char :signature, 8
|
38
|
+
unsigned_int :revision
|
39
|
+
unsigned_int :header_size
|
40
|
+
unsigned_int :crc32
|
41
|
+
unsigned_int :revserved
|
42
|
+
unsigned_long :current_lba
|
43
|
+
unsigned_long :backup_lba
|
44
|
+
unsigned_long :first_lba
|
45
|
+
unsigned_long :last_lba
|
46
|
+
unsigned_char :guid, 16
|
47
|
+
unsigned_long :partition_array_lba
|
48
|
+
unsigned_int :n_partition_array
|
49
|
+
unsigned_int :partition_entry_size
|
50
|
+
unsigned_int :partition_array_crc32
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
Fields are described by `<type> <symbol>, <number of elements>`. Nested structs are also supported
|
55
|
+
|
56
|
+
Ex:
|
57
|
+
```ruby
|
58
|
+
Bsp46DirEntry = RbStruct do
|
59
|
+
int :offset
|
60
|
+
int :n
|
61
|
+
end
|
62
|
+
|
63
|
+
Bsp46Header = RbStruct do
|
64
|
+
unsigned_char :magic, 4
|
65
|
+
int :version
|
66
|
+
struct Bsp46DirEntry, :direntries, 17
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
Reading and writing:
|
71
|
+
```ruby
|
72
|
+
f = File.open('map.bsp', 'r')
|
73
|
+
header = Bsp46Header.read(f)
|
74
|
+
|
75
|
+
header.write(f)
|
76
|
+
```
|
77
|
+
|
78
|
+
You can also choose to read multiple structs from the file in one go
|
79
|
+
```ruby
|
80
|
+
# this wouldt make sense but sure
|
81
|
+
headers = Bsp46Header.read(f, 10)
|
82
|
+
```
|
83
|
+
|
84
|
+
Getting and setting values:
|
85
|
+
```ruby
|
86
|
+
# Get value
|
87
|
+
header.version
|
88
|
+
|
89
|
+
# Set value
|
90
|
+
header.version = 10
|
91
|
+
```
|
92
|
+
|
93
|
+
RbStructs are also flexible letting you define your own methods on the structs
|
94
|
+
```ruby
|
95
|
+
Example = RbStruct do
|
96
|
+
int :version
|
97
|
+
def bump_version
|
98
|
+
version = version+1
|
99
|
+
end
|
100
|
+
```
|
26
101
|
|
27
102
|
## Development
|
28
103
|
|
@@ -32,7 +107,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
107
|
|
33
108
|
## Contributing
|
34
109
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
110
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kaffepanna/rbstruct.
|
36
111
|
|
37
112
|
|
38
113
|
## License
|
data/examples/gpt.rb
CHANGED
@@ -34,13 +34,22 @@ end
|
|
34
34
|
file = open(dev, 'r+')
|
35
35
|
|
36
36
|
file.seek(512)
|
37
|
-
header =
|
37
|
+
header = GptHeader.read(file)
|
38
38
|
|
39
|
+
file.seek(512*header.partition_array_lba)
|
40
|
+
lbas = GptPartEntry.read(file, header.n_partition_array)
|
39
41
|
file.seek(512*header.backup_lba)
|
40
|
-
backup =
|
42
|
+
backup = GptHeader.read(file)
|
43
|
+
|
41
44
|
|
42
45
|
puts "Primary header"
|
43
46
|
puts header.inspect
|
47
|
+
puts "----------------------------------------"
|
48
|
+
|
49
|
+
lbas.reject { |l| l.last_lba == 0 }.each do |l|
|
50
|
+
puts l.inspect
|
51
|
+
end
|
52
|
+
|
44
53
|
puts "----------------------------------------"
|
45
54
|
puts "Backup header"
|
46
55
|
puts backup.inspect
|