31 return (uint8_t)((nbits / BLOCK_SIZE) + ((nbits % BLOCK_SIZE) != 0));
39 time_t
get_s(
struct timespec * start,
struct timespec * end)
41 if((end->tv_nsec - start->tv_nsec)>0)
43 return (end->tv_sec - start->tv_sec);
45 return (end->tv_sec - start->tv_sec - 1);
48 unsigned int get_ns(
struct timespec * start,
struct timespec * end)
50 long t = end->tv_nsec - start->tv_nsec;
53 return (
unsigned int)floor(t/1000000);
55 return (
unsigned int)floor((1000000000 + t)/1000000);
61 for (i=0; i<length; i++)
63 printf(
"%02x", bytes_string[i]);
67 void bytes_string2hex(
char * hex_string,
const uint8_t * bytes_string, uint16_t length)
70 char * hex_string_pointer = hex_string;
71 for (i=0; i<length; i++)
73 sprintf(hex_string_pointer,
"%02x", bytes_string[i]);
74 hex_string_pointer+=2;
78 void hex_string2bytes(uint8_t * bytes_string,
const char * hex_string, uint16_t length)
85 const char * hex_string_pointer = hex_string;
86 for (i = 0; i < length; i++)
88 buf[2] = hex_string_pointer[0];
89 buf[3] = hex_string_pointer[1];
90 bytes_string[i] = strtol(buf, NULL, 0);
91 hex_string_pointer += 2;
95 void bin2hex(
const unsigned char *old,
const uint32_t oldlen,
unsigned char* result)
100 for (i = j = 0; i < oldlen; i++)
103 result[j++] = (char) (87 + b + (((b - 10) >> 31) & -39));
105 result[j++] = (char) (87 + b + (((b - 10) >> 31) & -39));
111 uint32_t
hex2bin(
const char *in,
unsigned char *out)
115 uint32_t len = strlen(in);
120 if ((in[n1] >=
'0') && (in[n1] <=
'9'))
122 else if ((in[n1] >=
'A') && (in[n1] <=
'F'))
123 ch = in[n1++] -
'A' + 10;
124 else if ((in[n1] >=
'a') && (in[n1] <=
'f'))
125 ch = in[n1++] -
'a' + 10;
132 for (; in[n1] && in[n1] !=
'\n' && in[n1] !=
'\r';)
134 if ((in[n1] >=
'0') && (in[n1] <=
'9'))
136 else if ((in[n1] >=
'A') && (in[n1] <=
'F'))
137 ch = in[n1++] -
'A' + 10;
138 else if ((in[n1] >=
'a') && (in[n1] <=
'f'))
139 ch = in[n1++] -
'a' + 10;
149 if ((in[n1] >=
'0') && (in[n1] <=
'9'))
151 else if ((in[n1] >=
'A') && (in[n1] <=
'F'))
152 ch = in[n1++] -
'A' + 10;
153 else if ((in[n1] >=
'a') && (in[n1] <=
'f'))
154 ch = in[n1++] -
'a' + 10;
164 #define MAX_MARKER_LEN 50 169 char line[MAX_MARKER_LEN];
172 len = (int)strlen(marker);
173 if ( len > MAX_MARKER_LEN-1 )
174 len = MAX_MARKER_LEN-1;
176 for ( i=0; i<len; i++ )
177 if ( (line[i] = fgetc(infile)) == EOF )
183 if ( !strncmp(line, marker, len) )
186 for ( i=0; i<len-1; i++ )
188 if ( (line[len-1] = fgetc(infile)) == EOF )
196 ReadHex (FILE *infile,
char *A,
const int Length,
char *str)
205 memset(A,
'0', Length/2);
209 while ( (ch = fgetc(infile)) != EOF )
215 if ((ch ==
'\n') || (ch ==
'\0'))
224 if ( (ch >=
'0') && (ch <=
'9') )
226 else if ( (ch >=
'A') && (ch <=
'F') )
228 else if ( (ch >=
'a') && (ch <=
'f') )
230 A[i / 2] = (A[i / 2] << 4) | ich;
231 if ( (++i / 2) == Length )
245 for (i = 7; i >= 0; --i)
257 for (i = 7; i >= 0; --i)
273 uint32_t len = strlen(s);
void hex_string2bytes(uint8_t *bytes_string, const char *hex_string, uint16_t length)
Convert the hexadecimal string "hex_string" under bytes string form.
time_t get_s(struct timespec *start, struct timespec *end)
Return the elapsed time in second between "start" and "end".
void char2bin(char c, uint8_t *b)
Convert a character into a byte (according to its ascii value)
unsigned int get_ns(struct timespec *start, struct timespec *end)
Return the elapsed time in nano second to add to the result of get_s().
uint32_t hex2bin(const char *in, unsigned char *out)
Convert an hexadecimal string into a binary string.
uint8_t bits_to_nblock(const uint16_t nbits)
Return the number of blocks required to store a nbits number.
int ReadHex(FILE *infile, char *A, const int Length, char *str)
Read an hexadecimal string in file "infile".
void bytes_string2hex(char *hex_string, const uint8_t *bytes_string, uint16_t length)
Convert the byte string "bytes_string" under hexadecimal form.
Declaration of miscellaneous function.
int FindMarker(FILE *infile, const char *marker)
Find a marker in a file.
void string2bin(char *s, uint8_t *output)
Convert a string into a byte array.
void printstring2bin(uint8_t *s, uint32_t len)
Print a byte array.
uint8_t max_size(uint8_t a, uint8_t b)
Return the max of a and b.
void print_bytes_string_hex(const uint8_t *bytes_string, uint16_t length)
Print the byte string "bytes_string" under hexadecimal form.
void printchar2bin(uint8_t b)
print a byte