summaryrefslogtreecommitdiff
path: root/src/vm/decodemd.h
blob: c76bdddb53bbb3e7c45a4cbe4f48e0b1858c42b5 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//



#ifndef __DECODEMD_H__
#define __DECODEMD_H__

// --------------------------------------------------------
// This is used to decode a bitstream encoding

class Decoder
{
public:
    Decoder();
    Decoder(PTR_BYTE bytes);
    void Init(PTR_BYTE bytes);
    unsigned Next();
    signed NextSigned();
    PTR_BYTE End();
    
    // --------------------------------------------------------
    // This structures contains the state of the FSM

    struct Decode
    {
        const BYTE* decoded;    //the already decoded values
        unsigned  next;   //what to do when no more decoded values
    };

private:
    // --------------------------------------------------------
    // This is used to access nibbles from a byte stream.

    class Nibbles
    {
        friend class Decoder;
    public:
        void SetContents(PTR_BYTE bytes);
        BYTE Next();
        BYTE Read();
        unsigned Bits(unsigned number);
    private:
        PTR_BYTE data;
        BYTE nibbles[2];
        unsigned next;
    };

    Decode state;
    Nibbles data;
};

// --------------------------------------------------------
// This is used to encode a bitstream encoding
#ifndef BINDER
class Encoder
{
public:
    Encoder(BYTE *buffer);
    void ContainsNegatives(BOOL b);
    void EncodeSigned(signed value);
    void Encode(unsigned value);
    void Encode(signed value, BOOL isSigned);
    void Add(unsigned value, unsigned length);
    void Add64(unsigned __int64 value, unsigned length);
    void Done();
    unsigned Contents(BYTE** contents);
    unsigned Length();
private:
    BYTE* buffer;
    BYTE encoding;
    unsigned unusedBits;
    BOOL done;
    BOOL signedNumbers;
    unsigned index;
};
#endif // !BINDER
#endif // __DECODEMD_H__