Subversion Repositories RepoView

Rev

Rev 3 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 nishi 1
/* $Id: util.c 1 2024-08-20 19:18:25Z nishi $ */
2
 
3
#include "rv_util.h"
4
 
5
#include "../config.h"
6
 
7
#include "rv_version.h"
8
 
9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <string.h>
12
 
13
char* rv_strcat(const char* a, const char* b){
14
	char* str = malloc(strlen(a) + strlen(b) + 1);
15
	memcpy(str, a, strlen(a));
16
	memcpy(str + strlen(a), b, strlen(b));
17
	str[strlen(a) + strlen(b)] = 0;
18
	return str;
19
}
20
 
21
char* rv_strcat3(const char* a, const char* b, const char* c){
22
	char* tmp = rv_strcat(a, b);
23
	char* str = rv_strcat(tmp, c);
24
	free(tmp);
25
	return str;
26
}
27
 
28
char* rv_strdup(const char* str){
29
	return rv_strcat(str, "");
30
}
31
 
32
void rv_error_http(void){
33
	printf("Content-Type: text/plain\r\n");
34
	printf("Status: 500 Internal Server Error\r\n");
35
	printf("\r\n");
36
	printf("This is RepoView version %s, named `%s`.\n", rv_get_version(), INSTANCE_NAME);
37
	printf("Unrecoverable error has occured.\n");
38
	printf("Admin contact: %s\n", INSTANCE_ADMIN);
39
	printf("Developer contact: Nishi <nishi@nishi.boats>\n");
40
	printf("-----\n");
41
}
42
 
43
int hex_to_num(char c){
44
	if('0' <= c && c <= '9'){
45
		return c - '0';
46
	}else if('a' <= c && c <= 'f'){
47
		return c - 'a' + 10;
48
	}else if('A' <= c && c <= 'F'){
49
		return c - 'A' + 10;
50
	}
51
	return 0;
52
}
53
 
54
char* rv_url_decode(const char* str){
55
	char* r = malloc(1);
56
	r[0] = 0;
57
	int i;
58
	char cbuf[2];
59
	cbuf[1] = 0;
60
	for(i = 0; str[i] != 0; i++){
61
		if(str[i] == '%'){
62
			if(str[i + 1] == 0) break;
63
			if(str[i + 2] == 0) break;
64
			cbuf[0] = (hex_to_num(str[i + 1]) << 4) | hex_to_num(str[i + 2]);
65
			char* tmp = r;
66
			r = rv_strcat(tmp, cbuf);
67
			free(tmp);
68
			i += 2;
69
		}else{
70
			cbuf[0] = str[i];
71
			char* tmp = r;
72
			r = rv_strcat(tmp, cbuf);
73
			free(tmp);
74
		}
75
	}
76
	return r;
77
}