|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
-
- unsigned long read_file_to_buffer( const char* const filepath,
- char* obuff,
- int remove_new_line_chr)
- {
- FILE* fh=0;
- size_t len=0;
- char* line=NULL;
- unsigned long total_len=0;
-
- if( (fh=fopen(filepath, "rb")) == 0 )
- {
- fprintf(stderr, "File %s doesn't exist. Exiting...\n", filepath);
- exit(1);
- }
-
- if(obuff=NULL)
- {
- //allocate
- fseek(fh, 0, SEEK_END);
- int pos = ftell(fh);
- fseek(fh, 0, SEEK_CUR);
- obuff = malloc(pos);
- }
-
- while( getline(&line, &len, fh) != -1 )
- {
- len = strlen(line);
- if(remove_new_line_chr)
- len--;
-
- strncpy(obuff+total_len, line, len);
- total_len+=len;
- free(line);
- line=NULL;
- }
- return total_len;
- }
-
- Result_t load_base64_to_hex(const char* const filepath, uint8_t* hex_buffer, size_t* len)
- {
- uint8_t* base64_buff;
- *len = read_file_to_buffer(filepath, base64_buff, 1);
- /// int base64_to_hex(const unsigned char* const i_string,
- //int i_string_len,
- //unsigned char* o_hex_array );
- base64_to_hex(base64_buff, *len, hex_buffer);
-
-
- }
-
- void check(int iFlag, const unsigned char* const msg)
- {
- if(!iFlag)
- {
- fprintf(stderr, "ERROR: %s\n", msg);
- exit(1);
- }
- }
|