TypeRules.h
上传用户:weiyuanprp
上传日期:2020-05-20
资源大小:1169k
文件大小:7k
源码类别:

传真(Fax)编程

开发平台:

C/C++

  1. /* $Id: TypeRules.h,v 1.4 2008/01/04 23:24:31 faxguy Exp $ */
  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. #ifndef _TYPERULES_
  27. #define _TYPERULES_
  28. /*
  29.  * Fax Submission File Type and Conversion Rules.
  30.  */
  31. #include "Types.h"
  32. #include "Str.h"
  33. #include "Obj.h"
  34. /*
  35.  * A type rule database is an ASCII file that contains
  36.  * type deduction and conversion rules.  The format is
  37.  * as follows:
  38.  *
  39.  * HylaFAX file type rules.
  40.  *
  41.  * This file contains the file typing rules used by the sendfax
  42.  * program to deduce how input files should be prepared for fax
  43.  * transmission.  The format of this file is based on the System
  44.  * V /etc/magic file used by the file(1) program.  The code that
  45.  * reads this file was written entirely based on the comments that
  46.  * exist at the top of the magic file and describe how it works.
  47.  * The use of magic-style rules is intended to make it easier for
  48.  * users to reuse rules already designed for use with file(1).
  49.  *
  50.  * The fields on each line are:
  51.  *
  52.  * offset: a byte offset in the file at which data should be extracted
  53.  *   and compared to a matching string or value.  If this value
  54.  *   begins with '>', then an additional rule is used and scanning
  55.  *   continues to the next type rule line that does not begin with
  56.  *   a '>'. Only a single level of '>' is permitted.
  57.  * datatype: the type of data value to extract the specified offset in the
  58.  *   for comparison purposes.  This can be byte, short, long, ascii or
  59.  *   string (a not necessarily null-terminated string of bytes).
  60.  *   A byte is 8 bits, short 16 bits, and long 32 bits.
  61.  * match:  the value and operation to use in matching; the value used is
  62.  *   based on the datatype field.  This value may be "x" to mean
  63.  *   "match anything".  The operation is "=" if nothing is specified;
  64.  *   otherwise it can be one of ">", "<", "<=", ">=", "!=", "&"
  65.  *   (for and-ing and comparing to zero), "^" (for xor-ing and
  66.  *   comparing to zero), and "!" (for taking one's complement and
  67.  *   comparing to zero).
  68.  *   For string and ascii data, no comparison operator is allowed,
  69.  *   and embedded spaces, but not embedded tabs, are allowed.
  70.  *   These data are taken literally; no C-style character escapes
  71.  *         are meaningful.
  72.  * result: one of "PS", "TIFF", "PDF", "PCL", or "error" (case insensitive).
  73.  *   The first two results specifiy whether the rule generates a PostScript
  74.  *   file or a bilevel TIFF image.  The "error" result indicates a
  75.  *   file is unsuitable for transmission and if supplied as an
  76.  *   argument to sendfax, the command should be aborted.
  77.  * rule:   a string passed to the shell to convert the input file
  78.  *   to the result format (suitable for sending as facsimile).
  79.  *   The rule string is a printf-like string that should use the
  80.  *   following "%" escapes:
  81.  * %i input file name
  82.  * %o output file name
  83.  * %r output horizontal resolution in pixels/mm
  84.  * %R output horizontal resolution in pixels/inch
  85.  * %v output vertical resolution in lines/mm
  86.  * %V output vertical resolution in lines/inch
  87.  * %f data format, 1 for 1-d encoding or 2 for 2-d encoding
  88.  * %w page width in pixels
  89.  * %W page width in mm
  90.  * %l page length in pixels
  91.  * %L page length in mm
  92.  * %s page size by name
  93.  * %F the pathname of the fax library (e.g./usr/local/lib/fax)
  94.  * %<x> the <x> character (e.g. ``%%'' results in ``%''
  95.  */
  96. class TypeRule;
  97. class TypeRuleArray;
  98. class TypeRules {
  99. public:
  100.     TypeRules();
  101.     ~TypeRules();
  102.     static TypeRules* read(const fxStr& file); // read rule database
  103.     void setVerbose(bool);
  104.     const TypeRule* match(const void* data, u_int size) const;
  105. private:
  106.     TypeRuleArray* rules;
  107.     bool verbose; // while matching
  108.     u_int match2(u_int base, const void* data, u_int size, bool verb) const;
  109. };
  110. typedef u_int TypeResult; // conversion result
  111. /*
  112.  * Type rules specify how to convert a file that is
  113.  * submitted for transmission into a format suitable
  114.  * for the fax server.  File types are based on an
  115.  * analysis of the file's ``magic number''.  Type
  116.  * conversions are specified by a parameterized string
  117.  * of commands to pass to a shell.
  118.  */
  119. class TypeRule : public fxObj {
  120. public:
  121.     enum {
  122. TIFF, // bilevel Group 3-encoded TIFF
  123. POSTSCRIPT, // PostScript
  124. PDF, // Portable Document Format
  125. PCL, // Printer Control Language
  126. ERROR // recognized erronious format
  127.     };
  128. private:
  129.     off_t off; // byte offset in file
  130.     bool cont; // continuation
  131.     enum {
  132. ASCII, // ascii-only string
  133. ASCIIESC, // ascii-only string + escape char (iso-2022 variants)
  134. STRING, // byte string
  135. ISTRING, // case-insensitive string
  136. ADDR, // address of match
  137. BYTE, // 8 bits
  138. SHORT, // 16 bits
  139. LONG // 32 bits
  140.     } type; // data value type
  141.     enum {
  142. ANY, // match anything
  143. EQ, // == value
  144. NE, // != value
  145. LT, // < value
  146. LE, // <= value
  147. GT, // > value
  148. GE, // >= value
  149. AND, // (&value) != 0
  150. XOR, // (^value) != 0
  151. NOT // (!value) != 0
  152.     } op; // match operation
  153.     union {
  154. long  v;
  155. char* s;
  156.     } value; // matching value
  157.     TypeResult result; // result of applying rule
  158.     fxStr cmd; // shell command/error message
  159.     friend TypeRules* TypeRules::read(const fxStr& file);
  160. public:
  161.     TypeRule();
  162.     TypeRule(const TypeRule& other);
  163.     virtual ~TypeRule();
  164.     bool match(const void*, size_t size, bool verbose = false) const;
  165.     bool isContinuation() const;
  166.     TypeResult getResult() const;
  167.     const fxStr& getCmd() const;
  168.     fxStr getErrMsg() const;
  169.     fxStr getFmtdCmd(const fxStr& input, const fxStr& output,
  170.     float hr, float vr,
  171.     const fxStr& df,
  172.     const fxStr& pname) const;
  173. };
  174. inline bool TypeRule::isContinuation() const { return cont; }
  175. inline TypeResult TypeRule::getResult() const { return result; }
  176. inline const fxStr& TypeRule::getCmd() const { return cmd; }
  177. inline fxStr TypeRule::getErrMsg() const { return cmd; }
  178. #endif /* _TYPERULES_ */