netops.c 2.6 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6
 */
7

8 9
#include "netops.h"

10
#include <ctype.h>
11 12
#include "git2/errors.h"

13
#include "posix.h"
14
#include "str.h"
15
#include "runtime.h"
16

17 18 19
int gitno_recv(gitno_buffer *buf)
{
	return buf->recv(buf);
20 21
}

22 23 24 25 26
void gitno_buffer_setup_callback(
	gitno_buffer *buf,
	char *data,
	size_t len,
	int (*recv)(gitno_buffer *buf), void *cb_data)
27 28 29 30 31 32 33 34 35
{
	memset(data, 0x0, len);
	buf->data = data;
	buf->len = len;
	buf->offset = 0;
	buf->recv = recv;
	buf->cb_data = cb_data;
}

36
static int recv_stream(gitno_buffer *buf)
37
{
38
	git_stream *io = (git_stream *) buf->cb_data;
39 40
	size_t readlen = buf->len - buf->offset;
	ssize_t ret;
41

42 43 44
	readlen = min(readlen, INT_MAX);

	ret = git_stream_read(io, buf->data + buf->offset, (int)readlen);
45 46 47 48
	if (ret < 0)
		return -1;

	buf->offset += ret;
49
	return (int)ret;
50 51
}

52 53 54 55 56 57
void gitno_buffer_setup_fromstream(git_stream *st, gitno_buffer *buf, char *data, size_t len)
{
	memset(data, 0x0, len);
	buf->data = data;
	buf->len = len;
	buf->offset = 0;
58 59
	buf->recv = recv_stream;
	buf->cb_data = st;
60 61
}

62
/* Consume up to ptr and move the rest of the buffer to the beginning */
63
int gitno_consume(gitno_buffer *buf, const char *ptr)
64
{
65
	size_t consumed;
66

67 68
	GIT_ASSERT(ptr - buf->data >= 0);
	GIT_ASSERT(ptr - buf->data <= (int) buf->len);
69

70
	consumed = ptr - buf->data;
71

72 73 74
	memmove(buf->data, ptr, buf->offset - consumed);
	memset(buf->data + buf->offset, 0x0, buf->len - buf->offset);
	buf->offset -= consumed;
75 76

	return 0;
77 78 79
}

/* Consume const bytes and move the rest of the buffer to the beginning */
80
void gitno_consume_n(gitno_buffer *buf, size_t cons)
81 82 83 84 85 86
{
	memmove(buf->data, buf->data + cons, buf->len - buf->offset);
	memset(buf->data + cons, 0x0, buf->len - buf->offset);
	buf->offset -= cons;
}

87
/* Match host names according to RFC 2818 rules */
88
int gitno__match_host(const char *pattern, const char *host)
89 90
{
	for (;;) {
91
		char c = git__tolower(*pattern++);
92 93 94 95 96 97 98 99 100 101

		if (c == '\0')
			return *host ? -1 : 0;

		if (c == '*') {
			c = *pattern;
			/* '*' at the end matches everything left */
			if (c == '\0')
				return 0;

102 103 104 105 106 107 108
	/*
	 * We've found a pattern, so move towards the next matching
	 * char. The '.' is handled specially because wildcards aren't
	 * allowed to cross subdomains.
	 */

			while(*host) {
109
				char h = git__tolower(*host);
110
				if (c == h)
111
					return gitno__match_host(pattern, host++);
112
				if (h == '.')
113
					return gitno__match_host(pattern, host);
114
				host++;
115
			}
116
			return -1;
117 118
		}

119
		if (c != git__tolower(*host++))
120 121 122 123 124
			return -1;
	}

	return -1;
}