static void handle_uevent(const char* udata) { const char *str = udata; char path[PATH_MAX]; char usb[2], ac[2]; int type = NO_CHARGER; memset(usb, 0, 2); memset(ac, 0, 2); ALOGE("Type: %d", type); write_path(type); (!strncmp(str, USB, strlen(USB))) ; snprintf(path, sizeof(path), "%s/usb/online", POWER_SUPPLY_PATH); read_path(path, usb, 1); snprintf(path, sizeof(path), "%s/pm8921-dc/online", POWER_SUPPLY_PATH); read_path(path, ac, 1); if (!strncmp(usb, "1", 1)) { type = CHARGER_USB; } else if (!strncmp(ac, "1", 1)) { type = CHARGER_AC; } ALOGE("Type: %d", type); write_path(type); }
void c_cmd_run(run_params* params) { int c, ac_in, ac_out, ac_err; int i = 0; int out_path = 1; char* line = (char*)malloc(sizeof(char) * MAX_LINE_LEN); pipe_out* in = params->in; pipe_in* out = params->out; pipe_in* err = params->err; if (out_path && pipe_out_is_keyboard(in)) write_path(params); ac_in = params->in->autoclose; ac_out = params->out->autoclose; ac_err = params->err->autoclose; params->in->autoclose = 0; params->out->autoclose = 0; params->err->autoclose = 0; int pos = 0; while (true) { c = pipe_read(in); if (pos >= MAX_CMD_LEN) { pipe_write_s(err, "Maximal length of a line reached.\n"); break; } if ((c == CMD_END_CHAR_LINE) || (c == -1)) { line[pos] = '\0'; int ret = parse_line(line, params); if (ret == 1) break; pos = 0; if (c == -1) { if (params->secret_params == 1) { ac_in = 1; ac_out = 1; ac_err = 1; } break; } if (out_path && pipe_out_is_keyboard(in)) write_path(params); } else { line[pos] = c; pos++; } } free(line); params->in->autoclose = ac_in; params->out->autoclose = ac_out; params->err->autoclose = ac_err; }
int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stdout, "Usage: ./rmdir_eval [path] [fanout]\n"); return -1; } int fanout = atoi(argv[2]); if (fanout > 9) { fprintf(stderr, "Too large fanout: %d > 9.\n", fanout); return -1; } char *prefix = argv[1]; g_offset = strlen(prefix); char path[MAX_LEN]; strcpy(path, prefix); int depth; for (depth = 0; depth < fanout; ++depth) { write_path(path, depth, fanout - 1); } --depth; long tran_cnt = 0; struct timeval begin, end; gettimeofday(&begin, NULL); while (depth >= 0) { end_path(path, depth); // Transaction executes. // fprintf(stdout, "%s\n", path); if (rmdir(path) != 0) { fprintf(stderr, "Error: creating dir %s.\n", path); return -1; } ++tran_cnt; int cur, i; for (i = 0; i <= depth; ++i) { cur = read_path(path, i); if (cur > 0) { dec_path(path, i); break; } else { write_path(path, i, fanout - 1); } } if (i == depth + 1) { --depth; } } gettimeofday(&end, NULL); double sec = end.tv_sec - begin.tv_sec + (double)(end.tv_usec - begin.tv_usec) / 1000000; fprintf(stdout, "# Evaluation of rmdir()\n" "# Transaction Count # Time (s) # Transactions per Second\n" "%ld\t%.2f\t%.2f\n", tran_cnt, sec, tran_cnt / sec); return 0; }
int if_init(struct interface *ifp) { char path[sizeof(PROC_PROMOTE) + IF_NAMESIZE]; int n; /* We enable promote_secondaries so that we can do this * add 192.168.1.2/24 * add 192.168.1.3/24 * del 192.168.1.2/24 * and the subnet mask moves onto 192.168.1.3/24 * This matches the behaviour of BSD which makes coding dhcpcd * a little easier as there's just one behaviour. */ snprintf(path, sizeof(path), PROC_PROMOTE, ifp->name); n = check_proc_int(path); if (n == -1) return errno == ENOENT ? 0 : -1; if (n == 1) return 0; return write_path(path, "1") == -1 ? -1 : 0; }
static int archive_write_zip_close(struct archive_write *a) { struct zip *zip; struct zip_file_header_link *l; uint8_t h[SIZE_FILE_HEADER]; uint8_t end[SIZE_CENTRAL_DIRECTORY_END]; uint8_t e[SIZE_EXTRA_DATA_CENTRAL]; int64_t offset_start, offset_end; int entries; int ret; zip = a->format_data; l = zip->central_directory; /* * Formatting central directory file header fields that are * fixed for all entries. * Fields not used (and therefor 0) are: * * - comment_length * - disk_number * - attributes_internal */ memset(h, 0, sizeof(h)); archive_le32enc(&h[FILE_HEADER_SIGNATURE], ZIP_SIGNATURE_FILE_HEADER); archive_le16enc(&h[FILE_HEADER_VERSION_BY], ZIP_VERSION_BY); archive_le16enc(&h[FILE_HEADER_VERSION_EXTRACT], ZIP_VERSION_EXTRACT); entries = 0; offset_start = zip->written_bytes; /* Formatting individual header fields per entry and * writing each entry. */ while (l != NULL) { archive_le16enc(&h[FILE_HEADER_FLAGS], l->flags); archive_le16enc(&h[FILE_HEADER_COMPRESSION], l->compression); archive_le32enc(&h[FILE_HEADER_TIMEDATE], dos_time(archive_entry_mtime(l->entry))); archive_le32enc(&h[FILE_HEADER_CRC32], l->crc32); archive_le32enc(&h[FILE_HEADER_COMPRESSED_SIZE], (uint32_t)l->compressed_size); archive_le32enc(&h[FILE_HEADER_UNCOMPRESSED_SIZE], (uint32_t)archive_entry_size(l->entry)); archive_le16enc(&h[FILE_HEADER_FILENAME_LENGTH], (uint16_t)path_length(l->entry)); archive_le16enc(&h[FILE_HEADER_EXTRA_LENGTH], sizeof(e)); archive_le16enc(&h[FILE_HEADER_ATTRIBUTES_EXTERNAL+2], archive_entry_mode(l->entry)); archive_le32enc(&h[FILE_HEADER_OFFSET], (uint32_t)l->offset); /* Formatting extra data. */ archive_le16enc(&e[EXTRA_DATA_CENTRAL_TIME_ID], ZIP_SIGNATURE_EXTRA_TIMESTAMP); archive_le16enc(&e[EXTRA_DATA_CENTRAL_TIME_SIZE], 1 + 4); e[EXTRA_DATA_CENTRAL_TIME_FLAG] = 0x07; archive_le32enc(&e[EXTRA_DATA_CENTRAL_MTIME], (uint32_t)archive_entry_mtime(l->entry)); archive_le16enc(&e[EXTRA_DATA_CENTRAL_UNIX_ID], ZIP_SIGNATURE_EXTRA_NEW_UNIX); archive_le16enc(&e[EXTRA_DATA_CENTRAL_UNIX_SIZE], 0x0000); ret = __archive_write_output(a, h, sizeof(h)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(h); ret = write_path(l->entry, a); if (ret <= ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += ret; ret = __archive_write_output(a, e, sizeof(e)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(e); l = l->next; entries++; } offset_end = zip->written_bytes; /* Formatting end of central directory. */ memset(end, 0, sizeof(end)); archive_le32enc(&end[CENTRAL_DIRECTORY_END_SIGNATURE], ZIP_SIGNATURE_CENTRAL_DIRECTORY_END); archive_le16enc(&end[CENTRAL_DIRECTORY_END_ENTRIES_DISK], entries); archive_le16enc(&end[CENTRAL_DIRECTORY_END_ENTRIES], entries); archive_le32enc(&end[CENTRAL_DIRECTORY_END_SIZE], (uint32_t)(offset_end - offset_start)); archive_le32enc(&end[CENTRAL_DIRECTORY_END_OFFSET], (uint32_t)offset_start); /* Writing end of central directory. */ ret = __archive_write_output(a, end, sizeof(end)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(end); return (ARCHIVE_OK); }
static int archive_write_zip_header(struct archive_write *a, struct archive_entry *entry) { struct zip *zip; uint8_t h[SIZE_LOCAL_FILE_HEADER]; uint8_t e[SIZE_EXTRA_DATA_LOCAL]; uint8_t *d; struct zip_file_header_link *l; struct archive_string_conv *sconv; int ret, ret2 = ARCHIVE_OK; int64_t size; mode_t type; /* Entries other than a regular file or a folder are skipped. */ type = archive_entry_filetype(entry); if (type != AE_IFREG && type != AE_IFDIR && type != AE_IFLNK) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Filetype not supported"); return ARCHIVE_FAILED; }; /* Directory entries should have a size of 0. */ if (type == AE_IFDIR) archive_entry_set_size(entry, 0); zip = a->format_data; /* Setup default conversion. */ if (zip->opt_sconv == NULL && !zip->init_default_conversion) { zip->sconv_default = archive_string_default_conversion_for_write(&(a->archive)); zip->init_default_conversion = 1; } if (zip->flags == 0) { /* Initialize the general purpose flags. */ zip->flags = ZIP_FLAGS; if (zip->opt_sconv != NULL) { if (strcmp(archive_string_conversion_charset_name( zip->opt_sconv), "UTF-8") == 0) zip->flags |= ZIP_FLAGS_UTF8_NAME; #if HAVE_NL_LANGINFO } else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) { zip->flags |= ZIP_FLAGS_UTF8_NAME; #endif } } d = zip->data_descriptor; size = archive_entry_size(entry); zip->remaining_data_bytes = size; /* Append archive entry to the central directory data. */ l = (struct zip_file_header_link *) malloc(sizeof(*l)); if (l == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate zip header data"); return (ARCHIVE_FATAL); } #if defined(_WIN32) && !defined(__CYGWIN__) /* Make sure the path separators in pahtname, hardlink and symlink * are all slash '/', not the Windows path separator '\'. */ l->entry = __la_win_entry_in_posix_pathseparator(entry); if (l->entry == entry) l->entry = archive_entry_clone(entry); #else l->entry = archive_entry_clone(entry); #endif if (l->entry == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate zip header data"); free(l); return (ARCHIVE_FATAL); } l->flags = zip->flags; if (zip->opt_sconv != NULL) sconv = zip->opt_sconv; else sconv = zip->sconv_default; if (sconv != NULL) { const char *p; size_t len; if (archive_entry_pathname_l(entry, &p, &len, sconv) != 0) { if (errno == ENOMEM) { archive_entry_free(l->entry); free(l); archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for Pathname"); return (ARCHIVE_FATAL); } archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Can't translate Pathname '%s' to %s", archive_entry_pathname(entry), archive_string_conversion_charset_name(sconv)); ret2 = ARCHIVE_WARN; } if (len > 0) archive_entry_set_pathname(l->entry, p); /* * Although there is no character-set regulation for Symlink, * it is suitable to convert a character-set of Symlinke to * what those of the Pathname has been converted to. */ if (type == AE_IFLNK) { if (archive_entry_symlink_l(entry, &p, &len, sconv)) { if (errno == ENOMEM) { archive_entry_free(l->entry); free(l); archive_set_error(&a->archive, ENOMEM, "Can't allocate memory " " for Symlink"); return (ARCHIVE_FATAL); } /* * Even if the strng conversion failed, * we should not report the error since * thre is no regulation for. */ } else if (len > 0) archive_entry_set_symlink(l->entry, p); } } /* If all characters in a filename are ASCII, Reset UTF-8 Name flag. */ if ((l->flags & ZIP_FLAGS_UTF8_NAME) != 0 && is_all_ascii(archive_entry_pathname(l->entry))) l->flags &= ~ZIP_FLAGS_UTF8_NAME; /* Initialize the CRC variable and potentially the local crc32(). */ l->crc32 = crc32(0, NULL, 0); if (type == AE_IFLNK) { const char *p = archive_entry_symlink(l->entry); if (p != NULL) size = strlen(p); else size = 0; zip->remaining_data_bytes = 0; archive_entry_set_size(l->entry, size); l->compression = COMPRESSION_STORE; l->compressed_size = size; } else { l->compression = zip->compression; l->compressed_size = 0; } l->next = NULL; if (zip->central_directory == NULL) { zip->central_directory = l; } else { zip->central_directory_end->next = l; } zip->central_directory_end = l; /* Store the offset of this header for later use in central * directory. */ l->offset = zip->written_bytes; memset(h, 0, sizeof(h)); archive_le32enc(&h[LOCAL_FILE_HEADER_SIGNATURE], ZIP_SIGNATURE_LOCAL_FILE_HEADER); archive_le16enc(&h[LOCAL_FILE_HEADER_VERSION], ZIP_VERSION_EXTRACT); archive_le16enc(&h[LOCAL_FILE_HEADER_FLAGS], l->flags); archive_le16enc(&h[LOCAL_FILE_HEADER_COMPRESSION], l->compression); archive_le32enc(&h[LOCAL_FILE_HEADER_TIMEDATE], dos_time(archive_entry_mtime(entry))); archive_le16enc(&h[LOCAL_FILE_HEADER_FILENAME_LENGTH], (uint16_t)path_length(l->entry)); switch (l->compression) { case COMPRESSION_STORE: /* Setting compressed and uncompressed sizes even when * specification says to set to zero when using data * descriptors. Otherwise the end of the data for an * entry is rather difficult to find. */ archive_le32enc(&h[LOCAL_FILE_HEADER_COMPRESSED_SIZE], (uint32_t)size); archive_le32enc(&h[LOCAL_FILE_HEADER_UNCOMPRESSED_SIZE], (uint32_t)size); break; #ifdef HAVE_ZLIB_H case COMPRESSION_DEFLATE: archive_le32enc(&h[LOCAL_FILE_HEADER_UNCOMPRESSED_SIZE], (uint32_t)size); zip->stream.zalloc = Z_NULL; zip->stream.zfree = Z_NULL; zip->stream.opaque = Z_NULL; zip->stream.next_out = zip->buf; zip->stream.avail_out = (uInt)zip->len_buf; if (deflateInit2(&zip->stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) { archive_set_error(&a->archive, ENOMEM, "Can't init deflate compressor"); return (ARCHIVE_FATAL); } break; #endif } /* Formatting extra data. */ archive_le16enc(&h[LOCAL_FILE_HEADER_EXTRA_LENGTH], sizeof(e)); archive_le16enc(&e[EXTRA_DATA_LOCAL_TIME_ID], ZIP_SIGNATURE_EXTRA_TIMESTAMP); archive_le16enc(&e[EXTRA_DATA_LOCAL_TIME_SIZE], 1 + 4 * 3); e[EXTRA_DATA_LOCAL_TIME_FLAG] = 0x07; archive_le32enc(&e[EXTRA_DATA_LOCAL_MTIME], (uint32_t)archive_entry_mtime(entry)); archive_le32enc(&e[EXTRA_DATA_LOCAL_ATIME], (uint32_t)archive_entry_atime(entry)); archive_le32enc(&e[EXTRA_DATA_LOCAL_CTIME], (uint32_t)archive_entry_ctime(entry)); archive_le16enc(&e[EXTRA_DATA_LOCAL_UNIX_ID], ZIP_SIGNATURE_EXTRA_NEW_UNIX); archive_le16enc(&e[EXTRA_DATA_LOCAL_UNIX_SIZE], 1 + (1 + 4) * 2); e[EXTRA_DATA_LOCAL_UNIX_VERSION] = 1; e[EXTRA_DATA_LOCAL_UNIX_UID_SIZE] = 4; archive_le32enc(&e[EXTRA_DATA_LOCAL_UNIX_UID], (uint32_t)archive_entry_uid(entry)); e[EXTRA_DATA_LOCAL_UNIX_GID_SIZE] = 4; archive_le32enc(&e[EXTRA_DATA_LOCAL_UNIX_GID], (uint32_t)archive_entry_gid(entry)); archive_le32enc(&d[DATA_DESCRIPTOR_UNCOMPRESSED_SIZE], (uint32_t)size); ret = __archive_write_output(a, h, sizeof(h)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(h); ret = write_path(l->entry, a); if (ret <= ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += ret; ret = __archive_write_output(a, e, sizeof(e)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(e); if (type == AE_IFLNK) { const unsigned char *p; p = (const unsigned char *)archive_entry_symlink(l->entry); ret = __archive_write_output(a, p, (size_t)size); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += size; l->crc32 = crc32(l->crc32, p, (unsigned)size); } if (ret2 != ARCHIVE_OK) return (ret2); return (ARCHIVE_OK); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { FILE *fob; mxArray *internal; element_t *pe; double *pd; int compound; double uu_to_dbu; /* check argument number */ if (nrhs != 4) { mexErrMsgTxt("gds_write_element : 4 input arguments expected."); } /* get file handle argument */ fob = get_file_ptr((mxArray *)prhs[0]); /* get unit conversion factor user units --> database units */ pd = (double *)mxGetData(prhs[2]); uu_to_dbu = pd[0]; /* decide what to do */ if ( !get_field_ptr((mxArray *)prhs[1], "internal", &internal) ) mexErrMsgTxt("gds_write_element : missing internal data field."); pe = (element_t *)mxGetData(internal); switch (pe->kind) { case GDS_BOUNDARY: pd = (double *)mxGetData(prhs[3]); /* compound */ compound = (int)pd[0]; if ( compound ) write_compound_boundary(fob, (mxArray *)prhs[1], uu_to_dbu); else write_boundary(fob, (mxArray *)prhs[1], uu_to_dbu); break; case GDS_PATH: pd = (double *)mxGetData(prhs[3]); /* compound */ compound = (int)pd[0]; if ( compound ) write_compound_path(fob, (mxArray *)prhs[1], uu_to_dbu); else write_path(fob, (mxArray *)prhs[1], uu_to_dbu); break; case GDS_SREF: pd = (double *)mxGetData(prhs[3]); /* compound */ compound = (int)pd[0]; if ( compound ) write_compound_sref(fob, (mxArray *)prhs[1], uu_to_dbu); else write_sref(fob, (mxArray *)prhs[1], uu_to_dbu); break; case GDS_AREF: write_aref(fob, (mxArray *)prhs[1], uu_to_dbu); break; case GDS_TEXT: write_text(fob, (mxArray *)prhs[1], uu_to_dbu); break; case GDS_NODE: write_node(fob, (mxArray *)prhs[1], uu_to_dbu); break; case GDS_BOX: write_box(fob, (mxArray *)prhs[1], uu_to_dbu); break; default: mexErrMsgTxt("gds_write_element : unknown element type."); } }
static int archive_write_zip_close(struct archive_write *a) { struct zip *zip; struct zip_file_header_link *l; struct zip_file_header h; struct zip_central_directory_end end; struct zip_extra_data_central e; int64_t offset_start, offset_end; int entries; int ret; zip = a->format_data; l = zip->central_directory; /* * Formatting central directory file header fields that are fixed for all entries. * Fields not used (and therefor 0) are: * * - comment_length * - disk_number * - attributes_internal */ memset(&h, 0, sizeof(h)); archive_le32enc(&h.signature, ZIP_SIGNATURE_FILE_HEADER); archive_le16enc(&h.version_by, ZIP_VERSION_BY); archive_le16enc(&h.version_extract, ZIP_VERSION_EXTRACT); entries = 0; offset_start = zip->written_bytes; /* Formatting individual header fields per entry and * writing each entry. */ while (l != NULL) { archive_le16enc(&h.flags, l->flags); archive_le16enc(&h.compression, l->compression); archive_le32enc(&h.timedate, dos_time(archive_entry_mtime(l->entry))); archive_le32enc(&h.crc32, l->crc32); archive_le32enc(&h.compressed_size, l->compressed_size); archive_le32enc(&h.uncompressed_size, archive_entry_size(l->entry)); archive_le16enc(&h.filename_length, (uint16_t)path_length(l->entry)); archive_le16enc(&h.extra_length, sizeof(e)); archive_le16enc(&h.attributes_external[2], archive_entry_mode(l->entry)); archive_le32enc(&h.offset, l->offset); /* Formatting extra data. */ archive_le16enc(&e.time_id, ZIP_SIGNATURE_EXTRA_TIMESTAMP); archive_le16enc(&e.time_size, sizeof(e.mtime) + sizeof(e.time_flag)); e.time_flag[0] = 0x07; archive_le32enc(&e.mtime, archive_entry_mtime(l->entry)); archive_le16enc(&e.unix_id, ZIP_SIGNATURE_EXTRA_NEW_UNIX); archive_le16enc(&e.unix_size, 0x0000); ret = __archive_write_output(a, &h, sizeof(h)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(h); ret = write_path(l->entry, a); if (ret <= ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += ret; ret = __archive_write_output(a, &e, sizeof(e)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(e); l = l->next; entries++; } offset_end = zip->written_bytes; /* Formatting end of central directory. */ memset(&end, 0, sizeof(end)); archive_le32enc(&end.signature, ZIP_SIGNATURE_CENTRAL_DIRECTORY_END); archive_le16enc(&end.entries_disk, entries); archive_le16enc(&end.entries, entries); archive_le32enc(&end.size, offset_end - offset_start); archive_le32enc(&end.offset, offset_start); /* Writing end of central directory. */ ret = __archive_write_output(a, &end, sizeof(end)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(end); return (ARCHIVE_OK); }
static int archive_write_zip_header(struct archive_write *a, struct archive_entry *entry) { struct zip *zip; struct zip_local_file_header h; struct zip_extra_data_local e; struct zip_data_descriptor *d; struct zip_file_header_link *l; struct archive_string_conv *sconv; int ret, ret2 = ARCHIVE_OK; int64_t size; mode_t type; /* Entries other than a regular file or a folder are skipped. */ type = archive_entry_filetype(entry); if ((type != AE_IFREG) & (type != AE_IFDIR)) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Filetype not supported"); return ARCHIVE_FAILED; }; /* Directory entries should have a size of 0. */ if (type == AE_IFDIR) archive_entry_set_size(entry, 0); zip = a->format_data; /* Setup default conversion. */ if (zip->opt_sconv == NULL && !zip->init_default_conversion) { zip->sconv_default = archive_string_default_conversion_for_write(&(a->archive)); zip->init_default_conversion = 1; } if (zip->flags == 0) { /* Initialize the general purpose flags. */ zip->flags = ZIP_FLAGS; if (zip->opt_sconv != NULL) { if (strcmp(archive_string_conversion_charset_name( zip->opt_sconv), "UTF-8") == 0) zip->flags |= ZIP_FLAGS_UTF8_NAME; #if HAVE_NL_LANGINFO } else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) { zip->flags |= ZIP_FLAGS_UTF8_NAME; #endif } } d = &zip->data_descriptor; size = archive_entry_size(entry); zip->remaining_data_bytes = size; /* Append archive entry to the central directory data. */ l = (struct zip_file_header_link *) malloc(sizeof(*l)); if (l == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate zip header data"); return (ARCHIVE_FATAL); } l->entry = archive_entry_clone(entry); l->flags = zip->flags; if (zip->opt_sconv != NULL) sconv = zip->opt_sconv; else sconv = zip->sconv_default; if (sconv != NULL) { const char *p; size_t len; if (archive_entry_pathname_l(entry, &p, &len, sconv) != 0) { if (errno == ENOMEM) { archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for Pathname"); return (ARCHIVE_FATAL); } archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Can't translate pathname '%s' to %s", archive_entry_pathname(entry), archive_string_conversion_charset_name(sconv)); ret2 = ARCHIVE_WARN; } if (len > 0) archive_entry_set_pathname(l->entry, p); } /* If all character of a filename is ASCII, Reset UTF-8 Name flag. */ if ((l->flags & ZIP_FLAGS_UTF8_NAME) != 0 && is_all_ascii(archive_entry_pathname(l->entry))) l->flags &= ~ZIP_FLAGS_UTF8_NAME; /* Initialize the CRC variable and potentially the local crc32(). */ l->crc32 = crc32(0, NULL, 0); l->compression = zip->compression; l->compressed_size = 0; l->next = NULL; if (zip->central_directory == NULL) { zip->central_directory = l; } else { zip->central_directory_end->next = l; } zip->central_directory_end = l; /* Store the offset of this header for later use in central directory. */ l->offset = zip->written_bytes; memset(&h, 0, sizeof(h)); archive_le32enc(&h.signature, ZIP_SIGNATURE_LOCAL_FILE_HEADER); archive_le16enc(&h.version, ZIP_VERSION_EXTRACT); archive_le16enc(&h.flags, l->flags); archive_le16enc(&h.compression, zip->compression); archive_le32enc(&h.timedate, dos_time(archive_entry_mtime(entry))); archive_le16enc(&h.filename_length, (uint16_t)path_length(l->entry)); switch (zip->compression) { case COMPRESSION_STORE: /* Setting compressed and uncompressed sizes even when specification says * to set to zero when using data descriptors. Otherwise the end of the * data for an entry is rather difficult to find. */ archive_le32enc(&h.compressed_size, size); archive_le32enc(&h.uncompressed_size, size); break; #ifdef HAVE_ZLIB_H case COMPRESSION_DEFLATE: archive_le32enc(&h.uncompressed_size, size); zip->stream.zalloc = Z_NULL; zip->stream.zfree = Z_NULL; zip->stream.opaque = Z_NULL; zip->stream.next_out = zip->buf; zip->stream.avail_out = zip->len_buf; if (deflateInit2(&zip->stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) { archive_set_error(&a->archive, ENOMEM, "Can't init deflate compressor"); return (ARCHIVE_FATAL); } break; #endif } /* Formatting extra data. */ archive_le16enc(&h.extra_length, sizeof(e)); archive_le16enc(&e.time_id, ZIP_SIGNATURE_EXTRA_TIMESTAMP); archive_le16enc(&e.time_size, sizeof(e.time_flag) + sizeof(e.mtime) + sizeof(e.atime) + sizeof(e.ctime)); e.time_flag[0] = 0x07; archive_le32enc(&e.mtime, archive_entry_mtime(entry)); archive_le32enc(&e.atime, archive_entry_atime(entry)); archive_le32enc(&e.ctime, archive_entry_ctime(entry)); archive_le16enc(&e.unix_id, ZIP_SIGNATURE_EXTRA_NEW_UNIX); archive_le16enc(&e.unix_size, sizeof(e.unix_version) + sizeof(e.unix_uid_size) + sizeof(e.unix_uid) + sizeof(e.unix_gid_size) + sizeof(e.unix_gid)); e.unix_version = 1; e.unix_uid_size = 4; archive_le32enc(&e.unix_uid, archive_entry_uid(entry)); e.unix_gid_size = 4; archive_le32enc(&e.unix_gid, archive_entry_gid(entry)); archive_le32enc(&d->uncompressed_size, size); ret = __archive_write_output(a, &h, sizeof(h)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(h); ret = write_path(l->entry, a); if (ret <= ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += ret; ret = __archive_write_output(a, &e, sizeof(e)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(e); if (ret2 != ARCHIVE_OK) return (ret2); return (ARCHIVE_OK); }
static int archive_write_zip_header(struct archive_write *a, struct archive_entry *entry) { struct zip *zip; struct zip_local_file_header h; struct zip_extra_data_local e; struct zip_data_descriptor *d; struct zip_file_header_link *l; int ret; int64_t size; mode_t type; /* Entries other than a regular file or a folder are skipped. */ type = archive_entry_filetype(entry); if ((type != AE_IFREG) & (type != AE_IFDIR)) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Filetype not supported"); return ARCHIVE_FAILED; }; /* Directory entries should have a size of 0. */ if (type == AE_IFDIR) archive_entry_set_size(entry, 0); zip = a->format_data; d = &zip->data_descriptor; size = archive_entry_size(entry); zip->remaining_data_bytes = size; /* Append archive entry to the central directory data. */ l = (struct zip_file_header_link *) malloc(sizeof(*l)); if (l == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate zip header data"); return (ARCHIVE_FATAL); } l->entry = archive_entry_clone(entry); /* Initialize the CRC variable and potentially the local crc32(). */ l->crc32 = crc32(0, NULL, 0); l->compression = zip->compression; l->compressed_size = 0; l->next = NULL; if (zip->central_directory == NULL) { zip->central_directory = l; } else { zip->central_directory_end->next = l; } zip->central_directory_end = l; /* Store the offset of this header for later use in central directory. */ l->offset = zip->written_bytes; memset(&h, 0, sizeof(h)); archive_le32enc(&h.signature, ZIP_SIGNATURE_LOCAL_FILE_HEADER); archive_le16enc(&h.version, ZIP_VERSION_EXTRACT); archive_le16enc(&h.flags, ZIP_FLAGS); archive_le16enc(&h.compression, zip->compression); archive_le32enc(&h.timedate, dos_time(archive_entry_mtime(entry))); archive_le16enc(&h.filename_length, (uint16_t)path_length(entry)); switch (zip->compression) { case COMPRESSION_STORE: /* Setting compressed and uncompressed sizes even when specification says * to set to zero when using data descriptors. Otherwise the end of the * data for an entry is rather difficult to find. */ archive_le32enc(&h.compressed_size, size); archive_le32enc(&h.uncompressed_size, size); break; #ifdef HAVE_ZLIB_H case COMPRESSION_DEFLATE: archive_le32enc(&h.uncompressed_size, size); zip->stream.zalloc = Z_NULL; zip->stream.zfree = Z_NULL; zip->stream.opaque = Z_NULL; zip->stream.next_out = zip->buf; zip->stream.avail_out = zip->len_buf; if (deflateInit2(&zip->stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) { archive_set_error(&a->archive, ENOMEM, "Can't init deflate compressor"); return (ARCHIVE_FATAL); } break; #endif } /* Formatting extra data. */ archive_le16enc(&h.extra_length, sizeof(e)); archive_le16enc(&e.time_id, ZIP_SIGNATURE_EXTRA_TIMESTAMP); archive_le16enc(&e.time_size, sizeof(e.time_flag) + sizeof(e.mtime) + sizeof(e.atime) + sizeof(e.ctime)); e.time_flag[0] = 0x07; archive_le32enc(&e.mtime, archive_entry_mtime(entry)); archive_le32enc(&e.atime, archive_entry_atime(entry)); archive_le32enc(&e.ctime, archive_entry_ctime(entry)); archive_le16enc(&e.unix_id, ZIP_SIGNATURE_EXTRA_UNIX); archive_le16enc(&e.unix_size, sizeof(e.unix_uid) + sizeof(e.unix_gid)); archive_le16enc(&e.unix_uid, archive_entry_uid(entry)); archive_le16enc(&e.unix_gid, archive_entry_gid(entry)); archive_le32enc(&d->uncompressed_size, size); ret = (a->compressor.write)(a, &h, sizeof(h)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(h); ret = write_path(entry, a); if (ret <= ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += ret; ret = (a->compressor.write)(a, &e, sizeof(e)); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += sizeof(e); return (ARCHIVE_OK); }
static int archive_write_zip_header(struct archive_write *a, struct archive_entry *entry) { unsigned char local_header[32]; unsigned char local_extra[128]; struct zip *zip = a->format_data; unsigned char *e; unsigned char *cd_extra; size_t filename_length; const char *slink = NULL; size_t slink_size = 0; struct archive_string_conv *sconv = get_sconv(a, zip); int ret, ret2 = ARCHIVE_OK; int64_t size; mode_t type; int version_needed = 10; /* Ignore types of entries that we don't support. */ type = archive_entry_filetype(entry); if (type != AE_IFREG && type != AE_IFDIR && type != AE_IFLNK) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Filetype not supported"); return ARCHIVE_FAILED; }; /* If we're not using Zip64, reject large files. */ if (zip->flags & ZIP_FLAG_AVOID_ZIP64) { /* Reject entries over 4GB. */ if (archive_entry_size_is_set(entry) && (archive_entry_size(entry) > 0xffffffff)) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Files > 4GB require Zip64 extensions"); return ARCHIVE_FAILED; } /* Reject entries if archive is > 4GB. */ if (zip->written_bytes > 0xffffffff) { archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Archives > 4GB require Zip64 extensions"); return ARCHIVE_FAILED; } } /* Only regular files can have size > 0. */ if (type != AE_IFREG) archive_entry_set_size(entry, 0); /* Reset information from last entry. */ zip->entry_offset = zip->written_bytes; zip->entry_uncompressed_limit = INT64_MAX; zip->entry_compressed_size = 0; zip->entry_uncompressed_size = 0; zip->entry_compressed_written = 0; zip->entry_uncompressed_written = 0; zip->entry_flags = 0; zip->entry_uses_zip64 = 0; zip->entry_crc32 = zip->crc32func(0, NULL, 0); if (zip->entry != NULL) { archive_entry_free(zip->entry); zip->entry = NULL; } #if defined(_WIN32) && !defined(__CYGWIN__) /* Make sure the path separators in pahtname, hardlink and symlink * are all slash '/', not the Windows path separator '\'. */ zip->entry = __la_win_entry_in_posix_pathseparator(entry); if (zip->entry == entry) zip->entry = archive_entry_clone(entry); #else zip->entry = archive_entry_clone(entry); #endif if (zip->entry == NULL) { archive_set_error(&a->archive, ENOMEM, "Can't allocate zip header data"); return (ARCHIVE_FATAL); } if (sconv != NULL) { const char *p; size_t len; if (archive_entry_pathname_l(entry, &p, &len, sconv) != 0) { if (errno == ENOMEM) { archive_set_error(&a->archive, ENOMEM, "Can't allocate memory for Pathname"); return (ARCHIVE_FATAL); } archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, "Can't translate Pathname '%s' to %s", archive_entry_pathname(entry), archive_string_conversion_charset_name(sconv)); ret2 = ARCHIVE_WARN; } if (len > 0) archive_entry_set_pathname(zip->entry, p); /* * There is no standard for symlink handling; we convert * it using the same character-set translation that we use * for filename. */ if (type == AE_IFLNK) { if (archive_entry_symlink_l(entry, &p, &len, sconv)) { if (errno == ENOMEM) { archive_set_error(&a->archive, ENOMEM, "Can't allocate memory " " for Symlink"); return (ARCHIVE_FATAL); } /* No error if we can't convert. */ } else if (len > 0) archive_entry_set_symlink(zip->entry, p); } } /* If filename isn't ASCII and we can use UTF-8, set the UTF-8 flag. */ if (!is_all_ascii(archive_entry_pathname(zip->entry))) { if (zip->opt_sconv != NULL) { if (strcmp(archive_string_conversion_charset_name( zip->opt_sconv), "UTF-8") == 0) zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME; #if HAVE_NL_LANGINFO } else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) { zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME; #endif } } filename_length = path_length(zip->entry); /* Determine appropriate compression and size for this entry. */ if (type == AE_IFLNK) { slink = archive_entry_symlink(zip->entry); if (slink != NULL) slink_size = strlen(slink); else slink_size = 0; zip->entry_uncompressed_limit = slink_size; zip->entry_compressed_size = slink_size; zip->entry_uncompressed_size = slink_size; zip->entry_crc32 = zip->crc32func(zip->entry_crc32, (const unsigned char *)slink, slink_size); zip->entry_compression = COMPRESSION_STORE; version_needed = 20; } else if (type != AE_IFREG) { zip->entry_compression = COMPRESSION_STORE; zip->entry_uncompressed_limit = 0; size = 0; version_needed = 20; } else if (archive_entry_size_is_set(zip->entry)) { size = archive_entry_size(zip->entry); zip->entry_uncompressed_limit = size; zip->entry_compression = zip->requested_compression; if (zip->entry_compression == COMPRESSION_UNSPECIFIED) { zip->entry_compression = COMPRESSION_DEFAULT; } if (zip->entry_compression == COMPRESSION_STORE) { zip->entry_compressed_size = size; zip->entry_uncompressed_size = size; version_needed = 10; } else { zip->entry_uncompressed_size = size; version_needed = 20; } if ((zip->flags & ZIP_FLAG_FORCE_ZIP64) /* User asked. */ || (zip->entry_uncompressed_size > ARCHIVE_LITERAL_LL(0xffffffff))) { /* Large entry. */ zip->entry_uses_zip64 = 1; version_needed = 45; } /* We may know the size, but never the CRC. */ zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END; } else { /* Prefer deflate if it's available, because deflate * has a clear end-of-data marker that makes * length-at-end more reliable. */ zip->entry_compression = COMPRESSION_DEFAULT; zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END; if ((zip->flags & ZIP_FLAG_AVOID_ZIP64) == 0) { zip->entry_uses_zip64 = 1; version_needed = 45; } else if (zip->entry_compression == COMPRESSION_STORE) { version_needed = 10; } else { version_needed = 20; } } /* Format the local header. */ memset(local_header, 0, sizeof(local_header)); memcpy(local_header, "PK\003\004", 4); archive_le16enc(local_header + 4, version_needed); archive_le16enc(local_header + 6, zip->entry_flags); archive_le16enc(local_header + 8, zip->entry_compression); archive_le32enc(local_header + 10, dos_time(archive_entry_mtime(zip->entry))); archive_le32enc(local_header + 14, zip->entry_crc32); if (zip->entry_uses_zip64) { /* Zip64 data in the local header "must" include both * compressed and uncompressed sizes AND those fields * are included only if these are 0xffffffff; * THEREFORE these must be set this way, even if we * know one of them is smaller. */ archive_le32enc(local_header + 18, ARCHIVE_LITERAL_LL(0xffffffff)); archive_le32enc(local_header + 22, ARCHIVE_LITERAL_LL(0xffffffff)); } else { archive_le32enc(local_header + 18, zip->entry_compressed_size); archive_le32enc(local_header + 22, zip->entry_uncompressed_size); } archive_le16enc(local_header + 26, filename_length); /* Format as much of central directory file header as we can: */ zip->file_header = cd_alloc(zip, 46); /* If (zip->file_header == NULL) XXXX */ ++zip->central_directory_entries; memset(zip->file_header, 0, 46); memcpy(zip->file_header, "PK\001\002", 4); /* "Made by PKZip 2.0 on Unix." */ archive_le16enc(zip->file_header + 4, 3 * 256 + version_needed); archive_le16enc(zip->file_header + 6, version_needed); archive_le16enc(zip->file_header + 8, zip->entry_flags); archive_le16enc(zip->file_header + 10, zip->entry_compression); archive_le32enc(zip->file_header + 12, dos_time(archive_entry_mtime(zip->entry))); archive_le16enc(zip->file_header + 28, filename_length); /* Following Info-Zip, store mode in the "external attributes" field. */ archive_le32enc(zip->file_header + 38, ((uint32_t)archive_entry_mode(zip->entry)) << 16); e = cd_alloc(zip, filename_length); /* If (e == NULL) XXXX */ copy_path(zip->entry, e); /* Format extra data. */ memset(local_extra, 0, sizeof(local_extra)); e = local_extra; /* First, extra blocks that are the same between * the local file header and the central directory. * We format them once and then duplicate them. */ /* UT timestamp, length depends on what timestamps are set. */ memcpy(e, "UT", 2); archive_le16enc(e + 2, 1 + (archive_entry_mtime_is_set(entry) ? 4 : 0) + (archive_entry_atime_is_set(entry) ? 4 : 0) + (archive_entry_ctime_is_set(entry) ? 4 : 0)); e += 4; *e++ = (archive_entry_mtime_is_set(entry) ? 1 : 0) | (archive_entry_atime_is_set(entry) ? 2 : 0) | (archive_entry_ctime_is_set(entry) ? 4 : 0); if (archive_entry_mtime_is_set(entry)) { archive_le32enc(e, (uint32_t)archive_entry_mtime(entry)); e += 4; } if (archive_entry_atime_is_set(entry)) { archive_le32enc(e, (uint32_t)archive_entry_atime(entry)); e += 4; } if (archive_entry_ctime_is_set(entry)) { archive_le32enc(e, (uint32_t)archive_entry_ctime(entry)); e += 4; } /* ux Unix extra data, length 11, version 1 */ /* TODO: If uid < 64k, use 2 bytes, ditto for gid. */ memcpy(e, "ux\013\000\001", 5); e += 5; *e++ = 4; /* Length of following UID */ archive_le32enc(e, (uint32_t)archive_entry_uid(entry)); e += 4; *e++ = 4; /* Length of following GID */ archive_le32enc(e, (uint32_t)archive_entry_gid(entry)); e += 4; /* Copy UT and ux into central directory as well. */ zip->file_header_extra_offset = zip->central_directory_bytes; cd_extra = cd_alloc(zip, e - local_extra); memcpy(cd_extra, local_extra, e - local_extra); /* * Following extra blocks vary between local header and * central directory. These are the local header versions. * Central directory versions get formatted in * archive_write_zip_finish_entry() below. */ /* "[Zip64 entry] in the local header MUST include BOTH * original [uncompressed] and compressed size fields." */ if (zip->entry_uses_zip64) { unsigned char *zip64_start = e; memcpy(e, "\001\000\020\000", 4); e += 4; archive_le64enc(e, zip->entry_uncompressed_size); e += 8; archive_le64enc(e, zip->entry_compressed_size); e += 8; archive_le16enc(zip64_start + 2, e - (zip64_start + 4)); } if (zip->flags & ZIP_FLAG_EXPERIMENT_EL) { /* Experimental 'el' extension to improve streaming. */ unsigned char *external_info = e; int included = 7; memcpy(e, "el\000\000", 4); // 0x6c65 + 2-byte length e += 4; e[0] = included; /* bitmap of included fields */ e += 1; if (included & 1) { archive_le16enc(e, /* "Version created by" */ 3 * 256 + version_needed); e += 2; } if (included & 2) { archive_le16enc(e, 0); /* internal file attributes */ e += 2; } if (included & 4) { archive_le32enc(e, /* external file attributes */ ((uint32_t)archive_entry_mode(zip->entry)) << 16); e += 4; } if (included & 8) { // Libarchive does not currently support file comments. } archive_le16enc(external_info + 2, e - (external_info + 4)); } /* Update local header with size of extra data and write it all out: */ archive_le16enc(local_header + 28, e - local_extra); ret = __archive_write_output(a, local_header, 30); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += 30; ret = write_path(zip->entry, a); if (ret <= ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += ret; ret = __archive_write_output(a, local_extra, e - local_extra); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->written_bytes += e - local_extra; /* For symlinks, write the body now. */ if (slink != NULL) { ret = __archive_write_output(a, slink, slink_size); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); zip->entry_compressed_written += slink_size; zip->entry_uncompressed_written += slink_size; zip->written_bytes += slink_size; } #ifdef HAVE_ZLIB_H if (zip->entry_compression == COMPRESSION_DEFLATE) { zip->stream.zalloc = Z_NULL; zip->stream.zfree = Z_NULL; zip->stream.opaque = Z_NULL; zip->stream.next_out = zip->buf; zip->stream.avail_out = (uInt)zip->len_buf; if (deflateInit2(&zip->stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) { archive_set_error(&a->archive, ENOMEM, "Can't init deflate compressor"); return (ARCHIVE_FATAL); } } #endif return (ret2); }