source: trunk/oscam-http-helpers.c@ 4433

Last change on this file since 4433 was 4433, checked in by alno, 10 years ago

WebIf: fix possible issue with to small malloc

File size: 19.1 KB
Line 
1//FIXME Not checked on threadsafety yet; after checking please remove this line
2#include "globals.h"
3#ifdef WEBIF
4#include "oscam-http.h"
5
6/* Adds a name->value-mapping or appends to it. You will get a reference back which you may freely
7 use (but you should not call free/realloc on this!)*/
8char *tpl_addVar(struct templatevars *vars, int append, char *name, char *value){
9 if(name == NULL || value == NULL) return "";
10 int i;
11 char *tmp,*result = NULL;
12 for(i = (*vars).varscnt-1; i >= 0; --i){
13 if(strcmp((*vars).names[i], name) == 0){
14 result = (*vars).values[i];
15 break;
16 }
17 }
18 if(result == NULL){
19 if((*vars).varsalloc <= (*vars).varscnt){
20 if(!cs_realloc(&(*vars).names, (*vars).varsalloc * 2 * sizeof(char**), -1)) return value;
21 if(!cs_realloc(&(*vars).values, (*vars).varsalloc * 2 * sizeof(char**), -1)) return value;
22 (*vars).varsalloc = (*vars).varscnt * 2;
23 }
24 if(!cs_malloc(&tmp,(strlen(name) + 1) * sizeof(char), -1)) return value;
25 strcpy(tmp, name);
26 (*vars).names[(*vars).varscnt] = tmp;
27 if(!cs_malloc(&tmp,(strlen(value) + 1) * sizeof(char), -1)){
28 free((*vars).names[(*vars).varscnt]);
29 return value;
30 }
31 strcpy(tmp, value);
32 (*vars).values[(*vars).varscnt] = tmp;
33 (*vars).varscnt = (*vars).varscnt + 1;
34 } else {
35 int newlen = strlen(value);
36 if(append == 1){
37 int oldlen = strlen((*vars).values[i]);
38 if(!cs_malloc(&tmp, (oldlen + newlen + 1) * sizeof(char), -1)) return value;
39 memcpy(tmp, (*vars).values[i], oldlen);
40 strcpy(tmp + oldlen, value);
41 } else {
42 if(!cs_malloc(&tmp, (newlen + 1) * sizeof(char), -1)) return value;
43 strcpy(tmp, value);
44 }
45 free((*vars).values[i]);
46 (*vars).values[i] = tmp;
47 }
48 return tmp;
49}
50
51/* Allows to add a char array which has been allocated by malloc. It will automatically get
52 freed when calling tpl_clear(). Please do NOT free the memory yourself or realloc
53 it after having added the array here! */
54char *tpl_addTmp(struct templatevars *vars, char *value){
55 if(value == NULL) return "";
56 if((*vars).tmpalloc <= (*vars).tmpcnt){
57 if(!cs_realloc (&(*vars).tmp, (*vars).tmpalloc * 2 * sizeof(char**), -1)) return value;
58 (*vars).tmpalloc = (*vars).tmpcnt * 2;
59 }
60 (*vars).tmp[(*vars).tmpcnt] = value;
61 (*vars).tmpcnt++;
62 return value;
63}
64
65/* Allows to do a dynamic printf without knowing and defining the needed memory size. If you specify
66 varname, the printf-result will be added/appended to the varlist, if varname=NULL it will only be returned.
67 In either case you will always get a reference back which you may freely use (but you should not call
68 free/realloc on this as it will be automatically cleaned!)*/
69char *tpl_printf(struct templatevars *vars, int append, char *varname, char *fmtstring, ...){
70 unsigned int needed;
71 char test[1];
72 va_list argptr;
73
74 va_start(argptr,fmtstring);
75 needed = vsnprintf(test, 1, fmtstring, argptr);
76 va_end(argptr);
77
78 char *result;
79 if(!cs_malloc(&result, (needed + 1) * sizeof(char), -1)) return "";
80 va_start(argptr,fmtstring);
81 vsnprintf(result, needed + 1, fmtstring, argptr);
82 va_end(argptr);
83
84 if(varname == NULL) tpl_addTmp(vars, result);
85 else {
86 char *tmp = tpl_addVar(vars, append, varname, result);
87 free(result);
88 result = tmp;
89 }
90 return result;
91}
92
93/* Returns the value for a name or an empty string if nothing was found. */
94char *tpl_getVar(struct templatevars *vars, char *name){
95 int i;
96 char *result = NULL;
97 for(i = (*vars).varscnt-1; i >= 0; --i){
98 if(strcmp((*vars).names[i], name) == 0){
99 result = (*vars).values[i];
100 break;
101 }
102 }
103 if(result == NULL) return "";
104 else return result;
105}
106
107/* Initializes all variables for a templatevar-structure and returns a pointer to it. Make
108 sure to call tpl_clear() when you are finished or you'll run into a memory leak! */
109struct templatevars *tpl_create(){
110 struct templatevars *vars;
111 if(!cs_malloc(&vars, sizeof(struct templatevars), -1)) return NULL;
112 (*vars).varsalloc = 64;
113 (*vars).varscnt = 0;
114 (*vars).tmpalloc = 64;
115 (*vars).tmpcnt = 0;
116 if(!cs_malloc(&(*vars).names, (*vars).varsalloc * sizeof(char**), -1)){
117 free(vars);
118 return NULL;
119 }
120 if(!cs_malloc(&(*vars).values, (*vars).varsalloc * sizeof(char**), -1)){
121 free((*vars).names);
122 free(vars);
123 return NULL;
124 };
125 if(!cs_malloc(&(*vars).tmp, (*vars).tmpalloc * sizeof(char**), -1)){
126 free((*vars).names);
127 free((*vars).values);
128 free(vars);
129 return NULL;
130 };
131 return vars;
132}
133
134/* Clears all allocated memory for the specified templatevar-structure. */
135void tpl_clear(struct templatevars *vars){
136 int i;
137 for(i = (*vars).varscnt-1; i >= 0; --i){
138 free((*vars).names[i]);
139 free((*vars).values[i]);
140 }
141 free((*vars).names);
142 free((*vars).values);
143 for(i = (*vars).tmpcnt-1; i >= 0; --i){
144 free((*vars).tmp[i]);
145 }
146 free((*vars).tmp);
147 free(vars);
148}
149
150/* Creates a path to a template file. You need to set the resultsize to the correct size of result. */
151char *tpl_getTplPath(const char *name, const char *path, char *result, unsigned int resultsize){
152 char *pch;
153 if((strlen(path) + strlen(name) + 6) <= resultsize){
154 snprintf(result, resultsize, "%s%s.tpl", path, name);
155 for(pch = result + strlen(path); pch[0] != '\0'; ++pch){
156 if(pch[0] == '/' || pch[0] == '\\') pch[0] = ' ';
157 }
158 } else result[0] = '\0';
159 return result;
160}
161
162/* Returns an unparsed template either from disk or from internal templates.
163 Note: You must free() the result after using it and you may get NULL if an error occured!*/
164char *tpl_getUnparsedTpl(const char* name){
165 int i;
166 int tplcnt = sizeof(tpl)/sizeof(char *);
167 int tplmapcnt = sizeof(tplmap)/sizeof(char *);
168 char *result;
169
170 for(i = 0; i < tplcnt; ++i){
171 if(strcmp(name, tpl[i]) == 0) break;
172 }
173
174 if(strlen(cfg->http_tpl) > 0){
175 char path[255];
176 if(strlen(tpl_getTplPath(name, cfg->http_tpl, path, 255)) > 0 && file_exists(path)){
177 FILE *fp;
178 char buffer[1024];
179 int read, allocated = 1025, size = 0;
180 if(!cs_malloc(&result, allocated * sizeof(char), -1)) return NULL;
181 if((fp = fopen(path,"r"))!=NULL){
182 while((read = fread(&buffer,sizeof(char),1024,fp)) > 0){
183 if(allocated < size + read + 1) {
184 allocated += size + 1024;
185 if(!cs_realloc(&result, allocated * sizeof(char), -1)) return NULL;
186 }
187 memcpy(result + size, buffer, read);
188 size += read;
189 }
190 result[size] = '\0';
191 fclose (fp);
192 return result;
193 }
194 }
195 }
196 if(i >= 0 && i < tplmapcnt){
197 int len = (strlen(tplmap[i])) + 1;
198 if(!cs_malloc(&result, len * sizeof(char), -1)) return NULL;
199 memcpy(result, tplmap[i], len);
200 } else {
201 if(!cs_malloc(&result, 1 * sizeof(char), -1)) return NULL;
202 result[0] = '\0';
203 }
204 return result;
205}
206
207/* Returns the specified template with all variables/other templates replaced or an
208 empty string if the template doesn't exist. Do not free the result yourself, it
209 will get automatically cleaned up! */
210char *tpl_getTpl(struct templatevars *vars, const char* name){
211 char *tplorg = tpl_getUnparsedTpl(name);
212 if(!tplorg) return "";
213 char *tplend = tplorg + strlen(tplorg);
214 char *pch, *pch2, *tpl=tplorg;
215 char varname[33];
216
217 int tmp,respos = 0;
218 int allocated = 2 * strlen(tpl) + 1;
219 char *result;
220 if(!cs_malloc(&result, allocated * sizeof(char), -1)) return "";
221
222 while(tpl < tplend){
223 if(tpl[0] == '#' && tpl[1] == '#' && tpl[2] != '#'){
224 pch2 = tpl;
225 pch = tpl + 2;
226 while(pch[0] != '\0' && (pch[0] != '#' || pch[1] != '#')) ++pch;
227 if(pch - pch2 < 32 && pch[0] == '#' && pch[1] == '#'){
228 memcpy(varname, pch2 + 2, pch - pch2 - 2);
229 varname[pch - pch2 - 2] = '\0';
230 if(strncmp(varname, "TPL", 3) == 0){
231 pch2 = tpl_getTpl(vars, varname + 3);
232 } else {
233 pch2 = tpl_getVar(vars, varname);
234 }
235 tmp = strlen(pch2);
236 if(tmp + respos + 2 >= allocated){
237 allocated = tmp + respos + 256;
238 if(!cs_realloc(&result, allocated * sizeof(char), -1)) return "";
239 }
240 memcpy(result + respos, pch2, tmp);
241 respos += tmp;
242 tpl = pch + 2;
243 }
244 } else {
245 if(respos + 2 >= allocated){
246 allocated = respos + 256;
247 if(!cs_realloc(&result, allocated * sizeof(char), -1)) return "";
248 }
249 result[respos] = tpl[0];
250 ++respos;
251 ++tpl;
252 }
253 }
254 free(tplorg);
255 result[respos] = '\0';
256 tpl_addTmp(vars, result);
257 return result;
258}
259
260/* Saves all templates to the specified paths. Existing files will be overwritten! */
261int tpl_saveIncludedTpls(const char *path){
262 int tplcnt = sizeof(tpl)/sizeof(char *);
263 int tplmapcnt = sizeof(tplmap)/sizeof(char *);
264 int i, cnt = 0;
265 char tmp[256];
266 FILE *fp;
267 for(i = 0; i < tplcnt && i < tplmapcnt; ++i){
268 if(strlen(tpl_getTplPath(tpl[i], path, tmp, 256)) > 0 && (fp = fopen(tmp,"w")) != NULL){
269 fwrite(tplmap[i], sizeof(char), strlen(tplmap[i]), fp);
270 fclose (fp);
271 ++cnt;
272 }
273 }
274 return cnt;
275}
276
277/* Parses a value in an authentication string by removing all quotes/whitespace. Note that the original array is modified. */
278char *parse_auth_value(char *value){
279 char *pch = value;
280 char *pch2;
281 value = strstr(value, "=");
282 if(value != NULL){
283 do{
284 ++value;
285 } while (value[0] == ' ' || value[0] == '"');
286 pch = value;
287 for(pch2 = value + strlen(value) - 1; pch2 >= value && (pch2[0] == ' ' || pch2[0] == '"' || pch2[0] == '\r' || pch2[0] == '\n'); --pch2) pch2[0] = '\0';
288 }
289 return pch;
290}
291
292/* Calculates the currently valid nonce value and copies it to result. Please note that result needs to be at least (MD5_DIGEST_LENGTH * 2) + 1 large. */
293void calculate_nonce(char *result){
294 char noncetmp[128];
295 unsigned char md5tmp[MD5_DIGEST_LENGTH];
296 sprintf(noncetmp, "%d:%s", (int)time(NULL)/AUTHNONCEVALIDSECS, noncekey);
297 char_to_hex(MD5((unsigned char*)noncetmp, strlen(noncetmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)result);
298}
299
300/* Checks if authentication is correct. Returns -1 if not correct, 1 if correct and 2 if nonce isn't valid anymore.
301 Note that authstring will be modified. */
302int check_auth(char *authstring, char *method, char *path, char *expectednonce){
303 int authok = 0, uriok = 0;
304 char *authnonce = "";
305 char *authnc = "";
306 char *authcnonce = "";
307 char *authresponse = "";
308 char *uri = "";
309 char *username = "";
310 char *expectedPassword = cfg->http_pwd;
311 char *pch = authstring + 22;
312 char *pch2;
313
314 pch = strtok (pch,",");
315 while (pch != NULL){
316 pch2 = pch;
317 while(pch2[0] == ' ' && pch2[0] != '\0') ++pch2;
318 if(strncmp(pch2, "nonce", 5) == 0){
319 authnonce=parse_auth_value(pch2);
320 } else if (strncmp(pch2, "nc", 2) == 0){
321 authnc=parse_auth_value(pch2);
322 } else if (strncmp(pch2, "cnonce", 6) == 0){
323 authcnonce=parse_auth_value(pch2);
324 } else if (strncmp(pch2, "response", 8) == 0){
325 authresponse=parse_auth_value(pch2);
326 } else if (strncmp(pch2, "uri", 3) == 0){
327 uri=parse_auth_value(pch2);
328 } else if (strncmp(pch2, "username", 8) == 0){
329 username=parse_auth_value(pch2);
330 }
331 pch = strtok (NULL, ",");
332 }
333
334 if(strncmp(uri, path, strlen(path)) == 0) uriok = 1;
335 else {
336 pch2 = uri;
337 for(pch = uri; pch[0] != '\0'; ++pch) {
338 if(pch[0] == '/') pch2 = pch;
339 }
340 if(strncmp(pch2, path, strlen(path)) == 0) uriok = 1;
341 }
342 if(uriok == 1 && strcmp(username, cfg->http_user) == 0){
343 char A1tmp[3 + strlen(username) + strlen(AUTHREALM) + strlen(expectedPassword)];
344 char A1[(MD5_DIGEST_LENGTH * 2) + 1], A2[(MD5_DIGEST_LENGTH * 2) + 1], A3[(MD5_DIGEST_LENGTH * 2) + 1];
345 unsigned char md5tmp[MD5_DIGEST_LENGTH];
346 sprintf(A1tmp, "%s:%s:%s", username, AUTHREALM, expectedPassword);
347 char_to_hex(MD5((unsigned char*)A1tmp, strlen(A1tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A1);
348
349 char A2tmp[2 + strlen(method) + strlen(uri)];
350 sprintf(A2tmp, "%s:%s", method, uri);
351 char_to_hex(MD5((unsigned char*)A2tmp, strlen(A2tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A2);
352
353 char A3tmp[10 + strlen(A1) + strlen(A2) + strlen(authnonce) + strlen(authnc) + strlen(authcnonce)];
354 sprintf(A3tmp, "%s:%s:%s:%s:auth:%s", A1, authnonce, authnc, authcnonce, A2);
355 char_to_hex(MD5((unsigned char*)A3tmp, strlen(A3tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A3);
356
357 if(strcmp(A3, authresponse) == 0) {
358 if(strcmp(expectednonce, authnonce) == 0) authok = 1;
359 else authok = 2;
360 }
361 }
362 return authok;
363}
364
365#ifdef WITH_SSL
366#include <openssl/crypto.h>
367#include <openssl/ssl.h>
368#include <openssl/err.h>
369#endif
370
371int webif_write_raw(char *buf, FILE* f, int len) {
372#ifdef WITH_SSL
373 if (cfg->http_use_ssl) {
374 return SSL_write((SSL*)f, buf, len);
375 } else
376#endif
377 return fwrite(buf, 1, len, f);
378}
379
380int webif_write(char *buf, FILE* f) {
381 return webif_write_raw(buf, f, strlen(buf));
382}
383
384int webif_read(char *buf, int num, FILE *f) {
385#ifdef WITH_SSL
386 if (cfg->http_use_ssl) {
387 return SSL_read((SSL*)f, buf, num);
388 } else
389#endif
390 return read(fileno(f), buf, num);
391}
392
393void send_headers(FILE *f, int status, char *title, char *extra, char *mime, int cache){
394 time_t now;
395 char timebuf[32];
396 char buf[sizeof(PROTOCOL) + sizeof(SERVER) + strlen(title) + (extra == NULL?0:strlen(extra)+2) + (mime == NULL?0:strlen(mime)+2) + 256];
397 char *pos = buf;
398
399 pos += sprintf(pos, "%s %d %s\r\n", PROTOCOL, status, title);
400 pos += sprintf(pos, "Server: %s\r\n", SERVER);
401
402 now = time(NULL);
403 strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now));
404 pos += sprintf(pos, "Date: %s\r\n", timebuf);
405
406 if (extra)
407 pos += sprintf(pos, "%s\r\n", extra);
408
409 if (mime)
410 pos += sprintf(pos, "Content-Type: %s\r\n", mime);
411
412 if(!cache){
413 pos += sprintf(pos, "Cache-Control: no-store, no-cache, must-revalidate\r\n");
414 pos += sprintf(pos, "Expires: Sat, 26 Jul 1997 05:00:00 GMT\r\n");
415 } else {
416 pos += sprintf(pos, "Cache-Control: public, max-age=7200\r\n");
417 }
418 pos += sprintf(pos, "Last-Modified: %s\r\n", timebuf);
419 pos += sprintf(pos, "Connection: close\r\n");
420 pos += sprintf(pos, "\r\n");
421 webif_write(buf, f);
422}
423
424/*
425 * function for sending files.
426 */
427void send_file(FILE *f, char *filename){
428 int fileno = 0;
429
430 if (!strcmp(filename, "CSS")){
431 filename = cfg->http_css;
432 fileno = 1;
433 } else if (!strcmp(filename, "JS")){
434 filename = cfg->http_jscript;
435 fileno = 2;
436 }
437
438 if(strlen(filename) > 0 && file_exists(filename) == 1){
439 FILE *fp;
440 char buffer[1024];
441 int read;
442
443 if((fp = fopen(filename, "r"))==NULL) return;
444 while((read = fread(buffer,sizeof(char), 1023, fp)) > 0) {
445 buffer[read] = '\0';
446 webif_write(buffer, f);
447 }
448
449 fclose (fp);
450 } else {
451 if (fileno == 1)
452 webif_write(CSS, f);
453 else if (fileno == 2)
454 webif_write(JSCRIPT, f);
455 }
456}
457
458void send_error(FILE *f, int status, char *title, char *extra, char *text){
459 char buf[(2* strlen(title)) + strlen(text) + 128];
460 char *pos = buf;
461 send_headers(f, status, title, extra, "text/html", 0);
462 pos += sprintf(pos, "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\r\n", status, title);
463 pos += sprintf(pos, "<BODY><H4>%d %s</H4>\r\n", status, title);
464 pos += sprintf(pos, "%s\r\n", text);
465 pos += sprintf(pos, "</BODY></HTML>\r\n");
466 webif_write(buf, f);
467}
468
469void send_error500(FILE *f){
470 send_error(f, 500, "Internal Server Error", NULL, "The server encountered an internal error that prevented it from fulfilling this request.");
471}
472
473char *getParam(struct uriparams *params, char *name){
474 int i;
475 for(i=(*params).paramcount-1; i>=0; --i){
476 if(strcmp((*params).params[i], name) == 0) return (*params).values[i];
477 }
478 return "";
479}
480
481/* Helper function for urldecode.*/
482int x2i(int i){
483 i=toupper(i);
484 i = i - '0';
485 if(i > 9) i = i - 'A' + '9' + 1;
486 return i;
487}
488
489/* Decodes values in a http url. Note: The original value is modified! */
490void urldecode(char *s){
491 int c, c1, n;
492 char *s0,*t;
493 t = s0 = s;
494 n = strlen(s);
495 while(n >0){
496 c = *s++;
497 if(c == '+') c = ' ';
498 else if(c == '%' && n > 2){
499 c = *s++;
500 c1 = c;
501 c = *s++;
502 c = 16*x2i(c1) + x2i(c);
503 n -= 2;
504 }
505 *t++ = c;
506 n--;
507 }
508 *t = 0;
509}
510
511/* Encode values in a http url. Do not call free() or realloc on the returned reference or you will get memory corruption! */
512char *urlencode(struct templatevars *vars, char *str){
513 char buf[strlen(str) * 3 + 1];
514 char *pstr = str, *pbuf = buf;
515 while (*pstr) {
516 if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') *pbuf++ = *pstr;
517 else if (*pstr == ' ') *pbuf++ = '+';
518 else {
519 *pbuf++ = '%';
520 *pbuf++ = to_hex(*pstr >> 4);
521 *pbuf++ = to_hex(*pstr & 15);
522 }
523 ++pstr;
524 }
525 *pbuf = '\0';
526 /* Allocate the needed memory size and store it in the templatevars */
527 if(!cs_malloc(&pbuf, strlen(buf) + 1, -1)) return "";
528 memcpy(pbuf, buf, strlen(buf) + 1);
529 return tpl_addTmp(vars, pbuf);
530}
531
532/* XML-Escapes a char array. The returned reference will be automatically cleaned through the templatevars-mechanism tpl_clear().
533 Do not call free() or realloc on the returned reference or you will get memory corruption! */
534char *xml_encode(struct templatevars *vars, char *chartoencode) {
535 int i, pos = 0, len = strlen(chartoencode);
536 char *result;
537 /* In worst case, every character could get converted to 6 chars (we only support ASCII, for Unicode it would be 7)*/
538 char encoded[len * 6 + 1], buffer[7];
539 for (i = 0; i < len; ++i){
540 switch(chartoencode[i]) {
541 case '&': memcpy(encoded + pos, "&amp;", 5); pos+=5; break;
542 case '<': memcpy(encoded + pos, "&lt;", 4); pos+=4; break;
543 case '>': memcpy(encoded + pos, "&gt;", 4); pos+=4; break;
544 case '"': memcpy(encoded + pos, "&quot;", 6); pos+=6; break;
545 case '\'': memcpy(encoded + pos, "&apos;", 6); pos+=6; break;
546
547 default:
548 if ( (unsigned int)chartoencode[i] < 32 || (unsigned int)chartoencode[i] > 127 ) {
549 snprintf(buffer, 7, "&#%d;", chartoencode[i] + 256);
550 memcpy(encoded + pos, buffer, strlen(buffer));
551 pos+=strlen(buffer);
552
553 } else {
554 encoded[pos] = chartoencode[i];
555 ++pos;
556 }
557
558 }
559 }
560 /* Allocate the needed memory size and store it in the templatevars */
561 if(!cs_malloc(&result, pos + 1, -1)) return "";
562 memcpy(result, encoded, pos);
563 result[pos] = '\0';
564 return tpl_addTmp(vars, result);
565}
566
567int b64decode(unsigned char *result){
568 char inalphabet[256], decoder[256];
569 unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
570 int i, len = strlen((char *)result), j = 0, bits = 0, char_count = 0;
571
572 for (i = sizeof(alphabet) - 1; i >= 0; --i) {
573 inalphabet[alphabet[i]] = 1;
574 decoder[alphabet[i]] = i;
575 }
576 for(i = 0; i < len; ++i){
577 if (result[i] == '=') break;
578 if (!inalphabet[result[i]]) continue;
579 bits += decoder[result[i]];
580 ++char_count;
581 if (char_count == 4) {
582 result[j++] = bits >> 16;
583 result[j++] = (bits >> 8) & 0xff;
584 result[j++] = bits & 0xff;
585 bits = 0;
586 char_count = 0;
587 } else {
588 bits <<= 6;
589 }
590 }
591 if (i == len) {
592 if (char_count) {
593 result[j] = '\0';
594 return 0;
595 }
596 } else {
597 switch (char_count) {
598 case 1:
599 result[j] = '\0';
600 return 0;
601 case 2:
602 result[j++] = bits >> 10;
603 result[j] = '\0';
604 break;
605 case 3:
606 result[j++] = bits >> 16;
607 result[j++] = (bits >> 8) & 0xff;
608 result[j] = '\0';
609 break;
610 }
611 }
612 return j;
613}
614
615/* Format a seconds integer to hh:mm:ss or dd hh:mm:ss depending hrs >24 */
616char *sec2timeformat(struct templatevars *vars, int seconds) {
617
618 char *value;
619 if(!cs_malloc(&value, 13 * sizeof(char), -1))
620 return "00:00:00";
621
622 if(!seconds)
623 return "00:00:00";
624
625 int secs = 0, fullmins = 0, mins = 0, fullhours = 0, hours = 0, days = 0;
626
627 if(seconds > 0) {
628 secs = seconds % 60;
629 if (seconds > 60) {
630 fullmins = seconds / 60;
631 mins = fullmins % 60;
632 if(fullmins > 60) {
633 fullhours = fullmins / 60;
634 hours = fullhours % 24;
635 days = fullhours / 24;
636 }
637 }
638 }
639 if(!days) sprintf(value, "%02d:%02d:%02d", hours, mins, secs);
640 else sprintf(value, "%02dd %02d:%02d:%02d", days, hours, mins, secs);
641
642 return tpl_addTmp(vars, value);
643}
644
645#endif
Note: See TracBrowser for help on using the repository browser.