mirror of
https://github.com/jedisct1/libsodium.git
synced 2026-08-25 08:37:13 +09:00
Rename ip2bytes and bytes2ip to ip2bin and bin2ip for consistency
This commit is contained in:
@@ -93,11 +93,11 @@ int sodium_base642bin(unsigned char *const bin, const size_t bin_maxlen, const c
|
||||
const char **const b64_end, const int variant) __attribute__((nonnull(1)));
|
||||
|
||||
SODIUM_EXPORT
|
||||
int sodium_ip2bytes(unsigned char out[16], const char *src) __attribute__((warn_unused_result))
|
||||
int sodium_ip2bin(unsigned char out[16], const char *src) __attribute__((warn_unused_result))
|
||||
__attribute__((nonnull));
|
||||
|
||||
SODIUM_EXPORT
|
||||
char *sodium_bytes2ip(char *dst, size_t dst_len, const unsigned char in[16])
|
||||
char *sodium_bin2ip(char *dst, size_t dst_len, const unsigned char in[16])
|
||||
__attribute__((nonnull));
|
||||
|
||||
SODIUM_EXPORT
|
||||
|
||||
@@ -474,7 +474,7 @@ parse_ipv6(const char *src, const char *end, unsigned char out[16])
|
||||
}
|
||||
|
||||
int
|
||||
sodium_ip2bytes(unsigned char out[16], const char *src)
|
||||
sodium_ip2bin(unsigned char out[16], const char *src)
|
||||
{
|
||||
const char *end;
|
||||
const char *z;
|
||||
@@ -536,7 +536,7 @@ ip_write_num(char **p, unsigned int val, int base)
|
||||
}
|
||||
|
||||
char *
|
||||
sodium_bytes2ip(char *dst, size_t dst_len, const unsigned char in[16])
|
||||
sodium_bin2ip(char *dst, size_t dst_len, const unsigned char in[16])
|
||||
{
|
||||
char buf[46];
|
||||
char *p = buf;
|
||||
|
||||
+54
-54
@@ -253,7 +253,7 @@ main(void)
|
||||
unsigned char ip_expected[16];
|
||||
char ip_str[46];
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "192.168.1.1") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "192.168.1.1") == 0);
|
||||
memset(ip_expected, 0, 10);
|
||||
ip_expected[10] = 0xff;
|
||||
ip_expected[11] = 0xff;
|
||||
@@ -264,23 +264,23 @@ main(void)
|
||||
assert(memcmp(ip_bytes, ip_expected, 16) == 0);
|
||||
printf("ip2bytes(192.168.1.1): OK\n");
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "0.0.0.0") == 0);
|
||||
assert(sodium_ip2bytes(ip_bytes, "255.255.255.255") == 0);
|
||||
assert(sodium_ip2bytes(ip_bytes, "127.0.0.1") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "0.0.0.0") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "255.255.255.255") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "127.0.0.1") == 0);
|
||||
printf("ip2bytes IPv4 basic: OK\n");
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "256.1.1.1") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "1.999.1.1") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "192.168.1") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "192.168.1.1.1") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, ".1.2.3") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "1.2.3.") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "1..2.3") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "1.2.a.3") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "256.1.1.1") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "1.999.1.1") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "192.168.1") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "192.168.1.1.1") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, ".1.2.3") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "1.2.3.") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "1..2.3") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "1.2.a.3") == -1);
|
||||
printf("ip2bytes IPv4 invalid: OK\n");
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "2001:db8:85a3:0:0:8a2e:370:7334") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "2001:db8:85a3:0:0:8a2e:370:7334") == 0);
|
||||
ip_expected[0] = 0x20; ip_expected[1] = 0x01;
|
||||
ip_expected[2] = 0x0d; ip_expected[3] = 0xb8;
|
||||
ip_expected[4] = 0x85; ip_expected[5] = 0xa3;
|
||||
@@ -292,34 +292,34 @@ main(void)
|
||||
assert(memcmp(ip_bytes, ip_expected, 16) == 0);
|
||||
printf("ip2bytes(2001:db8:85a3:0:0:8a2e:370:7334): OK\n");
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "::1") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "::1") == 0);
|
||||
memset(ip_expected, 0, 16);
|
||||
ip_expected[15] = 1;
|
||||
assert(memcmp(ip_bytes, ip_expected, 16) == 0);
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "::") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "::") == 0);
|
||||
memset(ip_expected, 0, 16);
|
||||
assert(memcmp(ip_bytes, ip_expected, 16) == 0);
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "2001:db8::1") == 0);
|
||||
assert(sodium_ip2bytes(ip_bytes, "fe80::1") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "2001:db8::1") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "fe80::1") == 0);
|
||||
printf("ip2bytes IPv6 compressed: OK\n");
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "fe80::1%eth0") == 0);
|
||||
assert(sodium_ip2bytes(ip_bytes, "fe80::1%15") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "fe80::1%eth0") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "fe80::1%15") == 0);
|
||||
printf("ip2bytes IPv6 zone: OK\n");
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "2001:::1") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "2001::1::1") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "1:2:3:4:5:6:7:8:9") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "12345:1:1:1:1:1:1:1") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "2001:db8:g:1:1:1:1:1") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, ":2001:db8::1") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "2001:db8:1:") == -1);
|
||||
assert(sodium_ip2bytes(ip_bytes, "fe80::1%") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "2001:::1") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "2001::1::1") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "1:2:3:4:5:6:7:8:9") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "12345:1:1:1:1:1:1:1") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "2001:db8:g:1:1:1:1:1") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, ":2001:db8::1") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "2001:db8:1:") == -1);
|
||||
assert(sodium_ip2bin(ip_bytes, "fe80::1%") == -1);
|
||||
printf("ip2bytes IPv6 invalid: OK\n");
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "::ffff:192.168.1.1") == 0);
|
||||
assert(sodium_ip2bin(ip_bytes, "::ffff:192.168.1.1") == 0);
|
||||
memset(ip_expected, 0, 10);
|
||||
ip_expected[10] = 0xff;
|
||||
ip_expected[11] = 0xff;
|
||||
@@ -339,35 +339,35 @@ main(void)
|
||||
ip_bytes[13] = 168;
|
||||
ip_bytes[14] = 1;
|
||||
ip_bytes[15] = 1;
|
||||
assert(sodium_bytes2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(sodium_bin2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(strcmp(ip_str, "192.168.1.1") == 0);
|
||||
|
||||
ip_bytes[12] = 0;
|
||||
ip_bytes[13] = 0;
|
||||
ip_bytes[14] = 0;
|
||||
ip_bytes[15] = 0;
|
||||
assert(sodium_bytes2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(sodium_bin2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(strcmp(ip_str, "0.0.0.0") == 0);
|
||||
|
||||
ip_bytes[12] = 255;
|
||||
ip_bytes[13] = 255;
|
||||
ip_bytes[14] = 255;
|
||||
ip_bytes[15] = 255;
|
||||
assert(sodium_bytes2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(sodium_bin2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(strcmp(ip_str, "255.255.255.255") == 0);
|
||||
printf("bytes2ip IPv4: OK\n");
|
||||
|
||||
memset(ip_bytes, 0, 16);
|
||||
ip_bytes[15] = 1;
|
||||
assert(sodium_bytes2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(sodium_bin2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(strcmp(ip_str, "::1") == 0);
|
||||
|
||||
memset(ip_bytes, 0, 16);
|
||||
assert(sodium_bytes2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(sodium_bin2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(strcmp(ip_str, "::") == 0);
|
||||
|
||||
memset(ip_bytes, 0xff, 16);
|
||||
assert(sodium_bytes2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(sodium_bin2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(strcmp(ip_str, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") == 0);
|
||||
printf("bytes2ip IPv6: OK\n");
|
||||
|
||||
@@ -378,40 +378,40 @@ main(void)
|
||||
ip_bytes[13] = 255;
|
||||
ip_bytes[14] = 255;
|
||||
ip_bytes[15] = 255;
|
||||
assert(sodium_bytes2ip(ip_str, 16, ip_bytes) != NULL);
|
||||
assert(sodium_bytes2ip(ip_str, 15, ip_bytes) == NULL);
|
||||
assert(sodium_bin2ip(ip_str, 16, ip_bytes) != NULL);
|
||||
assert(sodium_bin2ip(ip_str, 15, ip_bytes) == NULL);
|
||||
|
||||
memset(ip_bytes, 0xff, 16);
|
||||
assert(sodium_bytes2ip(ip_str, 40, ip_bytes) != NULL);
|
||||
assert(sodium_bytes2ip(ip_str, 39, ip_bytes) == NULL);
|
||||
assert(sodium_bin2ip(ip_str, 40, ip_bytes) != NULL);
|
||||
assert(sodium_bin2ip(ip_str, 39, ip_bytes) == NULL);
|
||||
|
||||
memset(ip_bytes, 0, 16);
|
||||
assert(sodium_bytes2ip(ip_str, 3, ip_bytes) != NULL);
|
||||
assert(sodium_bytes2ip(ip_str, 2, ip_bytes) == NULL);
|
||||
assert(sodium_bin2ip(ip_str, 3, ip_bytes) != NULL);
|
||||
assert(sodium_bin2ip(ip_str, 2, ip_bytes) == NULL);
|
||||
printf("bytes2ip buffer size: OK\n");
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "10.20.30.40") == 0);
|
||||
assert(sodium_bytes2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(sodium_ip2bin(ip_bytes, "10.20.30.40") == 0);
|
||||
assert(sodium_bin2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(strcmp(ip_str, "10.20.30.40") == 0);
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "::1") == 0);
|
||||
assert(sodium_bytes2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(sodium_ip2bin(ip_bytes, "::1") == 0);
|
||||
assert(sodium_bin2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(strcmp(ip_str, "::1") == 0);
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "2001:db8::1") == 0);
|
||||
assert(sodium_bytes2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(sodium_ip2bin(ip_bytes, "2001:db8::1") == 0);
|
||||
assert(sodium_bin2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(strcmp(ip_str, "2001:db8::1") == 0);
|
||||
|
||||
assert(sodium_ip2bytes(ip_bytes, "2001:db8:85a3:1234:5678:8a2e:370:7334") == 0);
|
||||
assert(sodium_bytes2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(sodium_ip2bin(ip_bytes, "2001:db8:85a3:1234:5678:8a2e:370:7334") == 0);
|
||||
assert(sodium_bin2ip(ip_str, sizeof(ip_str), ip_bytes) != NULL);
|
||||
assert(strcmp(ip_str, "2001:db8:85a3:1234:5678:8a2e:370:7334") == 0);
|
||||
printf("ip round-trip: OK\n");
|
||||
|
||||
{
|
||||
unsigned char lower[16], upper[16], mixed[16];
|
||||
assert(sodium_ip2bytes(lower, "abcd:ef01:2345:6789:abcd:ef01:2345:6789") == 0);
|
||||
assert(sodium_ip2bytes(upper, "ABCD:EF01:2345:6789:ABCD:EF01:2345:6789") == 0);
|
||||
assert(sodium_ip2bytes(mixed, "AbCd:eF01:2345:6789:aBcD:Ef01:2345:6789") == 0);
|
||||
assert(sodium_ip2bin(lower, "abcd:ef01:2345:6789:abcd:ef01:2345:6789") == 0);
|
||||
assert(sodium_ip2bin(upper, "ABCD:EF01:2345:6789:ABCD:EF01:2345:6789") == 0);
|
||||
assert(sodium_ip2bin(mixed, "AbCd:eF01:2345:6789:aBcD:Ef01:2345:6789") == 0);
|
||||
assert(memcmp(lower, upper, 16) == 0);
|
||||
assert(memcmp(lower, mixed, 16) == 0);
|
||||
}
|
||||
@@ -419,8 +419,8 @@ main(void)
|
||||
|
||||
{
|
||||
unsigned char direct[16], mapped[16];
|
||||
assert(sodium_ip2bytes(direct, "192.168.1.1") == 0);
|
||||
assert(sodium_ip2bytes(mapped, "::ffff:192.168.1.1") == 0);
|
||||
assert(sodium_ip2bin(direct, "192.168.1.1") == 0);
|
||||
assert(sodium_ip2bin(mapped, "::ffff:192.168.1.1") == 0);
|
||||
assert(memcmp(direct, mapped, 16) == 0);
|
||||
}
|
||||
printf("ip IPv4-mapped equivalence: OK\n");
|
||||
|
||||
@@ -790,7 +790,7 @@ sodium_base642bin
|
||||
sodium_base64_encoded_len
|
||||
sodium_bin2base64
|
||||
sodium_bin2hex
|
||||
sodium_bytes2ip
|
||||
sodium_bin2ip
|
||||
sodium_compare
|
||||
sodium_crit_enter
|
||||
sodium_crit_leave
|
||||
@@ -798,7 +798,7 @@ sodium_free
|
||||
sodium_hex2bin
|
||||
sodium_increment
|
||||
sodium_init
|
||||
sodium_ip2bytes
|
||||
sodium_ip2bin
|
||||
sodium_is_zero
|
||||
sodium_library_minimal
|
||||
sodium_library_version_major
|
||||
|
||||
Reference in New Issue
Block a user