1 /* 2 * MIME type/conversion database definitions for CUPS. 3 * 4 * Copyright © 2020-2024 by OpenPrinting. 5 * Copyright 2007-2013 by Apple Inc. 6 * Copyright 1997-2007 by Easy Software Products, all rights reserved. 7 * 8 * Licensed under Apache License v2.0. See the file "LICENSE" for more information. 9 */ 10 11 #ifndef _CUPS_MIME_H_ 12 # define _CUPS_MIME_H_ 13 14 # include <cups/array.h> 15 # include <cups/ipp.h> 16 # include <cups/file.h> 17 # include <cups/thread-private.h> 18 # include <regex.h> 19 20 21 /* 22 * C++ magic... 23 */ 24 25 # ifdef __cplusplus 26 extern "C" { 27 # endif /* __cplusplus */ 28 29 30 /* 31 * Constants... 32 */ 33 34 # define MIME_MAX_SUPER 16 /* Maximum size of supertype name */ 35 # define MIME_MAX_TYPE IPP_MAX_NAME /* Maximum size of type name */ 36 # define MIME_MAX_FILTER 256 /* Maximum size of filter pathname */ 37 # define MIME_MAX_BUFFER 8192 /* Maximum size of file buffer */ 38 39 40 /* 41 * Types/structures... 42 */ 43 44 typedef enum 45 { 46 MIME_MAGIC_NOP, /* No operation */ 47 MIME_MAGIC_AND, /* Logical AND of all children */ 48 MIME_MAGIC_OR, /* Logical OR of all children */ 49 MIME_MAGIC_MATCH, /* Filename match */ 50 MIME_MAGIC_ASCII, /* ASCII characters in range */ 51 MIME_MAGIC_PRINTABLE, /* Printable characters (32-255) in range */ 52 MIME_MAGIC_STRING, /* String matches */ 53 MIME_MAGIC_CHAR, /* Character/byte matches */ 54 MIME_MAGIC_SHORT, /* Short/16-bit word matches */ 55 MIME_MAGIC_INT, /* Integer/32-bit word matches */ 56 MIME_MAGIC_LOCALE, /* Current locale matches string */ 57 MIME_MAGIC_CONTAINS, /* File contains a string */ 58 MIME_MAGIC_ISTRING, /* Case-insensitive string matches */ 59 MIME_MAGIC_REGEX /* Regular expression matches */ 60 } mime_op_t; 61 62 typedef struct _mime_magic_s /**** MIME Magic Data ****/ 63 { 64 struct _mime_magic_s *prev, /* Previous rule */ 65 *next, /* Next rule */ 66 *parent, /* Parent rules */ 67 *child; /* Child rules */ 68 short op, /* Operation code (see above) */ 69 invert; /* Invert the result */ 70 int offset, /* Offset in file */ 71 region, /* Region length */ 72 length; /* Length of data */ 73 union 74 { 75 char matchv[64]; /* Match value */ 76 char localev[64]; /* Locale value */ 77 char stringv[64]; /* String value */ 78 unsigned char charv; /* Byte value */ 79 unsigned short shortv; /* Short value */ 80 unsigned intv; /* Integer value */ 81 regex_t rev; /* Regular expression value */ 82 } value; 83 } mime_magic_t; 84 85 typedef struct _mime_type_s /**** MIME Type Data ****/ 86 { 87 mime_magic_t *rules; /* Rules used to detect this type */ 88 int priority; /* Priority of this type */ 89 char super[MIME_MAX_SUPER], /* Super-type name ("image", "application", etc.) */ 90 type[MIME_MAX_TYPE]; /* Type name ("png", "postscript", etc.) */ 91 } mime_type_t; 92 93 typedef struct _mime_filter_s /**** MIME Conversion Filter Data ****/ 94 { 95 mime_type_t *src, /* Source type */ 96 *dst; /* Destination type */ 97 int cost; /* Relative cost */ 98 char filter[MIME_MAX_FILTER];/* Filter program to use */ 99 size_t maxsize; /* Maximum file size for this filter */ 100 } mime_filter_t; 101 102 typedef void (*mime_error_cb_t)(void *ctx, const char *message); 103 104 typedef struct _mime_s /**** MIME Database ****/ 105 { 106 cups_array_t *types; /* File types */ 107 cups_array_t *filters; /* Type conversion filters */ 108 cups_array_t *srcs; /* Filters sorted by source type */ 109 mime_error_cb_t error_cb; /* Error message callback */ 110 void *error_ctx; /* Pointer for callback */ 111 _cups_rwlock_t lock; /* Read/write lock for guarding data for background updates */ 112 } mime_t; 113 114 115 /* 116 * Functions... 117 */ 118 119 extern void mimeDelete(mime_t *mime); 120 extern mime_t *mimeNew(void) _CUPS_API_1_5; 121 extern mime_t *mimeLoad(const char *pathname, const char *filterpath); 122 extern mime_t *mimeLoadFilters(mime_t *mime, const char *pathname, 123 const char *filterpath); 124 extern mime_t *mimeLoadTypes(mime_t *mime, const char *pathname); 125 126 extern mime_type_t *mimeAddType(mime_t *mime, const char *super, 127 const char *type); 128 extern int mimeAddTypeRule(mime_type_t *mt, const char *rule); 129 extern void mimeDeleteType(mime_t *mime, mime_type_t *mt); 130 extern mime_type_t *mimeFileType(mime_t *mime, const char *pathname, 131 const char *filename, int *compression); 132 extern mime_type_t *mimeFirstType(mime_t *mime); 133 extern mime_type_t *mimeNextType(mime_t *mime); 134 extern int mimeNumTypes(mime_t *mime); 135 extern mime_type_t *mimeType(mime_t *mime, const char *super, 136 const char *type); 137 138 extern mime_filter_t *mimeAddFilter(mime_t *mime, mime_type_t *src, 139 mime_type_t *dst, int cost, 140 const char *filter); 141 extern void mimeDeleteFilter(mime_t *mime, mime_filter_t *filter); 142 extern cups_array_t *mimeFilter(mime_t *mime, mime_type_t *src, 143 mime_type_t *dst, int *cost); 144 extern cups_array_t *mimeFilter2(mime_t *mime, mime_type_t *src, 145 size_t srcsize, mime_type_t *dst, 146 int *cost); 147 extern mime_filter_t *mimeFilterLookup(mime_t *mime, mime_type_t *src, 148 mime_type_t *dst); 149 extern mime_filter_t *mimeFirstFilter(mime_t *mime); 150 extern mime_filter_t *mimeNextFilter(mime_t *mime); 151 extern int mimeNumFilters(mime_t *mime); 152 extern void mimeSetErrorCallback(mime_t *mime, mime_error_cb_t cb, 153 void *context) _CUPS_API_1_5; 154 155 # ifdef __cplusplus 156 } 157 # endif /* __cplusplus */ 158 #endif /* !_CUPS_MIME_H_ */ 159