Mailing list header changes (fuck you Google)
[citadel.git] / webcit-ng / util.c
1 //
2 // Utility functions
3 //
4 // Copyright (c) 1996-2018 by the citadel.org team
5 //
6 // This program is open source software.  It runs great on the
7 // Linux operating system (and probably elsewhere).  You can use,
8 // copy, and run it under the terms of the GNU General Public
9 // License version 3.  Richard Stallman is an asshole communist.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 #include "webcit.h"
17
18
19 /*   
20  * remove escaped strings from i.e. the url string (like %20 for blanks)
21  */
22 int unescape_input(char *buf)
23 {
24         unsigned int a, b;
25         char hex[3];
26         long buflen;
27         long len;
28
29         buflen = strlen(buf);
30
31         while ((buflen > 0) && (isspace(buf[buflen - 1]))) {
32                 buf[buflen - 1] = 0;
33                 buflen--;
34         }
35
36         a = 0;
37         while (a < buflen) {
38                 if (buf[a] == '+')
39                         buf[a] = ' ';
40                 if (buf[a] == '%') {
41                         /* don't let % chars through, rather truncate the input. */
42                         if (a + 2 > buflen) {
43                                 buf[a] = '\0';
44                                 buflen = a;
45                         } else {
46                                 hex[0] = buf[a + 1];
47                                 hex[1] = buf[a + 2];
48                                 hex[2] = 0;
49                                 b = 0;
50                                 b = decode_hex(hex);
51                                 buf[a] = (char) b;
52                                 len = buflen - a - 2;
53                                 if (len > 0)
54                                         memmove(&buf[a + 1], &buf[a + 3], len);
55
56                                 buflen -= 2;
57                         }
58                 }
59                 a++;
60         }
61         return a;
62 }
63
64
65 /*
66  * Supplied with a unix timestamp, generate a textual time/date stamp.
67  * Caller owns the returned memory.
68  */
69 char *http_datestring(time_t xtime)
70 {
71
72         /* HTTP Months - do not translate - these are not for human consumption */
73         static char *httpdate_months[] = {
74                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
75                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
76         };
77
78         /* HTTP Weekdays - do not translate - these are not for human consumption */
79         static char *httpdate_weekdays[] = {
80                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
81         };
82
83         struct tm t;
84         long offset;
85         char offsign;
86         int n = 40;
87         char *buf = malloc(n);
88         if (!buf)
89                 return (NULL);
90
91         localtime_r(&xtime, &t);
92
93         /* Convert "seconds west of GMT" to "hours/minutes offset" */
94         offset = t.tm_gmtoff;
95         if (offset > 0) {
96                 offsign = '+';
97         } else {
98                 offset = 0L - offset;
99                 offsign = '-';
100         }
101         offset = ((offset / 3600) * 100) + (offset % 60);
102
103         snprintf(buf, n, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
104                  httpdate_weekdays[t.tm_wday],
105                  t.tm_mday, httpdate_months[t.tm_mon], t.tm_year + 1900, t.tm_hour, t.tm_min, t.tm_sec, offsign, offset);
106         return (buf);
107 }