xref: /plan9/sys/src/cmd/gs/src/shc.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1992, 1995 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: shc.c,v 1.4 2002/02/21 22:24:54 giles Exp $ */
18 /* Support code for shc.h */
19 #include "std.h"
20 #include "scommon.h"
21 #include "shc.h"
22 
23 /* ------ Encoding ------ */
24 
25 /* Empty the 1-word buffer onto the output stream. */
26 /* q has already been incremented. */
27 void
hc_put_code_proc(bool reverse_bits,byte * q,uint cw)28 hc_put_code_proc(bool reverse_bits, byte * q, uint cw)
29 {
30 #define cb(n) ((byte)(cw >> (n * 8)))
31     if (reverse_bits) {
32 #if hc_bits_size > 16
33 	q[-3] = byte_reverse_bits[cb(3)];
34 	q[-2] = byte_reverse_bits[cb(2)];
35 #endif
36 	q[-1] = byte_reverse_bits[cb(1)];
37 	q[0] = byte_reverse_bits[cb(0)];
38     } else {
39 #if hc_bits_size > 16
40 	q[-3] = cb(3);
41 	q[-2] = cb(2);
42 #endif
43 	q[-1] = cb(1);
44 	q[0] = cb(0);
45     }
46 #undef cb
47 }
48 
49 /* Put out any final bytes. */
50 /* Note that this does a store_state, but not a load_state. */
51 byte *
hc_put_last_bits_proc(stream_hc_state * ss,byte * q,uint bits,int bits_left)52 hc_put_last_bits_proc(stream_hc_state * ss, byte * q, uint bits, int bits_left)
53 {
54     while (bits_left < hc_bits_size) {
55 	byte c = (byte) (bits >> (hc_bits_size - 8));
56 
57 	if (ss->FirstBitLowOrder)
58 	    c = byte_reverse_bits[c];
59 	*++q = c;
60 	bits <<= 8;
61 	bits_left += 8;
62     }
63     ss->bits = bits;
64     ss->bits_left = bits_left;
65     return q;
66 }
67