summaryrefslogtreecommitdiff
path: root/libmailutils/string/cstrunescape.c
blob: d3bea619669386e9d9aa6a5e7be977ddb72419b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2016-2018 Free Software Foundation, Inc.

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Mailutils is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/cstr.h>
#include <mailutils/errno.h>

/* Copy characters from IN to OUT.  Replace each occurrences of backslash
   followed by a character XTAB[i] with CHR[i].

   OUT should be large enough to accomodate the translated string (same length
   as IN, in the worst case).  It is OK if IN==OUT.

   Both XTAB and CHR must not be NULL and must contain the same number of
   elements. 
 */
static void
c_str_unescape (char const *in, char *out, char const *chr, char const *xtab)
{
  size_t i, j;
  
  for (i = j = 0; in[i]; i++, j++)
    {
      if (in[i] == '\\')
	break;
      out[j] = in[i];
    }

  if (in[i])
    {
      for (; in[i]; i++, j++)
	{
	  if (in[i] == '\\')
	    {
	      char *p;
	      if (in[i+1] && (p = strchr (xtab, in[i+1])) != NULL)
		{
		  out[j] = chr[p - xtab];
		  i++;
		  continue;
		}
	    }
	  out[j] = in[i];
	}
    }
  out[j] = 0;
}

/* Modifies STR, by replacing each occurrence of \ followed by a charater
   XTAB[i] with CHR[i].

   Either XTAB or CHR can be NULL, in which case XTAB=CHR is assumed.

   If both XTAB and CHR are NULL, STR is unchanged.
   
   STR == NULL is OK.

   Returns 0 success, and EINVAL if lengths of CHR and XTAB differ.
*/
int
mu_c_str_unescape_inplace (char *str, char const *chr, char const *xtab)
{
  if (!str)
    return 0;
  if (!xtab)
    {
      if (chr)
	xtab = chr;
      else
	return 0;
    }
  else if (!chr)
    chr = xtab;
  else if (strlen (chr) != strlen (xtab))
    return EINVAL;
  c_str_unescape (str, str, chr, xtab);
  return 0;
}

/* A counterpart of mu_c_str_escape.  Creates an allocated (using malloc(3))
   copy of STR, where each occurrence of \ followed by a charater XTAB[i]
   is replaced with single character CHR[i].

   Either XTAB or CHR can be NULL, in which case XTAB=CHR is assumed.

   If both XTAB and CHR are NULL, the result is *RET_STR will contain exact
   malloc'ed copy of STR.
   
   STR == NULL is OK: in that case *RET_STR will also be NULL.

   Returns 0 success, EINVAL if lengths of CHR and XTAB differ,
   MU_ERR_OUT_PTR_NULL if RET_STR is NULL, and ENOMEM if memory allocation
   failed.
 */
int
mu_c_str_unescape (char const *str, char const *chr, char const *xtab,
		   char **ret_str)
{
  char *newstr;
  size_t i, size;
  
  if (!ret_str)
    return MU_ERR_OUT_PTR_NULL;
  
  if (!str)
    {
      *ret_str = NULL;
      return 0;
    }

  if (!xtab)
    {
      if (chr)
	xtab = chr;
      else
	{
	  char *p = strdup (str);
	  if (!p)
	    return errno;
	  *ret_str = p;
	}
    }
  else if (!chr)
    chr = xtab;
  else if (strlen (chr) != strlen (xtab))
    return EINVAL;

  size = 0;
  for (i = 0; str[i]; i++)
    {
      if (str[i] == '\\' && str[i + 1] && strchr (xtab, str[i + 1]))
	i++;
      size++;
    }

  newstr = malloc (size + 1);
  if (!newstr)
    return errno;
  *ret_str = newstr;
  
  c_str_unescape (str, newstr, chr, xtab);

  return 0;
}

/* A counterpart of mu_c_str_escape_trans.

   Creates an allocated (using malloc(3)) copy of STR, where each occurrence 
   of \ followed by TRANS[i] is replaced by TRANS[i+1].

   Returns 0 on success, or error code if an error occurred.
   
   See also mu_wordsplit_c_escape_tab in wordsplit.c
 */
int
mu_c_str_unescape_trans (char const *str, char const *trans, char **ret_str)
{
  char *chr, *xtab;
  size_t n, i;
  int rc;
  
  if (trans)
    {
      n = strlen (trans);
      if (n % 2)
	return EINVAL;
      chr = malloc (n + 2);
      if (!chr)
	return errno;
      xtab = chr + n / 2 + 1;
      for (i = 0; i < n; i += 2)
	{
	  chr[i / 2] = trans[i + 1];
	  xtab[i / 2] = trans[i];
	}
      chr[i / 2] = xtab[i / 2] = 0;
    }
  else
    {
      chr = xtab = NULL;
    }

  rc = mu_c_str_unescape (str, chr, xtab, ret_str);

  free (chr);

  return rc;
}
  

Return to:

Send suggestions and report system problems to the System administrator.