gsasl  1.8.0
saslprep.c
Go to the documentation of this file.
00001 /* saslprep.c --- Internationalized SASL string processing.
00002  * Copyright (C) 2002-2012 Simon Josefsson
00003  *
00004  * This file is part of GNU SASL Library.
00005  *
00006  * GNU SASL Library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation; either version 2.1 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * GNU SASL Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License License along with GNU SASL Library; if not, write to the
00018  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "internal.h"
00024 
00025 #if HAVE_LIBIDN
00026 #include <stringprep.h>
00027 #if defined HAVE_PR29_H && defined HAVE_PR29_8Z
00028 #include <pr29.h>
00029 #endif
00030 #endif
00031 
00047 int
00048 gsasl_saslprep (const char *in, Gsasl_saslprep_flags flags,
00049                 char **out, int *stringpreprc)
00050 {
00051 #if HAVE_LIBIDN
00052   int rc;
00053 
00054   rc = stringprep_profile (in, out, "SASLprep",
00055                            (flags & GSASL_ALLOW_UNASSIGNED)
00056                            ? STRINGPREP_NO_UNASSIGNED : 0);
00057 
00058   if (stringpreprc)
00059     *stringpreprc = rc;
00060 
00061   if (rc != STRINGPREP_OK)
00062     {
00063       *out = NULL;
00064       return GSASL_SASLPREP_ERROR;
00065     }
00066 
00067 #if defined HAVE_PR29_8Z && defined HAVE_PR29_H
00068   if (pr29_8z (*out) != PR29_SUCCESS)
00069     {
00070       free (*out);
00071       *out = NULL;
00072       if (stringpreprc)
00073         *stringpreprc = STRINGPREP_NFKC_FAILED;
00074 
00075       return GSASL_SASLPREP_ERROR;
00076     }
00077 #endif
00078 
00079 #else
00080   size_t i, inlen = strlen (in);
00081 
00082   for (i = 0; i < inlen; i++)
00083     if (in[i] & 0x80)
00084       {
00085         *out = NULL;
00086         return GSASL_SASLPREP_ERROR;
00087       }
00088 
00089   *out = malloc (inlen + 1);
00090   if (!*out)
00091     return GSASL_MALLOC_ERROR;
00092   strcpy (*out, in);
00093 #endif
00094 
00095   return GSASL_OK;
00096 }