(root)/
glibc-2.38/
resolv/
ns_name_pack.c
       1  /* Compression of DNS domain names.
       2   * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
       3   * Copyright (c) 1996,1999 by Internet Software Consortium.
       4   *
       5   * Permission to use, copy, modify, and distribute this software for any
       6   * purpose with or without fee is hereby granted, provided that the above
       7   * copyright notice and this permission notice appear in all copies.
       8   *
       9   * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
      10   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      11   * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
      12   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
      13   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
      14   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
      15   * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      16   */
      17  
      18  #include <arpa/nameser.h>
      19  #include <errno.h>
      20  #include <string.h>
      21  #include <shlib-compat.h>
      22  
      23  /* Thinking in noninternationalized USASCII (per the DNS spec),
      24     convert this character to lower case if it's upper case.  */
      25  static int
      26  mklower (int ch)
      27  {
      28    if (ch >= 'A' && ch <= 'Z')
      29      return ch - 'A' + 'a';
      30    return ch;
      31  }
      32  
      33  /* Search for the counted-label name in an array of compressed names.
      34     Returns the offset from MSG if found, or -1.
      35  
      36     DNPTRS is the pointer to the first name on the list, not the
      37     pointer to the start of the message.  */
      38  static int
      39  dn_find (const unsigned char *domain, const unsigned char *msg,
      40           const unsigned char **dnptrs,
      41           const unsigned char **lastdnptr)
      42  {
      43    const unsigned char *dn, *cp, *sp;
      44    const unsigned char **cpp;
      45    unsigned int n;
      46  
      47    for (cpp = dnptrs; cpp < lastdnptr; cpp++)
      48      {
      49      sp = *cpp;
      50      /* Terminate search on: root label, compression pointer, unusable
      51         offset.  */
      52      while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 && (sp - msg) < 0x4000)
      53        {
      54          dn = domain;
      55          cp = sp;
      56          while ((n = *cp++) != 0)
      57            {
      58              /* Check for indirection.  */
      59              switch (n & NS_CMPRSFLGS)
      60                {
      61                case 0:                 /* Normal case, n == len.  */
      62                  if (n != *dn++)
      63                    goto next;
      64  
      65                  for (; n > 0; n--)
      66                    if (mklower (*dn++) != mklower (*cp++))
      67                      goto next;
      68                  /* Is next root for both?  */
      69                  if (*dn == '\0' && *cp == '\0')
      70                    return sp - msg;
      71                  if (*dn)
      72                    continue;
      73                  goto next;
      74                case NS_CMPRSFLGS: /* Indirection.  */
      75                  cp = msg + (((n & 0x3f) << 8) | *cp);
      76                  break;
      77  
      78                default:          /* Illegal type.  */
      79                  __set_errno (EMSGSIZE);
      80                  return -1;
      81                }
      82            }
      83        next: ;
      84          sp += *sp + 1;
      85        }
      86      }
      87    __set_errno (ENOENT);
      88    return -1;
      89  }
      90  
      91  /* Packs domain name SRC into DST.  Returns size of the compressed
      92     name, or -1.
      93  
      94     DNPTRS is an array of pointers to previous compressed names.
      95     DNPTRS[0] is a pointer to the beginning of the message. The array
      96     ends with NULL.  LASTDNPTR is a pointer to the end of the array
      97     pointed to by 'dnptrs'.
      98  
      99     The list of pointers in DNPTRS is updated for labels inserted into
     100     the message as we compress the name.  If DNPTRS is NULL, we don't
     101     try to compress names. If LASTDNPTR is NULL, we don't update the
     102     list.  */
     103  int
     104  ___ns_name_pack (const unsigned char *src, unsigned char *dst, int dstsiz,
     105                   const unsigned char **dnptrs, const unsigned char **lastdnptr)
     106  {
     107    unsigned char *dstp;
     108    const unsigned char **cpp, **lpp, *eob, *msg;
     109    const unsigned char *srcp;
     110    int n, l, first = 1;
     111  
     112    srcp = src;
     113    dstp = dst;
     114    eob = dstp + dstsiz;
     115    lpp = cpp = NULL;
     116    if (dnptrs != NULL)
     117      {
     118        if ((msg = *dnptrs++) != NULL)
     119          {
     120            for (cpp = dnptrs; *cpp != NULL; cpp++)
     121              ;
     122            lpp = cpp;            /* End of list to search.  */
     123          }
     124      }
     125    else
     126      msg = NULL;
     127  
     128    /* Make sure the domain we are about to add is legal.  */
     129    l = 0;
     130    do
     131      {
     132        n = *srcp;
     133        if (n >= 64)
     134          {
     135            __set_errno (EMSGSIZE);
     136            return -1;
     137          }
     138        l += n + 1;
     139        if (l > MAXCDNAME)
     140          {
     141            __set_errno (EMSGSIZE);
     142            return -1;
     143          }
     144        srcp += n + 1;
     145      }
     146    while (n != 0);
     147  
     148    /* from here on we need to reset compression pointer array on error */
     149    srcp = src;
     150    do
     151      {
     152        /* Look to see if we can use pointers.  */
     153        n = *srcp;
     154        if (n != 0 && msg != NULL)
     155          {
     156            l = dn_find (srcp, msg, dnptrs, lpp);
     157            if (l >= 0)
     158              {
     159                if (eob - dstp <= 1)
     160                  goto cleanup;
     161                *dstp++ = (l >> 8) | NS_CMPRSFLGS;
     162                *dstp++ = l % 256;
     163                return dstp - dst;
     164              }
     165            /* Not found, save it.  */
     166            if (lastdnptr != NULL && cpp < lastdnptr - 1
     167                && (dstp - msg) < 0x4000 && first)
     168              {
     169                *cpp++ = dstp;
     170                *cpp = NULL;
     171                first = 0;
     172              }
     173          }
     174        /* Copy label to buffer.  */
     175        if (n >= 64)
     176          /* Should not happen.  */
     177          goto cleanup;
     178        if (n + 1 > eob - dstp)
     179          goto cleanup;
     180        memcpy (dstp, srcp, n + 1);
     181        srcp += n + 1;
     182        dstp += n + 1;
     183      }
     184    while (n != 0);
     185  
     186    if (dstp > eob)
     187      {
     188      cleanup:
     189        if (msg != NULL)
     190          *lpp = NULL;
     191        __set_errno (EMSGSIZE);
     192        return -1;
     193      }
     194    return dstp - dst;
     195  }
     196  versioned_symbol (libc, ___ns_name_pack, ns_name_pack, GLIBC_2_34);
     197  versioned_symbol (libc, ___ns_name_pack, __ns_name_pack, GLIBC_PRIVATE);
     198  libc_hidden_ver (___ns_name_pack, __ns_name_pack)
     199  
     200  #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_9, GLIBC_2_34)
     201  compat_symbol (libresolv, ___ns_name_pack, ns_name_pack, GLIBC_2_9);
     202  #endif