moved whitespace around
[citadel.git] / webcit-ng / server / util.c
1 // Utility functions
2 // Copyright (c) 1996-2023 by the citadel.org team
3 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
4
5 #include "webcit.h"
6
7
8 // remove escaped strings from i.e. the url string (like %20 for blanks)
9 int unescape_input(char *buf) {
10         unsigned int a, b;
11         char hex[3];
12         long buflen;
13         long len;
14
15         buflen = strlen(buf);
16
17         while ((buflen > 0) && (isspace(buf[buflen - 1]))) {
18                 buf[buflen - 1] = 0;
19                 buflen--;
20         }
21
22         a = 0;
23         while (a < buflen) {
24                 if (buf[a] == '+') {
25                         buf[a] = ' ';
26                 }
27                 if (buf[a] == '%') {
28                         // don't let % chars through - instead truncate the input.
29                         if (a + 2 > buflen) {
30                                 buf[a] = '\0';
31                                 buflen = a;
32                         }
33                         else {
34                                 hex[0] = buf[a + 1];
35                                 hex[1] = buf[a + 2];
36                                 hex[2] = 0;
37                                 b = 0;
38                                 b = decode_hex(hex);
39                                 buf[a] = (char) b;
40                                 len = buflen - a - 2;
41                                 if (len > 0) {
42                                         memmove(&buf[a + 1], &buf[a + 3], len);
43                                 }
44                                 buflen -= 2;
45                         }
46                 }
47                 a++;
48         }
49         return a;
50 }
51
52
53 // Supplied with a unix timestamp, generate a textual time/date stamp.
54 // Caller owns the returned memory.
55 char *http_datestring(time_t xtime) {
56
57         // HTTP Months - do not translate - these are not for human consumption
58         static char *httpdate_months[] = {
59                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
60                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
61         };
62
63         // HTTP Weekdays - do not translate - these are not for human consumption
64         static char *httpdate_weekdays[] = {
65                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
66         };
67
68         struct tm t;
69         long offset;
70         char offsign;
71         int n = 40;
72         char *buf = malloc(n);
73         if (!buf) {
74                 return (NULL);
75         }
76
77         localtime_r(&xtime, &t);
78
79         // Convert "seconds west of GMT" to "hours/minutes offset"
80         offset = t.tm_gmtoff;
81         if (offset > 0) {
82                 offsign = '+';
83         }
84         else {
85                 offset = 0L - offset;
86                 offsign = '-';
87         }
88         offset = ((offset / 3600) * 100) + (offset % 60);
89
90         snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
91                  httpdate_weekdays[t.tm_wday],
92                  t.tm_mday, httpdate_months[t.tm_mon], t.tm_year + 1900, t.tm_hour, t.tm_min, t.tm_sec, offsign, offset);
93         return (buf);
94 }