Coverage Report

Created: 2025-11-03 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dovecot/src/lib-imap/imap-arg.c
Line
Count
Source
1
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
2
3
#include "lib.h"
4
#include "imap-arg.h"
5
6
bool imap_arg_get_atom(const struct imap_arg *arg, const char **str_r)
7
21.2k
{
8
21.2k
  if (arg->type != IMAP_ARG_ATOM && arg->type != IMAP_ARG_NIL)
9
16
    return FALSE;
10
11
21.2k
  *str_r = arg->_data.str;
12
21.2k
  return TRUE;
13
21.2k
}
14
15
bool imap_arg_get_quoted(const struct imap_arg *arg, const char **str_r)
16
0
{
17
0
  if (arg->type != IMAP_ARG_STRING)
18
0
    return FALSE;
19
20
0
  *str_r = arg->_data.str;
21
0
  return TRUE;
22
0
}
23
24
bool imap_arg_get_string(const struct imap_arg *arg, const char **str_r)
25
35.7M
{
26
35.7M
  if (arg->type != IMAP_ARG_STRING && arg->type != IMAP_ARG_LITERAL)
27
1.65k
    return FALSE;
28
29
35.7M
  *str_r = arg->_data.str;
30
35.7M
  return TRUE;
31
35.7M
}
32
33
bool imap_arg_get_astring(const struct imap_arg *arg, const char **str_r)
34
0
{
35
  /* RFC 3501 4.5. specifies that NIL is the same as "NIL" when
36
     reading astring. */
37
0
  if (!IMAP_ARG_IS_ASTRING(arg) && arg->type != IMAP_ARG_NIL)
38
0
    return FALSE;
39
40
0
  *str_r = arg->_data.str;
41
0
  return TRUE;
42
0
}
43
44
bool imap_arg_get_nstring(const struct imap_arg *arg, const char **str_r)
45
29.9M
{
46
29.9M
  if (arg->type == IMAP_ARG_NIL) {
47
52.2k
    *str_r = NULL;
48
52.2k
    return TRUE;
49
52.2k
  }
50
29.9M
  return imap_arg_get_string(arg, str_r);
51
29.9M
}
52
53
bool imap_arg_get_literal_size(const struct imap_arg *arg, uoff_t *size_r)
54
0
{
55
0
  if (arg->type != IMAP_ARG_LITERAL_SIZE &&
56
0
      arg->type != IMAP_ARG_LITERAL_SIZE_NONSYNC)
57
0
    return FALSE;
58
59
0
  *size_r = arg->_data.literal_size;
60
0
  return TRUE;
61
0
}
62
63
bool imap_arg_get_list(const struct imap_arg *arg,
64
           const struct imap_arg **list_r)
65
40.3k
{
66
40.3k
  unsigned int count;
67
68
40.3k
  return imap_arg_get_list_full(arg, list_r, &count);
69
40.3k
}
70
71
bool imap_arg_get_list_full(const struct imap_arg *arg,
72
          const struct imap_arg **list_r,
73
          unsigned int *list_count_r)
74
7.52M
{
75
7.52M
  unsigned int count;
76
77
7.52M
  if (arg->type != IMAP_ARG_LIST)
78
401
    return FALSE;
79
80
7.52M
  *list_r = array_get(&arg->_data.list, &count);
81
82
7.52M
  if (count > 0 && (*list_r)[count-1].type == IMAP_ARG_EOL)
83
7.52M
    count--;
84
0
  else {
85
    /* imap-parser stopped early (e.g. due to reading literal size).
86
       The IMAP_ARG_EOL was added to the list only temporarily. */
87
0
    i_assert((*list_r)[count].type == IMAP_ARG_EOL);
88
0
  }
89
7.52M
  *list_count_r = count;
90
7.52M
  return TRUE;
91
7.52M
}
92
93
const char *imap_arg_as_astring(const struct imap_arg *arg)
94
0
{
95
0
  const char *str;
96
97
0
  if (!imap_arg_get_astring(arg, &str))
98
0
    i_unreached();
99
0
  return str;
100
0
}
101
102
const char *imap_arg_as_nstring(const struct imap_arg *arg)
103
0
{
104
0
  const char *str;
105
106
0
  if (!imap_arg_get_nstring(arg, &str))
107
0
    i_unreached();
108
0
  return str;
109
0
}
110
111
uoff_t imap_arg_as_literal_size(const struct imap_arg *arg)
112
0
{
113
0
  uoff_t size;
114
115
0
  if (!imap_arg_get_literal_size(arg, &size))
116
0
    i_unreached();
117
0
  return size;
118
0
}
119
120
const struct imap_arg *
121
imap_arg_as_list(const struct imap_arg *arg)
122
22.7k
{
123
22.7k
  const struct imap_arg *ret;
124
125
22.7k
  if (!imap_arg_get_list(arg, &ret))
126
0
    i_unreached();
127
22.7k
  return ret;
128
22.7k
}
129
130
bool imap_arg_atom_equals(const struct imap_arg *arg, const char *str)
131
0
{
132
0
  const char *value;
133
134
0
  if (!imap_arg_get_atom(arg, &value))
135
0
    return FALSE;
136
0
  return strcasecmp(value, str) == 0;
137
0
}