~gl-az/percona-xtrabackup/2.1-io-block-size

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/******************************************************
Copyright (c) 2011 Percona Ireland Ltd.

The xbcrypt format reader implementation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

*******************************************************/

#include "xbcrypt.h"

struct xb_rcrypt_struct {
	void				*userdata;
	xb_crypt_read_callback		*read;
	void				*buffer;
	size_t				bufsize;
	ulonglong			offset;
};

xb_rcrypt_t *
xb_crypt_read_open(void *userdata, xb_crypt_read_callback *onread)
{
	xb_rcrypt_t	*crypt;

	xb_ad(onread);

	crypt = (xb_rcrypt_t *) my_malloc(sizeof(xb_rcrypt_t), MYF(MY_FAE));

	crypt->userdata = userdata;
	crypt->read = onread;
	crypt->buffer = NULL;
	crypt->bufsize = 0;
	crypt->offset = 0;

	return crypt;
}

xb_rcrypt_result_t
xb_crypt_read_chunk(xb_rcrypt_t *crypt, void **buf, size_t *olen, size_t *elen)
{
	uchar		tmpbuf[XB_CRYPT_CHUNK_MAGIC_SIZE + 8 + 8 + 8 + 4];
	uchar		*ptr;
	ulonglong	tmp;
	ulong		checksum, checksum_exp;
	ssize_t		bytesread;
	xb_rcrypt_result_t result = XB_CRYPT_READ_CHUNK;

	if ((bytesread = crypt->read(crypt->userdata, tmpbuf, sizeof(tmpbuf)))
	    != sizeof(tmpbuf)) {
		if (bytesread == 0) {
			result = XB_CRYPT_READ_EOF;
			goto err;
		} else {
			msg("%s:%s: unable to read chunk header data at "
			    "offset 0x%llx.\n",
			    my_progname, __FUNCTION__, crypt->offset);
			result =  XB_CRYPT_READ_ERROR;
			goto err;
		}
	}

	ptr = tmpbuf;

	if (memcmp(ptr, XB_CRYPT_CHUNK_MAGIC, XB_CRYPT_CHUNK_MAGIC_SIZE)
	    != 0) {
		msg("%s:%s: wrong chunk magic at offset 0x%llx.\n",
		    my_progname, __FUNCTION__, crypt->offset);
		result = XB_CRYPT_READ_ERROR;
		goto err;
	}

	ptr += XB_CRYPT_CHUNK_MAGIC_SIZE;
	crypt->offset += XB_CRYPT_CHUNK_MAGIC_SIZE;

	tmp = uint8korr(ptr);	/* reserved */
	ptr += 8;
	crypt->offset += 8;

	tmp = uint8korr(ptr);	/* original size */
	ptr += 8;
	if (tmp > INT_MAX) {
		msg("%s:%s: invalid original size at offset 0x%llx.\n",
		    my_progname, __FUNCTION__, crypt->offset);
		result = XB_CRYPT_READ_ERROR;
		goto err;
	}
	crypt->offset += 8;
	*olen = (size_t)tmp;

	tmp = uint8korr(ptr);	/* encrypted size */
	ptr += 8;
	if (tmp > INT_MAX) {
		msg("%s:%s: invalid encrypted size at offset 0x%llx.\n",
		    my_progname, __FUNCTION__, crypt->offset);
		result = XB_CRYPT_READ_ERROR;
		goto err;
	}
	crypt->offset += 8;
	*elen = (size_t)tmp;

	checksum_exp = uint4korr(ptr);	/* checksum */
	ptr += 4;
	crypt->offset += 4;

	if (*olen > crypt->bufsize) {
		if (crypt->buffer) {
			crypt->buffer = my_realloc(crypt->buffer, *olen,
						   MYF(MY_WME));
			if (crypt->buffer == NULL) {
				msg("%s:%s: failed to increase buffer to "
				    "%llu bytes.\n", my_progname, __FUNCTION__,
				    (ulonglong)*olen);
				result = XB_CRYPT_READ_ERROR;
				goto err;
			}
		} else {
			crypt->buffer = my_malloc(*olen, MYF(MY_WME));
			if (crypt->buffer == NULL) {
				msg("%s:%s: failed to allocate buffer of "
				    "%llu bytes.\n", my_progname, __FUNCTION__,
				    (ulonglong)*olen);
				result = XB_CRYPT_READ_ERROR;
				goto err;
			}
		}
		crypt->bufsize = *olen;
	}

	if (*elen > 0) {
		if (crypt->read(crypt->userdata, crypt->buffer, *elen)
		    != (ssize_t)*elen) {
			msg("%s:%s: failed to read %lld bytes for chunk payload "
			    "at offset 0x%llx.\n", my_progname, __FUNCTION__,
			    (ulonglong)*elen, crypt->offset);
			result = XB_CRYPT_READ_ERROR;
			goto err;
		}
	}

	checksum = crc32(0, crypt->buffer, *elen);
	if (checksum != checksum_exp) {
		msg("%s:%s invalid checksum at offset 0x%llx, "
		    "expected 0x%lx, actual 0x%lx.\n", my_progname, __FUNCTION__,
		    crypt->offset, checksum_exp, checksum);
		result = XB_CRYPT_READ_ERROR;
		goto err;
	}

	crypt->offset += *elen;
	*buf = crypt->buffer;
	goto exit;

err:
	*buf = NULL;
	*olen = 0;
	*elen = 0;
exit:
	return result;
}

int xb_crypt_read_close(xb_rcrypt_t *crypt)
{
	if (crypt->buffer)
		MY_FREE(crypt->buffer);
	MY_FREE(crypt);

	return 0;
}