api: return integer in function string_encode_base16
This commit is contained in:
parent
8848b0e22a
commit
6d72868e15
@ -370,9 +370,12 @@ secure_config_data_write_map_cb (void *data,
|
||||
buffer_base16 = malloc ((length_buffer * 2) + 1);
|
||||
if (buffer_base16)
|
||||
{
|
||||
string_encode_base16 (buffer, length_buffer, buffer_base16);
|
||||
config_file_write_line (config_file, key,
|
||||
"\"%s\"", buffer_base16);
|
||||
if (string_encode_base16 (buffer, length_buffer,
|
||||
buffer_base16) >= 0)
|
||||
{
|
||||
config_file_write_line (config_file, key,
|
||||
"\"%s\"", buffer_base16);
|
||||
}
|
||||
free (buffer_base16);
|
||||
}
|
||||
free (buffer);
|
||||
|
@ -2702,27 +2702,29 @@ string_format_size (unsigned long long size)
|
||||
*
|
||||
* Argument "length" is number of bytes in "from" to convert (commonly
|
||||
* strlen(from)).
|
||||
*
|
||||
* Returns length of string in "*to" (it does not count final \0).
|
||||
*/
|
||||
|
||||
void
|
||||
int
|
||||
string_encode_base16 (const char *from, int length, char *to)
|
||||
{
|
||||
int i;
|
||||
int i, count;
|
||||
const char *hexa = "0123456789ABCDEF";
|
||||
char *ptr_to;
|
||||
|
||||
if (!from || !to)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
count = 0;
|
||||
|
||||
ptr_to = to;
|
||||
ptr_to[0] = '\0';
|
||||
for (i = 0; i < length; i++)
|
||||
{
|
||||
ptr_to[0] = hexa[((unsigned char)from[i]) / 16];
|
||||
ptr_to[1] = hexa[((unsigned char)from[i]) % 16];
|
||||
ptr_to += 2;
|
||||
to[count++] = hexa[((unsigned char)from[i]) / 16];
|
||||
to[count++] = hexa[((unsigned char)from[i]) % 16];
|
||||
}
|
||||
ptr_to[0] = '\0';
|
||||
to[count] = '\0';
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -2734,17 +2736,15 @@ string_encode_base16 (const char *from, int length, char *to)
|
||||
int
|
||||
string_decode_base16 (const char *from, char *to)
|
||||
{
|
||||
int length, to_length, i, pos;
|
||||
unsigned char *ptr_to, value;
|
||||
int length, i, pos, count;
|
||||
unsigned char value;
|
||||
|
||||
if (!from || !to)
|
||||
return 0;
|
||||
|
||||
length = strlen (from) / 2;
|
||||
count = 0;
|
||||
|
||||
ptr_to = (unsigned char *)to;
|
||||
ptr_to[0] = '\0';
|
||||
to_length = 0;
|
||||
length = strlen (from) / 2;
|
||||
|
||||
for (i = 0; i < length; i++)
|
||||
{
|
||||
@ -2766,13 +2766,11 @@ string_decode_base16 (const char *from, char *to)
|
||||
else if ((from[pos] >= 'A') && (from[pos] <= 'F'))
|
||||
value |= from[pos] - 'A' + 10;
|
||||
|
||||
ptr_to[0] = value;
|
||||
ptr_to++;
|
||||
to_length++;
|
||||
to[count++] = value;
|
||||
}
|
||||
ptr_to[0] = '\0';
|
||||
to[count] = '\0';
|
||||
|
||||
return to_length;
|
||||
return count;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -105,7 +105,7 @@ extern char *string_iconv_from_internal (const char *charset,
|
||||
const char *string);
|
||||
extern int string_fprintf (FILE *file, const char *data, ...);
|
||||
extern char *string_format_size (unsigned long long size);
|
||||
extern void string_encode_base16 (const char *from, int length, char *to);
|
||||
extern int string_encode_base16 (const char *from, int length, char *to);
|
||||
extern int string_decode_base16 (const char *from, char *to);
|
||||
extern int string_encode_base32 (const char *from, int length, char *to);
|
||||
extern int string_decode_base32 (const char *from, char *to);
|
||||
|
@ -1342,17 +1342,19 @@ TEST(CoreString, Base16)
|
||||
char str[1024];
|
||||
|
||||
/* string_encode_base16 */
|
||||
string_encode_base16 (NULL, 0, NULL);
|
||||
string_encode_base16 (NULL, 0, str);
|
||||
string_encode_base16 ("", 0, NULL);
|
||||
LONGS_EQUAL(-1, string_encode_base16 (NULL, 0, NULL));
|
||||
LONGS_EQUAL(-1, string_encode_base16 (NULL, 0, str));
|
||||
LONGS_EQUAL(-1, string_encode_base16 ("", 0, NULL));
|
||||
str[0] = 0xAA;
|
||||
string_encode_base16 ("", -1, str);
|
||||
LONGS_EQUAL(0, string_encode_base16 ("", -1, str));
|
||||
BYTES_EQUAL(0x0, str[0]);
|
||||
str[0] = 0xAA;
|
||||
string_encode_base16 ("", 0, str);
|
||||
LONGS_EQUAL(0, string_encode_base16 ("", 0, str));
|
||||
BYTES_EQUAL(0x0, str[0]);
|
||||
string_encode_base16 ("abc", 3, str);
|
||||
LONGS_EQUAL(6, string_encode_base16 ("abc", 3, str));
|
||||
STRCMP_EQUAL("616263", str);
|
||||
LONGS_EQUAL(32, string_encode_base16 ("this is a *test*", 16, str));
|
||||
STRCMP_EQUAL("746869732069732061202A746573742A", str);
|
||||
|
||||
/* string_decode_base16 */
|
||||
LONGS_EQUAL(0, string_decode_base16 (NULL, NULL));
|
||||
@ -1361,6 +1363,9 @@ TEST(CoreString, Base16)
|
||||
LONGS_EQUAL(0, string_decode_base16 ("", str));
|
||||
LONGS_EQUAL(3, string_decode_base16 ("616263", str));
|
||||
STRCMP_EQUAL("abc", str);
|
||||
LONGS_EQUAL(16, string_decode_base16 ("746869732069732061202A746573742A",
|
||||
str));
|
||||
STRCMP_EQUAL("this is a *test*", str);
|
||||
}
|
||||
|
||||
/*
|
||||
|
Loading…
x
Reference in New Issue
Block a user