const char * archive_version_details(void) { static struct archive_string str; static int init = 0; if (!init) { archive_string_init(&str); archive_strcat(&str, ARCHIVE_VERSION_STRING); #ifdef HAVE_ZLIB_H archive_strcat(&str, " zlib/"); archive_strcat(&str, ZLIB_VERSION); #endif #ifdef HAVE_LZMA_H archive_strcat(&str, " liblzma/"); archive_strcat(&str, LZMA_VERSION_STRING); #endif #ifdef HAVE_BZLIB_H { const char *p = BZ2_bzlibVersion(); const char *sep = strchr(p, ','); if (sep == NULL) sep = p + strlen(p); archive_strcat(&str, " bz2lib/"); archive_strncat(&str, p, sep - p); } #endif #if defined(HAVE_LZ4_H) && defined(HAVE_LIBLZ4) archive_string_sprintf(&str, " liblz4/%d.%d.%d", LZ4_VERSION_MAJOR, LZ4_VERSION_MINOR, LZ4_VERSION_RELEASE); #endif } return str.s; }
static void test_archive_strncat(void) { struct archive_string s; archive_string_init(&s); assertExactString(0, 0, NULL, s); /* perfect length */ assert(&s == archive_strncat(&s, "snafu", 5)); assertExactString(5, EXTENT, "snafu", s); /* short read */ assert(&s == archive_strncat(&s, "barbazqux", 3)); assertExactString(8, EXTENT, "snafubar", s); /* long read is ok too! */ assert(&s == archive_strncat(&s, "snafu", 8)); assertExactString(13, EXTENT, "snafubarsnafu", s); }
static void mtree_quote(struct archive_string *s, const char *str) { const char *start; char buf[4]; unsigned char c; for (start = str; *str != '\0'; ++str) { if (mtree_safe_char(*str)) continue; if (start != str) archive_strncat(s, start, str - start); c = (unsigned char)*str; buf[0] = '\\'; buf[1] = (c / 64) + '0'; buf[2] = (c / 8 % 8) + '0'; buf[3] = (c % 8) + '0'; archive_strncat(s, buf, 4); start = str + 1; } if (start != str) archive_strncat(s, start, str - start); }
static void mtree_indent(struct mtree_writer *mtree) { int i, fn; const char *r, *s, *x; fn = 1; s = r = mtree->ebuf.s; x = NULL; while (*r == ' ') r++; while ((r = strchr(r, ' ')) != NULL) { if (fn) { fn = 0; archive_strncat(&mtree->buf, s, r - s); if (r -s > INDENTNAMELEN) { archive_strncat(&mtree->buf, " \\\n", 3); for (i = 0; i < (INDENTNAMELEN + 1); i++) archive_strappend_char(&mtree->buf, ' '); } else { for (i = r -s; i < (INDENTNAMELEN + 1); i++) archive_strappend_char(&mtree->buf, ' '); } s = ++r; x = NULL; continue; } if (r - s <= MAXLINELEN - 3 - INDENTNAMELEN) x = r++; else { if (x == NULL) x = r; archive_strncat(&mtree->buf, s, x - s); archive_strncat(&mtree->buf, " \\\n", 3); for (i = 0; i < (INDENTNAMELEN + 1); i++) archive_strappend_char(&mtree->buf, ' '); s = r = ++x; x = NULL; } } if (x != NULL && strlen(s) > MAXLINELEN - 3 - INDENTNAMELEN) { /* Last keyword is longer. */ archive_strncat(&mtree->buf, s, x - s); archive_strncat(&mtree->buf, " \\\n", 3); for (i = 0; i < (INDENTNAMELEN + 1); i++) archive_strappend_char(&mtree->buf, ' '); s = ++x; } archive_strcat(&mtree->buf, s); archive_string_empty(&mtree->ebuf); }
/* * Copy the given string to the buffer, quoting all shell meta characters * found. */ static void shar_quote(struct archive_string *buf, const char *str, int in_shell) { static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~"; size_t len; while (*str != '\0') { if ((len = strcspn(str, meta)) != 0) { archive_strncat(buf, str, len); str += len; } else if (*str == '\n') { if (in_shell) archive_strcat(buf, "\"\n\""); else archive_strcat(buf, "\\n"); ++str; } else { archive_strappend_char(buf, '\\'); archive_strappend_char(buf, *str); ++str; } } }
const char * archive_version_details(void) { static struct archive_string str; static int init = 0; const char *zlib = archive_zlib_version(); const char *liblzma = archive_liblzma_version(); const char *bzlib = archive_bzlib_version(); const char *liblz4 = archive_liblz4_version(); if (!init) { archive_string_init(&str); archive_strcat(&str, ARCHIVE_VERSION_STRING); if (zlib != NULL) { archive_strcat(&str, " zlib/"); archive_strcat(&str, zlib); } if (liblzma) { archive_strcat(&str, " liblzma/"); archive_strcat(&str, liblzma); } if (bzlib) { const char *p = bzlib; const char *sep = strchr(p, ','); if (sep == NULL) sep = p + strlen(p); archive_strcat(&str, " bz2lib/"); archive_strncat(&str, p, sep - p); } if (liblz4) { archive_strcat(&str, " liblz4/"); archive_strcat(&str, liblz4); } } return str.s; }
/* private routines */ static void xstrftime(struct archive_string *as, const char *fmt, time_t t) { /** like strftime(3) but for time_t objects */ struct tm *rt; #if defined(HAVE_GMTIME_R) || defined(HAVE__GMTIME64_S) struct tm timeHere; #endif char strtime[100]; size_t len; #ifdef HAVE_GMTIME_R if ((rt = gmtime_r(&t, &timeHere)) == NULL) return; #elif defined(HAVE__GMTIME64_S) _gmtime64_s(&timeHere, &t); #else if ((rt = gmtime(&t)) == NULL) return; #endif /* leave the hard yacker to our role model strftime() */ len = strftime(strtime, sizeof(strtime)-1, fmt, rt); archive_strncat(as, strtime, len); }
static int add_pattern_from_file(struct archive_match *a, struct match_list *mlist, int mbs, const void *pathname, int nullSeparator) { struct archive *ar; struct archive_entry *ae; struct archive_string as; const void *buff; size_t size; int64_t offset; int r; ar = archive_read_new(); if (ar == NULL) { archive_set_error(&(a->archive), ENOMEM, "No memory"); return (ARCHIVE_FATAL); } r = archive_read_support_format_raw(ar); r = archive_read_support_format_empty(ar); if (r != ARCHIVE_OK) { archive_copy_error(&(a->archive), ar); archive_read_free(ar); return (r); } if (mbs) r = archive_read_open_filename(ar, pathname, 512*20); else r = archive_read_open_filename_w(ar, pathname, 512*20); if (r != ARCHIVE_OK) { archive_copy_error(&(a->archive), ar); archive_read_free(ar); return (r); } r = archive_read_next_header(ar, &ae); if (r != ARCHIVE_OK) { archive_read_free(ar); if (r == ARCHIVE_EOF) { return (ARCHIVE_OK); } else { archive_copy_error(&(a->archive), ar); return (r); } } archive_string_init(&as); while ((r = archive_read_data_block(ar, &buff, &size, &offset)) == ARCHIVE_OK) { const char *b = (const char *)buff; while (size) { const char *s = (const char *)b; size_t length = 0; int found_separator = 0; while (length < size) { if (nullSeparator) { if (*b == '\0') { found_separator = 1; break; } } else { if (*b == 0x0d || *b == 0x0a) { found_separator = 1; break; } } b++; length++; } if (!found_separator) { archive_strncat(&as, s, length); /* Read next data block. */ break; } b++; size -= length + 1; archive_strncat(&as, s, length); /* If the line is not empty, add the pattern. */ if (archive_strlen(&as) > 0) { /* Add pattern. */ r = add_pattern_mbs(a, mlist, as.s); if (r != ARCHIVE_OK) { archive_read_free(ar); archive_string_free(&as); return (r); } archive_string_empty(&as); } } } /* If an error occurred, report it immediately. */ if (r < ARCHIVE_OK) { archive_copy_error(&(a->archive), ar); archive_read_free(ar); archive_string_free(&as); return (r); } /* If the line is not empty, add the pattern. */ if (r == ARCHIVE_EOF && archive_strlen(&as) > 0) { /* Add pattern. */ r = add_pattern_mbs(a, mlist, as.s); if (r != ARCHIVE_OK) { archive_read_free(ar); archive_string_free(&as); return (r); } } archive_read_free(ar); archive_string_free(&as); return (ARCHIVE_OK); }
static int _warc_header(struct archive_write *a, struct archive_entry *entry) { struct warc_s *w = a->format_data; struct archive_string hdr; #define MAX_HDR_SIZE 512 /* check whether warcinfo record needs outputting */ if (!w->omit_warcinfo) { ssize_t r; warc_essential_hdr_t wi = { WT_INFO, /*uri*/NULL, /*urn*/NULL, /*rtm*/0, /*mtm*/0, /*cty*/"application/warc-fields", /*len*/sizeof(warcinfo) - 1U, }; wi.rtime = w->now; wi.mtime = w->now; archive_string_init(&hdr); r = _popul_ehdr(&hdr, MAX_HDR_SIZE, wi); if (r >= 0) { /* jackpot! */ /* now also use HDR buffer for the actual warcinfo */ archive_strncat(&hdr, warcinfo, sizeof(warcinfo) -1); /* append end-of-record indicator */ archive_strncat(&hdr, "\r\n\r\n", 4); /* write to output stream */ __archive_write_output(a, hdr.s, archive_strlen(&hdr)); } /* indicate we're done with file header writing */ w->omit_warcinfo = 1U; archive_string_free(&hdr); } if (archive_entry_pathname(entry) == NULL) { archive_set_error(&a->archive, EINVAL, "Invalid filename"); return (ARCHIVE_WARN); } w->typ = archive_entry_filetype(entry); w->populz = 0U; if (w->typ == AE_IFREG) { warc_essential_hdr_t rh = { WT_RSRC, /*uri*/NULL, /*urn*/NULL, /*rtm*/0, /*mtm*/0, /*cty*/NULL, /*len*/0, }; ssize_t r; rh.tgturi = archive_entry_pathname(entry); rh.rtime = w->now; rh.mtime = archive_entry_mtime(entry); rh.cntlen = (size_t)archive_entry_size(entry); archive_string_init(&hdr); r = _popul_ehdr(&hdr, MAX_HDR_SIZE, rh); if (r < 0) { /* don't bother */ archive_set_error( &a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "cannot archive file"); return (ARCHIVE_WARN); } /* otherwise append to output stream */ __archive_write_output(a, hdr.s, r); /* and let subsequent calls to _data() know about the size */ w->populz = rh.cntlen; archive_string_free(&hdr); return (ARCHIVE_OK); } /* just resort to erroring as per Tim's advice */ archive_set_error( &a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "WARC can only process regular files"); return (ARCHIVE_FAILED); }
static void parse_rockridge(struct iso9660 *iso9660, struct file_info *file, const unsigned char *p, const unsigned char *end) { (void)iso9660; /* UNUSED */ while (p + 4 < end /* Enough space for another entry. */ && p[0] >= 'A' && p[0] <= 'Z' /* Sanity-check 1st char of name. */ && p[1] >= 'A' && p[1] <= 'Z' /* Sanity-check 2nd char of name. */ && p + p[2] <= end) { /* Sanity-check length. */ const unsigned char *data = p + 4; int data_length = p[2] - 4; int version = p[3]; /* * Yes, each 'if' here does test p[0] again. * Otherwise, the fall-through handling to catch * unsupported extensions doesn't work. */ switch(p[0]) { case 'C': if (p[0] == 'C' && p[1] == 'E' && version == 1) { /* * CE extension comprises: * 8 byte sector containing extension * 8 byte offset w/in above sector * 8 byte length of continuation */ file->ce_offset = toi(data, 4) * iso9660->logical_block_size + toi(data + 8, 4); file->ce_size = toi(data + 16, 4); break; } /* FALLTHROUGH */ case 'N': if (p[0] == 'N' && p[1] == 'M' && version == 1 && *data == 0) { /* NM extension with flag byte == 0 */ /* * NM extension comprises: * one byte flag * rest is long name */ /* TODO: Obey flags. */ char *old_name = file->name; data++; /* Skip flag byte. */ data_length--; file->name = (char *)malloc(data_length + 1); if (file->name != NULL) { free(old_name); memcpy(file->name, data, data_length); file->name[data_length] = '\0'; } else file->name = old_name; break; } /* FALLTHROUGH */ case 'P': if (p[0] == 'P' && p[1] == 'D' && version == 1) { /* * PD extension is padding; * contents are always ignored. */ break; } if (p[0] == 'P' && p[1] == 'X' && version == 1) { /* * PX extension comprises: * 8 bytes for mode, * 8 bytes for nlinks, * 8 bytes for uid, * 8 bytes for gid, * 8 bytes for inode. */ if (data_length == 32) { file->mode = toi(data, 4); file->nlinks = toi(data + 8, 4); file->uid = toi(data + 16, 4); file->gid = toi(data + 24, 4); file->inode = toi(data + 32, 4); } break; } /* FALLTHROUGH */ case 'R': if (p[0] == 'R' && p[1] == 'R' && version == 1) { iso9660->seenRockridge = 1; /* * RR extension comprises: * one byte flag value */ /* TODO: Handle RR extension. */ break; } /* FALLTHROUGH */ case 'S': if (p[0] == 'S' && p[1] == 'L' && version == 1 && *data == 0) { int cont = 1; /* SL extension with flags == 0 */ /* TODO: handle non-zero flag values. */ data++; /* Skip flag byte. */ data_length--; while (data_length > 0) { unsigned char flag = *data++; unsigned char nlen = *data++; data_length -= 2; if (cont == 0) archive_strcat(&file->symlink, "/"); cont = 0; switch(flag) { case 0x01: /* Continue */ archive_strncat(&file->symlink, (const char *)data, nlen); cont = 1; break; case 0x02: /* Current */ archive_strcat(&file->symlink, "."); break; case 0x04: /* Parent */ archive_strcat(&file->symlink, ".."); break; case 0x08: /* Root */ case 0x10: /* Volume root */ archive_string_empty(&file->symlink); break; case 0x20: /* Hostname */ archive_strcat(&file->symlink, "hostname"); break; case 0: archive_strncat(&file->symlink, (const char *)data, nlen); break; default: /* TODO: issue a warning ? */ break; } data += nlen; data_length -= nlen; } break; } if (p[0] == 'S' && p[1] == 'P' && version == 1 && data_length == 7 && data[0] == (unsigned char)'\xbe' && data[1] == (unsigned char)'\xef') { /* * SP extension stores the suspOffset * (Number of bytes to skip between * filename and SUSP records.) * It is mandatory by the SUSP standard * (IEEE 1281). * * It allows SUSP to coexist with * non-SUSP uses of the System * Use Area by placing non-SUSP data * before SUSP data. * * TODO: Add a check for 'SP' in * first directory entry, disable all SUSP * processing if not found. */ iso9660->suspOffset = data[2]; break; } if (p[0] == 'S' && p[1] == 'T' && data_length == 0 && version == 1) { /* * ST extension marks end of this * block of SUSP entries. * * It allows SUSP to coexist with * non-SUSP uses of the System * Use Area by placing non-SUSP data * after SUSP data. */ return; } case 'T': if (p[0] == 'T' && p[1] == 'F' && version == 1) { char flag = data[0]; /* * TF extension comprises: * one byte flag * create time (optional) * modify time (optional) * access time (optional) * attribute time (optional) * Time format and presence of fields * is controlled by flag bits. */ data++; if (flag & 0x80) { /* Use 17-byte time format. */ if (flag & 1) /* Create time. */ data += 17; if (flag & 2) { /* Modify time. */ file->mtime = isodate17(data); data += 17; } if (flag & 4) { /* Access time. */ file->atime = isodate17(data); data += 17; } if (flag & 8) { /* Attribute time. */ file->ctime = isodate17(data); data += 17; } } else { /* Use 7-byte time format. */ if (flag & 1) /* Create time. */ data += 7; if (flag & 2) { /* Modify time. */ file->mtime = isodate7(data); data += 7; } if (flag & 4) { /* Access time. */ file->atime = isodate7(data); data += 7; } if (flag & 8) { /* Attribute time. */ file->ctime = isodate7(data); data += 7; } } break; } /* FALLTHROUGH */ default: /* The FALLTHROUGHs above leave us here for * any unsupported extension. */ { const unsigned char *t; fprintf(stderr, "\nUnsupported RRIP extension for %s\n", file->name); fprintf(stderr, " %c%c(%d):", p[0], p[1], data_length); for (t = data; t < data + data_length && t < data + 16; t++) fprintf(stderr, " %02x", *t); fprintf(stderr, "\n"); } } p += p[2]; } }
static int slurp_central_directory(struct archive_read *a, struct zip *zip) { unsigned i; int64_t correction; static const struct archive_rb_tree_ops rb_ops = { &cmp_node, &cmp_key }; static const struct archive_rb_tree_ops rb_rsrc_ops = { &rsrc_cmp_node, &rsrc_cmp_key }; /* * Consider the archive file we are reading may be SFX. * So we have to calculate a SFX header size to revise * ZIP header offsets. */ correction = zip->end_of_central_directory_offset - (zip->central_directory_offset + zip->central_directory_size); /* The central directory offset is relative value, and so * we revise this offset for SFX. */ zip->central_directory_offset += correction; __archive_read_seek(a, zip->central_directory_offset, SEEK_SET); zip->offset = zip->central_directory_offset; __archive_rb_tree_init(&zip->tree, &rb_ops); __archive_rb_tree_init(&zip->tree_rsrc, &rb_rsrc_ops); zip->zip_entries = calloc(zip->central_directory_entries, sizeof(struct zip_entry)); for (i = 0; i < zip->central_directory_entries; ++i) { struct zip_entry *zip_entry = &zip->zip_entries[i]; size_t filename_length, extra_length, comment_length; uint32_t external_attributes; const char *name, *p, *r; if ((p = __archive_read_ahead(a, 46, NULL)) == NULL) return ARCHIVE_FATAL; if (memcmp(p, "PK\001\002", 4) != 0) { archive_set_error(&a->archive, -1, "Invalid central directory signature"); return ARCHIVE_FATAL; } zip->have_central_directory = 1; /* version = p[4]; */ zip_entry->system = p[5]; /* version_required = archive_le16dec(p + 6); */ zip_entry->flags = archive_le16dec(p + 8); zip_entry->compression = (char)archive_le16dec(p + 10); zip_entry->mtime = zip_time(p + 12); zip_entry->crc32 = archive_le32dec(p + 16); zip_entry->compressed_size = archive_le32dec(p + 20); zip_entry->uncompressed_size = archive_le32dec(p + 24); filename_length = archive_le16dec(p + 28); extra_length = archive_le16dec(p + 30); comment_length = archive_le16dec(p + 32); /* disk_start = archive_le16dec(p + 34); */ /* Better be zero. */ /* internal_attributes = archive_le16dec(p + 36); */ /* text bit */ external_attributes = archive_le32dec(p + 38); zip_entry->local_header_offset = archive_le32dec(p + 42) + correction; /* If we can't guess the mode, leave it zero here; when we read the local file header we might get more information. */ zip_entry->mode = 0; if (zip_entry->system == 3) { zip_entry->mode = external_attributes >> 16; } /* * Mac resource fork files are stored under the * "__MACOSX/" directory, so we should check if * it is. */ /* Make sure we have the file name. */ if ((p = __archive_read_ahead(a, 46 + filename_length, NULL)) == NULL) return ARCHIVE_FATAL; name = p + 46; r = rsrc_basename(name, filename_length); if (filename_length >= 9 && strncmp("__MACOSX/", name, 9) == 0) { /* If this file is not a resource fork nor * a directory. We should treat it as a non * resource fork file to expose it. */ if (name[filename_length-1] != '/' && (r - name < 3 || r[0] != '.' || r[1] != '_')) { __archive_rb_tree_insert_node(&zip->tree, &zip_entry->node); /* Expose its parent directories. */ expose_parent_dirs(zip, name, filename_length); } else { /* This file is a resource fork file or * a directory. */ archive_strncpy(&(zip_entry->rsrcname), name, filename_length); __archive_rb_tree_insert_node(&zip->tree_rsrc, &zip_entry->node); } } else { /* Generate resource fork name to find its resource * file at zip->tree_rsrc. */ archive_strcpy(&(zip_entry->rsrcname), "__MACOSX/"); archive_strncat(&(zip_entry->rsrcname), name, r - name); archive_strcat(&(zip_entry->rsrcname), "._"); archive_strncat(&(zip_entry->rsrcname), name + (r - name), filename_length - (r - name)); /* Register an entry to RB tree to sort it by * file offset. */ __archive_rb_tree_insert_node(&zip->tree, &zip_entry->node); } /* We don't read the filename until we get to the local file header. Reading it here would speed up table-of-contents operations (removing the need to find and read local file header to get the filename) at the cost of requiring a lot of extra space. */ /* We don't read the extra block here. We assume it will be duplicated at the local file header. */ __archive_read_consume(a, 46 + filename_length + extra_length + comment_length); }