gsasl  2.2.2
saslprep.c
Go to the documentation of this file.
1 /* saslprep.c --- Internationalized SASL string processing.
2  * Copyright (C) 2002-2025 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 along with GNU SASL Library; if not, see
18  * <https://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include <config.h>
23 #include "internal.h"
24 
25 #if HAVE_LIBIDN
26 
27 # include <stringprep.h>
28 
29 # if defined HAVE_PR29_H && defined HAVE_PR29_8Z
30 # include <pr29.h>
31 
32 # endif
33 
49 int
50 gsasl_saslprep (const char *in, Gsasl_saslprep_flags flags,
51  char **out, int *stringpreprc)
52 {
53  int rc;
54 
55  rc = stringprep_profile (in, out, "SASLprep",
56  (flags & GSASL_ALLOW_UNASSIGNED)
57  ? STRINGPREP_NO_UNASSIGNED : 0);
58 
59  if (stringpreprc)
60  *stringpreprc = rc;
61 
62  if (rc != STRINGPREP_OK)
63  {
64  *out = NULL;
65  return GSASL_SASLPREP_ERROR;
66  }
67 
68 # if defined HAVE_PR29_8Z && defined HAVE_PR29_H
69  if (pr29_8z (*out) != PR29_SUCCESS)
70  {
71  free (*out);
72  *out = NULL;
73  if (stringpreprc)
74  *stringpreprc = STRINGPREP_NFKC_FAILED;
75 
76  return GSASL_SASLPREP_ERROR;
77  }
78 # endif
79 
80  return GSASL_OK;
81 }
82 
83 #else /* HAVE_LIBIDN */
84 
85 int
86 gsasl_saslprep (const char *in, Gsasl_saslprep_flags flags _GL_UNUSED,
87  char **out, int *stringpreprc _GL_UNUSED)
88 {
89  size_t i, inlen = strlen (in);
90 
91  for (i = 0; i < inlen; i++)
92  if (in[i] & 0x80)
93  {
94  *out = NULL;
95  return GSASL_SASLPREP_ERROR;
96  }
97 
98  *out = malloc (inlen + 1);
99  if (!*out)
100  return GSASL_MALLOC_ERROR;
101  strcpy (*out, in);
102 
103  return GSASL_OK;
104 }
105 
106 #endif
int rc
Definition: error.c:36
Gsasl_saslprep_flags
Definition: gsasl.h:330
@ GSASL_ALLOW_UNASSIGNED
Definition: gsasl.h:331
@ GSASL_OK
Definition: gsasl.h:128
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:132
@ GSASL_SASLPREP_ERROR
Definition: gsasl.h:135
int gsasl_saslprep(const char *in, Gsasl_saslprep_flags flags _GL_UNUSED, char **out, int *stringpreprc _GL_UNUSED)
Definition: saslprep.c:86