summaryrefslogtreecommitdiff
path: root/libmailutils/url/decode.c
blob: 000d7be104c9f72772dbd89198eab0d476199725 (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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2010-2012, 2014 Free Software Foundation, Inc.

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

   This library 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
   Lesser General Public License for more details.

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

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

#include <mailutils/types.h>
#include <mailutils/util.h>
#include <mailutils/secret.h>
#include <mailutils/sys/url.h>

struct decode_tab
{
  int mask;
  int (*fun) (mu_url_t, size_t);
  size_t off;
};

static int
_url_dec_str (mu_url_t url, size_t off)
{
  char **pptr = (char**) ((char*) url + off);
  mu_str_url_decode_inline (*pptr);
  return 0;
}

static int
_url_dec_param (mu_url_t url, size_t off)
{
  int i;

  for (i = 0; i < url->fvcount; i++)
    mu_str_url_decode_inline (url->fvpairs[i]);
  return 0;
}

static int
_url_dec_query (mu_url_t url, size_t off)
{
  int i;

  for (i = 0; i < url->qargc; i++)
    mu_str_url_decode_inline (url->qargv[i]);
  return 0;
}

static int
_url_dec_secret (mu_url_t url, size_t off)
{
  char *pass;
  mu_secret_t newsec;
  int rc;

  rc = mu_str_url_decode (&pass, mu_secret_password (url->secret));
  if (rc)
    return rc;
  rc = mu_secret_create (&newsec, pass, strlen (pass));
  memset (pass, 0, strlen (pass));
  free (pass);
  if (rc)
    return rc;
  mu_secret_destroy (&url->secret);
  url->secret = newsec;
  return 0;
}

static struct decode_tab decode_tab[] = {
  { MU_URL_SCHEME, _url_dec_str, mu_offsetof (struct _mu_url, scheme) },
  { MU_URL_USER,   _url_dec_str, mu_offsetof (struct _mu_url, user) },
  { MU_URL_SECRET, _url_dec_secret },
  { MU_URL_AUTH,   _url_dec_str, mu_offsetof (struct _mu_url, auth) },
  { MU_URL_HOST,   _url_dec_str, mu_offsetof (struct _mu_url, host) },
  { MU_URL_PATH,   _url_dec_str, mu_offsetof (struct _mu_url, path) },
  { MU_URL_PARAM,  _url_dec_param, 0 },
  { MU_URL_QUERY,  _url_dec_query, 0 }
};

int
mu_url_decode (mu_url_t url)
{
  int i;

  if (!url)
    return EINVAL;
  for (i = 0; i < MU_ARRAY_SIZE (decode_tab); i++)
    {
      if (url->flags & decode_tab[i].mask)
	{
	  int rc = decode_tab[i].fun (url, decode_tab[i].off);
	  if (rc)
	    return rc;
	}
    }
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.