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 / ZBSEQ.C < prev    next >
C/C++ Source or Header  |  1992-07-20  |  8KB  |  291 lines

  1. /* Copyright (C) 1990, 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. /* zbseq.c */
  21. /* Level 2 binary object sequence operators */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "save.h"
  26. #include "store.h"
  27. #include "stream.h"
  28. #include "files.h"
  29. #include "name.h"
  30. #include "bnum.h"
  31. #include "btoken.h"
  32. #include "bseq.h"
  33.  
  34. /* Current binary format (in iscan.c) */
  35. extern ref binary_object_format;
  36.  
  37. /* System and user name arrays. */
  38. ref system_names, user_names;
  39.  
  40. /* Import the binary token scanner. */
  41. extern int scan_binary_token(P3(stream *, ref *, int));
  42. extern int (*scan_btoken_proc)(P3(stream *, ref *, int));
  43.  
  44. /* Forward references */
  45. private int write_bin_object(P2(stream *, os_ptr));
  46.  
  47. /* Initialize the binary object machinery. */
  48. private void
  49. zbseq_init()
  50. {    /* Initialize fake system and user name tables. */
  51.     /* PostScript code will install the real ones. */
  52.     make_tasv(&system_names, t_shortarray, a_readonly, 0, packed, NULL);
  53.     make_tasv(&user_names, t_array, a_all, 0, refs, NULL);
  54.     scan_btoken_proc = scan_binary_token;
  55. }
  56.  
  57. /* .installnames */
  58. int
  59. zinstallnames(register os_ptr op)
  60. {    check_read_type(op[-1], t_shortarray);
  61.     check_type(*op, t_array);
  62.     ref_assign_old(&system_names, op - 1, ".installnames");
  63.     ref_assign_old(&user_names, op, ".installnames");
  64.     pop(2);
  65.     return 0;
  66. }
  67.  
  68. /* currentobjectformat */
  69. int
  70. zcurrentobjectformat(register os_ptr op)
  71. {    push(1);
  72.     *op = binary_object_format;
  73.     return 0;
  74. }
  75.  
  76. /* printobject */
  77. int
  78. zprintobject(register os_ptr op)
  79. {    int code = write_bin_object(&std_files[1], op);
  80.     if ( code >= 0 )
  81.        {    pop(2);
  82.        }
  83.     return code;
  84. }
  85.  
  86. /* setobjectformat */
  87. int
  88. zsetobjectformat(register os_ptr op)
  89. {    check_type(*op, t_integer);
  90.     if ( op->value.intval < 0 || op->value.intval > 4 )
  91.         return e_rangecheck;
  92.     ref_save(&binary_object_format, "setobjectformat");
  93.     binary_object_format.value.intval = op->value.intval;
  94.     pop(1);
  95.     return 0;
  96. }
  97.  
  98. /* writeobject */
  99. int
  100. zwriteobject(register os_ptr op)
  101. {    stream *s;
  102.     int code;
  103.     check_write_file(s, op - 2);
  104.     code = write_bin_object(s, op);
  105.     if ( code >= 0 )
  106.        {    pop(3);
  107.        }
  108.     return code;
  109. }
  110.  
  111. /* ------ Initialization procedure ------ */
  112.  
  113. op_def zbseq_op_defs[] = {
  114.     {"2.installnames", zinstallnames},
  115.     {"0currentobjectformat", zcurrentobjectformat},
  116.     {"2printobject", zprintobject},
  117.     {"1setobjectformat", zsetobjectformat},
  118.     {"3writeobject", zwriteobject},
  119.     op_def_end(zbseq_init)
  120. };
  121.  
  122. /* ------ Internal routines ------ */
  123.  
  124. typedef struct { ulong refs, chars; } bin_space;
  125.  
  126. /* Compute the size of a binary object sequence */
  127. private int
  128. bin_seq_space(const ref *op, int array_ok, bin_space *sp)
  129. {    switch ( r_type(op) )
  130.       {
  131.       case t_null: case t_integer: case t_real:
  132.       case t_boolean: case t_mark:
  133.         sp->refs++;  break;
  134.       case t_string:
  135.         sp->refs++;  sp->chars += r_size(op);  break;
  136.       case t_name:
  137.         sp->refs++;
  138.         { ref nstr;
  139.           name_string_ref(op, &nstr);
  140.           sp->chars += r_size(&nstr);
  141.         }
  142.         break;
  143.       case t_array:
  144.         if ( !array_ok ) return e_limitcheck;
  145.         { uint i;
  146.           for ( i = 0; i < r_size(op); i++ )
  147.         { int code = bin_seq_space(op->value.refs + i, 0, sp);
  148.           if ( code < 0 ) return code;
  149.         }
  150.         }
  151.         break;
  152.       default:
  153.         return e_typecheck;
  154.       }
  155.     return 0;
  156. }
  157.  
  158. /* Write the objects part of a binary object sequence. */
  159. /* Return the new offset in the string part. */
  160. private uint
  161. bin_seq_write_objects(stream *s, const ref *op, byte tag, uint spos)
  162. {    bin_seq_obj ob;
  163.     ref nstr;
  164.     ob.unused = tag;
  165. #define swap_t(a, b) t = a, a = b, b = t
  166. #if arch_is_big_endian
  167. #  define must_swap(s) s_is_lsb(s)
  168. #else
  169. #  define must_swap(s) s_is_msb(s)
  170. #endif
  171.     switch ( r_type(op) )
  172.       {
  173.       case t_null: ob.tx = (byte)bs_null; break;
  174.       case t_mark: ob.tx = (byte)bs_mark; break;
  175.       case t_integer:
  176.         ob.tx = (byte)bs_integer;
  177.         ob.value.w = op->value.intval;
  178. num:        ob.size.w = 0;        /* (matters for reals) */
  179. swb:                /* swap bytes of value if needed */
  180.         if ( must_swap(s) )
  181.          { byte t;
  182.            swap_t(ob.value.b[0], ob.value.b[3]);
  183.            swap_t(ob.value.b[1], ob.value.b[2]);
  184.          }
  185.         break;
  186.       case t_real:
  187.         ob.tx = (byte)bs_real;
  188.         ob.value.f = op->value.realval;
  189.         /***** handle non-IEEE native *****/
  190.         goto num;
  191.       case t_boolean:
  192.         ob.tx = (byte)bs_boolean;
  193.         ob.value.w = op->value.index;
  194.         goto num;
  195.       case t_array:
  196.         { uint i;
  197.           for ( i = 0; i < r_size(op); i++ )
  198.         spos = bin_seq_write_objects(s, op->value.refs + i, 0, spos);
  199.         }
  200.         return spos;
  201.       case t_string:
  202.         ob.tx = (byte)bs_string;
  203.         if ( r_has_attr(op, a_executable) )
  204.           ob.tx += (byte)bs_executable;
  205. nos:        ob.size.w = r_size(op);
  206.         if ( must_swap(s) )
  207.          { byte t;
  208.            swap_t(ob.size.b[0], ob.size.b[1]);
  209.          }
  210.         ob.value.w = spos;
  211.         spos += r_size(op);
  212.         goto swb;
  213.       case t_name:
  214.         ob.tx = (byte)bs_name;
  215.         name_string_ref(op, &nstr);
  216.         op = &nstr;
  217.         goto nos;
  218.       }
  219.     sputs(s, (byte *)&ob, sizeof(bin_seq_obj));
  220.     return spos;
  221. }
  222.  
  223. /* Write the string part of a binary object sequence */
  224. private void
  225. bin_seq_write_strings(stream *s, const ref *op)
  226. {    switch ( r_type(op) )
  227.       {
  228.       case t_array:
  229.         { uint i;
  230.           for ( i = 0; i < r_size(op); i++ )
  231.         bin_seq_write_strings(s, op->value.refs + i);
  232.         }
  233.         break;
  234.       case t_name:
  235.         { ref nstr;
  236.           name_string_ref(op, &nstr);
  237.           sputs(s, nstr.value.bytes, r_size(&nstr));
  238.         }
  239.         break;
  240.       case t_string:
  241.         sputs(s, op->value.bytes, r_size(op));
  242.         break;
  243.       }
  244. }
  245.  
  246. /* Top-level routine for writing an object (printobject/writeobject) */
  247. private int
  248. write_bin_object(register stream *s, os_ptr op)
  249. {    int bin_format = (int)binary_object_format.value.intval - 1;
  250.     bin_space space;
  251.     int code;
  252.     ulong total;
  253.     byte tag;
  254.     os_ptr op1 = op - 1;
  255.     static int nfs[4] =
  256.        {    num_float_IEEE + num_msb,
  257.         num_float_IEEE + num_lsb,
  258.         num_float_native + num_msb,
  259.         num_float_native + num_lsb
  260.        };
  261.     if ( bin_format < 0 ) return e_undefined;
  262.     check_type(*op, t_integer);
  263.     if ( op->value.intval < 0 || op->value.intval > 255 )
  264.         return e_rangecheck;
  265.     tag = (byte)op->value.intval;
  266.     space.refs = space.chars = 0;
  267.     code = bin_seq_space(op1, 1, &space);
  268.     if ( code < 0 ) return code;
  269.     /* Object has been validated, only possible error now is */
  270.     /* ioerror (which we don't check for). */
  271.     total = space.refs * sizeof(bin_seq_obj) + space.chars;
  272.     if ( r_has_type(op1, t_array) )
  273.         total += sizeof(bin_seq_obj);
  274.     if ( total > 0xffff - 4 ) return e_limitcheck;
  275.     s->num_format = nfs[bin_format];
  276.     sputc(s, (byte)bt_seq + bin_format);
  277.     sputc(s, 1);
  278.     sputshort(s, (ushort)total + 4);
  279.     if ( r_has_type(op1, t_array) )
  280.       { sputc(s, (byte)bs_array +
  281.           (r_has_attr(op1, a_executable) ? (byte)bs_executable : 0));
  282.         sputc(s, tag);
  283.         sputshort(s, r_size(op1));
  284.         sputlong(s, sizeof(bin_seq_obj));
  285.         tag = 0;
  286.       }
  287.     bin_seq_write_objects(s, op1, tag, (uint)(total - space.chars));
  288.     bin_seq_write_strings(s, op1);
  289.     return 0;
  290. }
  291.