gsasl  2.2.1
saslprep.c
Go to the documentation of this file.
1 /* saslprep.c --- Internationalized SASL string processing.
2  * Copyright (C) 2002-2024 Simon Josefsson
3  *
4  * This file is part of GNU SASL Library.
5  *
6  * GNU SASL Library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GNU SASL Library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License License along with GNU SASL Library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include <config.h>
24 #include "internal.h"
25 
26 #if HAVE_LIBIDN
27 
28 # include <stringprep.h>
29 
30 # if defined HAVE_PR29_H && defined HAVE_PR29_8Z
31 # include <pr29.h>
32 
33 # endif
34 
50 int
51 gsasl_saslprep (const char *in, Gsasl_saslprep_flags flags,
52  char **out, int *stringpreprc)
53 {
54  int rc;
55 
56  rc = stringprep_profile (in, out, "SASLprep",
57  (flags & GSASL_ALLOW_UNASSIGNED)
58  ? STRINGPREP_NO_UNASSIGNED : 0);
59 
60  if (stringpreprc)
61  *stringpreprc = rc;
62 
63  if (rc != STRINGPREP_OK)
64  {
65  *out = NULL;
66  return GSASL_SASLPREP_ERROR;
67  }
68 
69 # if defined HAVE_PR29_8Z && defined HAVE_PR29_H
70  if (pr29_8z (*out) != PR29_SUCCESS)
71  {
72  free (*out);
73  *out = NULL;
74  if (stringpreprc)
75  *stringpreprc = STRINGPREP_NFKC_FAILED;
76 
77  return GSASL_SASLPREP_ERROR;
78  }
79 # endif
80 
81  return GSASL_OK;
82 }
83 
84 #else /* HAVE_LIBIDN */
85 
86 int
87 gsasl_saslprep (const char *in, Gsasl_saslprep_flags flags _GL_UNUSED,
88  char **out, int *stringpreprc _GL_UNUSED)
89 {
90  size_t i, inlen = strlen (in);
91 
92  for (i = 0; i < inlen; i++)
93  if (in[i] & 0x80)
94  {
95  *out = NULL;
96  return GSASL_SASLPREP_ERROR;
97  }
98 
99  *out = malloc (inlen + 1);
100  if (!*out)
101  return GSASL_MALLOC_ERROR;
102  strcpy (*out, in);
103 
104  return GSASL_OK;
105 }
106 
107 #endif
int rc
Definition: error.c:37
Gsasl_saslprep_flags
Definition: gsasl.h:331
@ GSASL_ALLOW_UNASSIGNED
Definition: gsasl.h:332
@ GSASL_OK
Definition: gsasl.h:129
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:133
@ GSASL_SASLPREP_ERROR
Definition: gsasl.h:136
int gsasl_saslprep(const char *in, Gsasl_saslprep_flags flags _GL_UNUSED, char **out, int *stringpreprc _GL_UNUSED)
Definition: saslprep.c:87