]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StdLib/BsdSocketLib/ns_name.c
edk2: Remove AppPkg, StdLib, StdLibPrivateInternalFiles
[mirror_edk2.git] / StdLib / BsdSocketLib / ns_name.c
diff --git a/StdLib/BsdSocketLib/ns_name.c b/StdLib/BsdSocketLib/ns_name.c
deleted file mode 100644 (file)
index 3b8706a..0000000
+++ /dev/null
@@ -1,629 +0,0 @@
-/*\r
- * Copyright (c) 1996 by Internet Software Consortium.\r
- *\r
- * Permission to use, copy, modify, and distribute this software for any\r
- * purpose with or without fee is hereby granted, provided that the above\r
- * copyright notice and this permission notice appear in all copies.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r
- * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r
- * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
- * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
- * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
- * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
- * SOFTWARE.\r
- */\r
-\r
-/*\r
- * Portions copyright (c) 1999, 2000\r
- * Intel Corporation.\r
- * All rights reserved.\r
- * \r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * \r
- * 1. Redistributions of source code must retain the above copyright\r
- *    notice, this list of conditions and the following disclaimer.\r
- * \r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- *    notice, this list of conditions and the following disclaimer in the\r
- *    documentation and/or other materials provided with the distribution.\r
- * \r
- * 3. All advertising materials mentioning features or use of this software\r
- *    must display the following acknowledgement:\r
- * \r
- *    This product includes software developed by Intel Corporation and\r
- *    its contributors.\r
- * \r
- * 4. Neither the name of Intel Corporation or its contributors may be\r
- *    used to endorse or promote products derived from this software\r
- *    without specific prior written permission.\r
- * \r
- * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED.  IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\r
- * THE POSSIBILITY OF SUCH DAMAGE.\r
- * \r
- */\r
-\r
-#include <sys/types.h>\r
-\r
-#include <netinet/in.h>\r
-#include <arpa/nameser.h>\r
-\r
-#include <errno.h>\r
-#include <resolv.h>\r
-#include <string.h>\r
-\r
-/* Data. */\r
-\r
-static char            digits[] = "0123456789";\r
-\r
-/* Forward. */\r
-\r
-static int             special(int);\r
-static int             printable(int);\r
-static int             dn_find(const u_char *, const u_char *,\r
-                               const u_char * const *,\r
-                               const u_char * const *);\r
-\r
-/* Public. */\r
-\r
-/*\r
- * ns_name_ntop(src, dst, dstsiz)\r
- *     Convert an encoded domain name to printable ascii as per RFC1035.\r
- * return:\r
- *     Number of bytes written to buffer, or -1 (with errno set)\r
- * notes:\r
- *     The root is returned as "."\r
- *     All other domains are returned in non absolute form\r
- */\r
-int\r
-ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {\r
-       const u_char *cp;\r
-       char *dn, *eom;\r
-       u_char c;\r
-       u_int n;\r
-\r
-       cp = src;\r
-       dn = dst;\r
-       eom = dst + dstsiz;\r
-\r
-       while ((n = *cp++) != 0) {\r
-               if ((n & NS_CMPRSFLGS) != 0) {\r
-                       /* Some kind of compression pointer. */\r
-                       errno = EMSGSIZE;\r
-                       return (-1);\r
-               }\r
-               if (dn != dst) {\r
-                       if (dn >= eom) {\r
-                               errno = EMSGSIZE;\r
-                               return (-1);\r
-                       }\r
-                       *dn++ = '.';\r
-               }\r
-               if (dn + n >= eom) {\r
-                       errno = EMSGSIZE;\r
-                       return (-1);\r
-               }\r
-               for ((void)NULL; n > 0; n--) {\r
-                       c = *cp++;\r
-                       if (special(c)) {\r
-                               if (dn + 1 >= eom) {\r
-                                       errno = EMSGSIZE;\r
-                                       return (-1);\r
-                               }\r
-                               *dn++ = '\\';\r
-                               *dn++ = (char)c;\r
-                       } else if (!printable(c)) {\r
-                               if (dn + 3 >= eom) {\r
-                                       errno = EMSGSIZE;\r
-                                       return (-1);\r
-                               }\r
-                               *dn++ = '\\';\r
-                               *dn++ = digits[c / 100];\r
-                               *dn++ = digits[(c % 100) / 10];\r
-                               *dn++ = digits[c % 10];\r
-                       } else {\r
-                               if (dn >= eom) {\r
-                                       errno = EMSGSIZE;\r
-                                       return (-1);\r
-                               }\r
-                               *dn++ = (char)c;\r
-                       }\r
-               }\r
-       }\r
-       if (dn == dst) {\r
-               if (dn >= eom) {\r
-                       errno = EMSGSIZE;\r
-                       return (-1);\r
-               }\r
-               *dn++ = '.';\r
-       }\r
-       if (dn >= eom) {\r
-               errno = EMSGSIZE;\r
-               return (-1);\r
-       }\r
-       *dn++ = '\0';\r
-       return ((int)(dn - dst));\r
-}\r
-\r
-/*\r
- * ns_name_pton(src, dst, dstsiz)\r
- *     Convert a ascii string into an encoded domain name as per RFC1035.\r
- * return:\r
- *     -1 if it fails\r
- *     1 if string was fully qualified\r
- *     0 is string was not fully qualified\r
- * notes:\r
- *     Enforces label and domain length limits.\r
- */\r
-\r
-int\r
-ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {\r
-       u_char *label, *bp, *eom;\r
-       int c, n, escaped;\r
-       char *cp;\r
-\r
-       escaped = 0;\r
-       bp = dst;\r
-       eom = dst + dstsiz;\r
-       label = bp++;\r
-\r
-       while ((c = *src++) != 0) {\r
-               if (escaped) {\r
-                       if ((cp = strchr(digits, c)) != NULL) {\r
-                               n = (int)(cp - digits) * 100;\r
-                               if ((c = *src++) == 0 ||\r
-                                   (cp = strchr(digits, c)) == NULL) {\r
-                                       errno = EMSGSIZE;\r
-                                       return (-1);\r
-                               }\r
-                               n += (int)(cp - digits) * 10;\r
-                               if ((c = *src++) == 0 ||\r
-                                   (cp = strchr(digits, c)) == NULL) {\r
-                                       errno = EMSGSIZE;\r
-                                       return (-1);\r
-                               }\r
-                               n += (int)(cp - digits);\r
-                               if (n > 255) {\r
-                                       errno = EMSGSIZE;\r
-                                       return (-1);\r
-                               }\r
-                               c = n;\r
-                       }\r
-                       escaped = 0;\r
-               } else if (c == '\\') {\r
-                       escaped = 1;\r
-                       continue;\r
-               } else if (c == '.') {\r
-                       c = ((int)(bp - label) - 1);\r
-                       if ((c & NS_CMPRSFLGS) != 0) {  /* Label too big. */\r
-                               errno = EMSGSIZE;\r
-                               return (-1);\r
-                       }\r
-                       if (label >= eom) {\r
-                               errno = EMSGSIZE;\r
-                               return (-1);\r
-                       }\r
-                       *label = (u_char)c;\r
-                       /* Fully qualified ? */\r
-                       if (*src == '\0') {\r
-                               if (c != 0) {\r
-                                       if (bp >= eom) {\r
-                                               errno = EMSGSIZE;\r
-                                               return (-1);\r
-                                       }\r
-                                       *bp++ = '\0';\r
-                               }\r
-                               if ((bp - dst) > MAXCDNAME) {\r
-                                       errno = EMSGSIZE;\r
-                                       return (-1);\r
-                               }\r
-                               return (1);\r
-                       }\r
-                       if (c == 0) {\r
-                               errno = EMSGSIZE;\r
-                               return (-1);\r
-                       }\r
-                       label = bp++;\r
-                       continue;\r
-               }\r
-               if (bp >= eom) {\r
-                       errno = EMSGSIZE;\r
-                       return (-1);\r
-               }\r
-               *bp++ = (u_char)c;\r
-       }\r
-       c = ((int)(bp - label) - 1);\r
-       if ((c & NS_CMPRSFLGS) != 0) {          /* Label too big. */\r
-               errno = EMSGSIZE;\r
-               return (-1);\r
-       }\r
-       if (label >= eom) {\r
-               errno = EMSGSIZE;\r
-               return (-1);\r
-       }\r
-       *label = (u_char)c;\r
-       if (c != 0) {\r
-               if (bp >= eom) {\r
-                       errno = EMSGSIZE;\r
-                       return (-1);\r
-               }\r
-               *bp++ = 0;\r
-       }\r
-       if ((bp - dst) > MAXCDNAME) {   /* src too big */\r
-               errno = EMSGSIZE;\r
-               return (-1);\r
-       }\r
-       return (0);\r
-}\r
-\r
-/*\r
- * ns_name_unpack(msg, eom, src, dst, dstsiz)\r
- *     Unpack a domain name from a message, source may be compressed.\r
- * return:\r
- *     -1 if it fails, or consumed octets if it succeeds.\r
- */\r
-int\r
-ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,\r
-              u_char *dst, size_t dstsiz)\r
-{\r
-       const u_char *srcp, *dstlim;\r
-       u_char *dstp;\r
-       int n, len, checked;\r
-\r
-       len = -1;\r
-       checked = 0;\r
-       dstp = dst;\r
-       srcp = src;\r
-       dstlim = dst + dstsiz;\r
-       if (srcp < msg || srcp >= eom) {\r
-               errno = EMSGSIZE;\r
-               return (-1);\r
-       }\r
-       /* Fetch next label in domain name. */\r
-       while ((n = *srcp++) != 0) {\r
-               /* Check for indirection. */\r
-               switch (n & NS_CMPRSFLGS) {\r
-               case 0:\r
-                       /* Limit checks. */\r
-                       if (dstp + n + 1 >= dstlim || srcp + n >= eom) {\r
-                               errno = EMSGSIZE;\r
-                               return (-1);\r
-                       }\r
-                       checked += n + 1;\r
-                       *dstp++ = (u_char)n;\r
-                       memcpy(dstp, srcp, n);\r
-                       dstp += n;\r
-                       srcp += n;\r
-                       break;\r
-\r
-               case NS_CMPRSFLGS:\r
-                       if (srcp >= eom) {\r
-                               errno = EMSGSIZE;\r
-                               return (-1);\r
-                       }\r
-                       if (len < 0)\r
-                               len = (int)(srcp - src) + 1;\r
-                       srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));\r
-                       if (srcp < msg || srcp >= eom) {  /* Out of range. */\r
-                               errno = EMSGSIZE;\r
-                               return (-1);\r
-                       }\r
-                       checked += 2;\r
-                       /*\r
-                        * Check for loops in the compressed name;\r
-                        * if we've looked at the whole message,\r
-                        * there must be a loop.\r
-                        */\r
-                       if (checked >= eom - msg) {\r
-                               errno = EMSGSIZE;\r
-                               return (-1);\r
-                       }\r
-                       break;\r
-\r
-               default:\r
-                       errno = EMSGSIZE;\r
-                       return (-1);                    /* flag error */\r
-               }\r
-       }\r
-       *dstp = '\0';\r
-       if (len < 0)\r
-               len = (int)(srcp - src);\r
-       return (len);\r
-}\r
-\r
-/*\r
- * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)\r
- *     Pack domain name 'domain' into 'comp_dn'.\r
- * return:\r
- *     Size of the compressed name, or -1.\r
- * notes:\r
- *     'dnptrs' is an array of pointers to previous compressed names.\r
- *     dnptrs[0] is a pointer to the beginning of the message. The array\r
- *     ends with NULL.\r
- *     'lastdnptr' is a pointer to the end of the array pointed to\r
- *     by 'dnptrs'.\r
- * Side effects:\r
- *     The list of pointers in dnptrs is updated for labels inserted into\r
- *     the message as we compress the name.  If 'dnptr' is NULL, we don't\r
- *     try to compress names. If 'lastdnptr' is NULL, we don't update the\r
- *     list.\r
- */\r
-int\r
-ns_name_pack(const u_char *src, u_char *dst, int dstsiz,\r
-            const u_char **dnptrs, const u_char **lastdnptr)\r
-{\r
-       u_char *dstp;\r
-       const u_char **cpp, **lpp, *eob, *msg;\r
-       const u_char *srcp;\r
-       int n, l;\r
-\r
-       srcp = src;\r
-       dstp = dst;\r
-       eob = dstp + dstsiz;\r
-       lpp = cpp = NULL;\r
-       if (dnptrs != NULL) {\r
-               if ((msg = *dnptrs++) != NULL) {\r
-                       for (cpp = dnptrs; *cpp != NULL; cpp++)\r
-                               (void)NULL;\r
-                       lpp = cpp;      /* end of list to search */\r
-               }\r
-       } else\r
-               msg = NULL;\r
-\r
-       /* make sure the domain we are about to add is legal */\r
-       l = 0;\r
-       do {\r
-               n = *srcp;\r
-               if ((n & NS_CMPRSFLGS) != 0) {\r
-                       errno = EMSGSIZE;\r
-                       return (-1);\r
-               }\r
-               l += n + 1;\r
-               if (l > MAXCDNAME) {\r
-                       errno = EMSGSIZE;\r
-                       return (-1);\r
-               }\r
-               srcp += n + 1;\r
-       } while (n != 0);\r
-\r
-       srcp = src;\r
-       do {\r
-               /* Look to see if we can use pointers. */\r
-               n = *srcp;\r
-               if (n != 0 && msg != NULL) {\r
-                       l = dn_find(srcp, msg, (const u_char * const *)dnptrs,\r
-                                   (const u_char * const *)lpp);\r
-                       if (l >= 0) {\r
-                               if (dstp + 1 >= eob) {\r
-                                       errno = EMSGSIZE;\r
-                                       return (-1);\r
-                               }\r
-                               *dstp++ = (u_char)((l >> 8) | NS_CMPRSFLGS );\r
-                               *dstp++ = (u_char)( l % 256 );\r
-                               return ((int)(dstp - dst));\r
-                       }\r
-                       /* Not found, save it. */\r
-                       if (lastdnptr != NULL && cpp < lastdnptr - 1 &&\r
-                           (dstp - msg) < 0x4000) {\r
-                               *cpp++ = dstp;\r
-                               *cpp = NULL;\r
-                       }\r
-               }\r
-               /* copy label to buffer */\r
-               if (n & NS_CMPRSFLGS) {         /* Should not happen. */\r
-                       errno = EMSGSIZE;\r
-                       return (-1);\r
-               }\r
-               if (dstp + 1 + n >= eob) {\r
-                       errno = EMSGSIZE;\r
-                       return (-1);\r
-               }\r
-               memcpy(dstp, srcp, n + 1);\r
-               srcp += n + 1;\r
-               dstp += n + 1;\r
-       } while (n != 0);\r
-\r
-       if (dstp > eob) {\r
-               if (msg != NULL)\r
-                       *lpp = NULL;\r
-               errno = EMSGSIZE;\r
-               return (-1);\r
-       } \r
-       return ((int)(dstp - dst));\r
-}\r
-\r
-/*\r
- * ns_name_uncompress(msg, eom, src, dst, dstsiz)\r
- *     Expand compressed domain name to presentation format.\r
- * return:\r
- *     Number of bytes read out of `src', or -1 (with errno set).\r
- * note:\r
- *     Root domain returns as "." not "".\r
- */\r
-int\r
-ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,\r
-                  char *dst, size_t dstsiz)\r
-{\r
-       u_char tmp[NS_MAXCDNAME];\r
-       int n;\r
-       \r
-       if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)\r
-               return (-1);\r
-       if (ns_name_ntop(tmp, dst, dstsiz) == -1)\r
-               return (-1);\r
-       return (n);\r
-}\r
-\r
-/*\r
- * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)\r
- *     Compress a domain name into wire format, using compression pointers.\r
- * return:\r
- *     Number of bytes consumed in `dst' or -1 (with errno set).\r
- * notes:\r
- *     'dnptrs' is an array of pointers to previous compressed names.\r
- *     dnptrs[0] is a pointer to the beginning of the message.\r
- *     The list ends with NULL.  'lastdnptr' is a pointer to the end of the\r
- *     array pointed to by 'dnptrs'. Side effect is to update the list of\r
- *     pointers for labels inserted into the message as we compress the name.\r
- *     If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'\r
- *     is NULL, we don't update the list.\r
- */\r
-int\r
-ns_name_compress(const char *src, u_char *dst, size_t dstsiz,\r
-                const u_char **dnptrs, const u_char **lastdnptr)\r
-{\r
-       u_char tmp[NS_MAXCDNAME];\r
-\r
-       if (ns_name_pton(src, tmp, sizeof tmp) == -1)\r
-               return (-1);\r
-       return (ns_name_pack(tmp, dst, (int)dstsiz, dnptrs, lastdnptr));\r
-}\r
-\r
-/*\r
- * ns_name_skip(ptrptr, eom)\r
- *     Advance *ptrptr to skip over the compressed name it points at.\r
- * return:\r
- *     0 on success, -1 (with errno set) on failure.\r
- */\r
-int\r
-ns_name_skip(const u_char **ptrptr, const u_char *eom) {\r
-       const u_char *cp;\r
-       u_int n;\r
-\r
-       cp = *ptrptr;\r
-       while (cp < eom && (n = *cp++) != 0) {\r
-               /* Check for indirection. */\r
-               switch (n & NS_CMPRSFLGS) {\r
-               case 0:                 /* normal case, n == len */\r
-                       cp += n;\r
-                       continue;\r
-               case NS_CMPRSFLGS:      /* indirection */\r
-                       cp++;\r
-                       break;\r
-               default:                /* illegal type */\r
-                       errno = EMSGSIZE;\r
-                       return (-1);\r
-               }\r
-               break;\r
-       }\r
-       if (cp > eom) {\r
-               errno = EMSGSIZE;\r
-               return (-1);\r
-       }\r
-       *ptrptr = cp;\r
-       return (0);\r
-}\r
-\r
-/* Private. */\r
-\r
-/*\r
- * special(ch)\r
- *     Thinking in noninternationalized USASCII (per the DNS spec),\r
- *     is this characted special ("in need of quoting") ?\r
- * return:\r
- *     boolean.\r
- */\r
-static int\r
-special(int ch) {\r
-       switch (ch) {\r
-       case 0x22: /* '"' */\r
-       case 0x2E: /* '.' */\r
-       case 0x3B: /* ';' */\r
-       case 0x5C: /* '\\' */\r
-       /* Special modifiers in zone files. */\r
-       case 0x40: /* '@' */\r
-       case 0x24: /* '$' */\r
-               return (1);\r
-       default:\r
-               return (0);\r
-       }\r
-}\r
-\r
-/*\r
- * printable(ch)\r
- *     Thinking in noninternationalized USASCII (per the DNS spec),\r
- *     is this character visible and not a space when printed ?\r
- * return:\r
- *     boolean.\r
- */\r
-static int\r
-printable(int ch) {\r
-       return (ch > 0x20 && ch < 0x7f);\r
-}\r
-\r
-/*\r
- *     Thinking in noninternationalized USASCII (per the DNS spec),\r
- *     convert this character to lower case if it's upper case.\r
- */\r
-static int\r
-mklower(int ch) {\r
-       if (ch >= 0x41 && ch <= 0x5A)\r
-               return (ch + 0x20);\r
-       return (ch);\r
-}\r
-\r
-/*\r
- * dn_find(domain, msg, dnptrs, lastdnptr)\r
- *     Search for the counted-label name in an array of compressed names.\r
- * return:\r
- *     offset from msg if found, or -1.\r
- * notes:\r
- *     dnptrs is the pointer to the first name on the list,\r
- *     not the pointer to the start of the message.\r
- */\r
-static int\r
-dn_find(const u_char *domain, const u_char *msg,\r
-       const u_char * const *dnptrs,\r
-       const u_char * const *lastdnptr)\r
-{\r
-       const u_char *dn, *cp, *sp;\r
-       const u_char * const *cpp;\r
-       u_int n;\r
-\r
-       for (cpp = dnptrs; cpp < lastdnptr; cpp++) {\r
-               dn = domain;\r
-               sp = cp = *cpp;\r
-               while ((n = *cp++) != 0) {\r
-                       /*\r
-                        * check for indirection\r
-                        */\r
-                       switch (n & NS_CMPRSFLGS) {\r
-                       case 0:                 /* normal case, n == len */\r
-                               if (n != *dn++)\r
-                                       goto next;\r
-                               for ((void)NULL; n > 0; n--)\r
-                                       if (mklower(*dn++) != mklower(*cp++))\r
-                                               goto next;\r
-                               /* Is next root for both ? */\r
-                               if (*dn == '\0' && *cp == '\0')\r
-                                       return ((int)(sp - msg));\r
-                               if (*dn)\r
-                                       continue;\r
-                               goto next;\r
-\r
-                       case NS_CMPRSFLGS:      /* indirection */\r
-                               cp = msg + (((n & 0x3f) << 8) | *cp);\r
-                               break;\r
-\r
-                       default:        /* illegal type */\r
-                               errno = EMSGSIZE;\r
-                               return (-1);\r
-                       }\r
-               }\r
- next: ;\r
-       }\r
-       errno = ENOENT;\r
-       return (-1);\r
-}\r