Преглед на файлове

Add basic zip extraction using libzip

Might crash at random, leak memory, ... but it does work somehow.
Lucas Stadler преди 10 години
родител
ревизия
bd9da65e81
променени са 5 файла, в които са добавени 65 реда и са изтрити 2 реда
  1. 1 1
      c/ton/.vimrc
  2. 2 1
      c/ton/Makefile
  3. 2 0
      c/ton/main.c
  4. 57 0
      c/ton/zip.c
  5. 3 0
      c/ton/zip.h

+ 1 - 1
c/ton/.vimrc

@ -1 +1 @@
1
let g:syntastic_c_include_dirs = ['/usr/include/webkitgtk-4.0']
1
let g:syntastic_c_include_dirs = ['/usr/include/webkitgtk-4.0', '/usr/include']

+ 2 - 1
c/ton/Makefile

@ -1,7 +1,8 @@
1 1
JSC_CFLAGS = $(shell pkg-config javascriptcoregtk-4.0 --cflags --libs)
2
LIBZIP_CFLAGS = $(shell pkg-config libzip --cflags --libs)
2 3
3 4
ton: *.c
4
	clang $(JSC_CFLAGS) $^ -o $@
5
	clang $(JSC_CFLAGS) $(LIBZIP_CFLAGS) $^ -o $@
5 6
6 7
jsc-funcs:
7 8
	grep -Rh JS_EXPORT /usr/include/webkitgtk-4.0/JavaScriptCore | sed 's/^JS_EXPORT //' | grep -v '^#' > $@

+ 2 - 0
c/ton/main.c

@ -5,6 +5,8 @@
5 5
6 6
#include <JavaScriptCore/JavaScript.h>
7 7
8
#include "zip.h"
9
8 10
#define CONSOLE_LOG_BUF_SIZE 1000
9 11
char console_log_buf[CONSOLE_LOG_BUF_SIZE];
10 12

+ 57 - 0
c/ton/zip.c

@ -0,0 +1,57 @@
1
// Utilities for getting entries from ZIP files.
2
// 
3
// Uses libzip, alternatives are minizip (from zlib) and zziplib.
4
5
#include <stdio.h>
6
#include <stdlib.h>
7
8
#include "zip.h"
9
10
void print_zip_err(char *prefix, zip_t *zip);
11
12
char *get_contents_zip(char *path, char *name) {
13
	zip_t *archive = zip_open(path, ZIP_RDONLY, NULL);
14
	if (archive == NULL) {
15
		print_zip_err("zip_open", archive);
16
		return NULL;
17
	}
18
19
	zip_stat_t stat;
20
	if (zip_stat(archive, name, 0, &stat) < 0) {
21
		print_zip_err("zip_stat", archive);
22
		goto close_archive;
23
	}
24
25
	zip_file_t *f = zip_fopen(archive, name, 0);
26
	if (f == NULL) {
27
		print_zip_err("zip_fopen", archive);
28
		goto close_archive;
29
	}
30
31
	char *buf = malloc(stat.size + 1);
32
	if (zip_fread(f, buf, stat.size) < 0) {
33
		print_zip_err("zip_fread", archive);
34
		goto free_buf;
35
	}
36
	buf[stat.size] = '\0';
37
38
	zip_fclose(f);
39
	zip_close(archive);
40
41
	return buf;
42
43
free_buf:
44
	free(buf);
45
close_file:
46
	zip_fclose(f);
47
close_archive:
48
	zip_close(archive);
49
50
	return NULL;
51
}
52
53
void print_zip_err(char *prefix, zip_t *zip) {
54
		zip_error_t *err = zip_get_error(zip);
55
		printf("%s: %s\n", prefix, zip_error_strerror(err));
56
		zip_error_fini(err);
57
}

+ 3 - 0
c/ton/zip.h

@ -0,0 +1,3 @@
1
#include <zip.h>
2
3
char *get_contents_zip(char *path, char *name);