Subversion Repositories RepoView

Rev

Rev 1 | Rev 5 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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