| 1 |
#include "imageri.h" |
| 2 |
#include <stdio.h> |
| 3 |
|
| 4 |
static volatile im_slot_t slot_count = 1; |
| 5 |
static im_slot_destroy_t *volatile slot_destructors; |
| 6 |
static volatile i_mutex_t slot_mutex; |
| 7 |
|
| 8 |
/* |
| 9 |
=item im_context_new() |
| 10 |
|
| 11 |
Create a new Imager context object. |
| 12 |
|
| 13 |
=cut |
| 14 |
*/ |
| 15 |
|
| 16 |
im_context_t |
| 17 |
im_context_new(void) { |
| 18 |
im_context_t ctx = malloc(sizeof(im_context_struct)); |
| 19 |
int i; |
| 20 |
|
| 21 |
if (!slot_mutex) |
| 22 |
slot_mutex = i_mutex_new(); |
| 23 |
|
| 24 |
if (!ctx) |
| 25 |
return NULL; |
| 26 |
|
| 27 |
ctx->error_sp = IM_ERROR_COUNT-1; |
| 28 |
for (i = 0; i < IM_ERROR_COUNT; ++i) { |
| 29 |
ctx->error_alloc[i] = 0; |
| 30 |
ctx->error_stack[i].msg = NULL; |
| 31 |
ctx->error_stack[i].code = 0; |
| 32 |
} |
| 33 |
#ifdef IMAGER_LOG |
| 34 |
ctx->log_level = 0; |
| 35 |
ctx->lg_file = NULL; |
| 36 |
#endif |
| 37 |
ctx->max_width = 0; |
| 38 |
ctx->max_height = 0; |
| 39 |
ctx->max_bytes = DEF_BYTES_LIMIT; |
| 40 |
|
| 41 |
ctx->slot_alloc = slot_count; |
| 42 |
ctx->slots = calloc(ctx->slot_alloc, sizeof(void *)); |
| 43 |
if (!ctx->slots) { |
| 44 |
free(ctx); |
| 45 |
return NULL; |
| 46 |
} |
| 47 |
|
| 48 |
ctx->file_magic = NULL; |
| 49 |
|
| 50 |
ctx->refcount = 1; |
| 51 |
|
| 52 |
#ifdef IMAGER_TRACE_CONTEXT |
| 53 |
fprintf(stderr, "im_context: created %p\n", ctx); |
| 54 |
#endif |
| 55 |
|
| 56 |
|
| 57 |
return ctx; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
=item im_context_refinc(ctx, where) |
| 62 |
X<im_context_refinc API> |
| 63 |
=section Context objects |
| 64 |
=synopsis im_context_refinc(aIMCTX, "a description"); |
| 65 |
|
| 66 |
Add a new reference to the context. |
| 67 |
|
| 68 |
=cut |
| 69 |
*/ |
| 70 |
|
| 71 |
void |
| 72 |
im_context_refinc(im_context_t ctx, const char *where) { |
| 73 |
++ctx->refcount; |
| 74 |
|
| 75 |
#ifdef IMAGER_TRACE_CONTEXT |
| 76 |
fprintf(stderr, "im_context:%s: refinc %p (count now %lu)\n", where, |
| 77 |
ctx, (unsigned long)ctx->refcount); |
| 78 |
#else |
| 79 |
(void)where; |
| 80 |
#endif |
| 81 |
} |
| 82 |
|
| 83 |
/* |
| 84 |
=item im_context_refdec(ctx, where) |
| 85 |
X<im_context_refdec API> |
| 86 |
=section Context objects |
| 87 |
=synopsis im_context_refdec(aIMCTX, "a description"); |
| 88 |
|
| 89 |
Remove a reference to the context, releasing it if all references have |
| 90 |
been removed. |
| 91 |
|
| 92 |
=cut |
| 93 |
*/ |
| 94 |
|
| 95 |
void |
| 96 |
im_context_refdec(im_context_t ctx, const char *where) { |
| 97 |
int i; |
| 98 |
size_t slot; |
| 99 |
|
| 100 |
im_assert(ctx->refcount > 0); |
| 101 |
|
| 102 |
--ctx->refcount; |
| 103 |
|
| 104 |
#ifdef IMAGER_TRACE_CONTEXT |
| 105 |
fprintf(stderr, "im_context:%s: delete %p (count now %lu)\n", where, |
| 106 |
ctx, (unsigned long)ctx->refcount); |
| 107 |
#else |
| 108 |
(void)where; |
| 109 |
#endif |
| 110 |
|
| 111 |
if (ctx->refcount != 0) |
| 112 |
return; |
| 113 |
|
| 114 |
/* lock here to avoid slot_destructors from being moved under us */ |
| 115 |
i_mutex_lock(slot_mutex); |
| 116 |
for (slot = 0; slot < ctx->slot_alloc; ++slot) { |
| 117 |
if (ctx->slots[slot] && slot_destructors[slot]) |
| 118 |
slot_destructors[slot](ctx->slots[slot]); |
| 119 |
} |
| 120 |
i_mutex_unlock(slot_mutex); |
| 121 |
|
| 122 |
free(ctx->slots); |
| 123 |
|
| 124 |
for (i = 0; i < IM_ERROR_COUNT; ++i) { |
| 125 |
if (ctx->error_stack[i].msg) |
| 126 |
myfree(ctx->error_stack[i].msg); |
| 127 |
} |
| 128 |
|
| 129 |
{ |
| 130 |
im_file_magic *p = ctx->file_magic; |
| 131 |
while (p != NULL) { |
| 132 |
im_file_magic *n = p->next; |
| 133 |
free(p->m.name); |
| 134 |
free(p->m.magic); |
| 135 |
free(p->m.mask); |
| 136 |
free(p); |
| 137 |
p = n; |
| 138 |
} |
| 139 |
} |
| 140 |
#ifdef IMAGER_LOG |
| 141 |
if (ctx->lg_file && ctx->own_log) |
| 142 |
fclose(ctx->lg_file); |
| 143 |
#endif |
| 144 |
|
| 145 |
free(ctx); |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
=item im_context_clone(ctx) |
| 150 |
|
| 151 |
Clone an Imager context object, returning the result. |
| 152 |
|
| 153 |
The error stack is not copied from the original context. |
| 154 |
|
| 155 |
=cut |
| 156 |
*/ |
| 157 |
|
| 158 |
im_context_t |
| 159 |
im_context_clone(im_context_t ctx, const char *where) { |
| 160 |
im_context_t nctx = malloc(sizeof(im_context_struct)); |
| 161 |
int i; |
| 162 |
|
| 163 |
if (!nctx) |
| 164 |
return NULL; |
| 165 |
|
| 166 |
nctx->slot_alloc = slot_count; |
| 167 |
nctx->slots = calloc(nctx->slot_alloc, sizeof(void *)); |
| 168 |
if (!nctx->slots) { |
| 169 |
free(nctx); |
| 170 |
return NULL; |
| 171 |
} |
| 172 |
|
| 173 |
nctx->error_sp = IM_ERROR_COUNT-1; |
| 174 |
for (i = 0; i < IM_ERROR_COUNT; ++i) { |
| 175 |
nctx->error_alloc[i] = 0; |
| 176 |
nctx->error_stack[i].msg = NULL; |
| 177 |
} |
| 178 |
#ifdef IMAGER_LOG |
| 179 |
nctx->log_level = ctx->log_level; |
| 180 |
if (ctx->lg_file) { |
| 181 |
if (ctx->own_log) { |
| 182 |
int newfd = dup(fileno(ctx->lg_file)); |
| 183 |
if (newfd >= 0) { |
| 184 |
nctx->own_log = 1; |
| 185 |
nctx->lg_file = fdopen(newfd, "w"); |
| 186 |
if (nctx->lg_file) |
| 187 |
setvbuf(nctx->lg_file, NULL, _IONBF, BUFSIZ); |
| 188 |
} |
| 189 |
else { |
| 190 |
#ifdef IMAGER_TRACE_CONTEXT |
| 191 |
perror("im_context:failed to clone log"); |
| 192 |
#endif |
| 193 |
free(nctx->slots); |
| 194 |
free(nctx); |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
} |
| 198 |
else { |
| 199 |
/* stderr */ |
| 200 |
nctx->lg_file = ctx->lg_file; |
| 201 |
nctx->own_log = 0; |
| 202 |
} |
| 203 |
} |
| 204 |
else { |
| 205 |
nctx->lg_file = NULL; |
| 206 |
} |
| 207 |
#endif |
| 208 |
nctx->max_width = ctx->max_width; |
| 209 |
nctx->max_height = ctx->max_height; |
| 210 |
nctx->max_bytes = ctx->max_bytes; |
| 211 |
|
| 212 |
nctx->refcount = 1; |
| 213 |
|
| 214 |
{ |
| 215 |
im_file_magic *inp = ctx->file_magic; |
| 216 |
im_file_magic **outpp = &nctx->file_magic; |
| 217 |
*outpp = NULL; |
| 218 |
while (inp) { |
| 219 |
im_file_magic *m = malloc(sizeof(im_file_magic)); |
| 220 |
if (!m) { |
| 221 |
/* should free any parts of the list already allocated */ |
| 222 |
im_context_refdec(nctx, "failed cloning"); |
| 223 |
return NULL; |
| 224 |
} |
| 225 |
m->next = NULL; |
| 226 |
m->m.name = strdup(inp->m.name); |
| 227 |
m->m.magic_size = inp->m.magic_size; |
| 228 |
m->m.magic = malloc(inp->m.magic_size); |
| 229 |
m->m.mask = malloc(inp->m.magic_size); |
| 230 |
if (m->m.name == NULL || m->m.magic == NULL || m->m.mask == NULL) { |
| 231 |
free(m->m.name); |
| 232 |
free(m->m.magic); |
| 233 |
free(m->m.mask); |
| 234 |
free(m); |
| 235 |
im_context_refdec(nctx, "failed cloning"); |
| 236 |
return NULL; |
| 237 |
} |
| 238 |
memcpy(m->m.magic, inp->m.magic, m->m.magic_size); |
| 239 |
memcpy(m->m.mask, inp->m.mask, m->m.magic_size); |
| 240 |
*outpp = m; |
| 241 |
outpp = &m->next; |
| 242 |
inp = inp->next; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
#ifdef IMAGER_TRACE_CONTEXT |
| 247 |
fprintf(stderr, "im_context:%s: cloned %p to %p\n", where, ctx, nctx); |
| 248 |
#else |
| 249 |
(void)where; |
| 250 |
#endif |
| 251 |
|
| 252 |
return nctx; |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
=item im_context_slot_new(destructor) |
| 257 |
|
| 258 |
Allocate a new context-local-storage slot. |
| 259 |
|
| 260 |
C<desctructor> will be called when the context is destroyed if the |
| 261 |
corresponding slot is non-NULL. |
| 262 |
|
| 263 |
=cut |
| 264 |
*/ |
| 265 |
|
| 266 |
im_slot_t |
| 267 |
im_context_slot_new(im_slot_destroy_t destructor) { |
| 268 |
im_slot_t new_slot; |
| 269 |
im_slot_destroy_t *new_destructors; |
| 270 |
if (!slot_mutex) |
| 271 |
slot_mutex = i_mutex_new(); |
| 272 |
|
| 273 |
i_mutex_lock(slot_mutex); |
| 274 |
|
| 275 |
new_slot = slot_count++; |
| 276 |
new_destructors = realloc(slot_destructors, sizeof(void *) * slot_count); |
| 277 |
if (!new_destructors) |
| 278 |
i_fatal(1, "Cannot allocate memory for slot destructors"); |
| 279 |
slot_destructors = new_destructors; |
| 280 |
|
| 281 |
slot_destructors[new_slot] = destructor; |
| 282 |
|
| 283 |
i_mutex_unlock(slot_mutex); |
| 284 |
|
| 285 |
return new_slot; |
| 286 |
} |
| 287 |
|
| 288 |
/* |
| 289 |
=item im_context_slot_set(slot, value) |
| 290 |
|
| 291 |
Set the value of a slot. |
| 292 |
|
| 293 |
Returns true on success. |
| 294 |
|
| 295 |
Aborts if the slot supplied is invalid. |
| 296 |
|
| 297 |
If reallocation of slot storage fails, returns false. |
| 298 |
|
| 299 |
=cut |
| 300 |
*/ |
| 301 |
|
| 302 |
int |
| 303 |
im_context_slot_set(im_context_t ctx, im_slot_t slot, void *value) { |
| 304 |
if (slot < 0 || slot >= slot_count) { |
| 305 |
fprintf(stderr, "Invalid slot %d (valid 0 - %d)\n", |
| 306 |
(int)slot, (int)slot_count-1); |
| 307 |
abort(); |
| 308 |
} |
| 309 |
|
| 310 |
if ((size_t)slot >= ctx->slot_alloc) { |
| 311 |
size_t slot_index; |
| 312 |
size_t new_alloc = slot_count; |
| 313 |
void **new_slots = realloc(ctx->slots, sizeof(void *) * new_alloc); |
| 314 |
|
| 315 |
if (!new_slots) |
| 316 |
return 0; |
| 317 |
|
| 318 |
for (slot_index = ctx->slot_alloc; slot_index < new_alloc; ++slot_index) |
| 319 |
new_slots[slot_index] = NULL; |
| 320 |
|
| 321 |
ctx->slots = new_slots; |
| 322 |
ctx->slot_alloc = new_alloc; |
| 323 |
} |
| 324 |
|
| 325 |
ctx->slots[slot] = value; |
| 326 |
|
| 327 |
return 1; |
| 328 |
} |
| 329 |
|
| 330 |
/* |
| 331 |
=item im_context_slot_get(ctx, slot) |
| 332 |
|
| 333 |
Retrieve the value previously stored in the given slot of the context |
| 334 |
object. |
| 335 |
|
| 336 |
=cut |
| 337 |
*/ |
| 338 |
|
| 339 |
void * |
| 340 |
im_context_slot_get(im_context_t ctx, im_slot_t slot) { |
| 341 |
if (slot < 0 || slot >= slot_count) { |
| 342 |
fprintf(stderr, "Invalid slot %d (valid 0 - %d)\n", |
| 343 |
(int)slot, (int)slot_count-1); |
| 344 |
abort(); |
| 345 |
} |
| 346 |
|
| 347 |
if ((size_t)slot >= ctx->slot_alloc) |
| 348 |
return NULL; |
| 349 |
|
| 350 |
return ctx->slots[slot]; |
| 351 |
} |
| 352 |
|
| 353 |
/* |
| 354 |
=item im_add_file_magic(ctx, name, bits, mask, length) |
| 355 |
|
| 356 |
Add file type magic to the given context. |
| 357 |
|
| 358 |
=cut |
| 359 |
*/ |
| 360 |
|
| 361 |
int |
| 362 |
im_add_file_magic(im_context_t ctx, const char *name, |
| 363 |
const unsigned char *bits, const unsigned char *mask, |
| 364 |
size_t length) { |
| 365 |
im_file_magic *m = malloc(sizeof(im_file_magic)); |
| 366 |
|
| 367 |
if (m == NULL) |
| 368 |
return 0; |
| 369 |
|
| 370 |
if (length > 512) |
| 371 |
length = 512; |
| 372 |
|
| 373 |
m->m.name = strdup(name); |
| 374 |
m->m.magic = malloc(length); |
| 375 |
m->m.mask = malloc(length); |
| 376 |
m->m.magic_size = length; |
| 377 |
|
| 378 |
if (name == NULL || bits == NULL || mask == NULL) { |
| 379 |
free(m->m.name); |
| 380 |
free(m->m.magic); |
| 381 |
free(m->m.mask); |
| 382 |
free(m); |
| 383 |
return 0; |
| 384 |
} |
| 385 |
memcpy(m->m.magic, bits, length); |
| 386 |
memcpy(m->m.mask, mask, length); |
| 387 |
m->next = ctx->file_magic; |
| 388 |
ctx->file_magic = m; |
| 389 |
|
| 390 |
return 1; |
| 391 |
} |