mahoro.git  about / heads / tags
Ruby interface to libmagic
blob 6476bc64d30212b133efbfd710063c3cfe24266d 4328 bytes (raw)
$ git show v0.1:mahoro.c	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
 
/*
 * This file is Public Domain.
 */

#include <ruby.h>
#include <magic.h>

struct MagicCookie
{
	magic_t cookie;
};

static VALUE cMahoro;
static VALUE eMahoroError;

static void
mahoro_free(ptr)
	struct MagicCookie *ptr;
{
	magic_close(ptr->cookie);
	free(ptr);
}

static VALUE
mahoro_allocate(klass)
	VALUE klass;
{
	return Data_Wrap_Struct(klass, 0, mahoro_free, 0);
}

static VALUE
mahoro_initialize(argc, argv, self)
	int argc;
	VALUE *argv, self;
{
	int flags = MAGIC_NONE;
	char *path = 0;
	struct MagicCookie *ptr;
	magic_t cookie;
	VALUE vpath, vflags;

	switch(rb_scan_args(argc, argv, "02", &vflags, &vpath)) {
		case 2:
			if(!NIL_P(vpath)) {
				path = StringValuePtr(vpath);
			}
			/* fallthrough */
		case 1:
			flags = FIX2INT(vflags);
			break;
	}

	if(!(cookie = magic_open(flags))) {
		rb_raise(eMahoroError, "failed to initialize magic cookie");
	}

	if(magic_load(cookie, path)) {
		rb_raise(eMahoroError, "failed to load database: %s",
		                      magic_error(cookie));
	}

	ptr = ALLOC(struct MagicCookie);
	ptr->cookie = cookie;
	DATA_PTR(self) = ptr;

	return self;
}

static VALUE
mahoro_file(self, path)
	VALUE self, path;
{
	const char *msg;
	magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;

	if(!(msg = magic_file(cookie, StringValuePtr(path)))) {
		rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie));
	}

	return rb_str_new2(msg);
}

static VALUE
mahoro_buffer(self, input)
	VALUE self, input;
{
	const char *msg;
	magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;

	if(!(msg = magic_buffer(cookie, StringValuePtr(input),
	                                RSTRING(StringValue(input))->len))) {
		rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie));
	}

	return rb_str_new2(msg);
}

static VALUE
mahoro_set_flags(self, flags)
	VALUE self, flags;
{
	magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;

	return INT2FIX(magic_setflags(cookie, FIX2INT(flags)));
}

static VALUE
mahoro_check(argc, argv, self)
	int argc;
	VALUE *argv, self;
{
	char *path = 0;
	VALUE vpath;
	magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;

	switch(rb_scan_args(argc, argv, "01", &vpath)) {
		case 1:
			if(!NIL_P(vpath)) { 
				path = StringValuePtr(vpath);
			}
			break;
	}

	if(!magic_check(cookie, path)) {
		return Qtrue;
	} else {
		return Qfalse;
	}
}

static VALUE
mahoro_compile(klass, path)
	VALUE klass, path;
{
	magic_t cookie = magic_open(MAGIC_NONE);

	if(magic_compile(cookie, StringValuePtr(path))) {
		rb_raise(eMahoroError, "failed compile: %s", magic_error(cookie));
	}

	magic_close(cookie);

	return Qtrue;
}

static VALUE
mahoro_load(self, path)
	VALUE self, path;
{
	magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;

	if(magic_load(cookie, StringValuePtr(path))) {
		rb_raise(eMahoroError, "failed load: %s", magic_error(cookie));
	}

	return self;
}

void Init_mahoro(void)
{
	cMahoro      = rb_define_class("Mahoro", rb_cObject);
	eMahoroError = rb_define_class_under(cMahoro, "Error", rb_eStandardError);

	rb_const_set(cMahoro, rb_intern("NONE"), INT2FIX(MAGIC_NONE));
	rb_const_set(cMahoro, rb_intern("DEBUG"), INT2FIX(MAGIC_DEBUG));
	rb_const_set(cMahoro, rb_intern("SYMLINK"), INT2FIX(MAGIC_SYMLINK));
	rb_const_set(cMahoro, rb_intern("COMPRESS"), INT2FIX(MAGIC_COMPRESS));
	rb_const_set(cMahoro, rb_intern("DEVICES"), INT2FIX(MAGIC_DEVICES));
	rb_const_set(cMahoro, rb_intern("MIME"), INT2FIX(MAGIC_MIME));
	rb_const_set(cMahoro, rb_intern("CONTINUE"), INT2FIX(MAGIC_CONTINUE));
	rb_const_set(cMahoro, rb_intern("CHECK"), INT2FIX(MAGIC_CHECK));
	rb_const_set(cMahoro, rb_intern("PRESERVE_ATIME"),
	                      INT2FIX(MAGIC_PRESERVE_ATIME));
	rb_const_set(cMahoro, rb_intern("RAW"), INT2FIX(MAGIC_RAW));
	rb_const_set(cMahoro, rb_intern("ERROR"), INT2FIX(MAGIC_ERROR));

	rb_define_alloc_func(cMahoro, mahoro_allocate);
	rb_define_method(cMahoro, "initialize", mahoro_initialize, -1);
	rb_define_method(cMahoro, "file", mahoro_file, 1);
	rb_define_method(cMahoro, "buffer", mahoro_buffer, 1);
	rb_define_method(cMahoro, "flags=", mahoro_set_flags, 1);
	rb_define_method(cMahoro, "valid?", mahoro_check, -1);
	rb_define_singleton_method(cMahoro, "compile", mahoro_compile, 1);
	rb_define_method(cMahoro, "load", mahoro_load, 1);
}

/* arch-tag: mahoro */

git clone https://yhbt.net/mahoro.git