Changeset: 22fc6b119e7d for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/22fc6b119e7d
Modified Files:
        gdk/gdk.h
        gdk/gdk_atoms.c
        gdk/gdk_calc_convert.c
        gdk/gdk_hash.c
        gdk/gdk_hash.h
        monetdb5/modules/atoms/inet-46.c
Branch: default
Log Message:

Store inet6 values as an array of 16 bytes instead of 8 shorts.
This means there is no difference between little- and big-endian.


diffs (truncated from 379 to 300 lines):

diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -457,7 +457,7 @@ typedef union {
 #else
        lng align[2];           /* force alignment, not otherwise used */
 #endif
-       uint16_t oct[8];
+       uint8_t hex[16];
 } inet6;
 
 #define SIZEOF_OID     SIZEOF_SIZE_T
diff --git a/gdk/gdk_atoms.c b/gdk/gdk_atoms.c
--- a/gdk/gdk_atoms.c
+++ b/gdk/gdk_atoms.c
@@ -1476,12 +1476,7 @@ static int
 INET6compare(const void *L, const void *R)
 {
        const inet6 *l = L, *r = R;
-       for (int i = 0; i < 8; i++) {
-               int v = (l->oct[i] > r->oct[i]) - (l->oct[i] < r->oct[i]);
-               if (v != 0)
-                       return v;
-       }
-       return 0;
+       return memcmp(l->hex, r->hex, sizeof(l->hex));
 }
 
 static ssize_t
@@ -1516,9 +1511,9 @@ INET6fromString(const char *svalue, size
                }
        } else if (strlen(s) == 32 && strspn(s, "0123456789abcdefABCDEF") == 
32) {
                /* special case: 32 hex digits without [ ] */
-               for (int i = 0; i < 8; i++) {
-                       uint16_t val = 0;
-                       for (int j = 12; j >= 0; j -= 4) {
+               for (int i = 0; i < 16; i++) {
+                       uint8_t val = 0;
+                       for (int j = 4; j >= 0; j -= 4) {
                                if ('0' <= *s && *s <= '9')
                                        val |= (*s - '0') << j;
                                else if ('a' <= *s && *s <= 'f')
@@ -1527,7 +1522,7 @@ INET6fromString(const char *svalue, size
                                        val |= (*s - 'A' + 10) << j;
                                s++;
                        }
-                       i6.oct[i] = val;
+                       i6.hex[i] = val;
                }
                **retval = i6;
                return (ssize_t) (s - svalue);
@@ -1535,7 +1530,7 @@ INET6fromString(const char *svalue, size
        int dcolpos = -1;
        int i;
        int maybeip4 = 0;
-       for (i = 0; i < 8; i++) {
+       for (i = 0; i < 16; i += 2) {
                if (s[0] == ':' && s[1] == ':') {
                        if (dcolpos >= 0) {
                                GDKerror("Invalid IPv6 address: multiple ::.");
@@ -1560,26 +1555,28 @@ INET6fromString(const char *svalue, size
                                        GDKerror("Invalid IPv6 address.");
                                        goto bailout;
                                }
-                               s = e + 1;
-                               unsigned long u2 = strtoul(s, &e, 10);
-                               if (e == s || *e != '.' || u2 > 255) {
-                                       GDKerror("Invalid IPv6 address.");
-                                       goto bailout;
-                               }
-                               i6.oct[i++] = (uint16_t) ((ul << 8) | u2);
+                               i6.hex[i++] = (uint8_t) ul;
                                s = e + 1;
                                ul = strtoul(s, &e, 10);
                                if (e == s || *e != '.' || ul > 255) {
                                        GDKerror("Invalid IPv6 address.");
                                        goto bailout;
                                }
+                               i6.hex[i++] = (uint8_t) ul;
                                s = e + 1;
-                               u2 = strtoul(s, &e, 10);
-                               if (e == s || u2 > 255) {
+                               ul = strtoul(s, &e, 10);
+                               if (e == s || *e != '.' || ul > 255) {
                                        GDKerror("Invalid IPv6 address.");
                                        goto bailout;
                                }
-                               i6.oct[i++] = (uint16_t) ((ul << 8) | u2);
+                               i6.hex[i++] = (uint8_t) ul;
+                               s = e + 1;
+                               ul = strtoul(s, &e, 10);
+                               if (e == s || ul > 255) {
+                                       GDKerror("Invalid IPv6 address.");
+                                       goto bailout;
+                               }
+                               i6.hex[i++] = (uint8_t) ul;
                                s = e;
                                break;
                        }
@@ -1589,7 +1586,8 @@ INET6fromString(const char *svalue, size
                        GDKerror("Invalid IPv6 address.");
                        goto bailout;
                }
-               i6.oct[i] = (uint16_t) ul;
+               i6.hex[i] = (uint8_t) ul >> 8;
+               i6.hex[i + 1] = (uint8_t) (ul & 0xFF);
                s = e;
                if (maybeip4 == 0) {
                        if (ul == 0xFFFF)
@@ -1605,15 +1603,15 @@ INET6fromString(const char *svalue, size
                }
                s++;
        }
-       if ((dcolpos < 0 && i < 8) || (dcolpos >= 0 && i == 8)) {
+       if ((dcolpos < 0 && i < 16) || (dcolpos >= 0 && i == 16)) {
                GDKerror("Invalid IPv6 address.");
                goto bailout;
        }
        if (dcolpos >= 0) {
                int j;
-               for (j = 7; i > dcolpos; j--) {
-                       i6.oct[j] = i6.oct[--i];
-                       i6.oct[i] = 0;
+               for (j = 15; i > dcolpos; j--) {
+                       i6.hex[j] = i6.hex[--i];
+                       i6.hex[i] = 0;
                }
        }
        while (GDKisspace(*s))
@@ -1678,8 +1676,8 @@ INET6toString(str *retval, size_t *len, 
        int rl1 = -1;           /* start of current stretch of zeros */
        int mrl = 0;            /* length of longest stretch of zeros */
        int mrl1 = -1;          /* start of longest stretch of zeros */
-       for (int i = 0; i < 8; i++) {
-               if (value->oct[i] == 0) {
+       for (int i = 0; i < 16; i += 2) {
+               if (value->hex[i] == 0 && value->hex[i + 1] == 0) {
                        if (rl++ == 0)
                                rl1 = i;
                } else {
@@ -1695,29 +1693,31 @@ INET6toString(str *retval, size_t *len, 
                mrl1 = rl1;
        }
        if (mrl1 < 0)
-               mrl1 = 8;
+               mrl1 = 16;
        int pos = 0;
-       if (mrl1 == 0 && mrl == 5 && value->oct[5] == 0xFFFF) {
+       if (mrl1 == 0 && mrl == 10
+           && value->hex[10] == 0xFF && value->hex[11] == 0xFF) {
                /* IPv4 address disguised as IPv6 */
                pos += snprintf(*retval + pos, *len - pos,
-                               "::%x:%d.%d.%d.%d",
-                               value->oct[5], value->oct[6] >> 8,
-                               value->oct[6] & 0xFF, value->oct[7] >> 8,
-                               value->oct[7] & 0xFF);
+                               "::ffff:%d.%d.%d.%d",
+                               value->hex[12], value->hex[13],
+                               value->hex[14], value->hex[15]);
                return pos;
        }
-       for (int i = 0; i < mrl1; i++) {
+       for (int i = 0; i < mrl1; i += 2) {
                if (i > 0)
                        (*retval)[pos++] = ':';
-               pos += snprintf(*retval + pos, *len - pos, "%x", value->oct[i]);
+               pos += snprintf(*retval + pos, *len - pos, "%x",
+                               (value->hex[i] << 8) | value->hex[i + 1]);
        }
        if (mrl1 < 8) {
                (*retval)[pos++] = ':';
                (*retval)[pos++] = ':';
        }
-       for (int i = mrl1 + mrl; i < 8; i++) {
-               pos += snprintf(*retval + pos, *len - pos, "%x", value->oct[i]);
-               if (i < 7)
+       for (int i = mrl1 + mrl; i < 16; i += 2) {
+               pos += snprintf(*retval + pos, *len - pos, "%x",
+                               (value->hex[i] << 8) | value->hex[i + 1]);
+               if (i < 15)
                        (*retval)[pos++] = ':';
        }
        return pos;
diff --git a/gdk/gdk_calc_convert.c b/gdk/gdk_calc_convert.c
--- a/gdk/gdk_calc_convert.c
+++ b/gdk/gdk_calc_convert.c
@@ -1,7 +1,7 @@
 /*
  * SPDX-License-Identifier: MPL-2.0
  *
- * This Source Code Form is subject to the terms of the Mozilla Public
+\ * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0.  If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  *
@@ -992,18 +992,27 @@ convert_inet6_inet4(const inet6 *src, in
                        if (is_inet6_nil(src[x])) {
                                dst[i] = inet4_nil;
                                nils++;
-                       } else if (src[x].oct[0] == 0 &&
-                                  src[x].oct[1] == 0 &&
-                                  src[x].oct[2] == 0 &&
-                                  src[x].oct[3] == 0 &&
-                                  src[x].oct[4] == 0 &&
-                                  src[x].oct[5] == 0xFFFF &&
-                                  (src[x].oct[6] != 0 || src[x].oct[7] != 0)) {
+                       } else if (src[x].hex[0] == 0 &&
+                                  src[x].hex[1] == 0 &&
+                                  src[x].hex[2] == 0 &&
+                                  src[x].hex[3] == 0 &&
+                                  src[x].hex[4] == 0 &&
+                                  src[x].hex[5] == 0 &&
+                                  src[x].hex[6] == 0 &&
+                                  src[x].hex[7] == 0 &&
+                                  src[x].hex[8] == 0 &&
+                                  src[x].hex[9] == 0 &&
+                                  src[x].hex[10] == 0xFF &&
+                                  src[x].hex[11] == 0xFF &&
+                                  (src[x].hex[12] != 0 ||
+                                   src[x].hex[13] != 0 ||
+                                   src[x].hex[14] != 0 ||
+                                   src[x].hex[15] != 0)) {
                                dst[i] = (inet4) {
-                                       .quad[0] = src[x].oct[6] >> 8,
-                                       .quad[1] = src[x].oct[6] & 0xFF,
-                                       .quad[2] = src[x].oct[7] >> 8,
-                                       .quad[3] = src[x].oct[7] & 0xFF,
+                                       .quad[0] = src[x].hex[12],
+                                       .quad[1] = src[x].hex[13],
+                                       .quad[2] = src[x].hex[14],
+                                       .quad[3] = src[x].hex[15],
                                };
                        } else {
                                char buf[40], *s = buf;
@@ -1021,18 +1030,27 @@ convert_inet6_inet4(const inet6 *src, in
                        if (is_inet6_nil(src[x])) {
                                dst[i] = inet4_nil;
                                nils++;
-                       } else if (src[x].oct[0] == 0 &&
-                                  src[x].oct[1] == 0 &&
-                                  src[x].oct[2] == 0 &&
-                                  src[x].oct[3] == 0 &&
-                                  src[x].oct[4] == 0 &&
-                                  src[x].oct[5] == 0xFFFF &&
-                                  (src[x].oct[6] != 0 || src[x].oct[7] != 0)) {
+                       } else if (src[x].hex[0] == 0 &&
+                                  src[x].hex[1] == 0 &&
+                                  src[x].hex[2] == 0 &&
+                                  src[x].hex[3] == 0 &&
+                                  src[x].hex[4] == 0 &&
+                                  src[x].hex[5] == 0 &&
+                                  src[x].hex[6] == 0 &&
+                                  src[x].hex[7] == 0 &&
+                                  src[x].hex[8] == 0 &&
+                                  src[x].hex[9] == 0 &&
+                                  src[x].hex[10] == 0xFF &&
+                                  src[x].hex[11] == 0xFF &&
+                                  (src[x].hex[12] != 0 ||
+                                   src[x].hex[13] != 0 ||
+                                   src[x].hex[14] != 0 ||
+                                   src[x].hex[15] != 0)) {
                                dst[i] = (inet4) {
-                                       .quad[0] = src[x].oct[6] >> 8,
-                                       .quad[1] = src[x].oct[6] & 0xFF,
-                                       .quad[2] = src[x].oct[7] >> 8,
-                                       .quad[3] = src[x].oct[7] & 0xFF,
+                                       .quad[0] = src[x].hex[12],
+                                       .quad[1] = src[x].hex[13],
+                                       .quad[2] = src[x].hex[14],
+                                       .quad[3] = src[x].hex[15],
                                };
                        } else {
                                char buf[40], *s = buf;
@@ -1065,9 +1083,12 @@ convert_inet4_inet6(const inet4 *src, in
                                nils++;
                        } else {
                                dst[i] = (inet6) {
-                                       .oct[5] = 0xffff,
-                                       .oct[6] = (src[x].quad[0] << 8) | 
src[x].quad[1],
-                                       .oct[7] = (src[x].quad[2] << 8) | 
src[x].quad[3],
+                                       .hex[10] = 0xff,
+                                       .hex[11] = 0xff,
+                                       .hex[12] = src[x].quad[0],
+                                       .hex[13] = src[x].quad[1],
+                                       .hex[14] = src[x].quad[2],
+                                       .hex[15] = src[x].quad[3],
                                };
                        }
                }
@@ -1080,9 +1101,12 @@ convert_inet4_inet6(const inet4 *src, in
                                nils++;
                        } else {
                                dst[i] = (inet6) {
-                                       .oct[5] = 0xffff,
-                                       .oct[6] = (src[x].quad[0] << 8) | 
src[x].quad[1],
-                                       .oct[7] = (src[x].quad[2] << 8) | 
src[x].quad[3],
+                                       .hex[10] = 0xff,
+                                       .hex[11] = 0xff,
+                                       .hex[12] = src[x].quad[0],
+                                       .hex[13] = src[x].quad[1],
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to