From 32139203e73e57883dcea84da2de2e50990170be Mon Sep 17 00:00:00 2001 From: cfig Date: Tue, 30 Apr 2019 23:55:37 +0800 Subject: [PATCH] fix several critial bug details - code refine "In Kotlin 1.3, it is now possible to capture the when subject into variable" - fix gradle version checking bug: now we can handle versions like "5.4-rc-1" and "5.4" - removed unwanted import of "UnImplNode" - add Struct3 doc --- bbootimg/src/main/kotlin/Helper.kt | 1 - bbootimg/src/main/kotlin/cfig/io/Struct3.kt | 4 ++-- build.gradle | 17 +++++++++++++++-- doc/Struct3.md | 21 +++++++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 doc/Struct3.md diff --git a/bbootimg/src/main/kotlin/Helper.kt b/bbootimg/src/main/kotlin/Helper.kt index 5670547..b0d2d01 100644 --- a/bbootimg/src/main/kotlin/Helper.kt +++ b/bbootimg/src/main/kotlin/Helper.kt @@ -2,7 +2,6 @@ package cfig import cfig.io.Struct3 import com.google.common.math.BigIntegerMath -import com.sun.org.apache.xml.internal.utils.UnImplNode import org.apache.commons.codec.binary.Hex import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream import org.apache.commons.compress.compressors.gzip.GzipParameters diff --git a/bbootimg/src/main/kotlin/cfig/io/Struct3.kt b/bbootimg/src/main/kotlin/cfig/io/Struct3.kt index 3f726c9..2fa3789 100644 --- a/bbootimg/src/main/kotlin/cfig/io/Struct3.kt +++ b/bbootimg/src/main/kotlin/cfig/io/Struct3.kt @@ -108,7 +108,7 @@ class Struct3 { fun calcSize(): Int? { var ret = 0 for (format in formats) { - when (format[0]) { + when (val formatType = format[0]) { Byte, UByte, Char, String, Type.Padding -> { ret += format[1] as Int } @@ -122,7 +122,7 @@ class Struct3 { ret += 8 * format[1] as Int } else -> { - throw IllegalArgumentException("Class [" + format[0] + "] not supported") + throw IllegalArgumentException("Class [" + formatType + "] not supported") } } } diff --git a/build.gradle b/build.gradle index f0ec782..5d21e86 100644 --- a/build.gradle +++ b/build.gradle @@ -9,8 +9,9 @@ subprojects { // ---------------------------------------------------------------------------- // global // ---------------------------------------------------------------------------- - -if (Float.parseFloat(gradle.gradleVersion) < 5.0) { +import java.util.regex.Matcher +import java.util.regex.Pattern +if (parseGradleVersion(gradle.gradleVersion) < 5) { logger.error("ERROR: Gradle Version MUST >= 5.0, current is {}", gradle.gradleVersion) throw new RuntimeException("ERROR: Gradle Version") } else { @@ -174,3 +175,15 @@ task rr { rebootRecovery() } } + +int parseGradleVersion(String version) { + Pattern VERSION_PATTERN = Pattern.compile("((\\d+)(\\.\\d+)+)(-(\\p{Alpha}+)-(\\w+))?(-(SNAPSHOT|\\d{14}([-+]\\d{4})?))?") + Matcher matcher = VERSION_PATTERN.matcher(version) + if (!matcher.matches()) { + throw new IllegalArgumentException(format("'%s' is not a valid Gradle version string (examples: '1.0', '1.0-rc-1')", version)) + } + String versionPart = matcher.group(1) + int majorPart = Integer.parseInt(matcher.group(2), 10) + logger.info("Gradle: versionPart {}, majorPart {}", versionPart, majorPart) + return majorPart +} diff --git a/doc/Struct3.md b/doc/Struct3.md new file mode 100644 index 0000000..2304b4f --- /dev/null +++ b/doc/Struct3.md @@ -0,0 +1,21 @@ +'Struct3' formats + + | Format | C Type | Python type | Standard size | Type | Parameter | Size | + | -- | -- | -- | -- | -- | -- | -- | + | x | pad byte | no value | | Type.Padding | "null,Byte,Int (only lower 8 bits are kept)" | 1 | + | c | char | bytes of length 1 | 1 | kotlin.Char | "Character.class (only lower 8 bits are kept, higher 8 bits are discarded)" | 1 | + | b | signed char | integer | 1 | kotlin.Byte | byte[] (item range: [-128~127]) | n | + | s | char[] | bytes | | kotlin.String | String.class | n | + | B | unsigned char | integer | 1 | Kotlin.UByte | byte[] (item range: [0~255]) | n | + | ? | _Bool | bool | 1 | | | | + | h | short | integer | 2 | kotlin.Short | "Int,Short, (range [-32768 , 32767])" | 2 | + | H | unsigned short | integer | 2 | kotlin.UShort | "Int,Short,UShort,(range [0 , 65535])" | 2 | + | i | int | integer | 4 | kotlin.Int | "[-2^31 , 2^31 - 1]" | 4 | + | l | long | integer | 4 | kotlin.Int | "[-2^31 , 2^31 - 1]" | 4 | + | I | unsigned int | integer | 4 | kotlin.UInt | "[0 , 2^32-1]" | | + | L | unsigned long | integer | 4 | kotlin.UInt | "[0 , 2^32-1]" | | + | q | long long | integer | 8 | kotlin.Long | | | | + | Q | unsigned long long | integer | 8 | kotlin.ULong | | | + | e | (7) | float | 2 | | | | + | f | float | float | 4 | | | | + | d | double | float | 8 | | | |