ctypes 0.2.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 +7 -0
- data/.standard.yml +3 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/CONTRIBUTING.md +55 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +21 -0
- data/MAINTAINERS.md +3 -0
- data/README.md +390 -0
- data/Rakefile +10 -0
- data/SECURITY.md +57 -0
- data/ctypes.gemspec +40 -0
- data/lib/ctypes/array.rb +180 -0
- data/lib/ctypes/bitfield/builder.rb +246 -0
- data/lib/ctypes/bitfield.rb +278 -0
- data/lib/ctypes/bitmap.rb +154 -0
- data/lib/ctypes/enum/builder.rb +85 -0
- data/lib/ctypes/enum.rb +201 -0
- data/lib/ctypes/exporter.rb +50 -0
- data/lib/ctypes/helpers.rb +190 -0
- data/lib/ctypes/importers/castxml/loader.rb +150 -0
- data/lib/ctypes/importers/castxml.rb +59 -0
- data/lib/ctypes/importers.rb +7 -0
- data/lib/ctypes/int.rb +147 -0
- data/lib/ctypes/missing_bytes_error.rb +24 -0
- data/lib/ctypes/pad.rb +56 -0
- data/lib/ctypes/pretty_print_helpers.rb +31 -0
- data/lib/ctypes/string.rb +154 -0
- data/lib/ctypes/struct/builder.rb +242 -0
- data/lib/ctypes/struct.rb +529 -0
- data/lib/ctypes/terminated.rb +65 -0
- data/lib/ctypes/type.rb +195 -0
- data/lib/ctypes/union/builder.rb +220 -0
- data/lib/ctypes/union.rb +637 -0
- data/lib/ctypes/version.rb +8 -0
- data/lib/ctypes.rb +102 -0
- data/sig/ctypes.rbs +4 -0
- metadata +92 -0
data/lib/ctypes.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# SPDX-FileCopyrightText: 2025 Cisco
|
4
|
+
# SPDX-License-Identifier: MIT
|
5
|
+
|
6
|
+
require "dry-types"
|
7
|
+
require "pp" # standard:disable Lint/RedundantRequireStatement
|
8
|
+
|
9
|
+
require_relative "ctypes/version"
|
10
|
+
require_relative "ctypes/type"
|
11
|
+
require_relative "ctypes/int"
|
12
|
+
require_relative "ctypes/helpers"
|
13
|
+
require_relative "ctypes/pretty_print_helpers"
|
14
|
+
require_relative "ctypes/enum"
|
15
|
+
require_relative "ctypes/bitmap"
|
16
|
+
require_relative "ctypes/struct"
|
17
|
+
require_relative "ctypes/string"
|
18
|
+
require_relative "ctypes/array"
|
19
|
+
require_relative "ctypes/terminated"
|
20
|
+
require_relative "ctypes/union"
|
21
|
+
require_relative "ctypes/bitfield"
|
22
|
+
require_relative "ctypes/exporter"
|
23
|
+
require_relative "ctypes/pad"
|
24
|
+
|
25
|
+
# Manipulate binary data in ruby using C-like data types
|
26
|
+
module CTypes
|
27
|
+
class Error < StandardError; end
|
28
|
+
|
29
|
+
class TruncatedValueError < Error; end
|
30
|
+
|
31
|
+
class UnknownAttributeError < Error; end
|
32
|
+
|
33
|
+
class UnknownMemberError < Error; end
|
34
|
+
|
35
|
+
class UnknownFieldError < Error; end
|
36
|
+
|
37
|
+
class TerminatorNotFoundError < Error; end
|
38
|
+
|
39
|
+
# @api private
|
40
|
+
Endian = Dry::Types["coercible.symbol"].enum(*%i[big little])
|
41
|
+
|
42
|
+
# set the endian for any datatype that does not have an explicit endian set
|
43
|
+
#
|
44
|
+
# @param value [Symbol] endian, :big or little
|
45
|
+
#
|
46
|
+
# @example big endian
|
47
|
+
# CTypes.default_endian = :big
|
48
|
+
# t = CTypes::UInt32
|
49
|
+
# t.pack(0xdeadbeef) # => "\xde\xad\xbe\xef"
|
50
|
+
# t.pack(0xdeadbeef, endian: :little) # => "\xef\xbe\xad\xde"
|
51
|
+
#
|
52
|
+
# # create a type that overrides the default endian
|
53
|
+
# l = CTypes::UInt32.with_endian(:little)
|
54
|
+
# l.pack(0xdeadbeef) # => "\xef\xbe\xad\xde"
|
55
|
+
#
|
56
|
+
# @example little endian
|
57
|
+
# CTypes.default_endian = :little
|
58
|
+
# t = CTypes::UInt32
|
59
|
+
# t.pack(0xdeadbeef) # => "\xef\xbe\xad\xde"
|
60
|
+
# t.pack(0xdeadbeef, endian: :big) # => "\xde\xad\xbe\xef"
|
61
|
+
#
|
62
|
+
def self.default_endian=(value)
|
63
|
+
@endian = Endian[value]
|
64
|
+
end
|
65
|
+
|
66
|
+
# get the default endian for the system; defaults to native endian
|
67
|
+
def self.default_endian
|
68
|
+
@endian ||= host_endian
|
69
|
+
end
|
70
|
+
|
71
|
+
# get the endian of the system this code is running on
|
72
|
+
def self.host_endian
|
73
|
+
@host_endian ||= ("\xde\xad".unpack1("S") == 0xDEAD) ? :big : :little
|
74
|
+
end
|
75
|
+
|
76
|
+
# set a unknown type lookup method to use in the layout blocks of
|
77
|
+
# `Ctypes::Struct` and `CTypes::Union`.
|
78
|
+
#
|
79
|
+
# Note: the current implementation is not thread-safe.
|
80
|
+
#
|
81
|
+
# @example
|
82
|
+
# @my_types = { id_t: uint32 }
|
83
|
+
# my_struct = CTypes.using_type_lookup(->(n) { @my_types[n] }) do
|
84
|
+
# struct do
|
85
|
+
# attribute id, id_t
|
86
|
+
# end
|
87
|
+
# end
|
88
|
+
def self.using_type_lookup(lookup)
|
89
|
+
@type_lookup ||= []
|
90
|
+
@type_lookup.push(lookup)
|
91
|
+
yield
|
92
|
+
ensure
|
93
|
+
@type_lookup.pop
|
94
|
+
end
|
95
|
+
|
96
|
+
# @api private
|
97
|
+
def self.type_lookup # :nodoc:
|
98
|
+
@type_lookup && @type_lookup[-1]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
require_relative "ctypes/missing_bytes_error"
|
data/sig/ctypes.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ctypes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David M. Lary
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-02-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-struct
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- dmlary@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".standard.yml"
|
35
|
+
- CODE_OF_CONDUCT.md
|
36
|
+
- CONTRIBUTING.md
|
37
|
+
- Gemfile
|
38
|
+
- LICENSE.txt
|
39
|
+
- MAINTAINERS.md
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- SECURITY.md
|
43
|
+
- ctypes.gemspec
|
44
|
+
- lib/ctypes.rb
|
45
|
+
- lib/ctypes/array.rb
|
46
|
+
- lib/ctypes/bitfield.rb
|
47
|
+
- lib/ctypes/bitfield/builder.rb
|
48
|
+
- lib/ctypes/bitmap.rb
|
49
|
+
- lib/ctypes/enum.rb
|
50
|
+
- lib/ctypes/enum/builder.rb
|
51
|
+
- lib/ctypes/exporter.rb
|
52
|
+
- lib/ctypes/helpers.rb
|
53
|
+
- lib/ctypes/importers.rb
|
54
|
+
- lib/ctypes/importers/castxml.rb
|
55
|
+
- lib/ctypes/importers/castxml/loader.rb
|
56
|
+
- lib/ctypes/int.rb
|
57
|
+
- lib/ctypes/missing_bytes_error.rb
|
58
|
+
- lib/ctypes/pad.rb
|
59
|
+
- lib/ctypes/pretty_print_helpers.rb
|
60
|
+
- lib/ctypes/string.rb
|
61
|
+
- lib/ctypes/struct.rb
|
62
|
+
- lib/ctypes/struct/builder.rb
|
63
|
+
- lib/ctypes/terminated.rb
|
64
|
+
- lib/ctypes/type.rb
|
65
|
+
- lib/ctypes/union.rb
|
66
|
+
- lib/ctypes/union/builder.rb
|
67
|
+
- lib/ctypes/version.rb
|
68
|
+
- sig/ctypes.rbs
|
69
|
+
homepage: https://github.com/cisco-open/ruby-ctypes
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 3.1.0
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubygems_version: 3.3.7
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Manipulate common C types in Ruby
|
92
|
+
test_files: []
|