gnosis 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.
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,41 @@
1
+ # Gnosis
2
+ [![Build Status](https://travis-ci.org/Solistra/gnosis.svg?branch=master)][travis]
3
+ [![Coverage Status](https://coveralls.io/repos/Solistra/gnosis/badge.png?branch=master)][coverage]
4
+ [![Gem Version](https://badge.fury.io/rb/gnosis.svg)][gem]
5
+ [![Inline docs](http://inch-ci.org/github/Solistra/gnosis.svg?branch=master)][inch]
6
+ [![Documentation](http://b.repl.ca/v1/yard-docs-blue.png)][yard]
7
+
8
+ 1. _noun: spiritual knowledge; mystical enlightenment._
9
+ 2. _noun: the act or process of knowing._
10
+
11
+ ## Summary
12
+ Gnosis performs in-memory decryption of the contents of encrypted RGSSAD archives (as used by the RPG Maker series) to binary strings via a class representing the archive. Encrypted archives may be searched for specific contents which can then be decrypted to a string -- perfect for implementing custom Ruby game players by converting the binary string directly into an IO instance with the Ruby `StringIO` class present in the standard library.
13
+
14
+ ## Installation
15
+ ```sh
16
+ $ gem install gnosis
17
+ ```
18
+
19
+ ## Disclaimer
20
+ This library has been designed to help facilitate the loading of the RPG Maker series' encrypted RGSS archives for alternative game players written in a compliant Ruby implementation. As such, Gnosis simply decrypts the requested contents of encrypted RGSSAD archives as binary strings which are intended to be used for loading resources, _not_ for writing them to disk.
21
+
22
+ Gnosis exists to ease the development of alternatives to the native `Game.exe` files given by the RPG Maker series by default -- it is _not_ designed to encourage piracy of encrypted resources.
23
+
24
+ **In short, ethical usage of Gnosis is your responsibility as a user, not the library itself.**
25
+
26
+ ## Attribution
27
+ The decryption algorithms used by this library are based upon algorithms previously released by [Porkness][porkness] for a fork of the [OpenRGSS][openrgss] project with specific modifications for this library.
28
+
29
+ Unfortunately, the original source of these algorithms could not be located -- please open an issue if you know the original source so they can be properly attributed.
30
+
31
+ ## License
32
+ Gnosis is made available under the terms of the LGPL version 3 license. See the included LICENSE file for more information.
33
+
34
+ [coverage]: https://coveralls.io/r/Solistra/gnosis?branch=master
35
+ [inch]: http://inch-ci.org/github/Solistra/gnosis
36
+ [repo]: https://www.github.com/Solistra/gnosis
37
+ [openrgss]: http://openrgss.org
38
+ [porkness]: https://www.github.com/Porkness
39
+ [gem]: http://badge.fury.io/rb/gnosis
40
+ [travis]: https://travis-ci.org/Solistra/gnosis
41
+ [yard]: http://rubydoc.info/gems/gnosis/frames
@@ -0,0 +1,14 @@
1
+ require 'gnosis/archive'
2
+ require 'gnosis/translators'
3
+ require 'gnosis/version'
4
+
5
+ # Gnosis performs in-memory decryption of the contents of encrypted RGSSAD
6
+ # archives (as used by the RPG Maker series) to binary strings via a class
7
+ # representing the archive. Encrypted archives may be searched for specific
8
+ # contents which can then be decrypted to a string -- perfect for implementing
9
+ # custom Ruby game players by converting the binary string directly into an IO
10
+ # instance with the Ruby `StringIO` class present in the standard library.
11
+ #
12
+ # @author Solistra <solistra@gmx.com>
13
+ module Gnosis
14
+ end
@@ -0,0 +1,127 @@
1
+ require 'gnosis/translators'
2
+
3
+ module Gnosis
4
+ # Representation of an encrypted RGSS archive. This class provides a layer of
5
+ # abstraction over the archive itself, providing methods to help ease the
6
+ # traversal of encrypted archives and allow in-memory decryption of their
7
+ # contents.
8
+ class Archive
9
+ # Full path to the encrypted archive this class represents.
10
+ # @return [String]
11
+ attr_reader :archive
12
+
13
+ # The translator instance in use by this archive representation.
14
+ # @return [Translator]
15
+ attr_reader :translator
16
+
17
+ # Hash of files with associated encryption information.
18
+ # @return [Hash{String => Hash{Symbol => Number}}]
19
+ attr_reader :files
20
+
21
+ # Instantiates a new {Archive Archive} representing an encrypted RGSS
22
+ # archive.
23
+ #
24
+ # @raise [InvalidArchiveError] if the given archive is invalid
25
+ # @param [String] archive the relative path to an encrypted archive
26
+ def initialize(archive)
27
+ @archive = File.expand_path(archive)
28
+ @translator = Translators.class_for(self)
29
+ @files = @translator.scan_files
30
+ end
31
+
32
+ # Convenience method for obtaining an array of all encrypted files within
33
+ # this RGSS archive representation.
34
+ #
35
+ # @return [Array<String>] an array of files contained in the archive
36
+ def contents
37
+ @files.keys
38
+ end
39
+
40
+ # Convenience method for obtaining the number of encrypted files within
41
+ # this RGSS archive representation.
42
+ #
43
+ # @return [Number] the number of files contained in the archive
44
+ def size
45
+ contents.size
46
+ end
47
+ alias_method :length, :size
48
+
49
+ # Returns the type of archive represented by this {Archive Archive}.
50
+ #
51
+ # @return [String] the type of archive represented
52
+ def type
53
+ @translator.class.name.split(/::/).last
54
+ end
55
+
56
+ # Decrypts the given internal filename. The returned binary string may be
57
+ # converted into an IO object in memory via the StringIO class present in
58
+ # the Ruby standard library. Yields the decrypted binary data if a block is
59
+ # given.
60
+ #
61
+ # @example Decrypt to StringIO
62
+ # require 'stringio'
63
+ # archive = Gnosis::Archive.new('path/to/Game.rgss3a')
64
+ # io = StringIO.new(archive.decrypt('Graphics/Titles1/Title.png'))
65
+ #
66
+ # @raise [Errno::ENOENT] if the given file does not exist in the archive
67
+ # @param [String] file internal path of encrypted file
68
+ # @return [String] the decrypted binary contents of the given file
69
+ def decrypt(file)
70
+ data = @translator.decrypt(file)
71
+ yield data if block_given?
72
+ data
73
+ end
74
+
75
+ # Search for files within the encrypted archive with a regular expression.
76
+ # Yields the results of the given expression if a block is given.
77
+ #
78
+ # @example Search with Regular Expression
79
+ # archive = Gnosis::Archive.new('path/to/Game.rgss3a')
80
+ # archive.search(/\.png$/) # => ["Graphics/Animations/Attack1.png", ... ]
81
+ #
82
+ # @param [String, Regexp] expression substring or regular expression to
83
+ # match files against
84
+ # @yield [result] an array of file names matching the given expression
85
+ # @return [Array<String>] an array of file names matching the given
86
+ # expression
87
+ #
88
+ # @see #glob
89
+ def search(expression)
90
+ result = @files.keys.select { |file| file[expression] }
91
+ yield result if block_given?
92
+ result
93
+ end
94
+ alias_method :match, :search
95
+
96
+ # Search for files within the encrypted archive with optional shell-style
97
+ # globbing. Yields the results of the given pattern if a block is given.
98
+ #
99
+ # @note Globbing is performed via `File#fnmatch`.
100
+ #
101
+ # @example Search with Globbing
102
+ # archive = Gnosis::Archive.new('path/to/Game.rgss3a')
103
+ # archive.glob('**/Title.png') # => ["Graphics/Titles1/Title.png"]
104
+ #
105
+ # @param [String] pattern file name pattern
106
+ # @yield [result] an array of file names matching the given pattern
107
+ # @return [Array<String>] an array of file names matching the given pattern
108
+ #
109
+ # @see http://www.ruby-doc.org/core-1.9.3/File.html#method-c-fnmatch
110
+ # File.fnmatch
111
+ # @see #search
112
+ def glob(pattern)
113
+ pattern.force_encoding('utf-8')
114
+ result = @files.keys.select { |file| File.fnmatch?(pattern, file) }
115
+ yield result if block_given?
116
+ result
117
+ end
118
+
119
+ # A representation of this {Archive Archive} as a string.
120
+ #
121
+ # @return [String] representation of this {Archive Archive} as a string.
122
+ def to_s
123
+ "#{type} Archive: #{contents.size} files"
124
+ end
125
+ alias_method :inspect, :to_s
126
+ end
127
+ end
@@ -0,0 +1,29 @@
1
+ require 'gnosis/translators/key'
2
+ require 'gnosis/translators/rgssad'
3
+ require 'gnosis/translators/rgss3a'
4
+
5
+ module Gnosis
6
+ # Namespace containing classes which facilitate translation for various
7
+ # archiving methods used by the RPG Maker series.
8
+ module Translators
9
+ # Attempts to locate an appropriate translator for the encrypted archive
10
+ # belonging to the passed {Archive Archive} object. The passed archive
11
+ # representation is automatically given to the translator as the parent
12
+ # object for the translator.
13
+ #
14
+ # @note This method determines the archive type independent of the file
15
+ # extension by reading the first 8 bytes of data contained within it.
16
+ #
17
+ # @return [Translator] the appropriate translator instance for the archive
18
+ # type belonging to the parent {Archive Archive} object
19
+ # @raise [InvalidArchiveError] if the archive belonging to the parent
20
+ # {Archive Archive} object is not a valid RGSS archive
21
+ def self.class_for(parent)
22
+ case File.read(parent.archive, 8).unpack('a6U2').join
23
+ when 'RGSSAD03' ; RGSS3A.new(parent)
24
+ when 'RGSSAD01' ; RGSSAD.new(parent)
25
+ else ; raise RGSSAD::InvalidArchiveError, parent.archive
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,36 @@
1
+ module Gnosis
2
+ module Translators
3
+ # Representation of the key used to decrypt files in an encrypted archive.
4
+ # The encryption method used by the RPG Maker series regularly mutates the
5
+ # key in order to provide encryption; this class provides a simple way to
6
+ # accurately manage these mutations.
7
+ class Key
8
+ # The current state of the encryption key.
9
+ attr_accessor :key
10
+ alias_method :current, :key
11
+
12
+ # Creates a new instance of {Key Key} with the given {#key key} as the
13
+ # starting point for encryption.
14
+ #
15
+ # @note The RPG Maker series uses `0xDEADCAFE` as the default starting
16
+ # point for the encryption key unless the project uses a custom
17
+ # encryption solution.
18
+ def initialize(key = 0xDEADCAFE)
19
+ @key = key
20
+ end
21
+
22
+ # Mutates the encryption key to facilitate advancement through the
23
+ # archive this key belongs to.
24
+ #
25
+ # @return [Fixnum] the next key sequence
26
+ def advance
27
+ @key = (@key * 7 + 3) & 0xFFFFFFFF
28
+ end
29
+
30
+ # Define common conversion methods; delegate these to @key.
31
+ [:to_c, :to_enum, :to_f, :to_i, :to_int, :to_r, :to_s].each do |method|
32
+ define_method(method) { @key.send(method) }
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,66 @@
1
+ require 'gnosis/translators/key'
2
+ require 'gnosis/translators/rgssad'
3
+ require 'gnosis/translators/rgssad/invalid_archive_error'
4
+
5
+ module Gnosis
6
+ module Translators
7
+ # Provides a translator for RGSS3A encrypted archives as used by RPG Maker
8
+ # VX Ace. This class provides the implementation-specific {#scan_files}
9
+ # method as used by an {Archive Archive} instance.
10
+ class RGSS3A < RGSSAD
11
+ # @raise [InvalidArchiveError] if the parent object's archive is not a
12
+ # valid RGSS3A encrypted archive
13
+ # @param [Archive] parent the archive which contains this translator
14
+ def initialize(parent)
15
+ unless File.read(parent.archive, 8).unpack('a6U*').join == 'RGSSAD03'
16
+ raise InvalidArchiveError, parent.archive
17
+ end
18
+ @parent = parent
19
+ end
20
+
21
+ # Builds a hash of file information stored in an encrypted archive. Keys
22
+ # are filenames, values contain a hash of information regarding file
23
+ # encryption (namely its position in the archive, size in bytes, and the
24
+ # subkey needed to decrypt the file).
25
+ #
26
+ # @return [Hash{String => Hash{Symbol => Number}}] a hash of files with
27
+ # associated encryption information
28
+ def scan_files
29
+ hash = {}
30
+ File.open(@parent.archive) do |archive|
31
+ archive.seek(8) # Skip archive version information.
32
+ key = (archive.read(4).unpack('V').first * 9 + 3) & 0xFFFFFFFF
33
+ loop do
34
+ info = archive.read(16).unpack('V4').map { |i| i ^ key }
35
+ break if info.first.zero?
36
+ file = translate_file(archive.read(info[3]), key)
37
+ hash[file] = { position: info[0], size: info[1], subkey: info[2] }
38
+ end
39
+ end
40
+ hash
41
+ end
42
+
43
+ private
44
+ # Translates an encrypted binary filename within the archive to a UTF-8
45
+ # string.
46
+ #
47
+ # @param [String] bytes string of encrypted bytes
48
+ # @param [Number] key subkey used to decrypt the given bytes
49
+ # @return [String] a decrypted UTF-8 string
50
+ def translate_file(bytes, key)
51
+ bytes.scan(/.{1,4}/m).map! do |word|
52
+ [word.ljust(4, "\x00").unpack('V')[0] ^ key].pack('V')[0...word.size]
53
+ end.join.force_encoding('utf-8').gsub!(/\\/, '/')
54
+ end
55
+
56
+ # Overwrite of {RGSSAD#translate_int} to raise a `NotImplementedError`,
57
+ # as RGSS3A archives do not make use of this method.
58
+ #
59
+ # @param [String] bytes string of encrypted bytes
60
+ # @raise [NotImplementedError] if called
61
+ def translate_int(bytes)
62
+ raise NotImplementedError, 'RGSS3A archives do not use encrypted ints'
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,114 @@
1
+ require 'gnosis/translators/key'
2
+ require 'gnosis/translators/rgssad/invalid_archive_error'
3
+
4
+ module Gnosis
5
+ module Translators
6
+ # Provides a translator for RGSSAD encrypted archives as used by RPG Maker
7
+ # XP and VX. This class provides the implementation-specific {#scan_files}
8
+ # and {#decrypt} methods as used by an {Archive Archive} instance.
9
+ class RGSSAD
10
+ # @raise [InvalidArchiveError] if the parent object's archive is not a
11
+ # valid RGSSAD encrypted archive
12
+ # @param [Archive] parent the archive which contains this translator
13
+ def initialize(parent)
14
+ unless File.read(parent.archive, 8).unpack('a6U*').join == 'RGSSAD01'
15
+ raise InvalidArchiveError, parent.archive
16
+ end
17
+ @parent = parent
18
+ end
19
+
20
+ # Builds a hash of file information stored in an encrypted archive. Keys
21
+ # are filenames, values contain a hash of information regarding file
22
+ # encryption (namely its position in the archive, size in bytes, and the
23
+ # subkey needed to decrypt the file).
24
+ #
25
+ # @return [Hash{String => Hash{Symbol => Number}}] a hash of files with
26
+ # associated encryption information
27
+ def scan_files
28
+ hash = {}
29
+ @key = Key.new
30
+ File.open(@parent.archive) do |archive|
31
+ archive.seek(8)
32
+ loop do
33
+ length = translate_int(archive.read(4)) rescue break
34
+ file = translate_file(archive.read(length)).gsub!(/\\/, '/')
35
+ hash[file] = { :position => archive.tell + 4 }
36
+ hash[file][:size] = translate_int(archive.read(4))
37
+ hash[file][:subkey] = @key.current
38
+ archive.seek(hash[file][:size], IO::SEEK_CUR)
39
+ end
40
+ end
41
+ @key = nil
42
+ hash
43
+ end
44
+
45
+ # Decrypts the given file in an encrypted archive, returning the original
46
+ # binary data as a string.
47
+ #
48
+ # @raise [Errno::ENOENT] if the given file does not exist in the parent
49
+ # {Archive} representation
50
+ # @param [String] file internal path of encrypted file
51
+ # @return [String] the decrypted binary contents of the given file
52
+ def decrypt(file)
53
+ raise Errno::ENOENT, file unless @parent.files[file]
54
+ data = ''
55
+ info = @parent.files[file]
56
+ key = Key.new(info[:subkey])
57
+ size, remainder = (info[:size] / 4.0).ceil, info[:size] % 4
58
+
59
+ File.open(@parent.archive) do |archive|
60
+ archive.seek(info[:position])
61
+ (1..size).each do |position|
62
+ data << \
63
+ if remainder > 0 && position == size
64
+ bytes = archive.read(remainder)
65
+ translate_data(bytes.ljust(4, "\x00"), key)[0...bytes.size]
66
+ else
67
+ translate_data(archive.read(4), key)
68
+ end
69
+ key.advance
70
+ end
71
+ end
72
+
73
+ data
74
+ end
75
+
76
+ private
77
+ # Translates encrypted binary data to decrypted binary data.
78
+ #
79
+ # @note This method should only be given a string of four encrypted bytes
80
+ # in order to function properly. Alternative lengths produce undefined
81
+ # strings.
82
+ #
83
+ # @param [String] bytes string of encrypted bytes
84
+ # @param [Number] key subkey used to decrypt the given bytes
85
+ # @return [String] string of decrypted bytes
86
+ def translate_data(bytes, key)
87
+ [bytes.unpack('V').first ^ key.current].pack('V')
88
+ end
89
+
90
+ # Translates an encrypted binary filename within the archive to a UTF-8
91
+ # string.
92
+ #
93
+ # @param [String] bytes string of encrypted bytes
94
+ # @return [String] a decrypted UTF-8 string
95
+ def translate_file(bytes)
96
+ bytes.each_byte.map do |byte|
97
+ result = (byte ^ @key.current & 0xFF).chr
98
+ @key.advance
99
+ result
100
+ end.join.force_encoding('utf-8')
101
+ end
102
+
103
+ # Translates an encrypted binary integer to a native Ruby integer.
104
+ #
105
+ # @param [String] bytes string of encrypted bytes
106
+ # @return [Number] a decrypted native integer
107
+ def translate_int(bytes)
108
+ result = bytes.unpack('V').first ^ @key.current
109
+ @key.advance
110
+ result
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,16 @@
1
+ module Gnosis
2
+ module Translators
3
+ class RGSSAD
4
+ # Exception raised when the decrypter is initialized with an invalid
5
+ # RGSSAD encrypted archive.
6
+ class InvalidArchiveError < StandardError
7
+ # @param [String] file the file which raised this exception
8
+ # @param [String] message message to be written -- `file` is added to
9
+ # this message through `sprintf`
10
+ def initialize(file, message = 'Invalid RGSSAD encrypted archive - %s')
11
+ super(message % file)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,4 @@
1
+ module Gnosis
2
+ # Semantic version of Gnosis.
3
+ VERSION = '0.1.0'
4
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gnosis
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Solistra
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2015-01-12 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: yard
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id004
59
+ description: Gnosis allows developers to perform in-memory decryption of the contents of RGSSAD and RGSS3A archives (as used by the RPG Maker series) to binary strings.
60
+ email:
61
+ - solistra@gmx.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files: []
67
+
68
+ files:
69
+ - lib/gnosis/archive.rb
70
+ - lib/gnosis/translators/key.rb
71
+ - lib/gnosis/translators/rgss3a.rb
72
+ - lib/gnosis/translators/rgssad/invalid_archive_error.rb
73
+ - lib/gnosis/translators/rgssad.rb
74
+ - lib/gnosis/translators.rb
75
+ - lib/gnosis/version.rb
76
+ - lib/gnosis.rb
77
+ - LICENSE
78
+ - README.md
79
+ homepage: https://github.com/Solistra/gnosis
80
+ licenses:
81
+ - LGPL-3.0
82
+ post_install_message:
83
+ rdoc_options: []
84
+
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.23
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Gnosis performs in-memory decryption of RGSS archives.
106
+ test_files: []
107
+
108
+ has_rdoc: