summaryrefslogtreecommitdiff
path: root/src/etag.c
blob: 0e16c5986b4c0181df64cfd2659c7df8e9128f55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <string.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if defined HAVE_STDINT_H
#include <stdint.h>
#elif defined HAVE_INTTYPES_H
#include <inttypes.h>
#endif

#include "buffer.h"
#include "etag.h"

int etag_is_equal(buffer *etag, const char *matches) {
	if (buffer_is_equal_string(etag, matches, strlen(matches))) return 1;
	return 0;
}

int etag_create(buffer *etag, struct stat *st, etag_flags_t flags) {

	if (0 == flags) return 0;

	buffer_reset(etag);

	if (flags & ETAG_USE_INODE) {
		buffer_append_off_t(etag, st->st_ino);
		buffer_append_string_len(etag, CONST_STR_LEN("-"));
	}
	if (flags & ETAG_USE_SIZE) {
		buffer_append_off_t(etag, st->st_size);
		buffer_append_string_len(etag, CONST_STR_LEN("-"));
	}
	if (flags & ETAG_USE_MTIME) {
		buffer_append_long(etag, st->st_mtime);
	}
	return 0;
}

int etag_mutate(buffer *mut, buffer *etag) {
	size_t i;
	uint32_t h;

	for (h=0, i=0; i < etag->used-1; ++i) h = (h<<5)^(h>>27)^(etag->ptr[i]);

	buffer_reset(mut);
	buffer_copy_string_len(mut, CONST_STR_LEN("\""));
	buffer_append_long(mut, h);
	buffer_append_string_len(mut, CONST_STR_LEN("\""));

	return 0;
}