cabriolet 0.1.2 → 0.2.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.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +703 -38
  3. data/lib/cabriolet/algorithm_factory.rb +250 -0
  4. data/lib/cabriolet/base_compressor.rb +206 -0
  5. data/lib/cabriolet/binary/bitstream.rb +167 -16
  6. data/lib/cabriolet/binary/bitstream_writer.rb +150 -21
  7. data/lib/cabriolet/binary/chm_structures.rb +2 -2
  8. data/lib/cabriolet/binary/hlp_structures.rb +258 -37
  9. data/lib/cabriolet/binary/lit_structures.rb +231 -65
  10. data/lib/cabriolet/binary/oab_structures.rb +17 -1
  11. data/lib/cabriolet/cab/command_handler.rb +226 -0
  12. data/lib/cabriolet/cab/compressor.rb +108 -84
  13. data/lib/cabriolet/cab/decompressor.rb +16 -20
  14. data/lib/cabriolet/cab/extractor.rb +142 -66
  15. data/lib/cabriolet/cab/file_compression_work.rb +52 -0
  16. data/lib/cabriolet/cab/file_compression_worker.rb +89 -0
  17. data/lib/cabriolet/checksum.rb +49 -0
  18. data/lib/cabriolet/chm/command_handler.rb +227 -0
  19. data/lib/cabriolet/chm/compressor.rb +7 -3
  20. data/lib/cabriolet/chm/decompressor.rb +39 -21
  21. data/lib/cabriolet/chm/parser.rb +5 -2
  22. data/lib/cabriolet/cli/base_command_handler.rb +127 -0
  23. data/lib/cabriolet/cli/command_dispatcher.rb +140 -0
  24. data/lib/cabriolet/cli/command_registry.rb +83 -0
  25. data/lib/cabriolet/cli.rb +356 -607
  26. data/lib/cabriolet/collections/file_collection.rb +175 -0
  27. data/lib/cabriolet/compressors/base.rb +1 -1
  28. data/lib/cabriolet/compressors/lzx.rb +241 -54
  29. data/lib/cabriolet/compressors/mszip.rb +35 -3
  30. data/lib/cabriolet/compressors/quantum.rb +36 -95
  31. data/lib/cabriolet/decompressors/base.rb +1 -1
  32. data/lib/cabriolet/decompressors/lzss.rb +13 -3
  33. data/lib/cabriolet/decompressors/lzx.rb +70 -33
  34. data/lib/cabriolet/decompressors/mszip.rb +126 -39
  35. data/lib/cabriolet/decompressors/quantum.rb +83 -53
  36. data/lib/cabriolet/errors.rb +3 -0
  37. data/lib/cabriolet/extraction/base_extractor.rb +88 -0
  38. data/lib/cabriolet/extraction/extractor.rb +171 -0
  39. data/lib/cabriolet/extraction/file_extraction_work.rb +60 -0
  40. data/lib/cabriolet/extraction/file_extraction_worker.rb +106 -0
  41. data/lib/cabriolet/file_entry.rb +156 -0
  42. data/lib/cabriolet/file_manager.rb +144 -0
  43. data/lib/cabriolet/format_base.rb +79 -0
  44. data/lib/cabriolet/hlp/command_handler.rb +282 -0
  45. data/lib/cabriolet/hlp/compressor.rb +28 -238
  46. data/lib/cabriolet/hlp/decompressor.rb +107 -147
  47. data/lib/cabriolet/hlp/parser.rb +52 -101
  48. data/lib/cabriolet/hlp/quickhelp/compression_stream.rb +138 -0
  49. data/lib/cabriolet/hlp/quickhelp/compressor.rb +151 -0
  50. data/lib/cabriolet/hlp/quickhelp/decompressor.rb +558 -0
  51. data/lib/cabriolet/hlp/quickhelp/file_writer.rb +125 -0
  52. data/lib/cabriolet/hlp/quickhelp/huffman_stream.rb +74 -0
  53. data/lib/cabriolet/hlp/quickhelp/huffman_tree.rb +167 -0
  54. data/lib/cabriolet/hlp/quickhelp/offset_calculator.rb +61 -0
  55. data/lib/cabriolet/hlp/quickhelp/parser.rb +274 -0
  56. data/lib/cabriolet/hlp/quickhelp/structure_builder.rb +93 -0
  57. data/lib/cabriolet/hlp/quickhelp/topic_builder.rb +52 -0
  58. data/lib/cabriolet/hlp/quickhelp/topic_compressor.rb +83 -0
  59. data/lib/cabriolet/hlp/winhelp/btree_builder.rb +289 -0
  60. data/lib/cabriolet/hlp/winhelp/compressor.rb +400 -0
  61. data/lib/cabriolet/hlp/winhelp/decompressor.rb +192 -0
  62. data/lib/cabriolet/hlp/winhelp/parser.rb +484 -0
  63. data/lib/cabriolet/hlp/winhelp/zeck_lz77.rb +271 -0
  64. data/lib/cabriolet/huffman/encoder.rb +15 -12
  65. data/lib/cabriolet/huffman/tree.rb +85 -1
  66. data/lib/cabriolet/kwaj/command_handler.rb +213 -0
  67. data/lib/cabriolet/kwaj/compressor.rb +7 -3
  68. data/lib/cabriolet/kwaj/decompressor.rb +18 -12
  69. data/lib/cabriolet/lit/command_handler.rb +221 -0
  70. data/lib/cabriolet/lit/compressor.rb +119 -168
  71. data/lib/cabriolet/lit/content_encoder.rb +76 -0
  72. data/lib/cabriolet/lit/content_type_detector.rb +50 -0
  73. data/lib/cabriolet/lit/decompressor.rb +518 -152
  74. data/lib/cabriolet/lit/directory_builder.rb +153 -0
  75. data/lib/cabriolet/lit/guid_generator.rb +16 -0
  76. data/lib/cabriolet/lit/header_writer.rb +124 -0
  77. data/lib/cabriolet/lit/parser.rb +670 -0
  78. data/lib/cabriolet/lit/piece_builder.rb +74 -0
  79. data/lib/cabriolet/lit/structure_builder.rb +252 -0
  80. data/lib/cabriolet/models/hlp_file.rb +130 -29
  81. data/lib/cabriolet/models/hlp_header.rb +105 -17
  82. data/lib/cabriolet/models/lit_header.rb +212 -25
  83. data/lib/cabriolet/models/szdd_header.rb +10 -2
  84. data/lib/cabriolet/models/winhelp_header.rb +127 -0
  85. data/lib/cabriolet/oab/command_handler.rb +257 -0
  86. data/lib/cabriolet/oab/compressor.rb +17 -8
  87. data/lib/cabriolet/oab/decompressor.rb +41 -10
  88. data/lib/cabriolet/offset_calculator.rb +81 -0
  89. data/lib/cabriolet/plugin.rb +233 -0
  90. data/lib/cabriolet/plugin_manager.rb +453 -0
  91. data/lib/cabriolet/plugin_validator.rb +422 -0
  92. data/lib/cabriolet/quantum_shared.rb +105 -0
  93. data/lib/cabriolet/system/io_system.rb +3 -0
  94. data/lib/cabriolet/system/memory_handle.rb +17 -4
  95. data/lib/cabriolet/szdd/command_handler.rb +217 -0
  96. data/lib/cabriolet/szdd/compressor.rb +15 -11
  97. data/lib/cabriolet/szdd/decompressor.rb +18 -9
  98. data/lib/cabriolet/version.rb +1 -1
  99. data/lib/cabriolet.rb +181 -20
  100. metadata +69 -4
  101. data/lib/cabriolet/auto.rb +0 -173
  102. data/lib/cabriolet/parallel.rb +0 -333
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cabriolet
4
+ module Commands
5
+ # Abstract base class for format-specific command handlers
6
+ #
7
+ # This class defines the interface that all format command handlers must implement.
8
+ # Each format (CAB, CHM, SZDD, KWAJ, HLP, LIT, OAB) should have its own
9
+ # CommandHandler subclass that inherits from this base class.
10
+ #
11
+ # The base class provides common functionality and enforces a consistent
12
+ # interface across all format handlers, following the Template Method pattern.
13
+ #
14
+ # @example Creating a format handler
15
+ # module Cabriolet
16
+ # module CAB
17
+ # class CommandHandler < CLI::BaseCommandHandler
18
+ # def list(file, options = {})
19
+ # # Implementation for listing CAB files
20
+ # end
21
+ # end
22
+ # end
23
+ # end
24
+ #
25
+ class BaseCommandHandler
26
+ # Initialize the command handler
27
+ #
28
+ # @param verbose [Boolean] Enable verbose output
29
+ def initialize(verbose: false)
30
+ @verbose = verbose
31
+ end
32
+
33
+ # List archive contents
34
+ #
35
+ # @param file [String] Path to the archive file
36
+ # @param options [Hash] Additional options
37
+ # @raise [NotImplementedError] Subclass must implement
38
+ def list(file, options = {})
39
+ raise NotImplementedError,
40
+ "#{self.class} must implement #list"
41
+ end
42
+
43
+ # Extract files from archive
44
+ #
45
+ # @param file [String] Path to the archive file
46
+ # @param output_dir [String] Output directory path
47
+ # @param options [Hash] Additional options
48
+ # @raise [NotImplementedError] Subclass must implement
49
+ def extract(file, output_dir, options = {})
50
+ raise NotImplementedError,
51
+ "#{self.class} must implement #extract"
52
+ end
53
+
54
+ # Create a new archive
55
+ #
56
+ # @param output [String] Output file path
57
+ # @param files [Array<String>] List of input files
58
+ # @param options [Hash] Additional options
59
+ # @raise [NotImplementedError] Subclass must implement
60
+ def create(output, files, options = {})
61
+ raise NotImplementedError,
62
+ "#{self.class} must implement #create"
63
+ end
64
+
65
+ # Display archive information
66
+ #
67
+ # @param file [String] Path to the archive file
68
+ # @param options [Hash] Additional options
69
+ # @raise [NotImplementedError] Subclass must implement
70
+ def info(file, options = {})
71
+ raise NotImplementedError,
72
+ "#{self.class} must implement #info"
73
+ end
74
+
75
+ # Test archive integrity
76
+ #
77
+ # @param file [String] Path to the archive file
78
+ # @param options [Hash] Additional options
79
+ # @raise [NotImplementedError] Subclass must implement
80
+ def test(file, options = {})
81
+ raise NotImplementedError,
82
+ "#{self.class} must implement #test"
83
+ end
84
+
85
+ protected
86
+
87
+ # Check if verbose output is enabled
88
+ #
89
+ # @return [Boolean] true if verbose mode is active
90
+ def verbose?
91
+ @verbose
92
+ end
93
+
94
+ # Detect format from file using FormatDetector
95
+ #
96
+ # This is a convenience method for handlers that need to perform
97
+ # format detection within their operations.
98
+ #
99
+ # @param file [String] Path to the file
100
+ # @return [Symbol, nil] Detected format symbol
101
+ def detect_format(file)
102
+ require_relative "../format_detector"
103
+ FormatDetector.detect(file)
104
+ end
105
+
106
+ # Validate that a file exists
107
+ #
108
+ # @param file [String] Path to the file
109
+ # @raise [ArgumentError] if file doesn't exist
110
+ def validate_file_exists(file)
111
+ return if File.exist?(file)
112
+
113
+ raise ArgumentError, "File does not exist: #{file}"
114
+ end
115
+
116
+ # Ensure output directory exists
117
+ #
118
+ # @param output_dir [String] Output directory path
119
+ # @return [String] The output directory path
120
+ def ensure_output_dir(output_dir)
121
+ require "fileutils"
122
+ FileUtils.mkdir_p(output_dir)
123
+ output_dir
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "command_registry"
4
+ require_relative "base_command_handler"
5
+ require_relative "../format_detector"
6
+
7
+ module Cabriolet
8
+ module Commands
9
+ # Unified command dispatcher that routes commands to format-specific handlers
10
+ #
11
+ # The dispatcher is responsible for:
12
+ # 1. Detecting the format of input files (or using manual override)
13
+ # 2. Selecting the appropriate format handler from the registry
14
+ # 3. Delegating command execution to the handler
15
+ #
16
+ # This class implements the Strategy pattern, where the format handler
17
+ # is the strategy selected based on the detected format.
18
+ #
19
+ # @example Using the dispatcher
20
+ # dispatcher = CLI::CommandDispatcher.new(format: :cab, verbose: true)
21
+ # dispatcher.dispatch(:list, "archive.cab")
22
+ #
23
+ class CommandDispatcher
24
+ # Initialize the command dispatcher
25
+ #
26
+ # @param options [Hash] Configuration options
27
+ # @option options [String, Symbol] :format Manual format override
28
+ # @option options [Boolean] :verbose Enable verbose output
29
+ def initialize(options = {})
30
+ @format_override = parse_format_option(options[:format])
31
+ @verbose = options[:verbose] || false
32
+ end
33
+
34
+ # Dispatch a command to the appropriate format handler
35
+ #
36
+ # This method detects the format (if not manually specified),
37
+ # retrieves the appropriate handler, and delegates the command execution.
38
+ #
39
+ # @param command [Symbol] The command to execute (:list, :extract, etc.)
40
+ # @param file [String] Path to the archive file
41
+ # @param args [Array] Additional positional arguments for the command
42
+ # @param options [Hash] Additional options to pass to the handler
43
+ # @raise [Cabriolet::Error] if format detection fails or handler not found
44
+ # @return [void]
45
+ def dispatch(command, file, *args, **options)
46
+ format = detect_format(file)
47
+ handler = get_handler_for(format)
48
+
49
+ execute_command(handler, command, file, args, options)
50
+ end
51
+
52
+ # Check if a format is supported
53
+ #
54
+ # @param format [Symbol] The format to check
55
+ # @return [Boolean] true if the format has a registered handler
56
+ def self.format_supported?(format)
57
+ CommandRegistry.format_registered?(format)
58
+ end
59
+
60
+ # Get list of supported formats
61
+ #
62
+ # @return [Array<Symbol>] List of supported format symbols
63
+ def self.supported_formats
64
+ CommandRegistry.registered_formats
65
+ end
66
+
67
+ private
68
+
69
+ # Parse format option to symbol
70
+ #
71
+ # @param format_value [String, Symbol, nil] The format option value
72
+ # @return [Symbol, nil] The format as a symbol
73
+ def parse_format_option(format_value)
74
+ return nil if format_value.nil?
75
+
76
+ format_value.to_sym
77
+ end
78
+
79
+ # Detect format from file with fallback to manual override
80
+ #
81
+ # @param file [String] Path to the archive file
82
+ # @return [Symbol] The detected format symbol
83
+ # @raise [Cabriolet::Error] if format cannot be detected
84
+ def detect_format(file)
85
+ return @format_override if @format_override
86
+
87
+ format = FormatDetector.detect(file)
88
+ if format.nil?
89
+ supported = CommandRegistry.registered_formats.join(", ")
90
+ raise Error,
91
+ "Cannot detect format for: #{file}. " \
92
+ "Use --format to specify (supported: #{supported})"
93
+ end
94
+
95
+ format
96
+ end
97
+
98
+ # Get the handler class for a format
99
+ #
100
+ # @param format [Symbol] The format symbol
101
+ # @return [Class] The handler class
102
+ # @raise [Cabriolet::Error] if no handler is registered
103
+ def get_handler_for(format)
104
+ handler = CommandRegistry.handler_for(format)
105
+
106
+ unless handler
107
+ raise Error,
108
+ "No command handler registered for format: #{format}"
109
+ end
110
+
111
+ handler
112
+ end
113
+
114
+ # Execute the command on the handler
115
+ #
116
+ # @param handler_class [Class] The handler class
117
+ # @param command [Symbol] The command to execute
118
+ # @param file [String] Path to the archive file
119
+ # @param args [Array] Additional positional arguments
120
+ # @param options [Hash] Additional options
121
+ # @return [void]
122
+ def execute_command(handler_class, command, file, args, options)
123
+ handler = handler_class.new(verbose: @verbose)
124
+
125
+ # Call the command method with appropriate arguments
126
+ case command
127
+ when :extract
128
+ output_dir = args.first || options[:output_dir]
129
+ handler.extract(file, output_dir, options)
130
+ when :create
131
+ files = args.first || options[:files] || []
132
+ handler.create(file, files, options)
133
+ else
134
+ # For commands that take just file and options
135
+ handler.public_send(command, file, options)
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cabriolet
4
+ module Commands
5
+ # Registry for mapping format symbols to command handler classes
6
+ #
7
+ # This registry provides a centralized location for managing format handlers,
8
+ # following the Open/Closed Principle - new formats can be added without
9
+ # modifying existing command logic.
10
+ #
11
+ # @example Registering a format handler
12
+ # CLI::CommandRegistry.register_format(:cab, CAB::CommandHandler)
13
+ #
14
+ # @example Getting a handler for a format
15
+ # handler = CLI::CommandRegistry.handler_for(:cab)
16
+ #
17
+ class CommandRegistry
18
+ @handlers = {}
19
+
20
+ class << self
21
+ # Get the command handler class for a given format
22
+ #
23
+ # @param format [Symbol] The format symbol (e.g., :cab, :chm, :szdd)
24
+ # @return [Class, nil] The handler class or nil if not registered
25
+ def handler_for(format)
26
+ @handlers[format]
27
+ end
28
+
29
+ # Register a command handler for a format
30
+ #
31
+ # This allows for dynamic registration of format handlers,
32
+ # supporting extensibility and plugin architectures.
33
+ #
34
+ # @param format [Symbol] The format symbol
35
+ # @param handler_class [Class] The command handler class
36
+ # @raise [ArgumentError] if handler_class doesn't implement required interface
37
+ def register_format(format, handler_class)
38
+ validate_handler_interface(handler_class)
39
+ @handlers[format] = handler_class
40
+ end
41
+
42
+ # Get all registered formats
43
+ #
44
+ # @return [Array<Symbol>] List of registered format symbols
45
+ def registered_formats
46
+ @handlers.keys
47
+ end
48
+
49
+ # Check if a format is registered
50
+ #
51
+ # @param format [Symbol] The format symbol
52
+ # @return [Boolean] true if the format has a registered handler
53
+ def format_registered?(format)
54
+ @handlers.key?(format)
55
+ end
56
+
57
+ # Clear all registered formats (primarily for testing)
58
+ #
59
+ # @return [void]
60
+ def clear
61
+ @handlers = {}
62
+ end
63
+
64
+ private
65
+
66
+ # Validate that a handler class implements the required interface
67
+ #
68
+ # @param handler_class [Class] The class to validate
69
+ # @raise [ArgumentError] if the class doesn't implement required methods
70
+ def validate_handler_interface(handler_class)
71
+ required_methods = %i[list extract create info test]
72
+
73
+ required_methods.each do |method|
74
+ unless handler_class.method_defined?(method)
75
+ raise ArgumentError,
76
+ "Handler class #{handler_class} must implement ##{method}"
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end