Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
e08d46aa
Commit
e08d46aa
authored
Mar 01, 2017
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add lz4 support
parent
feccc97a
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
173 additions
and
37 deletions
+173
-37
Android.mk
jni/Android.mk
+0
-5
Android.mk
jni/magiskboot/Android.mk
+8
-2
compress.c
jni/magiskboot/compress.c
+150
-22
magiskboot.h
jni/magiskboot/magiskboot.h
+6
-4
repack.c
jni/magiskboot/repack.c
+3
-1
unpack.c
jni/magiskboot/unpack.c
+3
-0
ndk-compression
jni/ndk-compression
+1
-1
sepolicy-inject
jni/sepolicy-inject
+1
-1
su
jni/su
+1
-1
No files found.
jni/Android.mk
View file @
e08d46aa
...
...
@@ -5,8 +5,3 @@ include jni/magiskhide/Android.mk
include jni/resetprop/Android.mk
include jni/sepolicy-inject/Android.mk
include jni/su/Android.mk
# Libraries
include jni/selinux/libsepol/Android.mk
include jni/selinux/libselinux/Android.mk
include jni/ndk-compression/xz/src/liblzma/Android.mk
jni/magiskboot/Android.mk
View file @
e08d46aa
...
...
@@ -2,8 +2,14 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := magiskboot
LOCAL_STATIC_LIBRARIES := liblzma
LOCAL_C_INCLUDES := jni/ndk-compression/xz/src/liblzma/api/
LOCAL_STATIC_LIBRARIES := liblzma liblz4
LOCAL_C_INCLUDES := \
jni/ndk-compression/xz/src/liblzma/api/ \
jni/ndk-compression/lz4/lib/
LOCAL_SRC_FILES := main.c unpack.c repack.c hexpatch.c parseimg.c compress.c
LOCAL_LDLIBS += -lz
include $(BUILD_EXECUTABLE)
include jni/ndk-compression/xz/src/liblzma/Android.mk
include jni/ndk-compression/lz4/lib/Android.mk
jni/magiskboot/compress.c
View file @
e08d46aa
#include <zlib.h>
#include <lzma.h>
#include <lz4frame.h>
#include "magiskboot.h"
// Mode: 0 = decode gz; 1 = encode gz
static
int
open_new
(
const
char
*
filename
)
{
int
fd
=
open
(
filename
,
O_WRONLY
|
O_CREAT
|
O_TRUNC
,
0644
);
if
(
fd
<
0
)
error
(
1
,
"Unable to create %s"
,
filename
);
return
fd
;
}
static
void
write_file
(
const
int
fd
,
const
unsigned
char
*
buf
,
const
size_t
size
,
const
char
*
filename
)
{
if
(
write
(
fd
,
buf
,
size
)
!=
size
)
error
(
1
,
"Error in writing %s"
,
filename
);
}
static
void
report
(
const
int
mode
,
const
char
*
filename
)
{
switch
(
mode
)
{
case
0
:
printf
(
"Decompressing to %s
\n
"
,
filename
);
break
;
default:
printf
(
"Compressing to %s
\n
"
,
filename
);
break
;
}
}
// Mode: 0 = decode; 1 = encode
void
gzip
(
int
mode
,
const
char
*
filename
,
unsigned
char
*
buf
,
size_t
size
)
{
size_t
ret
,
flush
,
have
,
pos
=
0
;
size_t
ret
=
0
,
flush
,
have
,
pos
=
0
;
z_stream
strm
;
unsigned
char
out
[
CHUNK
];
int
fd
=
open
(
filename
,
O_WRONLY
|
O_CREAT
|
O_TRUNC
,
0644
);
if
(
fd
<
0
)
error
(
1
,
"Unable to create %s"
,
filename
);
report
(
mode
,
filename
);
int
fd
=
open_new
(
filename
);
strm
.
zalloc
=
Z_NULL
;
strm
.
zfree
=
Z_NULL
;
...
...
@@ -30,19 +52,18 @@ void gzip(int mode, const char* filename, unsigned char* buf, size_t size) {
}
if
(
ret
!=
Z_OK
)
error
(
1
,
"Unable to init zlib"
);
error
(
1
,
"Unable to init zlib
stream
"
);
do
{
strm
.
next_in
=
buf
+
pos
;
if
(
pos
+
CHUNK
>=
size
)
{
strm
.
avail_in
=
size
-
pos
;
pos
=
size
;
flush
=
Z_FINISH
;
}
else
{
strm
.
avail_in
=
CHUNK
;
pos
+=
CHUNK
;
flush
=
Z_NO_FLUSH
;
}
pos
+=
strm
.
avail_in
;
do
{
strm
.
avail_out
=
CHUNK
;
...
...
@@ -59,8 +80,7 @@ void gzip(int mode, const char* filename, unsigned char* buf, size_t size) {
error
(
1
,
"Error when running gzip"
);
have
=
CHUNK
-
strm
.
avail_out
;
if
(
write
(
fd
,
out
,
have
)
!=
have
)
error
(
1
,
"Error when writing %s"
,
filename
);
write_file
(
fd
,
out
,
have
,
filename
);
}
while
(
strm
.
avail_out
==
0
);
...
...
@@ -81,16 +101,14 @@ void gzip(int mode, const char* filename, unsigned char* buf, size_t size) {
// Mode: 0 = decode xz/lzma; 1 = encode xz; 2 = encode lzma
void
lzma
(
int
mode
,
const
char
*
filename
,
unsigned
char
*
buf
,
size_t
size
)
{
size_t
have
,
pos
=
0
;
lzma_ret
ret
;
lzma_ret
ret
=
0
;
lzma_stream
strm
=
LZMA_STREAM_INIT
;
lzma_options_lzma
opt
;
lzma_action
action
=
LZMA_RUN
;
lzma_action
action
;
unsigned
char
out
[
BUFSIZ
];
int
fd
=
open
(
filename
,
O_WRONLY
|
O_CREAT
|
O_TRUNC
,
0644
);
if
(
fd
<
0
)
error
(
1
,
"Unable to create %s"
,
filename
);
report
(
mode
,
filename
);
int
fd
=
open_new
(
filename
);
// Initialize preset
lzma_lzma_preset
(
&
opt
,
LZMA_PRESET_DEFAULT
);
...
...
@@ -115,26 +133,25 @@ void lzma(int mode, const char* filename, unsigned char* buf, size_t size) {
if
(
ret
!=
LZMA_OK
)
error
(
1
,
"Unable to init lzma"
);
error
(
1
,
"Unable to init lzma
stream
"
);
do
{
strm
.
next_in
=
buf
+
pos
;
if
(
pos
+
BUFSIZ
>=
size
)
{
strm
.
avail_in
=
size
-
pos
;
pos
=
size
;
action
=
LZMA_FINISH
;
}
else
{
strm
.
avail_in
=
BUFSIZ
;
pos
+=
BUFSIZ
;
action
=
LZMA_RUN
;
}
pos
+=
strm
.
avail_in
;
do
{
strm
.
avail_out
=
BUFSIZ
;
strm
.
next_out
=
out
;
ret
=
lzma_code
(
&
strm
,
action
);
have
=
BUFSIZ
-
strm
.
avail_out
;
if
(
write
(
fd
,
out
,
have
)
!=
have
)
error
(
1
,
"Error in writing %s"
,
filename
);
write_file
(
fd
,
out
,
have
,
filename
);
}
while
(
strm
.
avail_out
==
0
&&
ret
==
LZMA_OK
);
if
(
ret
!=
LZMA_OK
&&
ret
!=
LZMA_STREAM_END
)
...
...
@@ -144,3 +161,114 @@ void lzma(int mode, const char* filename, unsigned char* buf, size_t size) {
lzma_end
(
&
strm
);
}
// Mode: 0 = decode; 1 = encode
void
lz4
(
int
mode
,
const
char
*
filename
,
unsigned
char
*
buf
,
size_t
size
)
{
LZ4F_decompressionContext_t
dctx
;
LZ4F_compressionContext_t
cctx
;
LZ4F_frameInfo_t
info
;
size_t
outCapacity
,
avail_in
,
ret
=
0
,
pos
=
0
;
size_t
have
,
read
;
unsigned
char
*
out
=
NULL
;
report
(
mode
,
filename
);
int
fd
=
open_new
(
filename
);
// Initialize context
switch
(
mode
)
{
case
0
:
ret
=
LZ4F_createDecompressionContext
(
&
dctx
,
LZ4F_VERSION
);
break
;
case
1
:
ret
=
LZ4F_createCompressionContext
(
&
cctx
,
LZ4F_VERSION
);
break
;
default:
error
(
1
,
"Unsupported lz4 mode!"
);
}
if
(
LZ4F_isError
(
ret
))
error
(
1
,
"Context creation error: %s
\n
"
,
LZ4F_getErrorName
(
ret
));
// Allocate out buffer
switch
(
mode
)
{
case
0
:
// Read header
read
=
CHUNK
;
ret
=
LZ4F_getFrameInfo
(
dctx
,
&
info
,
buf
,
&
read
);
if
(
LZ4F_isError
(
ret
))
error
(
1
,
"LZ4F_getFrameInfo error: %s
\n
"
,
LZ4F_getErrorName
(
ret
));
switch
(
info
.
blockSizeID
)
{
case
LZ4F_default
:
case
LZ4F_max64KB
:
outCapacity
=
1
<<
16
;
break
;
case
LZ4F_max256KB
:
outCapacity
=
1
<<
18
;
break
;
case
LZ4F_max1MB
:
outCapacity
=
1
<<
20
;
break
;
case
LZ4F_max4MB
:
outCapacity
=
1
<<
22
;
break
;
default:
error
(
1
,
"Impossible unless more block sizes are allowed
\n
"
);
}
pos
+=
read
;
break
;
case
1
:
outCapacity
=
LZ4F_compressBound
(
CHUNK
,
NULL
)
+
LZ4_HEADER_SIZE
+
LZ4_FOOTER_SIZE
;
break
;
}
out
=
malloc
(
outCapacity
);
if
(
!
out
)
error
(
1
,
"LZ4 malloc error!"
);
// Write header
if
(
mode
==
1
)
{
have
=
ret
=
LZ4F_compressBegin
(
cctx
,
out
,
size
,
NULL
);
if
(
LZ4F_isError
(
ret
))
error
(
1
,
"Failed to start compression: error %s
\n
"
,
LZ4F_getErrorName
(
ret
));
write_file
(
fd
,
out
,
have
,
filename
);
}
do
{
if
(
pos
+
CHUNK
>=
size
)
{
avail_in
=
size
-
pos
;
}
else
{
avail_in
=
CHUNK
;
}
do
{
switch
(
mode
)
{
case
0
:
have
=
outCapacity
,
read
=
avail_in
;
ret
=
LZ4F_decompress
(
dctx
,
out
,
&
have
,
buf
+
pos
,
&
read
,
NULL
);
break
;
case
1
:
read
=
avail_in
;
have
=
ret
=
LZ4F_compressUpdate
(
cctx
,
out
,
outCapacity
,
buf
+
pos
,
avail_in
,
NULL
);
break
;
}
if
(
LZ4F_isError
(
ret
))
error
(
1
,
"LZ4 coding error: %s
\n
"
,
LZ4F_getErrorName
(
ret
));
write_file
(
fd
,
out
,
have
,
filename
);
// Update status
pos
+=
read
;
avail_in
-=
read
;
}
while
(
avail_in
!=
0
&&
ret
!=
0
);
}
while
(
pos
<
size
&&
ret
!=
0
);
switch
(
mode
)
{
case
0
:
LZ4F_freeDecompressionContext
(
dctx
);
break
;
case
1
:
have
=
ret
=
LZ4F_compressEnd
(
cctx
,
out
,
outCapacity
,
NULL
);
if
(
LZ4F_isError
(
ret
))
error
(
1
,
"Failed to end compression: error %s
\n
"
,
LZ4F_getErrorName
(
ret
));
write_file
(
fd
,
out
,
have
,
filename
);
LZ4F_freeCompressionContext
(
cctx
);
break
;
}
free
(
out
);
}
jni/magiskboot/magiskboot.h
View file @
e08d46aa
#ifndef _
ARCHIVE
_H_
#define _
ARCHIVE
_H_
#ifndef _
MAGISKBOOT
_H_
#define _
MAGISKBOOT
_H_
#include <stdio.h>
#include <stdlib.h>
...
...
@@ -18,6 +18,9 @@
#define memLevel 8
#define CHUNK 0x40000
#define LZ4_HEADER_SIZE 19
#define LZ4_FOOTER_SIZE 4
#define CHROMEOS_MAGIC "CHROMEOS"
#define CHROMEOS_MAGIC_SIZE 8
...
...
@@ -51,12 +54,11 @@ void unpack(const char *image);
void
repack
(
const
char
*
image
);
void
hexpatch
(
char
*
image
,
char
*
from
,
char
*
to
);
void
error
(
int
rc
,
const
char
*
msg
,
...);
// Parse image
void
parse_img
(
unsigned
char
*
orig
,
size_t
size
);
// Compressions
void
gzip
(
int
mode
,
const
char
*
filename
,
unsigned
char
*
buf
,
size_t
size
);
void
lzma
(
int
mode
,
const
char
*
filename
,
unsigned
char
*
buf
,
size_t
size
);
void
lz4
(
int
mode
,
const
char
*
filename
,
unsigned
char
*
buf
,
size_t
size
);
#endif
jni/magiskboot/repack.c
View file @
e08d46aa
...
...
@@ -98,6 +98,7 @@ void repack(const char* image) {
break
;
case
LZOP
:
sprintf
(
name
,
"%s.%s"
,
RAMDISK_FILE
,
"lzo"
);
error
(
1
,
"Unsupported format! Please compress manually!"
);
break
;
case
XZ
:
sprintf
(
name
,
"%s.%s"
,
RAMDISK_FILE
,
"xz"
);
...
...
@@ -109,16 +110,17 @@ void repack(const char* image) {
break
;
case
BZIP2
:
sprintf
(
name
,
"%s.%s"
,
RAMDISK_FILE
,
"bz2"
);
error
(
1
,
"Unsupported format! Please compress manually!"
);
break
;
case
LZ4
:
sprintf
(
name
,
"%s.%s"
,
RAMDISK_FILE
,
"lz4"
);
lz4
(
1
,
name
,
cpio
,
cpio_size
);
break
;
default:
// Never happens
break
;
}
printf
(
"Re-compressed %s to %s
\n
"
,
RAMDISK_FILE
,
name
);
munmap
(
cpio
,
cpio_size
);
close
(
rfd
);
}
else
{
...
...
jni/magiskboot/unpack.c
View file @
e08d46aa
...
...
@@ -48,6 +48,7 @@ void unpack(const char* image) {
break
;
case
LZOP
:
sprintf
(
name
,
"%s.%s"
,
RAMDISK_FILE
,
"lzo"
);
printf
(
"Unsupported format! Please decompress manually!
\n
"
);
break
;
case
XZ
:
sprintf
(
name
,
"%s.%s"
,
RAMDISK_FILE
,
"xz"
);
...
...
@@ -59,9 +60,11 @@ void unpack(const char* image) {
break
;
case
BZIP2
:
sprintf
(
name
,
"%s.%s"
,
RAMDISK_FILE
,
"bz2"
);
printf
(
"Unsupported format! Please decompress manually!
\n
"
);
break
;
case
LZ4
:
sprintf
(
name
,
"%s.%s"
,
RAMDISK_FILE
,
"lz4"
);
lz4
(
0
,
RAMDISK_FILE
,
ramdisk
,
hdr
.
ramdisk_size
);
break
;
default:
// Never happens
...
...
ndk-compression
@
60275045
Subproject commit
7cfa31b27065249b4d35cd1c7ceef4c2453b48b8
Subproject commit
602750457f2383f9a726eb12d2907f4ed282be91
sepolicy-inject
@
9f35fa0f
Subproject commit
2fe4d9706148d19c4ff2dce9119f40ae0641925a
Subproject commit
9f35fa0fa3f9b52c2b6b31253bade848019d7ab2
su
@
addbe81c
Subproject commit
12cc7b7885c67b17340b4637b67e190148927a7e
Subproject commit
addbe81c82e178952e9560d6ba027b1dbe8b840c
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment