summaryrefslogtreecommitdiff
path: root/H64E-2/h64e-model.c
blob: 3bd0d1fe7b1d980f3156cd20f0c1d17264e22420 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
//  h64e-model.c
//  H64E-2
//
//  Created by dianshi on 3/13/20.
//  Copyright © 2020 dianshi. All rights reserved.
//

#include "h64e-model.h"

int h64e_si_init(H64E_stream_in *in, int32_t size)
{
    in->size = size;
    in->cur_size = 0;
    in->buf = malloc(size);
    return 0;
}

//return amount of copied bytes
int h64e_si_data_in(H64E_stream_in *in, uint8_t *data, int32_t size)
{
    int32_t start = in->cur_size;
    int32_t end = start+size;
    int32_t len = 0;
    
    if (end > in->size)
    {
        end = in->size;
    }
    
    len = end-start;
    memcpy(&in->buf[start], data, len);
    in->cur_size += len;
    return (int)len;
}

int h64e_si_data_out(H64E_stream_in *in, uint8_t *data, int32_t size)
{
    int32_t len=0;
    int32_t i=0;
    
    if (size < in->cur_size)
    {
        len = size;
    } else {
        len = in->cur_size;
    }
    
    //memcpy(data,&in->buf[0],len);
    for (i=0;i<len;i++)
    {
        data[i] = in->buf[i];
    }
    
    memmove(&in->buf[0],&in->buf[len],len);
    in->cur_size -= len;
    
    return len;
}

int h64e_si_len(H64E_stream_in *in)
{
    return in->cur_size;
}

int h64e_so_init(H64E_stream_out *out, int32_t size)
{
    out->size = size;
    out->cur_size = 0;
    out->buf = malloc(size);
    return 0;
}

int h64e_so_data_in(H64E_stream_out *out, uint8_t *data, int32_t size)
{
    int32_t start = out->cur_size;
    int32_t end = start+size;
    int32_t len = 0;
    
    if (end > out->size)
    {
        end = out->size;
    }
    
    len = end-start;
    memcpy(&out->buf[start], data, len);
    out->cur_size += len;
    return (int)len;
}

int h64e_so_data_out(H64E_stream_out *out, uint8_t *data, int32_t size)
{
    int32_t len=0;
    int32_t i=0;
    
    if (size < out->cur_size)
    {
        len = size;
    } else {
        len = out->cur_size;
    }
    
    //memcpy(data,&in->buf[0],len);
    for (i=0;i<len;i++)
    {
        data[i] = out->buf[i];
    }
    
    memmove(&out->buf[0],&out->buf[len],len);
    out->cur_size -= len;
    
    return len;
}

int h64e_so_ready(H64E_stream_out *out)
{
    int ret = 0;
    
    return ret;
}

int h64e_so_len(H64E_stream_out *out)
{
    return out->cur_size;
}