home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / ZFILTER.C < prev    next >
C/C++ Source or Header  |  1992-08-10  |  6KB  |  225 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zfilter.c */
  21. /* Filter creation for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "alloc.h"
  26. #include "stream.h"
  27.  
  28. /* Forward references */
  29. int filter_read(P4(os_ptr, const stream_procs _ds *, stream **, uint));
  30. int filter_write(P4(os_ptr, const stream_procs _ds *, stream **, uint));
  31.  
  32. /* Imported from zfile.c */
  33. extern const uint file_default_buffer_size;
  34.  
  35. /* .filterASCIIHexEncode */
  36. extern const stream_procs s_AXE_procs;
  37. int
  38. zAXE(os_ptr op)
  39. {    return filter_write(op, &s_AXE_procs, NULL, 0);
  40. }
  41.  
  42. /* .filterASCIIHexDecode */
  43. extern const stream_procs s_AXD_procs;
  44. extern void s_AXD_init(P1(stream *));
  45. int
  46. zAXD(os_ptr op)
  47. {    stream *s;
  48.     int code = filter_read(op, &s_AXD_procs, &s, 0);
  49.     if ( code < 0 ) return code;
  50.     s_AXD_init(s);
  51.     return code;
  52. }
  53.  
  54. /* .filtereexecDecode */
  55. extern const stream_procs s_exD_procs;
  56. extern void s_exD_init(P2(stream *, ushort));
  57. int
  58. zexD(register os_ptr op)
  59. {    stream *s;
  60.     ushort state;
  61.     int code;
  62.     check_type(*op, t_integer);
  63.     state = op->value.intval;
  64.     if ( op->value.intval != state )
  65.         return e_rangecheck;    /* state value was truncated */
  66.     /* The buffer size of 512 is part of the specification of eexec. */
  67.     code = filter_read(op - 1, &s_exD_procs, &s, 512);
  68.     if ( code < 0 ) return code;
  69.     s_exD_init(s, state);
  70.     pop(1);
  71.     return 0;
  72. }
  73.  
  74. /* .filterNullEncode */
  75. extern const stream_procs s_NullE_procs;
  76. int
  77. zNullE(os_ptr op)
  78. {    stream *s;
  79.     int code = filter_write(op, &s_NullE_procs, &s, 0);
  80.     if ( code >= 0 )
  81.     {    /* NullEncode filters can report their position */
  82.         s->modes |= s_mode_seek;
  83.     }
  84.     return code;
  85. }
  86.  
  87. /* .filterPFBDecode */
  88. extern const stream_procs s_PFBD_procs;
  89. extern void s_PFBD_init(P2(stream *, int));
  90. int
  91. zPFBD(os_ptr op)
  92. {    stream *s;
  93.     int code;
  94.     check_type(*op, t_boolean);
  95.     code = filter_read(op - 1, &s_PFBD_procs, &s, 0);
  96.     if ( code < 0 ) return code;
  97.     s_PFBD_init(s, op->value.index);
  98.     pop(1);
  99.     return 0;
  100. }
  101.  
  102. /* .filterSubFileDecode */
  103. extern const stream_procs s_SFD_procs;
  104. extern void s_SFD_init(P4(stream *, long, const byte *, uint));
  105. int
  106. zSFD(os_ptr op)
  107. {    stream *s;
  108.     int code;
  109.     check_type(op[-1], t_integer);
  110.     check_read_type(*op, t_string);
  111.     if ( op[-1].value.intval < 0 ) return e_rangecheck;
  112.     code = filter_read(op - 2, &s_SFD_procs, &s, r_size(op) * 2);
  113.     if ( code < 0 ) return code;
  114.     s_SFD_init(s, op[-1].value.intval, op->value.const_bytes, (uint)r_size(op));
  115.     pop(2);
  116.     return 0;
  117. }
  118.  
  119. /* ------ Utilities ------ */
  120.  
  121. /* Free the underlying string stream if needed. */
  122. private void
  123. filter_free_null(stream *s)
  124. {
  125. }
  126. private void
  127. filter_free_stream(stream *s)
  128. {    alloc_free((char *)s->strm, 1, sizeof(stream),
  129.            "filter_free_stream(stream)");
  130. }
  131.  
  132. /* Set up an input filter. */
  133. int
  134. filter_read(os_ptr op, const stream_procs _ds *procs, stream **ps,
  135.   uint min_size)
  136. {    stream *s;
  137.     stream *sstrm;
  138.     int is_temp;
  139.     int code;
  140.     /* Check to make sure that the underlying data */
  141.     /* can function as a source for reading. */
  142.     switch ( r_type(op) )
  143.        {
  144.     case t_string:
  145.         check_access(*op, a_read);
  146.         sstrm = (stream *)alloc(1, sizeof(stream),
  147.                     "filter_read(string stream)");
  148.         if ( sstrm == 0 ) return e_VMerror;
  149.         sread_string(sstrm, op->value.bytes, r_size(op));
  150.         is_temp = 1;
  151.         break;
  152.     case t_file:
  153.         check_access(*op, a_read);
  154.         sstrm = op->value.pfile;
  155.         is_temp = 0;
  156.         break;
  157.     default:
  158.         return e_typecheck;
  159.        }
  160.     code = file_open((byte *)0, 0, "r",
  161.              max(min_size, file_default_buffer_size),
  162.              (ref *)op, &s);
  163.     if ( code < 0 ) return code;
  164.     s_std_init(s, s->cbuf, s->bsize, procs, s_mode_read);
  165.     s->end_status = 0;
  166.     s->file = 0;            /* not a file stream */
  167.     s->strm = sstrm;
  168.     s->strm_is_temp = is_temp;
  169.     if ( ps != NULL ) *ps = s;
  170.     return 0;
  171. }
  172.  
  173. /* Set up an output filter. */
  174. int
  175. filter_write(os_ptr op, const stream_procs _ds *procs, stream **ps,
  176.   uint min_size)
  177. {    stream *s;
  178.     stream *sstrm;
  179.     int is_temp;
  180.     int code;
  181.     /* Check to make sure that the underlying data */
  182.     /* can function as a sink for writing. */
  183.     switch ( r_type(op) )
  184.        {
  185.     case t_string:
  186.         check_access(*op, a_write);
  187.         sstrm = (stream *)alloc(1, sizeof(stream),
  188.                     "filter_write(string)");
  189.         if ( sstrm == 0 ) return e_VMerror;
  190.         swrite_string(sstrm, op->value.bytes, r_size(op));
  191.         is_temp = 1;
  192.         break;
  193.     case t_file:
  194.         check_access(*op, a_write);
  195.         sstrm = op->value.pfile;
  196.         is_temp = 0;
  197.         break;
  198.     default:
  199.         return e_typecheck;
  200.        }
  201.     code = file_open((byte *)0, 0, "w",
  202.              max(min_size, file_default_buffer_size),
  203.              (ref *)op, &s);
  204.     if ( code < 0 ) return code;
  205.     s_std_init(s, s->cbuf, s->bsize, procs, s_mode_write);
  206.     s->end_status = 0;
  207.     s->file = 0;            /* not a file stream */
  208.     s->strm = sstrm;
  209.     s->strm_is_temp = is_temp;
  210.     if ( ps != NULL ) *ps = s;
  211.     return 0;
  212. }
  213.  
  214. /* ------ Initialization procedure ------ */
  215.  
  216. op_def zfilter_op_defs[] = {
  217.     {"1.filterASCIIHexEncode", zAXE},
  218.     {"1.filterASCIIHexDecode", zAXD},
  219.     {"2.filtereexecDecode", zexD},
  220.     {"1.filterNullEncode", zNullE},
  221.     {"2.filterPFBDecode", zPFBD},
  222.     {"1.filterSubFileDecode", zSFD},
  223.     op_def_end(0)
  224. };
  225.