Subversion Repositories RepoView

Rev

Rev 1 | Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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