1
/******************************************************
2
Copyright (c) 2011 Percona Ireland Ltd.
4
The xbcrypt format reader implementation.
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; version 2 of the License.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
*******************************************************/
23
struct xb_rcrypt_struct {
25
xb_crypt_read_callback *read;
32
xb_crypt_read_open(void *userdata, xb_crypt_read_callback *onread)
38
crypt = (xb_rcrypt_t *) my_malloc(sizeof(xb_rcrypt_t), MYF(MY_FAE));
40
crypt->userdata = userdata;
50
xb_crypt_read_chunk(xb_rcrypt_t *crypt, void **buf, size_t *olen, size_t *elen)
52
uchar tmpbuf[XB_CRYPT_CHUNK_MAGIC_SIZE + 8 + 8 + 8 + 4];
55
ulong checksum, checksum_exp;
57
xb_rcrypt_result_t result = XB_CRYPT_READ_CHUNK;
59
if ((bytesread = crypt->read(crypt->userdata, tmpbuf, sizeof(tmpbuf)))
62
result = XB_CRYPT_READ_EOF;
65
msg("%s:%s: unable to read chunk header data at "
67
my_progname, __FUNCTION__, crypt->offset);
68
result = XB_CRYPT_READ_ERROR;
75
if (memcmp(ptr, XB_CRYPT_CHUNK_MAGIC, XB_CRYPT_CHUNK_MAGIC_SIZE)
77
msg("%s:%s: wrong chunk magic at offset 0x%llx.\n",
78
my_progname, __FUNCTION__, crypt->offset);
79
result = XB_CRYPT_READ_ERROR;
83
ptr += XB_CRYPT_CHUNK_MAGIC_SIZE;
84
crypt->offset += XB_CRYPT_CHUNK_MAGIC_SIZE;
86
tmp = uint8korr(ptr); /* reserved */
90
tmp = uint8korr(ptr); /* original size */
93
msg("%s:%s: invalid original size at offset 0x%llx.\n",
94
my_progname, __FUNCTION__, crypt->offset);
95
result = XB_CRYPT_READ_ERROR;
101
tmp = uint8korr(ptr); /* encrypted size */
104
msg("%s:%s: invalid encrypted size at offset 0x%llx.\n",
105
my_progname, __FUNCTION__, crypt->offset);
106
result = XB_CRYPT_READ_ERROR;
112
checksum_exp = uint4korr(ptr); /* checksum */
116
if (*olen > crypt->bufsize) {
118
crypt->buffer = my_realloc(crypt->buffer, *olen,
120
if (crypt->buffer == NULL) {
121
msg("%s:%s: failed to increase buffer to "
122
"%llu bytes.\n", my_progname, __FUNCTION__,
124
result = XB_CRYPT_READ_ERROR;
128
crypt->buffer = my_malloc(*olen, MYF(MY_WME));
129
if (crypt->buffer == NULL) {
130
msg("%s:%s: failed to allocate buffer of "
131
"%llu bytes.\n", my_progname, __FUNCTION__,
133
result = XB_CRYPT_READ_ERROR;
137
crypt->bufsize = *olen;
141
if (crypt->read(crypt->userdata, crypt->buffer, *elen)
143
msg("%s:%s: failed to read %lld bytes for chunk payload "
144
"at offset 0x%llx.\n", my_progname, __FUNCTION__,
145
(ulonglong)*elen, crypt->offset);
146
result = XB_CRYPT_READ_ERROR;
151
checksum = crc32(0, crypt->buffer, *elen);
152
if (checksum != checksum_exp) {
153
msg("%s:%s invalid checksum at offset 0x%llx, "
154
"expected 0x%lx, actual 0x%lx.\n", my_progname, __FUNCTION__,
155
crypt->offset, checksum_exp, checksum);
156
result = XB_CRYPT_READ_ERROR;
160
crypt->offset += *elen;
161
*buf = crypt->buffer;
172
int xb_crypt_read_close(xb_rcrypt_t *crypt)
175
MY_FREE(crypt->buffer);