LCOV - code coverage report
Current view: top level - home/jas/src/libidn2 - register.c (source / functions) Hit Total Coverage
Test: libidn2 Lines: 46 71 64.8 %
Date: 2014-06-25 Functions: 1 2 50.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 33 54 61.1 %

           Branch data     Line data    Source code
       1                 :            : /* register.c - implementation of IDNA2008 register functions
       2                 :            :    Copyright (C) 2011-2014 Simon Josefsson
       3                 :            : 
       4                 :            :    Libidn2 is free software: you can redistribute it and/or modify it
       5                 :            :    under the terms of either:
       6                 :            : 
       7                 :            :      * the GNU Lesser General Public License as published by the Free
       8                 :            :        Software Foundation; either version 3 of the License, or (at
       9                 :            :        your option) any later version.
      10                 :            : 
      11                 :            :    or
      12                 :            : 
      13                 :            :      * the GNU General Public License as published by the Free
      14                 :            :        Software Foundation; either version 2 of the License, or (at
      15                 :            :        your option) any later version.
      16                 :            : 
      17                 :            :    or both in parallel, as here.
      18                 :            : 
      19                 :            :    This program is distributed in the hope that it will be useful,
      20                 :            :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      21                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      22                 :            :    GNU General Public License for more details.
      23                 :            : 
      24                 :            :    You should have received copies of the GNU General Public License and
      25                 :            :    the GNU Lesser General Public License along with this program.  If
      26                 :            :    not, see <http://www.gnu.org/licenses/>.
      27                 :            : */
      28                 :            : 
      29                 :            : #include <config.h>
      30                 :            : 
      31                 :            : #include "idn2.h"
      32                 :            : 
      33                 :            : #include <errno.h>                /* errno */
      34                 :            : #include <stdlib.h>               /* free */
      35                 :            : 
      36                 :            : #include "punycode.h"
      37                 :            : 
      38                 :            : #include "uniconv.h"          /* u8_strconv_from_locale */
      39                 :            : #include "unistr.h"           /* u32_to_u8 */
      40                 :            : 
      41                 :            : #include "idna.h"             /* _idn2_label_test */
      42                 :            : 
      43                 :            : /**
      44                 :            :  * idn2_register_u8:
      45                 :            :  * @ulabel: input zero-terminated UTF-8 and Unicode NFC string, or NULL.
      46                 :            :  * @alabel: input zero-terminated ACE encoded string (xn--), or NULL.
      47                 :            :  * @insertname: newly allocated output variable with name to register in DNS.
      48                 :            :  * @flags: optional #idn2_flags to modify behaviour.
      49                 :            :  *
      50                 :            :  * Perform IDNA2008 register string conversion on domain label @ulabel
      51                 :            :  * and @alabel, as described in section 4 of RFC 5891.  Note that the
      52                 :            :  * input @ulabel must be encoded in UTF-8 and be in Unicode NFC form.
      53                 :            :  *
      54                 :            :  * Pass %IDN2_NFC_INPUT in @flags to convert input @ulabel to NFC form
      55                 :            :  * before further processing.
      56                 :            :  *
      57                 :            :  * It is recommended to supply both @ulabel and @alabel for better
      58                 :            :  * error checking, but supplying just one of them will work.  Passing
      59                 :            :  * in only @alabel is better than only @ulabel.  See RFC 5891 section
      60                 :            :  * 4 for more information.
      61                 :            :  *
      62                 :            :  * Returns: On successful conversion %IDN2_OK is returned, when the
      63                 :            :  *   given @ulabel and @alabel does not match each other
      64                 :            :  *   %IDN2_UALABEL_MISMATCH is returned, when either of the input
      65                 :            :  *   labels are too long %IDN2_TOO_BIG_LABEL is returned, when @alabel
      66                 :            :  *   does does not appear to be a proper A-label %IDN2_INVALID_ALABEL
      67                 :            :  *   is returned, or another error code is returned.
      68                 :            :  **/
      69                 :            : int
      70                 :         46 : idn2_register_u8 (const uint8_t * ulabel, const uint8_t * alabel,
      71                 :            :                   uint8_t ** insertname, int flags)
      72                 :            : {
      73                 :            :   int rc;
      74                 :            : 
      75 [ +  + ][ -  + ]:         46 :   if (ulabel == NULL && alabel == NULL)
      76                 :            :     {
      77                 :          0 :       *insertname = NULL;
      78                 :          0 :       return IDN2_OK;
      79                 :            :     }
      80                 :            : 
      81 [ +  + ][ -  + ]:         46 :   if (ulabel && strlen (ulabel) >= IDN2_LABEL_MAX_LENGTH)
      82                 :          0 :     return IDN2_TOO_BIG_LABEL;
      83 [ +  + ][ -  + ]:         46 :   if (alabel && strlen (alabel) >= IDN2_LABEL_MAX_LENGTH)
      84                 :          0 :     return IDN2_TOO_BIG_LABEL;
      85                 :            : 
      86 [ +  + ][ -  + ]:         46 :   if (alabel && !_idn2_ascii_p (alabel, strlen (alabel)))
      87                 :          0 :     return IDN2_INVALID_ALABEL;
      88                 :            : 
      89         [ +  + ]:         46 :   if (alabel)
      90                 :            :     {
      91                 :          3 :       size_t alabellen = strlen (alabel), u32len = IDN2_LABEL_MAX_LENGTH * 4;
      92                 :            :       uint32_t u32[IDN2_DOMAIN_MAX_LENGTH * 4];
      93                 :            :       uint8_t *tmp;
      94                 :            :       uint8_t u8[IDN2_DOMAIN_MAX_LENGTH + 1];
      95                 :            :       size_t u8len;
      96                 :            : 
      97         [ +  + ]:          3 :       if (alabellen <= 4)
      98                 :          1 :         return IDN2_INVALID_ALABEL;
      99         [ +  - ]:          2 :       if (alabel[0] != 'x'
     100 [ +  - ][ +  - ]:          2 :           || alabel[1] != 'n' || alabel[2] != '-' || alabel[3] != '-')
                 [ -  + ]
     101                 :          0 :         return IDN2_INVALID_ALABEL;
     102                 :            : 
     103                 :          2 :       rc = _idn2_punycode_decode (alabellen - 4, alabel + 4,
     104                 :            :                                   &u32len, u32, NULL);
     105         [ -  + ]:          2 :       if (rc != IDN2_OK)
     106                 :          0 :         return rc;
     107                 :            : 
     108                 :          2 :       u8len = sizeof (u8);
     109         [ -  + ]:          2 :       if (u32_to_u8 (u32, u32len, u8, &u8len) == NULL)
     110                 :          0 :         return IDN2_ENCODING_ERROR;
     111                 :          2 :       u8[u8len] = '\0';
     112                 :            : 
     113         [ +  - ]:          2 :       if (ulabel)
     114                 :            :         {
     115         [ -  + ]:          2 :           if (strcmp (ulabel, u8) != 0)
     116                 :          0 :             return IDN2_UALABEL_MISMATCH;
     117                 :            :         }
     118                 :            : 
     119                 :          2 :       rc = idn2_register_u8 (u8, NULL, &tmp, 0);
     120         [ -  + ]:          2 :       if (rc != IDN2_OK)
     121                 :          0 :         return rc;
     122                 :            : 
     123                 :          2 :       rc = strcmp (alabel, tmp);
     124                 :          2 :       free (tmp);
     125         [ -  + ]:          2 :       if (rc != 0)
     126                 :          0 :         return IDN2_UALABEL_MISMATCH;
     127                 :            : 
     128                 :          3 :       *insertname = strdup (alabel);
     129                 :            :     }
     130                 :            :   else                          /* ulabel only */
     131                 :            :     {
     132                 :            :       uint32_t *u32;
     133                 :            :       size_t u32len;
     134                 :            :       size_t tmpl;
     135                 :            : 
     136                 :         43 :       *insertname = malloc (IDN2_LABEL_MAX_LENGTH + 1);
     137         [ -  + ]:         43 :       if (*insertname == NULL)
     138                 :          0 :         return IDN2_MALLOC;
     139                 :            : 
     140         [ +  + ]:         43 :       if (_idn2_ascii_p (ulabel, strlen (ulabel)))
     141                 :            :         {
     142                 :          1 :           strcpy (*insertname, ulabel);
     143                 :          1 :           return IDN2_OK;
     144                 :            :         }
     145                 :            : 
     146                 :         42 :       rc = _idn2_u8_to_u32_nfc (ulabel, strlen (ulabel), &u32, &u32len,
     147                 :            :                                 flags & IDN2_NFC_INPUT);
     148         [ -  + ]:         42 :       if (rc != IDN2_OK)
     149                 :            :         {
     150                 :          0 :           free (*insertname);
     151                 :          0 :           return rc;
     152                 :            :         }
     153                 :            : 
     154                 :         42 :       rc = _idn2_label_test (TEST_NFC
     155                 :            :                              | TEST_DISALLOWED
     156                 :            :                              | TEST_UNASSIGNED
     157                 :            :                              | TEST_2HYPHEN
     158                 :            :                              | TEST_HYPHEN_STARTEND
     159                 :            :                              | TEST_LEADING_COMBINING
     160                 :            :                              | TEST_CONTEXTJ_RULE
     161                 :            :                              | TEST_CONTEXTO_RULE | TEST_BIDI, u32, u32len);
     162         [ +  + ]:         42 :       if (rc != IDN2_OK)
     163                 :            :         {
     164                 :         23 :           free (*insertname);
     165                 :         23 :           free (u32);
     166                 :         23 :           return rc;
     167                 :            :         }
     168                 :            : 
     169                 :         19 :       (*insertname)[0] = 'x';
     170                 :         19 :       (*insertname)[1] = 'n';
     171                 :         19 :       (*insertname)[2] = '-';
     172                 :         19 :       (*insertname)[3] = '-';
     173                 :            : 
     174                 :         19 :       tmpl = IDN2_LABEL_MAX_LENGTH - 4;
     175                 :         19 :       rc = _idn2_punycode_encode (u32len, u32, NULL, &tmpl, *insertname + 4);
     176                 :         19 :       free (u32);
     177         [ -  + ]:         19 :       if (rc != IDN2_OK)
     178                 :            :         {
     179                 :          0 :           free (*insertname);
     180                 :          0 :           return rc;
     181                 :            :         }
     182                 :            : 
     183                 :         43 :       (*insertname)[4 + tmpl] = '\0';
     184                 :            :     }
     185                 :            : 
     186                 :         46 :   return IDN2_OK;
     187                 :            : }
     188                 :            : 
     189                 :            : /**
     190                 :            :  * idn2_register_ul:
     191                 :            :  * @ulabel: input zero-terminated locale encoded string, or NULL.
     192                 :            :  * @alabel: input zero-terminated ACE encoded string (xn--), or NULL.
     193                 :            :  * @insertname: newly allocated output variable with name to register in DNS.
     194                 :            :  * @flags: optional #idn2_flags to modify behaviour.
     195                 :            :  *
     196                 :            :  * Perform IDNA2008 register string conversion on domain label @ulabel
     197                 :            :  * and @alabel, as described in section 4 of RFC 5891.  Note that the
     198                 :            :  * input @ulabel is assumed to be encoded in the locale's default
     199                 :            :  * coding system, and will be transcoded to UTF-8 and NFC normalized
     200                 :            :  * by this function.
     201                 :            :  *
     202                 :            :  * It is recommended to supply both @ulabel and @alabel for better
     203                 :            :  * error checking, but supplying just one of them will work.  Passing
     204                 :            :  * in only @alabel is better than only @ulabel.  See RFC 5891 section
     205                 :            :  * 4 for more information.
     206                 :            :  *
     207                 :            :  * Returns: On successful conversion %IDN2_OK is returned, when the
     208                 :            :  *   given @ulabel and @alabel does not match each other
     209                 :            :  *   %IDN2_UALABEL_MISMATCH is returned, when either of the input
     210                 :            :  *   labels are too long %IDN2_TOO_BIG_LABEL is returned, when @alabel
     211                 :            :  *   does does not appear to be a proper A-label %IDN2_INVALID_ALABEL
     212                 :            :  *   is returned, or another error code is returned.
     213                 :            :  **/
     214                 :            : int
     215                 :          0 : idn2_register_ul (const char *ulabel, const char *alabel,
     216                 :            :                   char **insertname, int flags)
     217                 :            : {
     218                 :          0 :   uint8_t *utf8ulabel = u8_strconv_from_locale (ulabel);
     219                 :            :   int rc;
     220                 :            : 
     221         [ #  # ]:          0 :   if (utf8ulabel == NULL)
     222                 :            :     {
     223         [ #  # ]:          0 :       if (errno == ENOMEM)
     224                 :          0 :         return IDN2_MALLOC;
     225                 :          0 :       return IDN2_ICONV_FAIL;
     226                 :            :     }
     227                 :            : 
     228                 :          0 :   rc = idn2_register_u8 (utf8ulabel, (const uint8_t *) alabel,
     229                 :            :                          (uint8_t **) insertname, flags | IDN2_NFC_INPUT);
     230                 :            : 
     231                 :          0 :   free (utf8ulabel);
     232                 :            : 
     233                 :          0 :   return rc;
     234                 :            : }

Generated by: LCOV version 1.9