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