-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic4lazarus.pas
298 lines (266 loc) · 10 KB
/
magic4lazarus.pas
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
(****************************************************************************
*
* Magic4Lazarus:
* Retrive the mime of content from a file or a buffer.
*
* Copyright (c) Francisco Ernesto Teixeira 2010.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice immediately at the beginning of the file, without modification,
* this list of conditions, and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
****************************************************************************)
unit magic4lazarus;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
{ TMagic }
{ is a wrapper around the libmagic C library }
TMagic = class
private
FHANDLE: integer;
FOnlyMimeEncoding: boolean;
FOnlyMimeType: boolean;
FOnlyMimeEncodig: boolean;
FFileMagic: string;
FFlags: integer;
procedure SetOnlyMimeType(const AValue: boolean);
procedure SetOnlyMimeEncoding(const AValue: boolean);
procedure BeforeDetect;
public
property HANDLE: integer read FHANDLE;
property OnlyMimeType: boolean read FOnlyMimeType write SetOnlyMimeType;
property OnlyMimeEncoding: boolean read FOnlyMimeEncoding write SetOnlyMimeEncoding;
property FileMagic: string read FFileMagic write FFileMagic;
constructor Create();
destructor Destroy; override;
function FromFile(filename: string): string;
function FromBuffer(var buffer; count: longint): string;
end;
// Detect the mime of file's content and return it
function DetectMimeFromFile(filename: string; filemagic: string): string;
// Detect the mime type of file's content and return it
function DetectMimeTypeFromFile(filename: string; filemagic: string): string;
// Detect the mime encoding of file's content and return it
function DetectMimeEncodingFromFile(filename: string; filemagic: string): string;
implementation
const
{$IFDEF WINDOWS}
MAGIC_LIBRARY = 'magic1.dll';
{$ENDIF}
{$IFDEF LINUX}
MAGIC_LIBRARY = 'libmagic.so';
{$ENDIF}
{$IFDEF DARWIN}
MAGIC_LIBRARY = 'libmagic.dylib';
{$linklib libmagic}
{$ENDIF}
MAGIC_NONE = $000000; // No flags
{$EXTERNALSYM MAGIC_NONE}
MAGIC_DEBUG = $000001; // Turn on debugging
{$EXTERNALSYM MAGIC_DEBUG}
MAGIC_SYMLINK = $000002; // Follow symlinks
{$EXTERNALSYM MAGIC_SYMLINK}
MAGIC_COMPRESS = $000004; // Check inside compressed files
{$EXTERNALSYM MAGIC_COMPRESS}
MAGIC_DEVICES = $000008; // Look at the contents of devices
{$EXTERNALSYM MAGIC_DEVICES}
MAGIC_MIME_TYPE = $000010; // Return the MIME type
{$EXTERNALSYM MAGIC_MIME_TYPE}
MAGIC_CONTINUE = $000020; // Return all matches
{$EXTERNALSYM MAGIC_CONTINUE}
MAGIC_CHECK = $000040; // Print warnings to stderr
{$EXTERNALSYM MAGIC_CHECK}
MAGIC_PRESERVE_ATIME = $000080; // Restore access time on exit
{$EXTERNALSYM MAGIC_PRESERVE_ATIME}
MAGIC_RAW = $000100; // Don't translate unprintable chars
{$EXTERNALSYM MAGIC_RAW}
MAGIC_ERROR = $000200; // Handle ENOENT etc as real errors
{$EXTERNALSYM MAGIC_ERROR}
MAGIC_MIME_ENCODING = $000400; // Return the MIME encoding
{$EXTERNALSYM MAGIC_MIME_ENCODING}
MAGIC_MIME = (MAGIC_MIME_TYPE or MAGIC_MIME_ENCODING);
{$EXTERNALSYM MAGIC_MIME}
MAGIC_APPLE = $000800; // Return the Apple creator and type
{$EXTERNALSYM MAGIC_APPLE}
MAGIC_NO_CHECK_COMPRESS = $001000; // Don't check for compressed files
{$EXTERNALSYM MAGIC_NO_CHECK_COMPRESS}
MAGIC_NO_CHECK_TAR = $002000; // Don't check for tar files
{$EXTERNALSYM MAGIC_NO_CHECK_TAR}
MAGIC_NO_CHECK_SOFT = $004000; // Don't check magic entries
{$EXTERNALSYM MAGIC_NO_CHECK_SOFT}
MAGIC_NO_CHECK_APPTYPE = $008000; // Don't check application type
{$EXTERNALSYM MAGIC_NO_CHECK_APPTYPE}
MAGIC_NO_CHECK_ELF = $010000; // Don't check for elf details
{$EXTERNALSYM MAGIC_NO_CHECK_ELF}
MAGIC_NO_CHECK_TEXT = $020000; // Don't check for text files
{$EXTERNALSYM MAGIC_NO_CHECK_TEXT}
MAGIC_NO_CHECK_CDF = $040000; // Don't check for cdf files
{$EXTERNALSYM MAGIC_NO_CHECK_CDF}
MAGIC_NO_CHECK_TOKENS = $100000; // Don't check tokens
{$EXTERNALSYM MAGIC_NO_CHECK_TOKENS}
MAGIC_NO_CHECK_ENCODING = $200000; // Don't check text encodings
{$EXTERNALSYM MAGIC_NO_CHECK_ENCODING}
// Defined for backwards compatibility (renamed)
MAGIC_NO_CHECK_ASCII = MAGIC_NO_CHECK_TEXT;
{$EXTERNALSYM MAGIC_NO_CHECK_ASCII}
// Defined for backwards compatibility; do nothing
MAGIC_NO_CHECK_FORTRAN = $000000; // Don't check ascii/fortran
{$EXTERNALSYM MAGIC_NO_CHECK_FORTRAN}
MAGIC_NO_CHECK_TROFF = $000000; // Don't check ascii/troff
{$EXTERNALSYM MAGIC_NO_CHECK_TROFF}
function magic_open(flags: integer): integer; cdecl; external MAGIC_LIBRARY;
procedure magic_close(cookie: integer); cdecl; external MAGIC_LIBRARY;
function magic_file(cookie: integer; filename: PChar): PChar; cdecl; external MAGIC_LIBRARY;
function magic_descriptor(cookie: integer): PChar; cdecl; external MAGIC_LIBRARY;
function magic_buffer(cookie: integer; var buffer; Count: longint): PChar; cdecl; external MAGIC_LIBRARY;
//function magic_error(cookie: integer): PChar; cdecl; external MAGIC_LIBRARY;
function magic_setflags(coockie: integer; flags: integer): integer; cdecl; external MAGIC_LIBRARY;
function magic_load(cookie: integer; magic_file: PChar): integer; cdecl; external MAGIC_LIBRARY;
function magic_compile(cookie: integer; magic_file: PChar): integer; cdecl; external MAGIC_LIBRARY;
//function magic_check(cookie: integer; magic_file: PChar): Integer; cdecl; external MAGIC_LIBRARY;
function magic_errno(cookie: integer): integer; cdecl; external MAGIC_LIBRARY;
// Detect the mime of file's content and return it
function DetectMimeFromFile(filename: string; filemagic: string): string;
var
magic: TMagic;
begin
magic := TMagic.Create();
try
magic.FileMagic := filemagic;
Result := magic.FromFile(filename);
finally
FreeAndNil(magic);
end;
end;
// Detect the mime type of file's content and return it
function DetectMimeTypeFromFile(filename: string; filemagic: string): string;
var
magic: TMagic;
begin
magic := TMagic.Create();
try
magic.FileMagic := filemagic;
magic.OnlyMimeType := True;
Result := magic.FromFile(filename);
finally
FreeAndNil(magic);
end;
end;
// Detect the mime encoding of file's content and return it
function DetectMimeEncodingFromFile(filename: string; filemagic: string): string;
var
magic: TMagic;
begin
magic := TMagic.Create();
try
magic.FileMagic := filemagic;
magic.OnlyMimeEncoding := True;
Result := magic.FromFile(filename);
finally
FreeAndNil(magic);
end;
end;
{ TMagic }
// Create a new libmagic wrapper.
constructor TMagic.Create();
begin
Self.FHANDLE := 0;
FOnlyMimeType := False;
FOnlyMimeEncodig := False;
FFileMagic := '';
FFlags := MAGIC_MIME;
Self.FHANDLE := magic_open(Self.FFlags);
end;
// On destroy this class close the library.
destructor TMagic.Destroy;
begin
magic_close(Self.FHANDLE);
inherited Destroy;
end;
// When is defined the kind of detect is only mime type, or no.
procedure TMagic.SetOnlyMimeType(const AValue: boolean);
begin
if (Self.FOnlyMimeType = AValue) then
begin
exit;
end;
Self.FOnlyMimeType := AValue;
if (Self.FOnlyMimeType) then
begin
Self.FOnlyMimeEncoding := False;
Self.FFlags := MAGIC_MIME_TYPE;
end
else if (not Self.FOnlyMimeEncoding) then
begin
Self.FFlags := MAGIC_MIME;
end;
end;
// When is defined the kind of detect is only mime encoding, or no.
procedure TMagic.SetOnlyMimeEncoding(const AValue: boolean);
begin
if (Self.FOnlyMimeEncoding = AValue) then
begin
exit;
end;
Self.FOnlyMimeEncoding := AValue;
if (Self.FOnlyMimeEncoding) then
begin
Self.FOnlyMimeType := False;
Self.FFlags := MAGIC_MIME_ENCODING;
end
else if (not Self.FOnlyMimeType) then
begin
Self.FFlags := MAGIC_MIME;
end;
end;
// Before detect a mime of content.
// raises Exception if the file magic does not exist
procedure TMagic.BeforeDetect;
begin
if (not FileExists(Self.FFileMagic)) then
begin
raise Exception.Create('File magic does not exist: ' + Self.FFileMagic);
end;
magic_load(Self.FHANDLE, PChar(Self.FFileMagic));
magic_setflags(Self.FHANDLE, Self.FFlags);
end;
// Identify the contents of file 'filename'.
// raises Exception if the file does not exist
function TMagic.FromFile(filename: string): string;
begin
Self.BeforeDetect();
if (not FileExists(filename)) then
begin
raise Exception.Create('File does not exist: ' + filename);
end;
Result := magic_file(Self.FHANDLE, PChar(filename));
end;
// Identify the contents of buffer.
function TMagic.FromBuffer(var buffer; count: longint): string;
begin
Self.BeforeDetect();
Result := magic_buffer(Self.FHANDLE, buffer, count);
end;
end.