summaryrefslogtreecommitdiff
path: root/H64E-2/main.c
blob: 07e96d8d18cd86685458f2bacf6cd926b89e4ba3 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//
//  main.c
//  H64E-2
//
//  Created by dianshi on 3/13/20.
//  Copyright © 2020 dianshi. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <getopt.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include "h64e.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int file_open( const char *fname )
{
    int fd;

    fd = open( fname, O_RDONLY );
    if ( fd == -1 )
    {
        printf("Couldn open file %s\n", fname);
        return -1;
    }

    return fd;
}

int file_read( int fd, char *buf, int size )
{
    int ret;

    if ( size < 1 )
        return -1;

    ret = (int)read( fd, buf, size );

    return ret;
}

int file_seek( int fd, off_t pos )
{
    off_t rc;

    if (fd<1)
    {
        return -1;
    }

    rc = lseek( fd, pos, SEEK_SET );
    if (rc == -1)
        return -1;
    return 0;

}

int file_close( int fd )
{

    close( fd );

    return 0;
}

int group_name2int( char *name )
{
    if ( 0 == strncmp( name, "byte", 4 ) )
    {
        return H64E_G_BYTE;
    }
    if ( 0 == strncmp( name, "word", 4 ) )
    {
        return H64E_G_WORD;
    }
    if ( 0 == strncmp( name, "dword", 5 ) )
    {
        return H64E_G_DWORD;
    }
    if ( 0 == strncmp( name, "qword", 5 ) )
    {
        return H64E_G_QWORD;
    }

    return 1;
}

int output_name2int( char *name )
{
    if ( 0 == strncmp( name, "str", 3) )
    {
        return H64E_O_STRING;
    }
    if ( 0 == strncmp( name, "i8", 2) )
    {
        return H64E_O_INT8;
    }
    if ( 0 == strncmp( name, "u8", 2) )
    {
        return H64E_O_UINT8;
    }
    if ( 0 == strncmp( name, "i16", 3) )
    {
        return H64E_O_INT16;
    }
    if ( 0 == strncmp( name, "u16", 3) )
    {
        return H64E_O_UINT16;
    }
    if ( 0 == strncmp( name, "i32", 3) )
    {
        return H64E_O_INT32;
    }
    if ( 0 == strncmp( name, "u32", 3) )
    {
        return H64E_O_UINT32;
    }
    if ( 0 == strncmp( name, "i64", 3) )
    {
        return H64E_O_INT64;
    }
    if ( 0 == strncmp( name, "u64", 3) )
    {
        return H64E_O_UINT64;
    }
    return 0;
}

int group_typename2int( char *name )
{
    if ( 0 == strncmp( name, "i8", 2) )
    {
        return H64E_G_BYTE;
    }
    if ( 0 == strncmp( name, "u8", 2) )
    {
        return H64E_G_BYTE;
    }
    if ( 0 == strncmp( name, "i16", 3) )
    {
        return H64E_G_WORD;
    }
    if ( 0 == strncmp( name, "u16", 3) )
    {
        return H64E_G_WORD;
    }
    if ( 0 == strncmp( name, "i32", 3) )
    {
        return H64E_G_DWORD;
    }
    if ( 0 == strncmp( name, "u32", 3) )
    {
        return H64E_G_DWORD;
    }
    if ( 0 == strncmp( name, "i64", 3) )
    {
        return H64E_G_QWORD;
    }
    if ( 0 == strncmp( name, "u64", 3) )
    {
        return H64E_G_QWORD;
    }
    return 0;
}

void helper( char *progname )
{
    printf("Usage: %s [OPTS] <filename>\n\n"
        " -a - ascii output\n"
        " -b - show offset adress\n"
        " -o - file offset\n"
        " -i - output hex\n"
        " -l - length of output\n"
        " -c - column size in bytes\n"
        " -e - interp data output: u8,i8,u16,i16,u32,i32,u64,i64\n"
        " -s - disable space between columns\n"
        " -t - show offset numbers\n"
        " -h - extra hex output\n"
        "Version: 2.1.0 \n"
        "\n"
        , progname);
}

void version()
{
    printf("???\n");
}

/*
 Main fucntion, of this program
 */
int main(int argc, const char * argv[]) {
    // insert code here...
    int c;
    int param_length = -1;
    int param_offset = -1;
    char *fname = NULL;
    int fd;
    int in_bytes = 0, out_bytes=0, cnv_bytes=0;
    int in_total = 0, out_total=0, cnv_total=0;
    uint8_t buf_in[128+1],buf_cnv_in[128+1],buf_cnv_out[2048+1],buf_out[2048+1];
    int i=0;
    
    printf("H64E-2 Project started 1\n");
    
    //initialise structure
    H64E_t h64e;
    H64E_stream_in sin;
    H64E_stream_out sout;
    
    memset(&h64e,0,sizeof(H64E_t));
    h64e_init(&h64e);
    h64e_si_init(&sin, 128);
    h64e_so_init(&sout, 4096);
    h64e_set_input(&h64e, &sin);
    h64e_set_output(&h64e, &sout);
    
    H64E_format int8_fmt;
    
    //list of supported formats
    
    h64e_fmt_init(&int8_fmt);
    
    
    //set all params from arguments
    /*get cmd args and set options */
    while ( (c = getopt(argc, argv, "abo:il:vc:e:sht")) != -1 )
    {
        switch(c)
        {
        case 'a':
            h64e.fmt.flag_ascii = 1;
            break;
        case 'b':
            h64e.fmt.flag_offset = 1;
            break;
        case 'o':
            //param_offset = atoi( optarg );
            h64e.fmt.flag_offset = 1;
            h64e.fmt.offset_addr = atoi(optarg);
            break;
        case 'i':
            h64e.fmt.group = H64E_G_BYTE;
            h64e.fmt.flag_output_types = 0;
            h64e.fmt.flag_offset = 1;
            break;
        case 'l':
            param_length = atoi( optarg );
            break;
        case 'c':
            h64e.fmt.column_size = atoi(optarg);
            break;
        case 'e':
            h64e.fmt.flag_output_types = 1;
            h64e.fmt.output_type = output_name2int(optarg);
            h64e.fmt.group = group_typename2int(optarg);
            break;
        case 's':
            h64e.fmt.flag_space = 1;
            break;
        case 'h':
            h64e.fmt.flag_hex = 0;
            break;
        case 't':
            h64e.fmt.flag_show_offset = 1;
            break;
        case 'v':
            version();
            exit(1);
            break;
        default:
            helper( argv[0] );
            exit(1);
        }
    }

    if ( argc < 2 )
    {
        printf("At least one argument needed now\n");
        helper( argv[0] );
        return 1;
    }

    /* last argument is file name */
    fname = argv[argc-1];

    /* check params */
    if (-1 == h64e_check_param( &h64e ))
    {
        return -1;
    }
    
    

    /* open file */
    fd = file_open( fname );
    if ( fd == -1 )
    {
        printf("Cannot open file %s\n", fname);
        return -1;
    }
    
    //register formats
    //configure format of u8 to terminate on new line and have space between types
    //printf("Column size set to %d\n",h64e.fmt.column_size);
    int8_fmt.f_space = !h64e.fmt.flag_space;
    //printf("h64e.fmt.flag_space %d\n",h64e.fmt.flag_space);
    int8_fmt.f_new_line = 1;
    int8_fmt.column_size = h64e.fmt.column_size;
    int8_fmt.group_fmt = h64e.fmt.group;
    int8_fmt.output_fmt = h64e.fmt.output_type;
    int8_fmt.f_offset = h64e.fmt.flag_offset;
    int8_fmt.offset_addr = h64e.fmt.offset_addr;
    int8_fmt.f_output_types = h64e.fmt.flag_output_types;
    int8_fmt.output_fmt = h64e.fmt.output_type;
    int8_fmt.f_ascii = h64e.fmt.flag_ascii;
    int8_fmt.f_hex = h64e.fmt.flag_hex;
    int8_fmt.f_show_offset = h64e.fmt.flag_show_offset;
    
    if (param_length > -1)
    {
        printf("param length %d\n", param_length);
        int8_fmt.length = param_length;
        int8_fmt.f_length = 1;
    }
        
    //set offset in file
    if (int8_fmt.f_offset)
    {
        file_seek(fd, int8_fmt.offset_addr);
    }
    //do conversation, reading input and outputing
    while ((in_bytes = file_read(fd,(char *)&buf_in,int8_fmt.column_size)) != -1)
    {
        if (in_bytes == 0)
        {
            //printf("Stream ended\n");
            break;
        }
        
        if (in_total == int8_fmt.length)
        {
            break;
        }
        
        if (int8_fmt.f_length == 1)
        {
            /*
            printf("-->\n");
            printf("in_total %d\n", in_total);
            printf("in_bytes %d\n", in_bytes);
            printf("(in_total+in_bytes)=%d",(in_total+in_bytes));
            */
            //if ((int8_fmt.total_output+in_bytes) >= int8_fmt.length)
            if ((in_total+in_bytes) >= int8_fmt.length)
            {
                in_bytes = int8_fmt.length - in_total;
                //printf("in_bytes %d\n", in_bytes);
            }
        }
        
        //write to input stream, connect all streams in a pipe
        in_total += in_bytes;
        //printf("Read from file %d bytes %d bytes total\n",in_bytes,in_total);
        h64e_si_data_in(&sin, &buf_in[0], in_bytes);
        
        //if buffer have enought data then convert it
        if (h64e_si_len(&sin)>=int8_fmt.column_size)
        {
            int cnv_num=-1;
            //printf("Input Out\n");
            cnv_bytes = h64e_si_data_out(&sin, &buf_cnv_in[0], int8_fmt.column_size);
            //cnv_total += cnv_bytes;
            
            //Convert output data to desired format
            //printf("Start converting to byte8\n");
            switch (h64e.fmt.group)
            {
                case H64E_G_BYTE:
                    cnv_num = h64e_fmt_byte_align16(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 2048);
                    break;
                case H64E_G_WORD:
                case H64E_G_DWORD:
                case H64E_G_QWORD:
                    cnv_num = h64e_fmt_align(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 2048, (int32_t)h64e_data_sz(&h64e));
                    break;
                
                default:
                    printf("Unknown grouping\n");
            }
            //printf("Conversation amount\n");
            //Push all data ot output buffer
            //printf("Write to output stream data %d bytes\n",cnv_bytes);
            cnv_bytes = h64e_so_data_in(&sout, &buf_cnv_out[0], cnv_num);
            cnv_total += cnv_num;
            //printf("Written bytes to output steam %d bytes %d bytes total\n",cnv_bytes,cnv_total);
        }
        
        //Get data out of buffer
        if ((out_bytes = h64e_so_len(&sout)) > 0)
        {
            out_bytes = h64e_so_data_out(&sout, &buf_out[0],512);
            buf_out[out_bytes] = 0x00;
            printf("%s",buf_out);
        }
        /*
        out_bytes = h64e_so_data_out(&sout, &buf_out[0],512);
        printf("Output out\n");
        out_total += out_bytes;
        buf_out[out_bytes] = 0x00;
        printf("%s\n",buf_out);
        printf("Read converted data %d bytes %d total bytes\n",out_bytes,out_total);
        */
        //printf("Total: in %d cnv %d out %d\n",in_total,cnv_total,out_total);
        
        //printf("Reading %d bytes\n",in_bytes);
    }
    
    //check and empty all buffers
    while (h64e_si_len(&sin)>0)
    {
        int cnv_num = -1;
        if (h64e_si_len(&sin)>int8_fmt.column_size) {
            //printf("Full column buffer\n");
            cnv_bytes = h64e_si_data_out(&sin, &buf_cnv_in[0], int8_fmt.column_size);
            cnv_num = h64e_fmt_byte_align16(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 2048);
            
            cnv_bytes = h64e_so_data_in(&sout, &buf_cnv_out[0], cnv_num);
            cnv_total += cnv_num;
        } else {
            //printf("some buffer!!!\n");
            //less then column size
            cnv_bytes = h64e_si_data_out(&sin, &buf_cnv_in[0], int8_fmt.column_size);
            //printf("cnv_bytes %d\n", cnv_bytes);
            cnv_num = h64e_fmt_byte_align16(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 2048);
            //printf("cnv_num %d\n", cnv_num);
            
            cnv_bytes = h64e_so_data_in(&sout, &buf_cnv_out[0], cnv_num);
            cnv_total += cnv_num;
        }
        
        if ((out_bytes = h64e_so_len(&sout)) > 0)
        {
            out_bytes = h64e_so_data_out(&sout, &buf_out[0],512);
            buf_out[out_bytes] = 0x00;
            printf("%s",buf_out);
        }
        break;
    }
    
    
    //deinit all structures
    h64e_destroy(&h64e);
    file_close(fd);

    printf("\nEnding Execution\n");

    return 0;
}