File: magiclib_stub.c

package info (click to toggle)
mldonkey 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,524 kB
  • sloc: ml: 149,175; cpp: 11,805; ansic: 8,780; sh: 4,226; asm: 3,870; xml: 1,092; perl: 102; makefile: 95
file content (280 lines) | stat: -rw-r--r-- 7,177 bytes parent folder | download | duplicates (6)
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
/* File: magic.ml

   Copyright (C) 2005

     Christophe Troestler
     email: Christophe.Troestler@umh.ac.be
     WWW: http://www.umh.ac.be/math/an/software/

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   version 2.1 as published by the Free Software Foundation, with the
   special exception on linking described in file LICENSE.

   This library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the file
   LICENSE for more details.
*/
/* 	$Id: magiclib_stub.c,v 1.2 2006/05/18 17:26:18 spiralvoice Exp $	 */

#include "../../../config/config.h"
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/custom.h>
#include <caml/fail.h>
#include <caml/callback.h>

#include <magic.h>
#include <errno.h>
#define _XOPEN_SOURCE 600
#include <string.h>
#include <stdio.h>

/* Outputs an error message and terminates the program. */
#define DEBUG(...) \
  fprintf(stderr, "DEBUG magic_stub: " __VA_ARGS__);  \
  printf("\n")

#define CAML_MAGIC_VERSION "0.2"

/*
 * Failure
 */

static void raise_magic_failure(const char * msg)
{
  static value * exn = NULL;
  if (exn == NULL) exn = caml_named_value("Magiclib.Failure");
  raise_with_string(*exn, (char *) msg);
}

/* [fname] is the function name. */
static void raise_on_error(const char* fname, magic_t cookie)
{
  const char *err_magic;
  char *errmsg; /* For thread safety of error messages */
  int flen;
  
  flen = strlen(fname);
  if ((err_magic = magic_error(cookie)) != NULL) {
    if ((errmsg = malloc(flen + strlen(err_magic) + 1)) == NULL)
      raise_out_of_memory();
    strcpy(errmsg, fname);
    strcpy(errmsg + flen, err_magic);
    raise_magic_failure(errmsg);
  }
  else {
    int len = 80;  /* Initial buffer length */
    int err;
  
    /* Allocate buffer [errmsg] until there is enough space for the
     * error message. */
    err = magic_errno(cookie);
    if ((errmsg = malloc(len)) == NULL) raise_out_of_memory();
    strcpy(errmsg, fname);
#ifdef HAVE_STRERROR_R
    while (strerror_r(err, errmsg + flen, len - flen) < 0) {
      len *= 2;
      errmsg = realloc(errmsg, len);
      if (errmsg == NULL) raise_out_of_memory();
    }
#else
      strcat (errmsg, strerror(err));        
#endif
    raise_sys_error(copy_string(errmsg));
  }
}


/*
 * magic_t
 */

/* magic_t is a pointer on 'struct magic_set' so one can set it to NULL */
#define COOKIE_VAL(v) (* ((magic_t *) Data_custom_val(v)))

/* If the cookie has not been forcibly closed with [magic_close], free it. */
static void free_cookie(value c)
{
  magic_t cookie = COOKIE_VAL(c);
  if (cookie != NULL) {
    magic_close(cookie);
    COOKIE_VAL(c) = NULL;
  }
}

/* compare magic_t pointers (=> total order) */
static int compare_cookie(value c1, value c2)
{
  magic_t cookie1 = COOKIE_VAL(c1), cookie2 = COOKIE_VAL(c2);
  
  if (cookie1 == cookie2)      return 0;
  else if (cookie1 < cookie2)  return -1;
  else return 1;
}

static struct custom_operations cookie_ops = {
    /* identifier */ "be.ac.umh.math/magic.cookie." CAML_MAGIC_VERSION,
    /* finalize */ free_cookie,
    /* compare */ compare_cookie,
    /* hash */ custom_hash_default,
    /* serialize */ custom_serialize_default,
    /* deserialize */ custom_deserialize_default
};

#define ALLOC_COOKIE alloc_custom(&cookie_ops, sizeof(magic_t), \
                     sizeof(magic_t), 40 * sizeof(magic_t))

/*
 * Stubs
 */

CAMLprim value ocaml_magic_open(value flags)
{
  CAMLparam1(flags);
  CAMLlocal1(c);
  char *errmsg;
  int len = 80;
  
  c = ALLOC_COOKIE;
  if ((COOKIE_VAL(c) = magic_open(Int_val(flags) | MAGIC_ERROR)) == NULL) {
    if (errno == EINVAL)
      /* An unsupported value for flags was given */
      raise_magic_failure("Magiclib.create: Preserve_atime not supported");
    else {
      /* No cookie yet, so one cannot use the above generic err fun */
      if ((errmsg = malloc(len)) == NULL) raise_out_of_memory();
      strcpy(errmsg, "Magiclib.create: "); /* 14 chars */
#ifdef HAVE_STRERROR_R
      while (strerror_r(errno, errmsg + 14, len - 14) < 0) {
        len *= 2;
        if ((errmsg = realloc(errmsg, len)) == NULL) raise_out_of_memory();
      }
#else
      strcat (errmsg, strerror(errno));
#endif
      raise_sys_error(copy_string(errmsg));
    }
  }
  CAMLreturn(c);
}

CAMLprim value ocaml_magic_close(value c)
{
  CAMLparam1(c);
  magic_t cookie = COOKIE_VAL(c);
  if (cookie != NULL) /* if first time it is called */
    magic_close(cookie);
  COOKIE_VAL(c) = NULL; /* For the finalization function & multiple calls */
  CAMLreturn(Val_unit);
}


CAMLprim value ocaml_magic_file(value c, value fname)
{
  CAMLparam2(c, fname);
  const char * ans;
  magic_t cookie = COOKIE_VAL(c);

  if (cookie == NULL) invalid_argument("Magiclib.file");
  if ((ans = magic_file(cookie, String_val(fname))) == NULL) {
    raise_on_error("Magiclib.file: ", cookie);
  }
  CAMLreturn(copy_string(ans));
}

CAMLprim value ocaml_magic_buffer(value c, value buf, value len)
{
  CAMLparam3(c, buf, len);
  const char * ans;
  magic_t cookie = COOKIE_VAL(c);

  if (cookie == NULL) caml_invalid_argument("Magiclib.buffer");
  if ((ans = magic_buffer(cookie, String_val(buf), Int_val(len)))
      == NULL)
    raise_on_error("Magiclib.buffer: ", cookie);
  CAMLreturn(copy_string(ans));
}


CAMLprim value ocaml_magic_setflags(value c, value flags)
{
  CAMLparam2(c, flags);
  magic_t cookie = COOKIE_VAL(c);

  if (cookie == NULL) caml_invalid_argument("Magiclib.setflags");
  if (magic_setflags(cookie, Int_val(flags)) < 0)
    raise_magic_failure("Magiclib.setflags: Preserve_atime not supported");
  CAMLreturn(Val_unit);
}



#define CHECK(fname) \
  magic_t cookie = COOKIE_VAL(c); \
  \
  if (cookie == NULL) caml_invalid_argument("Magiclib.check"); \
  if (magic_check(cookie, fname) < 0) \
    CAMLreturn(Val_false); \
  else \
    CAMLreturn(Val_true)

CAMLprim value ocaml_magic_check_default(value c)
{
  CAMLparam1(c);
  CHECK(NULL);
}
CAMLprim value ocaml_magic_check(value c, value filenames)
{
  CAMLparam2(c, filenames);
  CHECK(String_val(filenames));
}



#define COMPILE(fname) \
  magic_t cookie = COOKIE_VAL(c); \
  \
  if (cookie == NULL) caml_invalid_argument("Magiclib.compile"); \
  if (magic_compile(cookie, fname) < 0) \
    raise_on_error("Magiclib.compile: ", cookie);  \
  CAMLreturn(Val_unit)

CAMLprim value ocaml_magic_compile_default(value c)
{
  CAMLparam1(c);
  COMPILE(NULL);
}

CAMLprim value ocaml_magic_compile(value c, value filenames)
{
  CAMLparam2(c, filenames);
  COMPILE(String_val(filenames));
}



#define LOAD(fname) \
  magic_t cookie = COOKIE_VAL(c); \
  \
  if (cookie == NULL) caml_invalid_argument("Magiclib.load"); \
  if (magic_load(cookie, fname) < 0) \
    raise_on_error("Magiclib.load: ", cookie);  \
  CAMLreturn(Val_unit)

CAMLprim
value ocaml_magic_load_default(value c)
{
  CAMLparam1(c);
  LOAD(NULL);
}

CAMLprim
value ocaml_magic_load(value c, value filenames)
{
  CAMLparam2(c, filenames);
  LOAD(String_val(filenames));
}