diff options
Diffstat (limited to 'c/src/encarchive.c')
-rw-r--r-- | c/src/encarchive.c | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/c/src/encarchive.c b/c/src/encarchive.c new file mode 100644 index 0000000..45e840f --- /dev/null +++ b/c/src/encarchive.c @@ -0,0 +1,102 @@ +/** + * GPGSecure manages GPG encrypted archives + * Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> + * + * 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, 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "encarchive.h" + +int cd_open(struct archive *a, void *client_data) { + struct clientdata *cd = client_data; + cd->fd = open(cd->name, O_RDONLY); + return (cd->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL); +} + +la_ssize_t cd_read(struct archive *a, void *client_data, const void **buff) { + struct clientdata *cd = client_data; + *buff = cd->buff; + return (read(cd->fd, cd->buff, 10240)); +} + +int cd_close(struct archive *a, void *client_data) { + struct clientdata *cd = client_data; + + if(cd->fd > 0) + close(cd->fd); + return(ARCHIVE_OK); +} + +int enc_archive_enum(char* path) { + struct clientdata *cd; + struct archive *a; + struct archive_entry *entry; + + cd = malloc(sizeof(struct clientdata)); + a = archive_read_new(); + cd->name = path; + + // Add read support options + int r; + r = archive_read_support_filter_all(a); + if(r > 0) + return r; + r = archive_read_support_format_all(a); + if(r > 0) + return r; + + // Open the archive + archive_read_open(a, cd, cd_open, cd_read, cd_close); + while(archive_read_next_header(a, &entry) == ARCHIVE_OK) { + // Print archive filename + printf("%s\n", archive_entry_pathname(entry)); + archive_read_data_skip(a); + } + + archive_read_free(a); + free(cd); + return 0; +} + + +int enc_archive_extract(char* srcarchive, char* dest) { + struct clientdata *cd; + struct archive *a; + struct archive_entry *entry; + + cd = malloc(sizeof(struct clientdata)); + a = archive_read_new(); + cd->name = srcarchive; + + // Add read support options + int r; + r = archive_read_support_filter_all(a); + if(r > 0) + return r; + r = archive_read_support_format_all(a); + if(r > 0) + return r; + + // Open the archive + archive_read_open(a, cd, cd_open, cd_read, cd_close); + while(archive_read_next_header(a, &entry) == ARCHIVE_OK) { + archive_read_extract(a, entry, ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS); + archive_read_data_skip(a); + // Print archive filename + printf("extracting %s\n", archive_entry_pathname(entry)); + } + + archive_read_free(a); + free(cd); + return 0; +} |