Debian Codegraph @

libhylafax/TypeRules.c++

raw ◀ ▶
2 Debian patches touch this file
1 /* $Id$ */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include <ctype.h>
27
28 #include "Array.h"
29 #include "TypeRules.h"
30 #include "PageSize.h"
31 #include "config.h"
32
33 #include <string.h>
34 #include <stdlib.h>
35
36 #ifdef _XOPEN_SOURCE_EXTENDED
37 #include <arpa/inet.h>
38 #else
39 extern "C" {
40 #include <netinet/in.h>
41 }
42 #endif
43
44 #include "NLS.h"
45
46 #ifndef TRUE
47 #define TRUE 1
48 #endif
49
50 #ifndef FALSE
51 #define FALSE 0
52 #endif
53
54 TypeRule::TypeRule() {}
55 TypeRule::~TypeRule() {}
56 TypeRule::TypeRule(const TypeRule& other)
57 : fxObj(other)
58 , cmd(other.cmd)
59 {
60 off = other.off;
61 cont = other.cont;
62 type = other.type;
63 op = other.op;
64 value.v = other.value.v;
65 result = other.result;
66 }
67
68 static const char* typeNames[] =
69 { "ascii", "asciiesc", "string", "istring", "address", "byte", "short", "long" };
70 static const char* opNames[] =
71 { "<any>", "=", "!=", "<", "<=", ">", ">=", "&", "^", "!" };
72 static const char* resultNames[] = { "tiff", "postscript", "pdf", "pcl", "error" };
73
74 fxStr
75 quoted(const fxStr& s)
76 {
77 fxStr q("'");;
78 for (u_int i = 0; i < s.length(); i++) {
79 if (s[i] == '\'')
80 q.append("'\\''");
81 else
82 q.append(s[i]);
83 }
84 q.append("'");
85 return (q);
86 }
87
88
89 bool
90 TypeRule::match(const void* data, size_t size, bool verbose) const
91 {
92 if (verbose) {
93 // translator: This shows the rule to be processed (the last two %s) starting at offset %#lx.
94 printf(NLS::TEXT("rule: %soffset %#lx %s %s"),
95 cont ? ">" : "",
96 (u_long) off,
97 typeNames[type],
98 opNames[op]
99 );
100 if (type == STRING || type == ISTRING)
101 printf(" \"%s\"", value.s);
102 else if (type != ASCII && type != ASCIIESC) {
103 if (op == ANY)
•104 printf("%s", NLS::TEXT(" <any value>"));
105 else
106 printf(" %#llx", (long long) value.v);
107 }
108 printf(" -- ");
109 }
110 if (off > (off_t)size) {
111 if (verbose)
•112 printf("%s", NLS::TEXT("failed (offset past data)\n"));
113 return (false);
114 }
115 bool ok = false;
116 long v = 0;
117 const u_char* cp = (const u_char*) data;
118 switch (type) {
119 case ASCII:
120 {
121 u_int i;
122 for (i = 0; i < size; i++)
123 if (!isprint(cp[i]) && !isspace(cp[i])) {
124 if (verbose)
125 printf(NLS::TEXT("failed (unprintable char %#x)\n"), cp[i]);
126 return (false);
127 }
128 ok = true;
129 goto done;
130 }
131 case ASCIIESC:
132 {
133 u_int i;
134 for (i = 0; i < size; i++)
135 if (!isprint(cp[i]) && !isspace(cp[i]) && cp[i] != '\033') {
136 if (verbose)
137 printf(NLS::TEXT("failed (unprintable char %#x)\n"), cp[i]);
138 return (FALSE);
139 }
140 ok = TRUE;
141 goto done;
142 }
143 case STRING:
144 ok = (strncmp((const char*)(cp+off), value.s,
145 fxmin((u_int) strlen(value.s), (u_int)(size-off))) == 0);
146 goto done;
147 case ISTRING:
148 ok = (strncasecmp((const char*)(cp+off), value.s,
149 fxmin((u_int) strlen(value.s), (u_int)(size-off))) == 0);
150 goto done;
151 case ADDR:
152 v = (u_long) off;
153 break;
154 case BYTE:
155 v = *cp;
156 break;
157 case SHORT:
158 if (off + 2 < (off_t)size) {
159 u_short w;
160 memcpy(&w, cp+off, 2);
161 v = ntohs(w);
162 break;
163 }
164 if (verbose)
•165 printf("%s", NLS::TEXT("failed (insufficient data)\n"));
166 return (false);
167 case LONG:
168 if (off + 4 < (off_t)size) {
169 memcpy(&v, cp+off, 4);
170 v = ntohl(v);
171 break;
172 }
173 if (verbose)
•174 printf("%s", NLS::TEXT("failed (insufficient data)\n"));
175 return (false);
176 }
177 /*
178 * Numeric value, use operation.
179 */
180 switch (op) {
181 case ANY: ok = true; break;
182 case EQ: ok = (v == value.v); break;
183 case NE: ok = (v != value.v); break;
184 case LT: ok = (v < value.v); break;
185 case LE: ok = (v <= value.v); break;
186 case GT: ok = (v > value.v); break;
187 case GE: ok = (v >= value.v); break;
188 case AND: ok = ((v&value.v) == value.v); break;
189 case NOT: ok = ((v&value.v) != value.v); break;
190 case XOR: ok = ((v^value.v) != 0); break;
191 }
192 done:
193 if (verbose) {
194 if (ok)
195 printf(NLS::TEXT("success (result %s, rule \"%s\")\n"),
196 resultNames[result], (const char*) cmd);
197 else
•198 printf("%s", NLS::TEXT("failed (comparison)\n"));
199 }
200 return (ok);
201 }
202
203 /*
204 * rule: a string passed to the shell to convert the input file
205 * to the result format (suitable for sending as facsimile).
206 * The rule string is a printf-like string that should use the
207 * following "%" escapes:
208 * %i input file name
209 * %o output file name
210 * %r output horizontal resolution in pixels/mm
211 * %R output horizontal resolution in pixels/inch
212 * %v output vertical resolution in lines/mm
213 * %V output vertical resolution in lines/inch
214 * %f data format, 1 for 1-d encoding or 2 for 2-d encoding
215 * %w page width in pixels
216 * %W page width in mm
217 * %l page length in pixels
218 * %L page length in mm
219 * %s page size by name
220 * %F the pathname of the fax library (e.g./usr/local/lib/fax)
221 * %<x> the <x> character (e.g. ``%%'' results in ``%''
222 */
223 fxStr
224 TypeRule::getFmtdCmd(
225 const fxStr& input, const fxStr& output,
226 float hr, float vr, const fxStr& df, const fxStr& pname) const
227 {
228 fxStr fmtd;
229 const PageSizeInfo* info = PageSizeInfo::getPageSizeByName(pname);
230 float pw = info->width();
231 float pl = info->height();
232
233 for (u_int i = 0, n = cmd.length(); i < n; i++) {
234 char c = cmd[i];
235 if (c == '%' && i+1 < n) {
236 i++;
237 switch (c = cmd[i]) {
238 case 'i': fmtd.append(quoted(input)); continue;
239 case 'o': fmtd.append(quoted(output)); continue;
•240 case 'R': fmtd.append(fxStr(hr, "%.0f")); continue;
241 case 'r': fmtd.append(fxStr(hr/25.4, "%.2g")); continue;
•242 case 'V': fmtd.append(fxStr(vr, "%.0f")); continue;
243 case 'v': fmtd.append(fxStr(vr/25.4, "%.2g")); continue;
244 case 'f': fmtd.append(df); continue;
245 case 'W': fmtd.append(fxStr(pw, "%.4g")); continue;
246 case 'w': fmtd.append(fxStr(pw*hr/25.4, "%.0f")); continue;
247 case 'L': fmtd.append(fxStr(pl, "%.4g")); continue;
248 case 'l': fmtd.append(fxStr(pl*vr/25.4, "%.0f")); continue;
249 case 'F': fmtd.append(fxStr(FAX_LIBEXEC)); continue;
250 case 's': fmtd.append(pname); continue;
251 }
252 }
253 fmtd.append(c);
254 }
255 return fmtd;
256 }
257
258 fxDECLARE_ObjArray(TypeRuleArray, TypeRule)
259 fxIMPLEMENT_ObjArray(TypeRuleArray, TypeRule)
260
261 TypeRules::TypeRules()
262 {
263 verbose = false;
264 rules = new TypeRuleArray;
265 }
266
267 TypeRules::~TypeRules()
268 {
269 delete rules;
270 }
271
272 void
273 TypeRules::setVerbose(bool b)
274 {
275 verbose = b;
276 }
277
278 #include <ctype.h>
279
280 static bool
281 appendLine(fxStr& s, const char* line)
282 {
283 const char* cp = line;
284 for (; isspace(*cp); cp++)
285 ;
286 if (cp > line) // put back one bit of white space
287 cp--;
288 const char* cmd = cp;
289 if (*cp != '\0' && *cp != '\n' && *cp != '#') {
290 do {
291 cp++;
292 if (cp[-1] == '\\') {
293 if (*cp == '\n') { // continue cmd string on next line
294 s.append(cmd, cp-1 - cmd);
295 return (true);
296 } else if (*cp) // accept anything but \\0
297 cp++;
298 }
299 } while (*cp != '\0' && *cp != '\n' && *cp != '#');
300 s.append(fxStr(cmd, cp-cmd));
301 }
302 return (false);
303 }
304
305 #include <stdarg.h>
306
307 static void
308 parseError(const char* file, u_int lineno, const char* fmt ...)
309 {
310 va_list ap;
311 va_start(ap, fmt);
312 // translator: "line" as in the line number %u of filename %s.
313 fprintf(stderr, NLS::TEXT("%s: line %u: "), file, lineno);
314 vfprintf(stderr, fmt, ap);
315 va_end(ap);
316 }
317
318 TypeRules*
319 TypeRules::read(const fxStr& file)
320 {
321 FILE* fp;
322
323 fp = fopen(file, "r");
324 if (fp == NULL) {
325 fprintf(stderr, NLS::TEXT("%s: Can not open type rules file.\n"),
326 (const char*) file);
327 return NULL;
328 }
329 TypeRules* tr = new TypeRules;
330 char line[256];
331 u_int lineno = 0;
332 while (fgets(line, sizeof (line), fp) != NULL) {
333 lineno++;
334 char* cp = line;
335 if (*cp == '\n' || *cp == '#')
336 continue;
337 TypeRule rule;
338 if (*cp == '>') { // continuation
339 rule.cont = true;
340 cp++;
341 } else
342 rule.cont = false;
343 const char *op = cp;
344 rule.off = strtoul(op, &cp, 0); // file offset
345 if (cp == op) {
346 parseError(file, lineno, NLS::TEXT("Missing file offset"));
347 continue;
348 }
349 while (isspace(*cp))
350 cp++;
351 const char* tp = cp;
352 while (*cp && !isspace(*cp)) // data type
353 cp++;
354 if (strncasecmp(tp, "byte", cp-tp) == 0)
355 rule.type = TypeRule::BYTE;
356 else if (strncasecmp(tp, "short", cp-tp) == 0)
357 rule.type = TypeRule::SHORT;
358 else if (strncasecmp(tp, "long", cp-tp) == 0)
359 rule.type = TypeRule::LONG;
360 else if (strncasecmp(tp, "string", cp-tp) == 0)
361 rule.type = TypeRule::STRING;
362 else if (strncasecmp(tp, "istring", cp-tp) == 0)
363 rule.type = TypeRule::ISTRING;
364 else if (strncasecmp(tp, "ascii", cp-tp) == 0)
365 rule.type = TypeRule::ASCII;
366 else if (strncasecmp(tp, "asciiesc", cp-tp) == 0)
367 rule.type = TypeRule::ASCIIESC;
368 else if (strncasecmp(tp, "addr", cp-tp) == 0)
369 rule.type = TypeRule::ADDR;
370 else {
371 parseError(file, lineno, NLS::TEXT("Unknown datatype \"%.*s\""), cp-tp, tp);
372 continue; // bad type
373 }
374 while (isspace(*cp))
375 cp++;
376 rule.op = TypeRule::EQ; // default is '='
377 const char* vp = cp;
378 if (rule.type != TypeRule::STRING && rule.type != TypeRule::ISTRING
379 && rule.type != TypeRule::ASCII && rule.type != TypeRule::ASCIIESC) {
380 // numeric value
381 switch (*vp) {
382 case '=': rule.op = TypeRule::EQ; cp++; break;
383 case '^': rule.op = TypeRule::XOR; cp++; break;
384 case '&': rule.op = TypeRule::AND; cp++; break;
385 case 'x': rule.op = TypeRule::ANY; cp++; break;
386 case '>':
387 if (cp[1] == '=') {
388 rule.op = TypeRule::GE; cp++;
389 } else
390 rule.op = TypeRule::GT;
391 cp++;
392 break;
393 case '<':
394 if (cp[1] == '=') {
395 rule.op = TypeRule::LE; cp++;
396 } else
397 rule.op = TypeRule::LT;
398 cp++;
399 break;
400 case '!':
401 if (cp[1] == '=') {
402 rule.op = TypeRule::NE; cp++;
403 } else
404 rule.op = TypeRule::NOT;
405 cp++;
406 break;
407 }
408 if (rule.op != TypeRule::ANY) {
409 const char* vp = cp;
410 rule.value.v = strtol(vp, &cp, 0);
411 if (vp == cp) {
412 parseError(file, lineno, NLS::TEXT("Missing match value"));
413 continue;
414 }
415 }
416 } else { // string value
417 while (*cp != '\0' && *cp != '\t') // NB: accept blanks
418 cp++;
419 if (*cp != '\t')
420 continue;
421 u_int len = cp-vp;
422 rule.value.s = (char*) malloc(len+1); // +1 for \0
423 memcpy(rule.value.s, vp, len);
424 rule.value.s[len] = '\0';
425 }
426 while (isspace(*cp))
427 cp++;
428 const char* rp = cp;
429 while (isalpha(*cp))
430 cp++;
431 if (strncasecmp(rp, "tiff", cp-rp) == 0)
432 rule.result = TypeRule::TIFF;
433 else if (strncasecmp(rp, "ps", cp-rp) == 0)
434 rule.result = TypeRule::POSTSCRIPT;
435 else if (strncasecmp(rp, "pdf", cp-rp) == 0)
436 rule.result = TypeRule::PDF;
437 else if (strncasecmp(rp, "pcl", cp-rp) == 0)
438 rule.result = TypeRule::PCL;
439 else if (strncasecmp(rp, "error", cp-rp) == 0)
440 rule.result = TypeRule::ERROR;
441 else {
442 parseError(file, lineno, NLS::TEXT("Unknown result \"%.*s\""), cp-rp, rp);
443 continue;
444 }
445 while (isspace(*cp))
446 cp++;
447 const char* cmd = cp; // collect cmd string
448 if (*cp != '\0' && *cp != '\n' && *cp != '#') {
449 bool done = false; // XXX workaround compiler limitation
450 do {
451 cp++;
452 if (cp[-1] == '\\') {
453 if (*cp == '\n') { // continue cmd string on next line
454 rule.cmd = fxStr(cmd, cp-1 - cmd);
455 while (fgets(line, sizeof (line), fp) != NULL &&
456 appendLine(rule.cmd, line))
457 ;
458 done = true;
459 } else if (*cp) // accept anything but \\0
460 cp++;
461 }
462 } while (!done && *cp != '\0' && *cp != '\n' && *cp != '#');
463 if (!done)
464 rule.cmd = fxStr(cmd, cp-cmd);
465 }
466 tr->rules->append(rule);
467 }
468 (void) fclose(fp);
469 return (tr);
470 }
471
472 /*
473 * Check secondary matching rules after a primary match.
474 */
475 u_int
476 TypeRules::match2(u_int base, const void* data, u_int size, bool verb) const
477 {
478 for (u_int i = 1, n = (*rules).length() - base; i < n; i++) {
479 TypeRule& rule = (*rules)[base+i];
480 if (!rule.isContinuation())
481 break;
482 if (rule.match(data, size, verb))
483 return (i);
484 }
485 return 0;
486 }
487
488 const TypeRule*
489 TypeRules::match(const void* data, u_int size) const
490 {
491 if (verbose)
492 printf(NLS::TEXT("match against (..., %u)\n"), size);
493 for (u_int i = 0, n = (*rules).length(); i < n; i++) {
494 TypeRule& rule = (*rules)[i];
495 if (!rule.isContinuation() && rule.match(data, size, verbose))
496 return (&(*rules)[i + match2(i, data, size, verbose)]);
497 }
498 if (verbose)
•499 printf("%s", NLS::TEXT("no match\n"));
500 return (NULL);
501 }