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