2013-11-10 17:39:42 +00:00
|
|
|
/*
|
|
|
|
* This file is a part of Pcompress, a chunked parallel multi-
|
|
|
|
* algorithm lossless compression and decompression program.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2013 Moinak Ghosh. All rights reserved.
|
|
|
|
* Use is subject to license terms.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this program.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* moinakg@belenix.org, http://moinakg.wordpress.com/
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <utils.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <archive.h>
|
|
|
|
#include <archive_entry.h>
|
|
|
|
#include "pc_arc_filter.h"
|
|
|
|
#include "pc_archive.h"
|
|
|
|
|
|
|
|
#define PACKJPG_DEF_BUFSIZ (512 * 1024)
|
2013-11-26 15:54:01 +00:00
|
|
|
#define JPG_SIZE_LIMIT (8 * 1024 * 1024)
|
2013-12-08 17:54:06 +00:00
|
|
|
#define PJG_APPVERSION1 (25)
|
|
|
|
#define PJG_APPVERSION2 (25)
|
2013-11-10 17:39:42 +00:00
|
|
|
|
2013-12-11 18:52:15 +00:00
|
|
|
struct scratch_buffer {
|
2013-11-26 15:54:01 +00:00
|
|
|
uchar_t *in_buff;
|
|
|
|
size_t in_bufflen;
|
2013-11-10 17:39:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern size_t packjpg_filter_process(uchar_t *in_buf, size_t len, uchar_t **out_buf);
|
|
|
|
|
2013-11-13 17:58:01 +00:00
|
|
|
int64_t packjpg_filter(struct filter_info *fi, void *filter_private);
|
2013-11-10 17:39:42 +00:00
|
|
|
|
|
|
|
void
|
2013-11-14 16:24:46 +00:00
|
|
|
add_filters_by_type(struct type_data *typetab, struct filter_flags *ff)
|
2013-11-10 17:39:42 +00:00
|
|
|
{
|
2013-12-11 18:52:15 +00:00
|
|
|
struct scratch_buffer *sdat;
|
2013-11-13 17:58:01 +00:00
|
|
|
int slot;
|
2013-11-10 17:39:42 +00:00
|
|
|
|
2013-11-14 16:24:46 +00:00
|
|
|
if (ff->enable_packjpg) {
|
2013-12-11 18:52:15 +00:00
|
|
|
sdat = (struct scratch_buffer *)malloc(sizeof (struct scratch_buffer));
|
|
|
|
sdat->in_buff = NULL;
|
|
|
|
sdat->in_bufflen = 0;
|
2013-11-14 16:24:46 +00:00
|
|
|
|
|
|
|
slot = TYPE_JPEG >> 3;
|
2013-12-11 18:52:15 +00:00
|
|
|
typetab[slot].filter_private = sdat;
|
2013-11-14 16:24:46 +00:00
|
|
|
typetab[slot].filter_func = packjpg_filter;
|
|
|
|
typetab[slot].filter_name = "packJPG";
|
|
|
|
}
|
2013-11-13 17:58:01 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 18:52:15 +00:00
|
|
|
static void
|
|
|
|
ensure_buffer(struct scratch_buffer *sdat, uint64_t len)
|
|
|
|
{
|
|
|
|
if (sdat->in_bufflen < len) {
|
|
|
|
if (sdat->in_buff) free(sdat->in_buff);
|
|
|
|
sdat->in_bufflen = len;
|
|
|
|
sdat->in_buff = malloc(sdat->in_bufflen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 16:24:46 +00:00
|
|
|
/*
|
|
|
|
* Copy current entry data from the archive being extracted into the given buffer.
|
|
|
|
*/
|
2013-11-13 17:58:01 +00:00
|
|
|
static ssize_t
|
|
|
|
copy_archive_data(struct archive *ar, uchar_t *out_buf)
|
|
|
|
{
|
|
|
|
int64_t offset;
|
|
|
|
const void *buff;
|
|
|
|
size_t size, tot;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
tot = 0;
|
|
|
|
for (;;) {
|
|
|
|
r = archive_read_data_block(ar, &buff, &size, &offset);
|
|
|
|
if (r == ARCHIVE_EOF)
|
|
|
|
break;
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
return (0);
|
|
|
|
memcpy(out_buf + offset, buff, size);
|
|
|
|
tot += size;
|
2013-11-10 17:39:42 +00:00
|
|
|
}
|
2013-11-13 17:58:01 +00:00
|
|
|
return (tot);
|
2013-11-10 17:39:42 +00:00
|
|
|
}
|
|
|
|
|
2013-11-14 16:24:46 +00:00
|
|
|
/*
|
|
|
|
* Copy the given buffer into the archive stream.
|
|
|
|
*/
|
2013-11-13 17:58:01 +00:00
|
|
|
static ssize_t
|
|
|
|
write_archive_data(struct archive *aw, uchar_t *out_buf, size_t len, int block_size)
|
|
|
|
{
|
|
|
|
int64_t offset;
|
|
|
|
uchar_t *buff;
|
|
|
|
int r;
|
|
|
|
size_t tot;
|
|
|
|
|
|
|
|
buff = out_buf;
|
|
|
|
offset = 0;
|
|
|
|
tot = len;
|
|
|
|
while (len > 0) {
|
|
|
|
if (len < block_size)
|
|
|
|
block_size = len;
|
|
|
|
r = (int)archive_write_data_block(aw, buff, block_size, offset);
|
|
|
|
if (r < ARCHIVE_WARN)
|
|
|
|
r = ARCHIVE_WARN;
|
|
|
|
if (r != ARCHIVE_OK) {
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
offset += block_size;
|
|
|
|
len -= block_size;
|
2013-11-14 16:24:46 +00:00
|
|
|
buff += block_size;
|
2013-11-13 17:58:01 +00:00
|
|
|
}
|
|
|
|
return (tot);
|
|
|
|
}
|
|
|
|
|
2013-12-08 17:54:06 +00:00
|
|
|
int
|
|
|
|
pjg_version_supported(char ver)
|
|
|
|
{
|
|
|
|
return (ver >= PJG_APPVERSION1 && ver <= PJG_APPVERSION2);
|
|
|
|
}
|
|
|
|
|
2013-11-13 17:58:01 +00:00
|
|
|
ssize_t
|
2013-11-10 17:39:42 +00:00
|
|
|
packjpg_filter(struct filter_info *fi, void *filter_private)
|
|
|
|
{
|
2013-12-11 18:52:15 +00:00
|
|
|
struct scratch_buffer *sdat = (struct scratch_buffer *)filter_private;
|
2013-11-10 17:39:42 +00:00
|
|
|
uchar_t *mapbuf, *out;
|
2013-11-22 15:14:26 +00:00
|
|
|
uint64_t len, in_size = 0, len1;
|
2013-11-26 15:54:01 +00:00
|
|
|
ssize_t rv;
|
2013-11-10 17:39:42 +00:00
|
|
|
|
|
|
|
len = archive_entry_size(fi->entry);
|
2013-11-14 16:24:46 +00:00
|
|
|
len1 = len;
|
2013-11-10 17:39:42 +00:00
|
|
|
if (len > JPG_SIZE_LIMIT) // Bork on massive JPEGs
|
2013-11-13 17:58:01 +00:00
|
|
|
return (FILTER_RETURN_SKIP);
|
2013-11-10 17:39:42 +00:00
|
|
|
|
2013-11-13 17:58:01 +00:00
|
|
|
if (fi->compressing) {
|
|
|
|
mapbuf = mmap(NULL, len, PROT_READ, MAP_SHARED, fi->fd, 0);
|
|
|
|
if (mapbuf == NULL) {
|
|
|
|
log_msg(LOG_ERR, 1, "Mmap failed in packJPG filter.");
|
|
|
|
return (FILTER_RETURN_ERROR);
|
|
|
|
}
|
2013-11-10 17:39:42 +00:00
|
|
|
|
2013-11-13 17:58:01 +00:00
|
|
|
/*
|
2013-12-08 17:54:06 +00:00
|
|
|
* We are trying to compress and this is not a proper jpeg. Skip.
|
2013-11-13 17:58:01 +00:00
|
|
|
*/
|
2013-12-08 17:54:06 +00:00
|
|
|
if (mapbuf[0] != 0xFF || mapbuf[1] != 0xD8) {
|
|
|
|
munmap(mapbuf, len);
|
|
|
|
return (FILTER_RETURN_SKIP);
|
|
|
|
}
|
|
|
|
if (strncmp((char *)&mapbuf[6], "Exif", 4) != 0 &&
|
|
|
|
strncmp((char *)&mapbuf[6], "JFIF", 4) != 0) {
|
2013-11-13 17:58:01 +00:00
|
|
|
munmap(mapbuf, len);
|
|
|
|
return (FILTER_RETURN_SKIP);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Allocate input buffer and read archive data stream for the entry
|
|
|
|
* into this buffer.
|
|
|
|
*/
|
2013-12-11 18:52:15 +00:00
|
|
|
ensure_buffer(sdat, len);
|
|
|
|
if (sdat->in_buff == NULL) {
|
|
|
|
log_msg(LOG_ERR, 1, "Out of memory.");
|
|
|
|
return (FILTER_RETURN_ERROR);
|
2013-11-13 17:58:01 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 18:52:15 +00:00
|
|
|
in_size = copy_archive_data(fi->source_arc, sdat->in_buff);
|
2013-11-13 17:58:01 +00:00
|
|
|
if (in_size != len) {
|
|
|
|
log_msg(LOG_ERR, 0, "Failed to read archive data.");
|
|
|
|
return (FILTER_RETURN_ERROR);
|
|
|
|
}
|
2013-11-14 16:24:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First 8 bytes in the data is the compressed size of the entry.
|
|
|
|
* LibArchive always zero-pads entries to their original size so
|
|
|
|
* we need to separately store the compressed size.
|
|
|
|
*/
|
2013-12-11 18:52:15 +00:00
|
|
|
in_size = LE64(U64_P(sdat->in_buff));
|
|
|
|
mapbuf = sdat->in_buff + 8;
|
2013-11-13 17:58:01 +00:00
|
|
|
|
|
|
|
/*
|
2013-11-26 15:54:01 +00:00
|
|
|
* We are trying to decompress and this is not a packJPG file.
|
2013-12-08 17:54:06 +00:00
|
|
|
* Write the raw data and skip. Third byte in PackJPG file is
|
|
|
|
* version number. We also check if it is supported.
|
2013-11-13 17:58:01 +00:00
|
|
|
*/
|
2013-12-08 17:54:06 +00:00
|
|
|
if (mapbuf[0] != 'J' || mapbuf[1] != 'S' || !pjg_version_supported(mapbuf[2])) {
|
2013-12-11 18:52:15 +00:00
|
|
|
return (write_archive_data(fi->target_arc, sdat->in_buff,
|
2013-11-26 15:54:01 +00:00
|
|
|
len, fi->block_size));
|
2013-11-13 17:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compression case.
|
|
|
|
*/
|
|
|
|
if (fi->compressing) {
|
2013-11-26 15:54:01 +00:00
|
|
|
out = NULL;
|
|
|
|
len = packjpg_filter_process(mapbuf, len, &out);
|
|
|
|
if (len == 0 || len >= (len1 - 8)) {
|
2013-11-15 17:32:09 +00:00
|
|
|
munmap(mapbuf, len1);
|
2013-11-26 15:54:01 +00:00
|
|
|
free(out);
|
2013-11-13 17:58:01 +00:00
|
|
|
return (FILTER_RETURN_SKIP);
|
2013-11-10 17:39:42 +00:00
|
|
|
}
|
2013-11-15 17:32:09 +00:00
|
|
|
munmap(mapbuf, len1);
|
2013-11-15 15:36:23 +00:00
|
|
|
|
2013-11-13 17:58:01 +00:00
|
|
|
in_size = LE64(len);
|
|
|
|
rv = archive_write_data(fi->target_arc, &in_size, 8);
|
|
|
|
if (rv != 8)
|
|
|
|
return (rv);
|
2013-11-26 15:54:01 +00:00
|
|
|
rv = archive_write_data(fi->target_arc, out, len);
|
|
|
|
free(out);
|
|
|
|
return (rv);
|
2013-11-10 17:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-13 17:58:01 +00:00
|
|
|
* Decompression case.
|
2013-11-10 17:39:42 +00:00
|
|
|
*/
|
2013-11-26 15:54:01 +00:00
|
|
|
out = NULL;
|
2013-11-13 17:58:01 +00:00
|
|
|
if ((len = packjpg_filter_process(mapbuf, in_size, &out)) == 0) {
|
2013-11-14 16:24:46 +00:00
|
|
|
/*
|
|
|
|
* If filter failed we write out the original data and indicate skip
|
|
|
|
* to continue the archive extraction.
|
|
|
|
*/
|
2013-11-26 15:54:01 +00:00
|
|
|
free(out);
|
2013-11-14 16:24:46 +00:00
|
|
|
if (write_archive_data(fi->target_arc, mapbuf, len1, fi->block_size) < len1)
|
|
|
|
return (FILTER_RETURN_ERROR);
|
|
|
|
return (FILTER_RETURN_SKIP);
|
2013-11-10 17:39:42 +00:00
|
|
|
}
|
2013-11-26 15:54:01 +00:00
|
|
|
rv = write_archive_data(fi->target_arc, out, len, fi->block_size);
|
|
|
|
free(out);
|
|
|
|
return (rv);
|
2013-11-10 17:39:42 +00:00
|
|
|
}
|
|
|
|
|