ZIP_EXTERN zip_int64_t zip_add(zip_t *za, const char *name, zip_source_t *source) { return zip_file_add(za, name, source, 0); }
static int zwr(struct zws *zws, size_t offset) { int err, fd; size_t len; DIR *dir; struct dirent *ent; zip_int64_t z64; struct zip_source *zsrc; FILE *fp; fd = openat(workdirfd, offset ? zws->name : ".", O_DIRECTORY|O_RDONLY); if (fd < 0) { ERROR("opendir %.*s failed in zwr", (int)offset, zws->name); return -1; } dir = fdopendir(fd); if (!dir) { close(fd); ERROR("opendir %.*s failed in zwr", (int)offset, zws->name); return -1; } if (offset != 0) zws->name[offset++] = '/'; ent = readdir(dir); while (ent != NULL) { len = strlen(ent->d_name); if (ent->d_name[0] == '.' && (len == 1 || (ent->d_name[1] == '.' && len == 2))) ; else if (offset + len >= sizeof(zws->name)) { ERROR("name too long in zwr"); errno = ENAMETOOLONG; goto error; } else { memcpy(zws->name + offset, ent->d_name, 1+len); if (!is_valid_filename(ent->d_name)) { ERROR("invalid name %s", zws->name); goto error; } switch (ent->d_type) { case DT_DIR: z64 = zip_dir_add(zws->zip, zws->name, ZIP_FL_ENC_UTF_8); if (z64 < 0) { ERROR("zip_dir_add of %s failed", zws->name); goto error; } err = zwr(zws, offset + len); if (err) goto error; break; case DT_REG: fd = openat(workdirfd, zws->name, O_RDONLY); if (fd < 0) { ERROR("openat of %s failed", zws->name); goto error; } fp = fdopen(fd, "r"); if (fp == NULL) { ERROR("fdopen of %s failed", zws->name); close(fd); goto error; } zsrc = zip_source_filep(zws->zip, fp, 0, 0); if (zsrc == NULL) { ERROR("zip_source_file of %s failed", zws->name); fclose(fp); goto error; } z64 = zip_file_add(zws->zip, zws->name, zsrc, ZIP_FL_ENC_UTF_8); if (z64 < 0) { ERROR("zip_file_add of %s failed", zws->name); zip_source_free(zsrc); goto error; } break; default: break; } } ent = readdir(dir); } closedir(dir); return 0; error: closedir(dir); return -1; }
void kappa_fifo_save_jpeg(char *group,kafifo_t *info,AVFrame *stillframe,char *filename,int isscene) { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; JSAMPROW row_pointer[ 1 ]; FILE *outfile; unsigned char *mem = NULL; unsigned long memsize = 0; cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); jpeg_mem_dest(&cinfo,&mem,&memsize); cinfo.image_width = stillframe->width; cinfo.image_height = stillframe->height; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); jpeg_start_compress(&cinfo,TRUE); row_pointer[ 0 ] = stillframe->data[ 0 ]; while (cinfo.next_scanline < cinfo.image_height) { jpeg_write_scanlines(&cinfo,row_pointer,1); row_pointer[ 0 ] += stillframe->linesize[ 0 ]; } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); if (isscene) { struct zip_source *source = zip_source_buffer(info->scenezip,mem,memsize,1); char *zipname = strncmp(filename,"../",3) ? filename : (filename + 3); if (zip_file_add(info->scenezip,zipname,source,ZIP_FL_ENC_UTF_8) < 0) { fprintf(stderr,"Could not add still to zip %s => %s, exitting now...\n", group,info->scenezipname, zip_strerror(info->scenezip) ); exit(1); } } else { if ((outfile = fopen(filename,"wb")) == NULL) { fprintf(stderr,"Could not create still %s, exitting now...\n",filename); exit(1); } fwrite(mem,memsize,1,outfile); fclose(outfile); free(mem); fprintf(stderr,"Stillimage:%s\n",filename); fflush(stderr); } }
int main(int argc, char *argv[]) { int fail, ze; int c; struct zip *z; struct zip_source *zs; char *archive; char errstr[1024]; verbose = 0; fail = 0; prg = argv[0]; while ((c=getopt(argc, argv, "v")) != -1) { switch (c) { case 'v': verbose = 1; break; default: fprintf(stderr, usage, prg); return 1; } } if (argc-optind != 1) { fprintf(stderr, usage, prg); return 1; } archive = argv[optind]; if ((z=zip_open(archive, 0, &ze)) == NULL) { zip_error_to_str(errstr, sizeof(errstr), ze, errno); printf("%s: can't open zip archive '%s': %s\n", prg, archive, errstr); return 1; } fail += do_read(z, "storedok", 0, WHEN_NEVER, 0, 0); fail += do_read(z, "deflateok", 0, WHEN_NEVER, 0, 0); fail += do_read(z, "storedcrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0); fail += do_read(z, "deflatecrcerror", 0, WHEN_READ, ZIP_ER_CRC, 0); fail += do_read(z, "deflatezliberror", 0, WHEN_READ, ZIP_ER_ZLIB, -3); fail += do_read(z, NULL, 0, WHEN_OPEN, ZIP_ER_INVAL, 0); fail += do_read(z, "nosuchfile", 0, WHEN_OPEN, ZIP_ER_NOENT, 0); fail += do_read(z, "deflatezliberror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0,0); fail += do_read(z, "deflatecrcerror", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0); fail += do_read(z, "storedcrcerror", ZIP_FL_COMPRESSED, WHEN_READ, ZIP_ER_CRC, 0); fail += do_read(z, "storedok", ZIP_FL_COMPRESSED, WHEN_NEVER, 0, 0); fail += do_read(z, "cryptok", 0, WHEN_OPEN, ZIP_ER_NOPASSWD, 0); zip_set_default_password(z, "crypt"); fail += do_read(z, "cryptok", 0, WHEN_NEVER, 0, 0); zip_set_default_password(z, "wrong"); fail += do_read(z, "cryptok", 0, WHEN_OPEN, ZIP_ER_WRONGPASSWD, 0); zip_set_default_password(z, NULL); zs = zip_source_buffer(z, "asdf", 4, 0); zip_replace(z, zip_name_locate(z, "storedok", 0), zs); fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0); fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0); zip_delete(z, zip_name_locate(z, "storedok", 0)); fail += do_read(z, "storedok", 0, WHEN_OPEN, ZIP_ER_NOENT, 0); fail += do_read(z, "storedok", ZIP_FL_UNCHANGED, WHEN_NEVER, 0, 0); zs = zip_source_buffer(z, "asdf", 4, 0); if (zip_file_add(z, "new_file", zs, 0) < 0) fprintf(stderr, "%s: can't add file to zip archive '%s': %s\n", prg, archive, zip_strerror(z)); fail += do_read(z, "new_file", 0, WHEN_OPEN, ZIP_ER_CHANGED, 0); zip_unchange_all(z); if (zip_close(z) == -1) { fprintf(stderr, "%s: can't close zip archive '%s': %s\n", prg, archive, zip_strerror(z)); return 1; } exit(fail ? 1 : 0); }