$darkmode
Libical API Documentation 4.0 STABLE VERSION Visit the v3.0 documentation
icaltypes.c
Go to the documentation of this file.
1 /*======================================================================
2  FILE: icaltypes.c
3  CREATOR: eric 16 May 1999
4 
5  SPDX-FileCopyrightText: 2000, Eric Busboom <eric@civicknowledge.com>
6  SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0
7  ======================================================================*/
8 
14 #ifdef HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17 
18 #include "icaltypes.h"
19 #include "icalerror_p.h"
20 #include "icalmemory.h"
21 
23 #define TMP_BUF_SIZE 1024
24 
26 #if ICAL_SYNC_MODE == ICAL_SYNC_MODE_PTHREAD
27 #include <pthread.h>
28 static pthread_mutex_t unk_token_mutex = PTHREAD_MUTEX_INITIALIZER;
29 #endif
30 #include <ctype.h>
31 static ICAL_GLOBAL_VAR ical_unknown_token_handling unknownTokenHandling = ICAL_TREAT_AS_ERROR;
32 
34 {
36  return true;
37  }
38 
39  return false;
40 }
41 
43 {
45  return true;
46  }
47 
48  return false;
49 }
50 
52 {
53  struct icaltriggertype tr;
54 
55  tr.time = icaltime_null_time();
57 
58  return tr;
59 }
60 
62 {
63  struct icaltriggertype tr;
64  icalerrorstate es;
65  icalerrorenum e;
66 
67  tr.time = icaltime_null_time();
69 
70  /* Suppress errors so a failure in icaltime_from_string() does not cause an abort */
72  if (str == 0) {
73  goto error;
74  }
76  e = icalerrno;
78 
79  tr.time = icaltime_from_string(str);
80 
81  if (icaltime_is_null_time(tr.time)) {
83 
85  goto error;
86  }
87  }
88 
91  return tr;
92 
93 error:
96  return tr;
97 }
98 
100 {
101  const char *s, *p1, *p2;
102  struct icalreqstattype stat;
103  short major = 0, minor = 0;
104 
105  icalerror_check_arg((str != 0), "str");
106 
107  stat.code = ICAL_UNKNOWN_STATUS;
108  stat.debug = 0;
109  stat.desc = 0;
110 
111  // Don't allow (fuzzer) garbage chars anywhere in the reqstat string
112  s = str;
113  /* cppcheck-suppress nullPointerRedundantCheck */
114  while (*s && isprint((unsigned char)*s)) {
115  ++s;
116  }
117  if (*s != '\0') {
118  // garbage encountered. return the empty stat
119  return stat;
120  }
121 
122  /* Get the status numbers */
123 
124  sscanf(str, "%hd.%hd", &major, &minor);
125 
126  if (major <= 0 || minor < 0) {
128  return stat;
129  }
130 
131  stat.code = icalenum_num_to_reqstat(major, minor);
132 
133  if (stat.code == ICAL_UNKNOWN_STATUS) {
135  return stat;
136  }
137 
138  p1 = strchr(str, ';');
139 
140  if (p1 == 0) {
141  /* icalerror_set_errno(ICAL_BADARG_ERROR);*/
142  return stat;
143  }
144 
145  /* Just ignore the second clause; it will be taken from inside the library
146  */
147 
148  p2 = strchr(p1 + 1, ';');
149  if (p2 != 0 && *(p2 + 1) != 0) { // skipping empty debug strings
150  stat.debug = icalmemory_tmp_copy(p2 + 1);
151  }
152 
153  return stat;
154 }
155 
157 {
158  char *buf;
159 
160  buf = icalreqstattype_as_string_r(stat);
162  return buf;
163 }
164 
166 {
167  char *temp;
168 
169  icalerror_check_arg_rz((stat.code != ICAL_UNKNOWN_STATUS), "Status");
170 
171  temp = (char *)icalmemory_new_buffer(TMP_BUF_SIZE);
172 
173  if (stat.desc == 0) {
174  stat.desc = icalenum_reqstat_desc(stat.code);
175  }
176 
177  if (stat.debug != 0) {
178  snprintf(temp, TMP_BUF_SIZE, "%d.%d;%s;%s", icalenum_reqstat_major(stat.code),
179  icalenum_reqstat_minor(stat.code), stat.desc, stat.debug);
180  } else {
181  snprintf(temp, TMP_BUF_SIZE, "%d.%d;%s", icalenum_reqstat_major(stat.code),
182  icalenum_reqstat_minor(stat.code), stat.desc);
183  }
184 
185  return temp;
186 }
187 
189 {
190  ical_unknown_token_handling myHandling;
191 
192 #if ICAL_SYNC_MODE == ICAL_SYNC_MODE_PTHREAD
193  if (pthread_mutex_lock(&unk_token_mutex) != 0) {
195  }
196 #endif
197 
198  myHandling = unknownTokenHandling;
199 
200 #if ICAL_SYNC_MODE == ICAL_SYNC_MODE_PTHREAD
201  if (pthread_mutex_unlock(&unk_token_mutex) != 0) {
203  }
204 #endif
205 
206  return myHandling;
207 }
208 
210 {
211 #if ICAL_SYNC_MODE == ICAL_SYNC_MODE_PTHREAD
212  if (pthread_mutex_lock(&unk_token_mutex) != 0) {
214  }
215 #endif
216 
217  unknownTokenHandling = newSetting;
218 
219 #if ICAL_SYNC_MODE == ICAL_SYNC_MODE_PTHREAD
220  if (pthread_mutex_unlock(&unk_token_mutex) != 0) {
222  }
223 #endif
224 }
ical_unknown_token_handling ical_get_unknown_token_handling_setting(void)
Definition: icaltypes.c:188
char * icalreqstattype_as_string_r(struct icalreqstattype stat)
Definition: icaltypes.c:165
Common memory management routines.
short icalenum_reqstat_minor(icalrequeststatus stat)
Definition: icalenums.c:127
bool icaltriggertype_is_bad_trigger(struct icaltriggertype tr)
Definition: icaltypes.c:42
const char * desc
Definition: icaltypes.h:112
struct icaldurationtype icaldurationtype_from_seconds(int seconds)
Creates a new icaldurationtype from a duration in seconds.
Definition: icalduration.c:28
icalerrorstate
Determine if an error is fatal or non-fatal.
Definition: icalerror.h:88
struct icaltriggertype icaltriggertype_from_seconds(const int reltime)
Definition: icaltypes.c:51
const char * icalreqstattype_as_string(struct icalreqstattype stat)
Definition: icaltypes.c:156
void icalerror_set_errno(icalerrorenum x)
Sets the icalerrno to a given error.
Definition: icalerror.c:90
icalerrorenum
Represents the different types of errors that can be triggered in libical.
Definition: icalerror.h:41
void ical_set_unknown_token_handling_setting(ical_unknown_token_handling newSetting)
Definition: icaltypes.c:209
icalerrorstate icalerror_get_error_state(icalerrorenum error)
Gets the error state (severity) for a given error.
short icalenum_reqstat_major(icalrequeststatus stat)
Definition: icalenums.c:114
struct icalreqstattype icalreqstattype_from_string(const char *str)
Definition: icaltypes.c:99
Define internal types and provide functions to manipulate them.
bool icaldurationtype_is_null_duration(struct icaldurationtype d)
Checks if a duration is a null duration.
Definition: icalduration.c:270
struct icaltimetype icaltime_null_time(void)
Definition: icaltime.c:579
icalrequeststatus code
Definition: icaltypes.h:111
struct icaldurationtype duration
Definition: icaltypes.h:51
void * icalmemory_new_buffer(size_t size)
Creates new buffer with the specified size.
Definition: icalmemory.c:315
struct icaltimetype icaltime_from_string(const char *str)
Definition: icaltime.c:374
const char * debug
Definition: icaltypes.h:113
struct icaltimetype time
Definition: icaltypes.h:50
struct icaltriggertype icaltriggertype_from_string(const char *str)
Definition: icaltypes.c:61
ical_unknown_token_handling
Definition: icaltypes.h:154
icalrequeststatus icalenum_num_to_reqstat(short major, short minor)
Definition: icalenums.c:140
#define icalerrno
Access the current icalerrno value.
Definition: icalerror.h:133
const char * icalenum_reqstat_desc(icalrequeststatus stat)
Definition: icalenums.c:75
void icalerror_set_error_state(icalerrorenum error, icalerrorstate state)
Sets the icalerrorstate for a given icalerrorenum error.
bool icaltime_is_null_time(const struct icaltimetype t)
Definition: icaltime.c:630
char * icalmemory_tmp_copy(const char *str)
Creates a copy of the given string, stored on the ring buffer, and returns it.
Definition: icalmemory.c:222
struct icaldurationtype icaldurationtype_from_string(const char *str)
Creates a new icaldurationtype from a duration given as a string.
Definition: icalduration.c:43
bool icaldurationtype_is_bad_duration(struct icaldurationtype d)
Checks if a duration is a bad duration.
Definition: icalduration.c:292
void icalmemory_add_tmp_buffer(void *buf)
Adds an externally allocated buffer to the ring.
Definition: icalmemory.c:158
bool icaltriggertype_is_null_trigger(struct icaltriggertype tr)
Definition: icaltypes.c:33