summaryrefslogtreecommitdiff
path: root/base/shc.c
blob: c0319c27107dc00611900b91f211eaa62528f579 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* Support code for shc.h */
#include "std.h"
#include "scommon.h"
#include "shc.h"

/* ------ Encoding ------ */

/* Empty the 1-word buffer onto the output stream. */
/* q has already been incremented. */
void
hc_put_code_proc(bool reverse_bits, byte * q, uint cw)
{
#define cb(n) ((byte)(cw >> (n * 8)))
    if (reverse_bits) {
#if hc_bits_size > 16
        q[-3] = byte_reverse_bits[cb(3)];
        q[-2] = byte_reverse_bits[cb(2)];
#endif
        q[-1] = byte_reverse_bits[cb(1)];
        q[0] = byte_reverse_bits[cb(0)];
    } else {
#if hc_bits_size > 16
        q[-3] = cb(3);
        q[-2] = cb(2);
#endif
        q[-1] = cb(1);
        q[0] = cb(0);
    }
#undef cb
}

/* Put out any final bytes. */
/* Note that this does a store_state, but not a load_state. */
byte *
hc_put_last_bits_proc(stream_hc_state * ss, byte * q, uint bits, int bits_left)
{
    while (bits_left < hc_bits_size) {
        byte c = (byte) (bits >> (hc_bits_size - 8));

        if (ss->FirstBitLowOrder)
            c = byte_reverse_bits[c];
        *++q = c;
        bits <<= 8;
        bits_left += 8;
    }
    ss->bits = bits;
    ss->bits_left = bits_left;
    return q;
}