Beispiel #1
0
int try_to_open_zip(const char *filename)
{
	int success = 0;
	/* Grr. libzip needs to re-open the file, it can't take a buffer */
	struct zip *zip = subsurface_zip_open_readonly(filename, ZIP_CHECKCONS, NULL);

	if (zip) {
		int index;
		for (index = 0;; index++) {
			struct zip_file *file = zip_fopen_index(zip, index, 0);
			if (!file)
				break;
			/* skip parsing the divelogs.de pictures */
			if (strstr(zip_get_name(zip, index, 0), "pictures/"))
				continue;
			zip_read(file, filename);
			zip_fclose(file);
			success++;
		}
		subsurface_zip_close(zip);

		if (!success)
			return report_error(translate("gettextFromC", "No dives in the input file '%s'"), filename);
	}
	return success;
}
static int
_call(lua_State *L)
{
	app_t *app = lua_touserdata(L, lua_upvalueindex(1));

	const char *key = luaL_checkstring(L, 1);

	size_t size;
	uint8_t *chunk = zip_read(app, key, &size);

	if(chunk)
	{
		lua_pushlstring(L, (char *)chunk, size);
		free(chunk);
	}
	else
		lua_pushnil(L);

	return 1;
}
Beispiel #3
0
static int try_to_open_zip(const char *filename, struct memblock *mem, char **error)
{
	int success = 0;
	/* Grr. libzip needs to re-open the file, it can't take a buffer */
	struct zip *zip = zip_open(filename, ZIP_CHECKCONS, NULL);

	if (zip) {
		int index;
		for (index = 0; ;index++) {
			struct zip_file *file = zip_fopen_index(zip, index, 0);
			if (!file)
				break;
			zip_read(file, error, filename);
			zip_fclose(file);
			success++;
		}
		zip_close(zip);
	}
	return success;
}
Beispiel #4
0
static int try_to_open_zip(const char *filename, struct memblock *mem)
{
	int success = 0;
	/* Grr. libzip needs to re-open the file, it can't take a buffer */
	struct zip *zip = subsurface_zip_open_readonly(filename, ZIP_CHECKCONS, NULL);

	if (zip) {
		int index;
		for (index = 0;; index++) {
			struct zip_file *file = zip_fopen_index(zip, index, 0);
			if (!file)
				break;
			/* skip parsing the divelogs.de pictures */
			if (strstr(zip_get_name(zip, index, 0), "pictures/"))
				continue;
			zip_read(file, filename);
			zip_fclose(file);
			success++;
		}
		subsurface_zip_close(zip);
	}
	return success;
}
osd_file* osd_fopen(int pathtype, int pathindex, const char* filename, const char* mode, osd_file_error *error)
{
	struct fileio_item* i;
	char path_buffer[FILE_MAXPATH];
	adv_fz* h;
	char* split;
	struct advance_fileio_context* context = &CONTEXT.fileio;

	log_std(("osd: osd_fopen(pathtype:%d,pathindex:%d,filename:%s,mode:%s)\n", pathtype, pathindex, filename, mode));

	/* set a default error */
	*error = FILEERR_FAILURE;

	i = fileio_find(pathtype);
	if (!i) {
		log_std(("WARNING:fileio: file type %d unknown\n", pathtype));
		return 0;
	}

	/* HACK: for .CHD file MAME adds an initial slash */
	/* remove it assuming always relative paths */
	if (filename[0] == file_dir_slash())
		++filename;

	sncpy(path_buffer, sizeof(path_buffer), file_abs(i->dir_map[pathindex], filename));

	split = strchr(path_buffer, '=');
	if (split != 0) {
		char zip_file_buffer[FILE_MAXPATH];
		char file_buffer[FILE_MAXPATH];
		adv_zip* zip;
		adv_zipent* ent;

		*split = 0;
		snprintf(zip_file_buffer, sizeof(zip_file_buffer), "%s.zip", path_buffer);
		sncpy(file_buffer, sizeof(file_buffer), split + 1);

		log_std(("osd: osd_fopen() try %s %s\n", zip_file_buffer, file_buffer));

		if (access(zip_file_buffer, R_OK)!=0) {
			osd_errno_to_filerr(error);
			log_std(("osd: osd_fopen() -> failed, zip %s not readable\n", zip_file_buffer));
			return 0;
		}

		zip = zip_open(zip_file_buffer);
		if (!zip) {
			osd_errno_to_filerr(error);
			log_std(("osd: osd_fopen() -> failed, zip %s not openable\n", zip_file_buffer));
			return 0;
		}

		h = 0;
		while ((ent = zip_read(zip))!=0) {
			if (partialequal(ent->name, file_buffer)) {
				if (ent->compression_method == 0) {
					h = fzopenzipuncompressed(zip_file_buffer, ent->offset_lcl_hdr_frm_frst_disk, ent->uncompressed_size);
					if (h == 0)
						osd_errno_to_filerr(error);
				} else if (ent->compression_method == 8) {
					h = fzopenzipcompressed(zip_file_buffer, ent->offset_lcl_hdr_frm_frst_disk, ent->compressed_size, ent->uncompressed_size);
					if (h == 0)
						osd_errno_to_filerr(error);
				}
				break;
			}
		}

		zip_close(zip);
	} else {
		log_std(("osd: osd_fopen() try file %s\n", path_buffer));

		/* for the diff file support the write in memory mode */
		if (i->type == FILETYPE_IMAGE_DIFF) {
			/* if the file is already open reuse it */
			if (context->state.diff_handle && strcmp(path_buffer, context->state.diff_file_buffer)==0) {
				h = context->state.diff_handle;
			} else {
				/* try a normal open */
				h = fzopen(path_buffer, mode);
				if (h == 0) {
					osd_errno_to_filerr(error);
					log_std(("osd: fzopen() failed, %s\n", strerror(errno)));
					if (errno == EACCES || errno == EROFS) {
						log_std(("osd: retry with readonly\n"));
						/* reopen in memory */
						h = fzopennullwrite(path_buffer, mode);
						if (h == 0) {
							osd_errno_to_filerr(error);
							log_std(("osd: fzopenullwrite() failed, %s\n", strerror(errno)));
						} else {
							if (context->state.diff_handle == 0) {
								/* save the handle if not already saved */
								context->state.diff_handle = h;
								sncpy(context->state.diff_file_buffer, sizeof(context->state.diff_file_buffer), path_buffer);
							}
						}
					}
				}
			}
		} else {
			/* open a regular file */
			h = fzopen(path_buffer, mode);
			if (h == 0) {
				osd_errno_to_filerr(error);
				log_std(("osd: fzopen() failed, %s\n", strerror(errno)));
			}
		}
	}

	log_std(("osd: osd_fopen() -> return %p\n", h));

	/* clear the error if success */
	if (h != 0)
		*error = FILEERR_SUCCESS;

	return (osd_file*)h;
}