byteman 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 12aa595840a63b8f3737a5f0ed88241d4db1c26d5fdac6a232d2fe2f44fdc1b4
4
- data.tar.gz: f6e7c849df1d5194a96a18024aca9210120033b0bde2fb48ef4972c4e4998c72
3
+ metadata.gz: f97c79daebb65b447178abe2eb39be01d98583d0f813cd60a92cd2b6172d4f31
4
+ data.tar.gz: a700e4f59a8621fb2c6b9289e42db7ce4715cf897ae487dc2b0ca9021be0d3f5
5
5
  SHA512:
6
- metadata.gz: f8f5a4caed096e505a08bd44c26bd54f6eef2625018f8c628c7e7010884cde132c9893be2dd928a0922d9126c46aa987d4d2ee4b5e0ce402ae58f2d87ca7c371
7
- data.tar.gz: 9f63095dd068bb73b346957450ddbdf776d57c953c60a49d1170543c6f71e6008c622232f2f1cdc7a994840846bb5c47cc568cb27b52c6c56a70afd48b0d3b26
6
+ metadata.gz: 11117098c87ef66779236692bb2c8dcda4e2225fffe56afbdb606a6467a943e3fab2d77f53cceec083e10878b4913c1b649ef619b2f605d7a1d16f9865e37f9c
7
+ data.tar.gz: 054cda5e6c527b0ef81032eedb80efd68c50636ffab2fbe68b9153838585b33c55c938fe94cb6e16458d367aa44b2a16302674e30932db55b33c83a670bbc01a
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- byteman (0.1.0)
4
+ byteman (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Byteman
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/byteman`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Simple gem to allow manipulation of data into other forms. Specifically, transform data between byte buffers (integer arrays with every number representing a byte ie an integer from 0-255), hexdigest strings, hex strings, and integers.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,37 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ See spec for most examples. Usage is relatively simple. For example, the `.hex` method can transform hexdigest strings, byte arrays, and integers into its corresponding hex string. For example:
24
+
25
+ ```ruby
26
+ Byteman.hex(123456) #=> "\x01\xE2@"
27
+ Byteman.hex([92, 123, 1, 209]) #=> "\\{\x01\xD1"
28
+ ```
29
+
30
+ You can pad your numbers to a specific byte length or bit length using the `.pad` method:
31
+
32
+ ```ruby
33
+ Byteman.pad(num: [43,223], len: 4) #=> [0,0,43, 223]
34
+ Byteman.pad(num: 1234, len: 4) #=> "\x00\x00\x04\xD2"
35
+ Byteman.pad(num: "\xE3\x09", len: 8) #=> "\x00\x00\x00\x00\x00\x00\xE3\x09"
36
+ Byteman.pad(num: 42, len: 8, type: :bits) #=> "00101010"
37
+ Byteman.pad(num: "11001", len: 16, type: :bits) #=> "0000000000011001"
38
+ ```
39
+ And there are more methods to facilitate easy manipulation of bytestreams. All are documented with rdoc and are tested in the spec folder. Some of these methods are:
40
+
41
+ `.digest2buf`, which turns a hexdigest string into a byte buffer:
42
+ ```ruby
43
+ Byteman.digest2buf("68656c6c6f20776f726c64") #=> [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
44
+ ```
45
+
46
+ `.digest2int`, which turns a hexdigest string into an integer:
47
+ ```ruby
48
+ Byteman.digest2int("168656c6c6f20776f726c64") #=> 435692254137895873546447972
49
+
50
+ ```
51
+
52
+ And similar methods such as `.hexdigest`, `.buf2int`, etc.
53
+
26
54
 
27
55
  ## Development
28
56
 
@@ -24,16 +24,26 @@ module Byteman
24
24
  # Note that this pads 0s to the front of the number, and returns a byte string if an integer or string is the input arg, and returns an array buffer if an array buffer is entered
25
25
  #
26
26
  # Unexpected results will occur if the number is greater than the number of total padded byte length
27
- def self.pad(num: nil, len: , type: :bytes)
27
+ def self.pad(num: nil, len: , type: :bytes, lsb: false)
28
28
  raise ArgumentError.new("Type must be :bytes or :bits") if type != :bytes && type != :bits
29
29
  if type == :bytes
30
30
  if num.is_a?(Array)
31
- overflow = len - (num.length % len)
32
- return [0] * overflow + num
31
+ overflow = len - num.length
32
+ if overflow == 0
33
+ return num
34
+ elsif overflow < 0
35
+ return num[(num.length - len)..-1]
36
+ else
37
+ if lsb
38
+ return num + ([0] * overflow)
39
+ else
40
+ return [0] * overflow + num
41
+ end
42
+ end
33
43
  elsif num.is_a?(Integer)
34
- hex(pad(num: int2buf(num), len: len))
44
+ hex(pad(num: int2buf(num), len: len, lsb: lsb))
35
45
  elsif num.is_a?(String)
36
- pad(num: num.unpack("C*"), len: len).pack("C*")
46
+ pad(num: num.unpack("C*"), len: len, lsb: lsb).pack("C*")
37
47
  else
38
48
  raise ArgumentError.new("Num must be a Array, Integer, or ByteString")
39
49
  end
@@ -45,9 +55,20 @@ module Byteman
45
55
  else
46
56
  raise ArgumentError("If performing bit padding, the input must be an Integer or a string of bits")
47
57
  end
48
- overflow = len - (bin_num.length % len)
49
- padded = "0" * overflow + bin_num
50
- return padded
58
+ overflow = len - bin_num.length
59
+ if overflow == 0
60
+ return bin_num
61
+ elsif overflow < 0
62
+ return bin_num[(bin_num.length - len)..-1]
63
+ else
64
+ if lsb
65
+ padded = bin_num + "0" * overflow
66
+ return padded
67
+ else
68
+ padded = "0" * overflow + bin_num
69
+ return padded
70
+ end
71
+ end
51
72
  end
52
73
  end
53
74
 
@@ -1,3 +1,3 @@
1
1
  module Byteman
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: byteman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - micahshute
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-26 00:00:00.000000000 Z
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Allows simple transformation of data into hex strings, hexdigest strings,
14
14
  integer byte arrays, etc. as well as padding your data to a certain byte length