gsasl  2.2.1
xcode.c
Go to the documentation of this file.
1 /* xcode.c --- Encode and decode application payload in libgsasl session.
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 static int
27 _gsasl_code (Gsasl_session *sctx,
29  const char *input, size_t input_len,
30  char **output, size_t *output_len)
31 {
32 
33  if (code == NULL)
34  {
35  *output_len = input_len;
36  *output = malloc (*output_len);
37  if (!*output)
38  return GSASL_MALLOC_ERROR;
39 
40  memcpy (*output, input, input_len);
41  return GSASL_OK;
42  }
43 
44  return code (sctx, sctx->mech_data, input, input_len, output, output_len);
45 }
46 
65 int
67  const char *input, size_t input_len,
68  char **output, size_t *output_len)
69 {
71 
72  if (sctx->clientp)
73  code = sctx->mech->client.encode;
74  else
75  code = sctx->mech->server.encode;
76 
77  return _gsasl_code (sctx, code, input, input_len, output, output_len);
78 }
79 
98 int
100  const char *input, size_t input_len,
101  char **output, size_t *output_len)
102 {
103  Gsasl_code_function code;
104 
105  if (sctx->clientp)
106  code = sctx->mech->client.decode;
107  else
108  code = sctx->mech->server.decode;
109 
110  return _gsasl_code (sctx, code, input, input_len, output, output_len);
111 }
int(* Gsasl_code_function)(Gsasl_session *sctx, void *mech_data, const char *input, size_t input_len, char **output, size_t *output_len)
Definition: gsasl-mech.h:134
@ GSASL_OK
Definition: gsasl.h:129
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:133
Gsasl_code_function encode
Definition: gsasl-mech.h:158
Gsasl_code_function decode
Definition: gsasl-mech.h:159
struct Gsasl_mechanism_functions server
Definition: gsasl-mech.h:176
struct Gsasl_mechanism_functions client
Definition: gsasl-mech.h:175
void * mech_data
Definition: internal.h:53
Gsasl_mechanism * mech
Definition: internal.h:52
int gsasl_decode(Gsasl_session *sctx, const char *input, size_t input_len, char **output, size_t *output_len)
Definition: xcode.c:99
int gsasl_encode(Gsasl_session *sctx, const char *input, size_t input_len, char **output, size_t *output_len)
Definition: xcode.c:66