Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ For more information and some examples, see `Magic::TypeChecker` and

## Development

Create an issue if you think anything needs revision!
Create an issue or pull request to discuss any suggestions.

### Known bugs (help wanted)
- `Magic::TypeChecker.max_regex=()` does not actually do *anything*. The value
remains constant regardless of the value passed to this method.

## Contributing

Expand Down
3 changes: 1 addition & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ version: 1.1.0
authors:
- D. Scott Boggs <scott@tams.tech>

crystal: 0.35.0
crystal: 1.0.0

development_dependencies:
ameba:
github: crystal-ameba/ameba

license: MIT

44 changes: 32 additions & 12 deletions spec/libmagic/magic_spec.cr
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
require "../spec_helper"
include Magic

describe "LibMagic" do
cookie = LibMagic.open LibMagic::NONE
module Magic
describe "LibMagic" do
cookie = LibMagic.open(LibMagic::Options::NONE).not_nil!
describe ".set_flags (and .flags)" do
it "sets a flag" do
# Magic::LibMagic.flags(cookie).should eq 0
# This seems to be a bug in spec, if you uncomment this line
# `crystal spec` will fail, but running
# `crystal run spec/libmagic/magic_spec.cr` will pass.
LibMagic.set_flags cookie, LibMagic::Options::CONTINUE
LibMagic::Options::CONTINUE.should eq LibMagic.flags cookie
LibMagic.set_flags cookie, LibMagic::Options::NONE
LibMagic.flags(cookie).should eq LibMagic::Options::NONE
end
end

describe ".set_flags (and .flags)" do
pending "sets a flag" do
LibMagic.flags(cookie).should eq 0
LibMagic.set_flags(cookie, LibMagic::CONTINUE).should eq 0
LibMagic.flags(cookie).should eq LibMagic::CONTINUE
LibMagic.set_flags(cookie, LibMagic::NONE).should eq 0
LibMagic.flags(cookie).should eq 0
describe ".set_param and .param" do
it "sets the regex param" do
value = 12345
LibMagic.set_param cookie, LibMagic::Param::MAX_REGEX, pointerof(value)
LibMagic.get_param cookie, LibMagic::Param::MAX_REGEX, out result
result.should eq 12345
end
{% for param in %w(indirection name elf_notes elf_phnum elf_shnum bytes) %}
it "sets the {{param.id}} param" do
value = 12345
LibMagic.set_param cookie, LibMagic::Param::MAX_{{param.id.upcase}}, pointerof(value)
LibMagic.get_param cookie, LibMagic::Param::MAX_{{param.id.upcase}}, out result
result.should eq 12345
end
{% end %}
end
ensure
cookie.try &->Magic::LibMagic.close(LibMagic::MagicT)
end

LibMagic.close cookie
end
50 changes: 25 additions & 25 deletions spec/magic.cr_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ describe Magic do

describe "Magic::LibMagic" do
it "works" do
magic_cookie = Magic::LibMagic.open Magic::LibMagic::NONE
magic_cookie = Magic::LibMagic.open Magic::LibMagic::Options::NONE
Magic::LibMagic.close magic_cookie
end
it "knows the mime-type of a directory" do
magic_cookie = Magic::LibMagic.open Magic::LibMagic::MIME_TYPE
magic_cookie = Magic::LibMagic.open Magic::LibMagic::Options::MIME_TYPE
Magic::LibMagic.load magic_cookie, nil
String.new(Magic::LibMagic.file(magic_cookie, ".")).should eq "inode/directory"
end
Expand All @@ -59,59 +59,59 @@ describe Magic do
chkr = Magic::TypeChecker.new
chkr.options.should eq Magic::TypeChecker::DEFAULT_OPTIONS
chkr.options = 1
chkr.options.should eq Magic::LibMagic::DEBUG
chkr.options.should eq Magic::LibMagic::Options::DEBUG
end
end

# options
describe(".max_indir=") do
it("sets a value") do
describe ".max_indirection=" do
it "sets a value" do
chkr = Magic::TypeChecker.new
chkr.max_indir = 12345
chkr.max_indir.should(eq(12345))
chkr.max_indirection = 12345
chkr.max_indirection.should eq 12345
end
end
describe(".max_name=") do
it("sets a value") do
describe ".max_name=" do
it "sets a value" do
chkr = Magic::TypeChecker.new
chkr.max_name = 12345
chkr.max_name.should(eq(12345))
chkr.max_name.should eq 12345
end
end
describe(".max_elf_phnum=") do
it("sets a value") do
describe ".max_elf_phnum=" do
it "sets a value" do
chkr = Magic::TypeChecker.new
chkr.max_elf_phnum = 12345
chkr.max_elf_phnum.should(eq(12345))
chkr.max_elf_phnum.should eq 12345
end
end
describe(".max_elf_notes=") do
it("sets a value") do
describe ".max_elf_notes=" do
it "sets a value" do
chkr = Magic::TypeChecker.new
chkr.max_elf_notes = 12345
chkr.max_elf_notes.should(eq(12345))
chkr.max_elf_notes.should eq 12345
end
end
describe(".max_elf_shnum=") do
it("sets a value") do
describe ".max_elf_shnum=" do
it "sets a value" do
chkr = Magic::TypeChecker.new
chkr.max_elf_shnum = 12345
chkr.max_elf_shnum.should(eq(12345))
chkr.max_elf_shnum.should eq 12345
end
end
# PARAM_REGEX_MAX cannot be changed on my system.
describe(".max_regex=") do
describe ".max_regex=" do
pending "sets a value" do
chkr = Magic::TypeChecker.new
chkr.max_regex = 12345
chkr.max_regex.should(eq(12345))
chkr.max_regex.should eq 12345
end
end
describe(".max_bytes=") do
it("sets a value") do
describe ".max_bytes=" do
it "sets a value" do
chkr = Magic::TypeChecker.new
chkr.max_bytes = 12345
chkr.max_bytes.should(eq(12345))
chkr.max_bytes.should eq 12345
end
end

Expand Down Expand Up @@ -187,7 +187,7 @@ describe Magic do
end
describe "#reset_options" do
chkr = Magic::TypeChecker.new
chkr.debug_output.options.should eq (Magic::TypeChecker::DEFAULT_OPTIONS | Magic::LibMagic::DEBUG)
chkr.debug_output.options.should eq (Magic::TypeChecker::DEFAULT_OPTIONS | Magic::LibMagic::Options::DEBUG)
chkr.reset_options.options.should eq Magic::TypeChecker::DEFAULT_OPTIONS
end
end
Expand Down
5 changes: 5 additions & 0 deletions src/core_ext.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct Int32
def ==(opts : Magic::LibMagic::Options)
self == opts.value
end
end
91 changes: 50 additions & 41 deletions src/libmagic/magic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,46 @@ module Magic

@[Link(ldflags: "-lmagic")]
lib LibMagic
# No special handling.
NONE = 0x0000000
# Print debugging messages to stderr.
DEBUG = 0x0000001
# TODO: more docs
SYMLINK = 0x0000002
COMPRESS = 0x0000004
DEVICES = 0x0000008
MIME_TYPE = 0x0000010
CONTINUE = 0x0000020
CHECK = 0x0000040
PRESERVE_ATIME = 0x0000080
RAW = 0x0000100
ERROR = 0x0000200
MIME_ENCODING = 0x0000400
MIME = MIME_TYPE | MIME_ENCODING
APPLE = 0x0000800
EXTENSION = 0x1000000
COMPRESS_TRANSP = 0x2000000
NODESC = MAGIC_EXTENSION | MAGIC_MIME | MAGIC_APPLE
NO_CHECK_COMPRESS = 0x0001000
NO_CHECK_TAR = 0x0002000
NO_CHECK_SOFT = 0x0004000
NO_CHECK_APPTYPE = 0x0008000
NO_CHECK_ELF = 0x0010000
NO_CHECK_TEXT = 0x0020000
NO_CHECK_CDF = 0x0040000
NO_CHECK_TOKENS = 0x0100000
NO_CHECK_ENCODING = 0x0200000
NO_CHECK_BUILTIN = MAGIC_NO_CHECK_COMPRESS | MAGIC_NO_CHECK_TAR | MAGIC_NO_CHECK_APPTYPE | MAGIC_NO_CHECK_ELF | MAGIC_NO_CHECK_TEXT | MAGIC_NO_CHECK_CDF | MAGIC_NO_CHECK_TOKENS | MAGIC_NO_CHECK_ENCODING | 0 \
enum Options : Int32
# No special handling.
NONE = 0x0000000
# Print debugging messages to stderr.
DEBUG = 0x0000001
# TODO: more docs
SYMLINK = 0x0000002
COMPRESS = 0x0000004
DEVICES = 0x0000008
MIME_TYPE = 0x0000010
CONTINUE = 0x0000020
CHECK = 0x0000040
PRESERVE_ATIME = 0x0000080
RAW = 0x0000100
ERROR = 0x0000200
MIME_ENCODING = 0x0000400
MIME = MIME_TYPE | MIME_ENCODING
APPLE = 0x0000800
EXTENSION = 0x1000000
COMPRESS_TRANSP = 0x2000000
NODESC = EXTENSION | MIME | APPLE
NO_CHECK_COMPRESS = 0x0001000
NO_CHECK_TAR = 0x0002000
NO_CHECK_SOFT = 0x0004000
NO_CHECK_APPTYPE = 0x0008000
NO_CHECK_ELF = 0x0010000
NO_CHECK_TEXT = 0x0020000
NO_CHECK_CDF = 0x0040000
NO_CHECK_TOKENS = 0x0100000
NO_CHECK_ENCODING = 0x0200000
NO_CHECK_BUILTIN = NO_CHECK_COMPRESS | NO_CHECK_TAR | NO_CHECK_APPTYPE | NO_CHECK_ELF | NO_CHECK_TEXT | NO_CHECK_CDF | NO_CHECK_TOKENS | NO_CHECK_ENCODING | 0 \

NO_CHECK_ASCII = NO_CHECK_TEXT
NO_CHECK_FORTRAN = 0x000000
NO_CHECK_TROFF = 0x000000

def ==(other : Int32)
value == other
end
end

SNPRINTB = "\177\020\
b\0debug\0\
Expand Down Expand Up @@ -90,17 +100,16 @@ b\27no_check_reserved2\0\
b\30extension\0\
b\31transp_compression\0\
"
NO_CHECK_ASCII = MAGIC_NO_CHECK_TEXT
NO_CHECK_FORTRAN = 0x000000
NO_CHECK_TROFF = 0x000000
VERSION = 532
PARAM_INDIR_MAX = 0o0
PARAM_NAME_MAX = 1
PARAM_ELF_PHNUM_MAX = 2
PARAM_ELF_SHNUM_MAX = 3
PARAM_ELF_NOTES_MAX = 4
PARAM_REGEX_MAX = 5
PARAM_BYTES_MAX = 6
VERSION = 532
enum Param
MAX_INDIRECTION
MAX_NAME
MAX_ELF_PHNUM
MAX_ELF_SHNUM
MAX_ELF_NOTES
MAX_REGEX
MAX_BYTES
end
# type MagicSet = Object
alias MagicT = MagicSet*
# returns a magic cookie on success and NULL on failure setting errno to
Expand Down
Loading