summaryrefslogtreecommitdiff
path: root/H64E-2/h64e.c
blob: 1e0662cfd8ea938def01439527bc725b0caffb03 (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
126
127
128
129
130
131
132
133
134
//
//  h64e.c
//  H64E-2
//
//  Created by dianshi on 3/13/20.
//  Copyright © 2020 dianshi. All rights reserved.
//

#include "h64e.h"

int h64e_init(H64E_t *s)
{
    printf("init structure\n");
    s->fmt.column_size = 16;
    s->fmt.offset_addr = 0;
    s->fmt.flag_offset = 0;
    s->fmt.flag_no_group = 0;
    s->fmt.group = H64E_G_BYTE;
    s->fmt.flag_output = 0;
    s->fmt.output_type = H64E_O_NONE;
    
    s->sin = NULL;
    s->sout = NULL;
    return 0;
}

int h64e_check_param( H64E_t *fmt )
{
    printf("Check parametrs set\n");
    return 0;
}

int h64e_set_input(H64E_t *s, H64E_stream_in *sin)
{
    return 0;
}

int h64e_set_output(H64E_t *s, H64E_stream_out *sout)
{
    return 0;
}

int h64e_convert(H64E_t *s)
{
    return 0;
}

int h64e_destroy(H64E_t *s)
{
    return 0;
}

int h64e_fmt_init( H64E_format *fs)
{
    memset(fs,0,sizeof(H64E_format));
    
    return 0;
}

/*
 * out_size - allways give enought data to buffer otherwise it will partially write data, or flush data out more often
 */
int h64e_fmt_byte(H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size)
{
    //printf("h64e_fmt_byte in %d out %d\n",in_size,out_size);
    int ret=0;
    int cur_size=0;
    int i,j=0;
    const int SZ=16;
    uint8_t buf[SZ];
    int buf_sz=0;
    int trail_size=0;
    
    for (i=0; i<in_size; i++)
    {
        buf_sz = snprintf(&buf[0], SZ, "%02x",in_data[i]);
        //count converted chars per line
        fs->cur_pos += 1;
        //add space if configured
        if (fs->f_space)
        {
            if (buf_sz+1<SZ)
            {
                buf[buf_sz] = ' ';
                buf_sz += 1;
            }
        }

        if (fs->cur_pos == fs->column_size)
        {
            if (fs->f_new_line) {
                //set triger to newline
                fs->t_new_line = 1;
                fs->cur_pos = 0;
                //set new line to buffer
                if (buf_sz+1<SZ)
                {
                    buf[buf_sz] = '\n';
                    buf_sz += 1;
                }
            }
        }
        //printf("D: %s",buf);
        if (trail_size+buf_sz<out_size)
        {
            for(j=0;j<buf_sz;j++)
            {
                out_data[trail_size+j] = buf[j];
            }
            ret = buf_sz;
            
        } else {
           //not enought output to handle data
            ret=-1;
        }
    }
#if 0
    printf("D:[");
    for (i=0;i<ret;i++)
    {
        printf("%c",(unsigned char)out_data[i]);
    }
    printf("]\n");
#endif

    return ret;
}

int h64e_fmt_finish(H64E_format *fs)
{
    
    
    return 0;
}