You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/03/14 09:30:35 UTC

[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5737: Add dn resolution function

pkarashchenko commented on a change in pull request #5737:
URL: https://github.com/apache/incubator-nuttx/pull/5737#discussion_r825722755



##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)

Review comment:
       ```suggestion
         if (*s == 0)
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)

Review comment:
       ```suggestion
         while ((*s & 0xc0) != 0)
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)

Review comment:
       why do we need `1u` and not `1`?

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)

Review comment:
       ```suggestion
     if (noff == 0)
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))
+        {
+          return m;
+        }
+
+      *offset = o;
+      m += l;
+      if (nlen)
+        {
+          m++;
+        }
+
+      if (!nlen || !noff)
+        {
+          return m;
+        }
+
+      end--;
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int dn_comp(FAR const char *src, FAR unsigned char *dst, int space,
+            FAR unsigned char **dnptrs, FAR unsigned char **lastdnptr)
+{
+  int i;
+  int j;
+  int n;
+  int m = 0;
+  int offset = 0;
+  int bestlen = 0;
+  int bestoff = 0;
+  unsigned char lens[127];
+  FAR unsigned char **p;
+  FAR const char *end;
+  size_t l = strnlen(src, 255);
+
+  if (l && src[l - 1] == '.')
+    {
+      l--;
+    }
+
+  if (l > 253 || space <= 0)
+    {
+      return -1;
+    }
+
+  if (!l)
+    {
+      *dst = 0;
+      return 1;
+    }
+
+  end = src + l;
+  n = getlens(lens, src, l);
+  if (!n)

Review comment:
       ```suggestion
     if (n == 0)
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))
+        {
+          return m;
+        }
+
+      *offset = o;
+      m += l;
+      if (nlen)
+        {
+          m++;
+        }
+
+      if (!nlen || !noff)
+        {
+          return m;
+        }
+
+      end--;
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int dn_comp(FAR const char *src, FAR unsigned char *dst, int space,
+            FAR unsigned char **dnptrs, FAR unsigned char **lastdnptr)
+{
+  int i;
+  int j;
+  int n;
+  int m = 0;
+  int offset = 0;
+  int bestlen = 0;
+  int bestoff = 0;
+  unsigned char lens[127];
+  FAR unsigned char **p;
+  FAR const char *end;
+  size_t l = strnlen(src, 255);
+
+  if (l && src[l - 1] == '.')
+    {
+      l--;
+    }
+
+  if (l > 253 || space <= 0)
+    {
+      return -1;
+    }
+
+  if (!l)
+    {
+      *dst = 0;
+      return 1;
+    }
+
+  end = src + l;
+  n = getlens(lens, src, l);
+  if (!n)
+    {
+      return -1;
+    }
+
+  p = dnptrs;
+  if (p && *p)

Review comment:
       ```suggestion
     if (p != NULL && *p != NULL)
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))
+        {
+          return m;
+        }
+
+      *offset = o;
+      m += l;
+      if (nlen)
+        {
+          m++;
+        }
+
+      if (!nlen || !noff)
+        {
+          return m;
+        }
+
+      end--;
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int dn_comp(FAR const char *src, FAR unsigned char *dst, int space,
+            FAR unsigned char **dnptrs, FAR unsigned char **lastdnptr)
+{
+  int i;
+  int j;
+  int n;
+  int m = 0;
+  int offset = 0;
+  int bestlen = 0;
+  int bestoff = 0;
+  unsigned char lens[127];
+  FAR unsigned char **p;
+  FAR const char *end;
+  size_t l = strnlen(src, 255);
+
+  if (l && src[l - 1] == '.')
+    {
+      l--;
+    }
+
+  if (l > 253 || space <= 0)
+    {
+      return -1;
+    }
+
+  if (!l)
+    {
+      *dst = 0;
+      return 1;
+    }
+
+  end = src + l;
+  n = getlens(lens, src, l);
+  if (!n)
+    {
+      return -1;
+    }
+
+  p = dnptrs;
+  if (p && *p)
+    {
+      for (p++; *p; p++)

Review comment:
       Can we rewrite this and above to
   ```
   if (dnptrs != NULL && *dnptrs != NULL)
     {
       for (p = dnptrs + 1; *p != NULL; p++)
     }
   ```
   ?

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))

Review comment:
       ```suggestion
         if (l != base[o] || (memcmp(base + o + 1, end, l) != 0))
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))
+        {
+          return m;
+        }
+
+      *offset = o;
+      m += l;
+      if (nlen)
+        {
+          m++;
+        }
+
+      if (!nlen || !noff)
+        {
+          return m;
+        }
+
+      end--;
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int dn_comp(FAR const char *src, FAR unsigned char *dst, int space,
+            FAR unsigned char **dnptrs, FAR unsigned char **lastdnptr)
+{
+  int i;
+  int j;
+  int n;
+  int m = 0;
+  int offset = 0;
+  int bestlen = 0;
+  int bestoff = 0;
+  unsigned char lens[127];
+  FAR unsigned char **p;
+  FAR const char *end;
+  size_t l = strnlen(src, 255);
+
+  if (l && src[l - 1] == '.')
+    {
+      l--;
+    }
+
+  if (l > 253 || space <= 0)
+    {
+      return -1;
+    }
+
+  if (!l)
+    {
+      *dst = 0;
+      return 1;
+    }
+
+  end = src + l;
+  n = getlens(lens, src, l);
+  if (!n)
+    {
+      return -1;
+    }
+
+  p = dnptrs;
+  if (p && *p)
+    {
+      for (p++; *p; p++)
+        {
+          m = match(&offset, *dnptrs, *p, end, lens, n);
+          if (m > bestlen)
+            {
+              bestlen = m;
+              bestoff = offset;
+              if (m == l)
+                {
+                  break;
+                }
+            }
+        }
+    }
+
+  /* encode unmatched part */
+
+  if (space < l - bestlen + 2 + (bestlen - 1 < l - 1))
+    {
+      return -1;
+    }
+
+  memcpy(dst + 1, src, l - bestlen);
+  for (i = j = 0; i < l - bestlen; i += lens[j++] + 1)
+    {
+      dst[i] = lens[j];
+    }
+
+  /* add tail */
+
+  if (bestlen)
+    {
+      dst[i++] = 0xc0 | bestoff >> 8;
+      dst[i++] = bestoff;
+    }
+  else
+    {
+      dst[i++] = 0;
+    }
+
+  /* save dst pointer */
+
+  if (i > 2 && lastdnptr && dnptrs && *dnptrs)
+    {
+      while (*p)

Review comment:
       ```suggestion
         while (*p != NULL)
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))
+        {
+          return m;
+        }
+
+      *offset = o;
+      m += l;
+      if (nlen)

Review comment:
       ```suggestion
         if (nlen > 0)
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))
+        {
+          return m;
+        }
+
+      *offset = o;
+      m += l;
+      if (nlen)
+        {
+          m++;
+        }
+
+      if (!nlen || !noff)
+        {
+          return m;
+        }
+
+      end--;
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int dn_comp(FAR const char *src, FAR unsigned char *dst, int space,
+            FAR unsigned char **dnptrs, FAR unsigned char **lastdnptr)
+{
+  int i;
+  int j;
+  int n;
+  int m = 0;
+  int offset = 0;
+  int bestlen = 0;
+  int bestoff = 0;
+  unsigned char lens[127];
+  FAR unsigned char **p;
+  FAR const char *end;
+  size_t l = strnlen(src, 255);
+
+  if (l && src[l - 1] == '.')

Review comment:
       ```suggestion
     if (l > 0 && src[l - 1] == '.')
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))
+        {
+          return m;
+        }
+
+      *offset = o;
+      m += l;
+      if (nlen)
+        {
+          m++;
+        }
+
+      if (!nlen || !noff)
+        {
+          return m;
+        }
+
+      end--;
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int dn_comp(FAR const char *src, FAR unsigned char *dst, int space,
+            FAR unsigned char **dnptrs, FAR unsigned char **lastdnptr)
+{
+  int i;
+  int j;
+  int n;
+  int m = 0;
+  int offset = 0;
+  int bestlen = 0;
+  int bestoff = 0;
+  unsigned char lens[127];
+  FAR unsigned char **p;
+  FAR const char *end;
+  size_t l = strnlen(src, 255);
+
+  if (l && src[l - 1] == '.')
+    {
+      l--;
+    }
+
+  if (l > 253 || space <= 0)
+    {
+      return -1;
+    }
+
+  if (!l)
+    {
+      *dst = 0;
+      return 1;
+    }
+
+  end = src + l;
+  n = getlens(lens, src, l);
+  if (!n)
+    {
+      return -1;
+    }
+
+  p = dnptrs;
+  if (p && *p)
+    {
+      for (p++; *p; p++)
+        {
+          m = match(&offset, *dnptrs, *p, end, lens, n);
+          if (m > bestlen)
+            {
+              bestlen = m;
+              bestoff = offset;
+              if (m == l)
+                {
+                  break;
+                }
+            }
+        }
+    }
+
+  /* encode unmatched part */
+
+  if (space < l - bestlen + 2 + (bestlen - 1 < l - 1))
+    {
+      return -1;
+    }
+
+  memcpy(dst + 1, src, l - bestlen);
+  for (i = j = 0; i < l - bestlen; i += lens[j++] + 1)
+    {
+      dst[i] = lens[j];
+    }
+
+  /* add tail */
+
+  if (bestlen)
+    {
+      dst[i++] = 0xc0 | bestoff >> 8;
+      dst[i++] = bestoff;
+    }
+  else
+    {
+      dst[i++] = 0;
+    }
+
+  /* save dst pointer */
+
+  if (i > 2 && lastdnptr && dnptrs && *dnptrs)

Review comment:
       ```suggestion
     if (i > 2 && lastdnptr != NULL && dnptrs != NULL && *dnptrs != NULL)
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))
+        {
+          return m;
+        }
+
+      *offset = o;
+      m += l;
+      if (nlen)
+        {
+          m++;
+        }
+
+      if (!nlen || !noff)
+        {
+          return m;
+        }
+
+      end--;
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int dn_comp(FAR const char *src, FAR unsigned char *dst, int space,
+            FAR unsigned char **dnptrs, FAR unsigned char **lastdnptr)
+{
+  int i;
+  int j;
+  int n;
+  int m = 0;
+  int offset = 0;
+  int bestlen = 0;
+  int bestoff = 0;
+  unsigned char lens[127];
+  FAR unsigned char **p;
+  FAR const char *end;
+  size_t l = strnlen(src, 255);
+
+  if (l && src[l - 1] == '.')
+    {
+      l--;
+    }
+
+  if (l > 253 || space <= 0)
+    {
+      return -1;
+    }
+
+  if (!l)

Review comment:
       ```suggestion
     if (l == 0)
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))
+        {
+          return m;
+        }
+
+      *offset = o;
+      m += l;
+      if (nlen)
+        {
+          m++;
+        }
+
+      if (!nlen || !noff)

Review comment:
       ```suggestion
         if (nlen == 0 || noff == 0)
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))
+        {
+          return m;
+        }
+
+      *offset = o;
+      m += l;
+      if (nlen)
+        {
+          m++;
+        }
+
+      if (!nlen || !noff)
+        {
+          return m;
+        }
+
+      end--;
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int dn_comp(FAR const char *src, FAR unsigned char *dst, int space,
+            FAR unsigned char **dnptrs, FAR unsigned char **lastdnptr)
+{
+  int i;
+  int j;
+  int n;
+  int m = 0;
+  int offset = 0;
+  int bestlen = 0;
+  int bestoff = 0;
+  unsigned char lens[127];
+  FAR unsigned char **p;
+  FAR const char *end;
+  size_t l = strnlen(src, 255);
+
+  if (l && src[l - 1] == '.')
+    {
+      l--;
+    }
+
+  if (l > 253 || space <= 0)
+    {
+      return -1;
+    }
+
+  if (!l)
+    {
+      *dst = 0;
+      return 1;
+    }
+
+  end = src + l;
+  n = getlens(lens, src, l);
+  if (!n)
+    {
+      return -1;
+    }
+
+  p = dnptrs;
+  if (p && *p)
+    {
+      for (p++; *p; p++)
+        {
+          m = match(&offset, *dnptrs, *p, end, lens, n);
+          if (m > bestlen)
+            {
+              bestlen = m;
+              bestoff = offset;
+              if (m == l)
+                {
+                  break;
+                }
+            }
+        }
+    }
+
+  /* encode unmatched part */
+
+  if (space < l - bestlen + 2 + (bestlen - 1 < l - 1))
+    {
+      return -1;
+    }
+
+  memcpy(dst + 1, src, l - bestlen);
+  for (i = j = 0; i < l - bestlen; i += lens[j++] + 1)
+    {
+      dst[i] = lens[j];
+    }
+
+  /* add tail */
+
+  if (bestlen)
+    {
+      dst[i++] = 0xc0 | bestoff >> 8;
+      dst[i++] = bestoff;
+    }
+  else
+    {
+      dst[i++] = 0;
+    }
+
+  /* save dst pointer */
+
+  if (i > 2 && lastdnptr && dnptrs && *dnptrs)
+    {
+      while (*p)
+        {
+          p++;
+        }
+
+      if (p + 1 < lastdnptr)
+        {
+          *p++ = dst;
+          *p = 0;
+        }
+    }
+
+  return i;
+}
+
+int dn_expand(FAR const unsigned char *base, FAR const unsigned char *end,
+              FAR const unsigned char *src, FAR char *dest, int space)
+{
+  FAR const unsigned char *p = src;
+  FAR char *dend;
+  FAR char *dbegin = dest;
+  int len = -1;
+  int i;
+  int j;
+
+  if (p == end || space <= 0)
+    {
+      return -1;
+    }
+
+  dend = dest + (space > 254 ? 254 : space);
+
+  /* detect reference loop using an iteration counter */
+
+  for (i = 0; i < end - base; i += 2)
+    {
+      /* loop invariants: p<end, dest<dend */
+
+      if (*p & 0xc0)

Review comment:
       ```suggestion
         if ((*p & 0xc0) != 0)
   ```

##########
File path: libs/libc/netdb/lib_dn.c
##########
@@ -0,0 +1,315 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dn.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <string.h>
+#include <resolv.h>
+
+/* RFC 1035 message compression */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* label start offsets of a compressed domain name s */
+
+static int getoffs(FAR short *offs, FAR const unsigned char *base,
+                   FAR const unsigned char *s)
+{
+  int i = 0;
+
+  for (; ; )
+    {
+      while (*s & 0xc0)
+        {
+          if ((*s & 0xc0) != 0xc0)
+            {
+              return 0;
+            }
+
+          s = base + ((s[0] & 0x3f) << 8 | s[1]);
+        }
+
+      if (!*s)
+        {
+          return i;
+        }
+
+      if (s - base >= 0x4000)
+        {
+          return 0;
+        }
+
+      offs[i++] = s - base;
+      s += *s + 1;
+    }
+}
+
+/* label lengths of an ascii domain name s */
+
+static int getlens(FAR unsigned char *lens, FAR const char *s, int l)
+{
+  int i = 0;
+  int j = 0;
+  int k = 0;
+
+  for (; ; )
+    {
+      for (; j < l && s[j] != '.'; j++);
+      if (j - k - 1u > 62)
+        {
+          return 0;
+        }
+
+      lens[i++] = j - k;
+      if (j == l)
+        {
+          return i;
+        }
+
+      k = ++j;
+    }
+}
+
+/* longest suffix match of an ascii domain with a compressed domain name dn */
+
+static int match(FAR int *offset, FAR const unsigned char *base,
+                 FAR const unsigned char *dn, FAR const char *end,
+                 FAR const unsigned char *lens, int nlen)
+{
+  int l;
+  int o;
+  int m = 0;
+  short offs[128];
+  int noff = getoffs(offs, base, dn);
+
+  if (!noff)
+    {
+      return 0;
+    }
+
+  for (; ; )
+    {
+      l = lens[--nlen];
+      o = offs[--noff];
+      end -= l;
+      if (l != base[o] || memcmp(base + o + 1, end, l))
+        {
+          return m;
+        }
+
+      *offset = o;
+      m += l;
+      if (nlen)
+        {
+          m++;
+        }
+
+      if (!nlen || !noff)
+        {
+          return m;
+        }
+
+      end--;
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int dn_comp(FAR const char *src, FAR unsigned char *dst, int space,
+            FAR unsigned char **dnptrs, FAR unsigned char **lastdnptr)
+{
+  int i;
+  int j;
+  int n;
+  int m = 0;
+  int offset = 0;
+  int bestlen = 0;
+  int bestoff = 0;
+  unsigned char lens[127];
+  FAR unsigned char **p;
+  FAR const char *end;
+  size_t l = strnlen(src, 255);
+
+  if (l && src[l - 1] == '.')
+    {
+      l--;
+    }
+
+  if (l > 253 || space <= 0)
+    {
+      return -1;
+    }
+
+  if (!l)
+    {
+      *dst = 0;
+      return 1;
+    }
+
+  end = src + l;
+  n = getlens(lens, src, l);
+  if (!n)
+    {
+      return -1;
+    }
+
+  p = dnptrs;
+  if (p && *p)
+    {
+      for (p++; *p; p++)
+        {
+          m = match(&offset, *dnptrs, *p, end, lens, n);
+          if (m > bestlen)
+            {
+              bestlen = m;
+              bestoff = offset;
+              if (m == l)
+                {
+                  break;
+                }
+            }
+        }
+    }
+
+  /* encode unmatched part */
+
+  if (space < l - bestlen + 2 + (bestlen - 1 < l - 1))
+    {
+      return -1;
+    }
+
+  memcpy(dst + 1, src, l - bestlen);
+  for (i = j = 0; i < l - bestlen; i += lens[j++] + 1)
+    {
+      dst[i] = lens[j];
+    }
+
+  /* add tail */
+
+  if (bestlen)

Review comment:
       ```suggestion
     if (bestlen > 0)
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org