pngcheck 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77f6b0b4cde175efe7c530fb62150a710ba5bd7cd168154eb4831099bbca2266
4
- data.tar.gz: 3d23346db77e440df57b7d549ddd212572e5a579c1f48091d9d81f1fce1e1ae0
3
+ metadata.gz: 8538794c331c3eefe5a4aff7c853db22e20aaebcc042a27842faaf1b885756f7
4
+ data.tar.gz: f94d9281e7c0a4f473a58cba444fe99b4a2504e93525fc36d958e890d50f2d5e
5
5
  SHA512:
6
- metadata.gz: 1cf14b05a1d79ecc94a1eb797469f77ef1a240990cb1b3e85064dda1681e6991f214642d7c5c43c95ca760a6bc3c41a0ce958b9bff14caece3f3a5d559e7e053
7
- data.tar.gz: e346ce0c089ed7d560670ad9556cd6fd954fce00ff750bfeaab84cb40c1b1c3ea92e8e22888f103d13c7ce251a0d4c3e01600ad7c1fe5d4edbc2bbf6bfddf001
6
+ metadata.gz: 4bed6f839abd1c4a0a597d451fb4d3271019b29b13200c394a0fb820175afe3b154d8b27db95d48f19cb2ec35f051616b87c286b10d9193de13b4bb01e7e5e37
7
+ data.tar.gz: 2a479f9dafab807d5581f1511f4cfc578932c49c8f9dd4789a8065032b94fdc74c48928f3fccb7fa088256b5f0181d23b48ab1520735e936581b4bd54233fab6
data/README.adoc CHANGED
@@ -53,7 +53,7 @@ $ gem install pngcheck
53
53
 
54
54
  === Usage
55
55
 
56
- ==== PngCeck status codes
56
+ ==== PngCheck status codes
57
57
 
58
58
  [source,ruby]
59
59
  ----
@@ -84,7 +84,7 @@ valid = PngCheck.check_file("spec/examples/correct.png")
84
84
  Where:
85
85
 
86
86
  * `valid` is `true` if the file is correct
87
- * otherwise an exception of type `PngCheck::CorruptPngError` is raised
87
+ * otherwise an exception is raised. Possible exception types are `PngCheck::EmptyPngError`, `PngCheck::CorruptPngError`
88
88
 
89
89
 
90
90
  ==== Memory buffer processing
@@ -110,8 +110,8 @@ valid = PngCheck.check_buffer(data)
110
110
 
111
111
  Where:
112
112
 
113
- * `valid` is `true` if the file is correct
114
- * otherwise an exception of type `PngCheck::CorruptPngError` is raised
113
+ * `valid` is `true` if the buffer is correct image
114
+ * otherwise an exception is raised. Possible exception types are `PngCheck::EmptyPngError`, `PngCheck::CorruptPngError`
115
115
 
116
116
 
117
117
  ==== Pre-validation with libpng-ruby
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PngCheck
4
- VERSION = "0.2.4"
4
+ VERSION = "0.2.5"
5
5
  end
data/lib/pngcheck.rb CHANGED
@@ -5,8 +5,16 @@ require "tempfile"
5
5
  require_relative "pngcheck/version"
6
6
 
7
7
  module PngCheck
8
+ EMPTY_IMAGE = "Image is empty"
9
+
8
10
  class CorruptPngError < StandardError; end
9
11
 
12
+ class EmptyPngError < StandardError
13
+ def initialize(msg = EMPTY_IMAGE)
14
+ super
15
+ end
16
+ end
17
+
10
18
  STATUS_OK = 0
11
19
  STATUS_WARNING = 1 # an error in some circumstances but not in all
12
20
  STATUS_MINOR_ERROR = 3 # minor spec errors (e.g., out-of-range values)
@@ -37,25 +45,49 @@ module PngCheck
37
45
 
38
46
  class << self
39
47
  def analyze_file(path)
40
- Tempfile.open("captured-stream-") do |captured_stream|
41
- extra_msg = FFI::Buffer.alloc_out(EXTRA_MESSAGE_SIZE, 1, false)
42
- @@semaphore.lock
43
- status = pngcheck_file(path, captured_stream.path, extra_msg)
44
- @@semaphore.unlock
45
- # we assume that pngcheck_file returns either captured_stream
46
- # or extra message but not both
47
- [status, captured_stream.read + extra_msg.get_string(16)]
48
- end
48
+ return [STATUS_CRITICAL_ERROR, EMPTY_IMAGE] if File.zero? path
49
+
50
+ do_analyze_file(path)
49
51
  end
50
52
 
51
53
  def check_file(path)
52
54
  status, info = analyze_file(path)
55
+ raise EmptyPngError.new if info.eql? EMPTY_IMAGE
53
56
  raise CorruptPngError.new info unless status == STATUS_OK
54
57
 
55
58
  true
56
59
  end
57
60
 
58
61
  def analyze_buffer(data)
62
+ return [STATUS_CRITICAL_ERROR, EMPTY_IMAGE] if data.empty?
63
+
64
+ do_analyze_buffer(data)
65
+ end
66
+
67
+ def check_buffer(data)
68
+ status, info = analyze_buffer(data)
69
+ raise EmptyPngError.new if info.eql? EMPTY_IMAGE
70
+ raise CorruptPngError.new info unless status == STATUS_OK
71
+
72
+ true
73
+ end
74
+
75
+ private
76
+
77
+ def do_analyze_file(path)
78
+ Tempfile.open("captured-stream-") do |captured_stream|
79
+ extra_msg = FFI::Buffer.alloc_out(EXTRA_MESSAGE_SIZE, 1, false)
80
+ @@semaphore.lock
81
+ status = pngcheck_file(path, captured_stream.path, extra_msg)
82
+ @@semaphore.unlock
83
+ # we assume that pngcheck_file returns either captured_stream
84
+ # or extra message but not both
85
+
86
+ [status, captured_stream.read + extra_msg.get_string(0)]
87
+ end
88
+ end
89
+
90
+ def do_analyze_buffer(data)
59
91
  Tempfile.open("captured-stream-") do |captured_stream|
60
92
  extra_msg = FFI::Buffer.alloc_out(EXTRA_MESSAGE_SIZE, 1, false)
61
93
  mem_buf = FFI::MemoryPointer.new(:char, data.bytesize)
@@ -64,15 +96,8 @@ module PngCheck
64
96
  status = pngcheck_buffer(mem_buf, data.bytesize, captured_stream.path,
65
97
  extra_msg)
66
98
  @@semaphore.unlock
67
- [status, captured_stream.read + extra_msg.get_string(16)]
99
+ [status, captured_stream.read + extra_msg.get_string(0)]
68
100
  end
69
101
  end
70
-
71
- def check_buffer(data)
72
- status, info = analyze_buffer(data)
73
- raise CorruptPngError.new info unless status == STATUS_OK
74
-
75
- true
76
- end
77
102
  end
78
103
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pngcheck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-08 00:00:00.000000000 Z
11
+ date: 2022-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -94,7 +94,7 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '1.4'
97
- description:
97
+ description:
98
98
  email:
99
99
  - open.source@ribose.com
100
100
  executables:
@@ -127,7 +127,7 @@ metadata:
127
127
  homepage_uri: https://github.com/metanorma/pngcheck-ruby
128
128
  source_code_uri: https://github.com/metanorma/pngcheck-ruby
129
129
  changelog_uri: https://github.com/metanorma/pngcheck-ruby
130
- post_install_message:
130
+ post_install_message:
131
131
  rdoc_options: []
132
132
  require_paths:
133
133
  - lib
@@ -142,8 +142,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0'
144
144
  requirements: []
145
- rubygems_version: 3.3.7
146
- signing_key:
145
+ rubygems_version: 3.1.6
146
+ signing_key:
147
147
  specification_version: 4
148
148
  summary: Ruby interface to pngcheck.
149
149
  test_files: []