moved timetoa() from asn1.c to types.c
[strongswan.git] / src / libstrongswan / types.c
1 /**
2 * @file types.c
3 *
4 * @brief Generic types.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <string.h>
24 #include <time.h>
25
26 #include "types.h"
27
28
29 /**
30 * String mappings for type status_t.
31 */
32 mapping_t status_m[] = {
33 {SUCCESS, "SUCCESS"},
34 {FAILED, "FAILED"},
35 {OUT_OF_RES, "OUT_OF_RES"},
36 {ALREADY_DONE, "ALREADY_DONE"},
37 {NOT_SUPPORTED, "NOT_SUPPORTED"},
38 {INVALID_ARG, "INVALID_ARG"},
39 {NOT_FOUND, "NOT_FOUND"},
40 {PARSE_ERROR, "PARSE_ERROR"},
41 {VERIFY_ERROR, "VERIFY_ERROR"},
42 {INVALID_STATE, "INVALID_STATE"},
43 {DESTROY_ME, "DESTROY_ME"},
44 {CREATED, "CREATED"},
45 {MAPPING_END, NULL}
46 };
47
48 #define UNDEFINED_TIME 0
49
50 /**
51 * @brief Display a date either in local or UTC time
52 *
53 * @param buf buffer where displayed time will be written
54 * @param buflen buffer length
55 * @param time time to be displayed
56 * @param utc UTC (TRUE) or local time (FALSE)
57 *
58 */
59 void timetoa(char *buf, size_t buflen, const time_t *time, bool utc);
60
61 /**
62 * Empty chunk.
63 */
64 chunk_t CHUNK_INITIALIZER = {NULL,0};
65
66 /**
67 * Described in header.
68 */
69 chunk_t chunk_clone(chunk_t chunk)
70 {
71 chunk_t clone = CHUNK_INITIALIZER;
72
73 if (chunk.ptr && chunk.len > 0)
74 {
75 clone.ptr = malloc(chunk.len);
76 clone.len = chunk.len;
77 memcpy(clone.ptr, chunk.ptr, chunk.len);
78 }
79
80 return clone;
81 }
82
83 /**
84 * Described in header.
85 */
86 void chunk_free(chunk_t *chunk)
87 {
88 free(chunk->ptr);
89 chunk->ptr = NULL;
90 chunk->len = 0;
91 }
92
93 /**
94 * Described in header.
95 */
96 chunk_t chunk_alloc(size_t bytes)
97 {
98 chunk_t new_chunk;
99 new_chunk.ptr = malloc(bytes);
100 new_chunk.len = bytes;
101 return new_chunk;
102 }
103
104 /**
105 * Described in header.
106 */
107 bool chunk_equals(chunk_t a, chunk_t b)
108 {
109 return a.len == b.len &&
110 a.ptr != NULL &&
111 b.ptr != NULL &&
112 memcmp(a.ptr, b.ptr, a.len) == 0;
113 }
114
115 /**
116 * Described in header.
117 */
118 void chunk_to_hex(char *buf, size_t buflen, chunk_t chunk)
119 {
120 bool first = TRUE;
121
122 buflen--; /* reserve space for null termination */
123
124 while (chunk.len > 0 && buflen > 2)
125 {
126 static char hexdig[] = "0123456789abcdef";
127
128 if (first)
129 {
130 first = FALSE;
131 }
132 else
133 {
134 *buf++ = ':'; buflen--;
135 }
136 *buf++ = hexdig[(*chunk.ptr >> 4) & 0x0f];
137 *buf++ = hexdig[ *chunk.ptr++ & 0x0f];
138 buflen -= 2; chunk.len--;
139 }
140 *buf = '\0';
141 }
142
143 /**
144 * Described in header.
145 */
146 void *clalloc(void * pointer, size_t size)
147 {
148 void *data;
149 data = malloc(size);
150
151 memcpy(data, pointer,size);
152
153 return (data);
154 }
155
156 /*
157 * Names of the months used by timetoa()
158 */
159 static const char* months[] = {
160 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
161 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
162 };
163
164 /*
165 * Described in header file
166 */
167 void timetoa(char *buf, size_t buflen, const time_t *time, bool utc)
168 {
169 if (*time == UNDEFINED_TIME)
170 snprintf(buf, buflen, "--- -- --:--:--%s----", (utc)?" UTC ":" ");
171 else
172 {
173 struct tm *t = (utc)? gmtime(time) : localtime(time);
174
175 snprintf(buf, buflen, "%s %02d %02d:%02d:%02d%s%04d",
176 months[t->tm_mon], t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
177 (utc)?" UTC ":" ", t->tm_year + 1900);
178 }
179 }
180