Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
115ccec9
Commit
115ccec9
authored
Mar 06, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rearrange classes
parent
d8c64286
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
523 additions
and
1 deletion
+523
-1
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/IOFormat.kt
...in/net/mamoe/mirai/qqandroid/io/serialization/IOFormat.kt
+14
-0
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/Jce.kt
.../kotlin/net/mamoe/mirai/qqandroid/io/serialization/Jce.kt
+5
-1
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceDecoder.kt
.../mamoe/mirai/qqandroid/io/serialization/jce/JceDecoder.kt
+83
-0
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceInput.kt
...et/mamoe/mirai/qqandroid/io/serialization/jce/JceInput.kt
+237
-0
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceNew.kt
.../net/mamoe/mirai/qqandroid/io/serialization/jce/JceNew.kt
+57
-0
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/common.kt
.../net/mamoe/mirai/qqandroid/io/serialization/jce/common.kt
+60
-0
mirai-core-qqandroid/src/commonTest/kotlin/net.mamoe.mirai.qqandroid.io.serialization/JceInputTest.kt
...et.mamoe.mirai.qqandroid.io.serialization/JceInputTest.kt
+67
-0
No files found.
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/IOFormat.kt
0 → 100644
View file @
115ccec9
package
net.mamoe.mirai.qqandroid.io.serialization
import
io.ktor.utils.io.core.Input
import
io.ktor.utils.io.core.Output
import
kotlinx.serialization.DeserializationStrategy
import
kotlinx.serialization.SerialFormat
import
kotlinx.serialization.SerializationStrategy
interface
IOFormat
:
SerialFormat
{
fun
<
T
>
dump
(
serializer
:
SerializationStrategy
<
T
>,
input
:
Input
):
ByteArray
fun
<
T
>
load
(
deserializer
:
DeserializationStrategy
<
T
>,
output
:
Output
):
T
}
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/Jce.kt
View file @
115ccec9
...
...
@@ -21,6 +21,7 @@ import kotlinx.serialization.modules.SerialModule
import
kotlinx.serialization.protobuf.ProtoId
import
net.mamoe.mirai.qqandroid.io.JceStruct
import
net.mamoe.mirai.qqandroid.io.ProtoBuf
import
net.mamoe.mirai.qqandroid.io.serialization.jce.JceHead
import
net.mamoe.mirai.utils.io.readString
import
net.mamoe.mirai.utils.io.toReadPacket
...
...
@@ -588,7 +589,10 @@ class Jce private constructor(private val charset: JceCharset, override val cont
}
tag
=
readUByte
().
toUInt
()
}
currentJceHead
=
JceHead
(
tag
=
tag
.
toInt
(),
type
=
type
.
toByte
())
currentJceHead
=
JceHead
(
tag
=
tag
.
toInt
(),
type
=
type
.
toByte
()
)
// println("doReadHead: $currentJceHead")
return
currentJceHead
}
...
...
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceDecoder.kt
0 → 100644
View file @
115ccec9
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package
net.mamoe.mirai.qqandroid.io.serialization.jce
import
kotlinx.serialization.*
import
kotlinx.serialization.internal.TaggedDecoder
import
kotlinx.serialization.modules.SerialModule
import
kotlinx.serialization.protobuf.ProtoId
@OptIn
(
InternalSerializationApi
::
class
)
// 将来 kotlinx 修改后再复制过来 mirai.
private
class
JceDecoder
(
val
jce
:
JceInput
,
override
val
context
:
SerialModule
)
:
TaggedDecoder
<
JceTag
>()
{
override
val
updateMode
:
UpdateMode
get
()
=
UpdateMode
.
BANNED
override
fun
SerialDescriptor
.
getTag
(
index
:
Int
):
JceTag
{
val
annotations
=
this
.
getElementAnnotations
(
index
)
val
id
=
(
annotations
.
asSequence
().
filterIsInstance
<
JceId
>().
firstOrNull
()
?.
id
?:
annotations
.
asSequence
().
filterIsInstance
<
ProtoId
>().
firstOrNull
()
?.
id
)
// 旧版本兼容
?:
error
(
"cannot find @JceId or @ProtoId for ${this.getElementName(index)} in ${this.serialName}"
)
return
JceTag
(
id
,
this
.
getElementDescriptor
(
index
).
isNullable
)
}
override
fun
beginStructure
(
descriptor
:
SerialDescriptor
,
vararg
typeParams
:
KSerializer
<
*
>):
CompositeDecoder
{
TODO
(
"Not yet implemented"
)
}
override
fun
decodeElementIndex
(
descriptor
:
SerialDescriptor
):
Int
{
TODO
(
"Not yet implemented"
)
}
override
fun
decodeTaggedInt
(
tag
:
JceTag
):
Int
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceIntValue
(
it
)
}
override
fun
decodeTaggedByte
(
tag
:
JceTag
):
Byte
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceByteValue
(
it
)
}
override
fun
decodeTaggedBoolean
(
tag
:
JceTag
):
Boolean
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceBooleanValue
(
it
)
}
override
fun
decodeTaggedFloat
(
tag
:
JceTag
):
Float
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceFloatValue
(
it
)
}
override
fun
decodeTaggedDouble
(
tag
:
JceTag
):
Double
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceDoubleValue
(
it
)
}
override
fun
decodeTaggedShort
(
tag
:
JceTag
):
Short
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceShortValue
(
it
)
}
override
fun
decodeTaggedLong
(
tag
:
JceTag
):
Long
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceLongValue
(
it
)
}
override
fun
decodeTaggedString
(
tag
:
JceTag
):
String
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceStringValue
(
it
)
}
override
fun
decodeTaggedEnum
(
tag
:
JceTag
,
enumDescription
:
SerialDescriptor
):
Int
{
return
super
.
decodeTaggedEnum
(
tag
,
enumDescription
)
}
override
fun
decodeTaggedChar
(
tag
:
JceTag
):
Char
{
return
super
.
decodeTaggedChar
(
tag
)
}
override
fun
decodeTaggedNotNullMark
(
tag
:
JceTag
):
Boolean
{
println
(
"!! decodeTaggedNotNullMark: $tag"
)
return
super
.
decodeTaggedNotNullMark
(
tag
)
}
}
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/
JceNew
.kt
→
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/
jce/JceInput
.kt
View file @
115ccec9
...
...
@@ -7,138 +7,18 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package
net.mamoe.mirai.qqandroid.io.serialization
package
net.mamoe.mirai.qqandroid.io.serialization
.jce
import
io.ktor.utils.io.core.*
import
kotlinx.serialization.*
import
kotlinx.serialization.internal.TaggedDecoder
import
kotlinx.serialization.modules.EmptyModule
import
kotlinx.serialization.modules.SerialModule
import
kotlinx.serialization.protobuf.ProtoId
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.BYTE
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.DOUBLE
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.FLOAT
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.INT
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.LIST
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.LONG
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.MAP
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.SHORT
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.SIMPLE_LIST
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.STRING1
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.STRING4
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.STRUCT_BEGIN
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.STRUCT_END
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.ZERO_TYPE
import
net.mamoe.mirai.qqandroid.io.serialization.Jce
import
net.mamoe.mirai.qqandroid.io.serialization.JceCharset
import
net.mamoe.mirai.utils.io.readString
interface
IOFormat
:
SerialFormat
{
fun
<
T
>
dump
(
serializer
:
SerializationStrategy
<
T
>,
input
:
Input
):
ByteArray
fun
<
T
>
load
(
deserializer
:
DeserializationStrategy
<
T
>,
output
:
Output
):
T
}
/**
* Jce 数据结构序列化和反序列化器.
*
* @author Him188
*/
class
JceNew
(
override
val
context
:
SerialModule
)
:
SerialFormat
,
IOFormat
{
companion
object
Default
:
IOFormat
by
JceNew
(
EmptyModule
)
override
fun
<
T
>
dump
(
serializer
:
SerializationStrategy
<
T
>,
input
:
Input
):
ByteArray
{
TODO
(
"Not yet implemented"
)
}
override
fun
<
T
>
load
(
deserializer
:
DeserializationStrategy
<
T
>,
output
:
Output
):
T
{
TODO
(
"Not yet implemented"
)
}
}
/**
*
标注 JCE 序列化时使用的 ID
*
Jce Input. 需要手动管理 head.
*/
@SerialInfo
annotation
class
JceId
(
val
id
:
Int
)
/**
* 类中元素的 tag
*
* 保留这个结构, 为将来增加功能的兼容性.
*/
internal
data class
JceTag
(
val
id
:
Int
,
val
isNullable
:
Boolean
)
@OptIn
(
InternalSerializationApi
::
class
)
// 将来 kotlinx 修改后再复制过来 mirai.
private
class
JceDecoder
(
val
jce
:
JceInput
,
override
val
context
:
SerialModule
)
:
TaggedDecoder
<
JceTag
>()
{
override
val
updateMode
:
UpdateMode
get
()
=
UpdateMode
.
BANNED
override
fun
SerialDescriptor
.
getTag
(
index
:
Int
):
JceTag
{
val
annotations
=
this
.
getElementAnnotations
(
index
)
val
id
=
(
annotations
.
asSequence
().
filterIsInstance
<
JceId
>().
firstOrNull
()
?.
id
?:
annotations
.
asSequence
().
filterIsInstance
<
ProtoId
>().
firstOrNull
()
?.
id
)
// 旧版本兼容
?:
error
(
"cannot find @JceId or @ProtoId for ${this.getElementName(index)} in ${this.serialName}"
)
return
JceTag
(
id
,
this
.
getElementDescriptor
(
index
).
isNullable
)
}
override
fun
beginStructure
(
descriptor
:
SerialDescriptor
,
vararg
typeParams
:
KSerializer
<
*
>):
CompositeDecoder
{
TODO
(
"Not yet implemented"
)
}
override
fun
decodeElementIndex
(
descriptor
:
SerialDescriptor
):
Int
{
TODO
(
"Not yet implemented"
)
}
override
fun
decodeTaggedInt
(
tag
:
JceTag
):
Int
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceIntValue
(
it
)
}
override
fun
decodeTaggedByte
(
tag
:
JceTag
):
Byte
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceByteValue
(
it
)
}
override
fun
decodeTaggedBoolean
(
tag
:
JceTag
):
Boolean
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceBooleanValue
(
it
)
}
override
fun
decodeTaggedFloat
(
tag
:
JceTag
):
Float
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceFloatValue
(
it
)
}
override
fun
decodeTaggedDouble
(
tag
:
JceTag
):
Double
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceDoubleValue
(
it
)
}
override
fun
decodeTaggedShort
(
tag
:
JceTag
):
Short
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceShortValue
(
it
)
}
override
fun
decodeTaggedLong
(
tag
:
JceTag
):
Long
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceLongValue
(
it
)
}
override
fun
decodeTaggedString
(
tag
:
JceTag
):
String
=
jce
.
skipToTagAndUseIfPossibleOrFail
(
tag
.
id
)
{
jce
.
readJceStringValue
(
it
)
}
override
fun
decodeTaggedEnum
(
tag
:
JceTag
,
enumDescription
:
SerialDescriptor
):
Int
{
return
super
.
decodeTaggedEnum
(
tag
,
enumDescription
)
}
override
fun
decodeTaggedChar
(
tag
:
JceTag
):
Char
{
return
super
.
decodeTaggedChar
(
tag
)
}
override
fun
decodeTaggedNotNullMark
(
tag
:
JceTag
):
Boolean
{
println
(
"!! decodeTaggedNotNullMark: $tag"
)
return
super
.
decodeTaggedNotNullMark
(
tag
)
}
}
private
class
JceInput
(
class
JceInput
(
val
input
:
Input
,
val
charset
:
JceCharset
)
{
private
var
_head
:
JceHead
?
=
null
...
...
@@ -146,6 +26,10 @@ private class JceInput(
val
currentHead
:
JceHead
get
()
=
_head
?:
error
(
"No current JceHead available"
)
val
currentHeadOrNull
:
JceHead
?
get
()
=
_head
init
{
prepareNextHead
()
}
/**
* 读取下一个 [JceHead] 并保存. 可通过 [currentHead] 获取这个 [JceHead].
*
...
...
@@ -176,7 +60,10 @@ private class JceInput(
}
tag
=
input
.
readUByte
().
toUInt
()
}
return
JceHead
(
tag
=
tag
.
toInt
(),
type
=
type
.
toByte
())
return
JceHead
(
tag
=
tag
.
toInt
(),
type
=
type
.
toByte
()
)
}
/**
...
...
@@ -219,36 +106,39 @@ private class JceInput(
}
}
inline
fun
skipToHeadOrFail
(
tag
:
Int
,
message
:
()
->
String
=
{
"head not found: $tag"
}):
JceHead
{
inline
fun
skipToHeadOrFail
(
tag
:
Int
,
message
:
()
->
String
=
{
"head not found: $tag"
}
):
JceHead
{
return
checkNotNull
(
skipToHeadOrNull
(
tag
),
message
)
}
@OptIn
(
ExperimentalUnsignedTypes
::
class
)
@PublishedApi
internal
fun
skipField
(
type
:
Byte
):
Unit
=
when
(
type
)
{
BYTE
->
this
.
input
.
discardExact
(
1
)
SHORT
->
this
.
input
.
discardExact
(
2
)
INT
->
this
.
input
.
discardExact
(
4
)
LONG
->
this
.
input
.
discardExact
(
8
)
FLOAT
->
this
.
input
.
discardExact
(
4
)
DOUBLE
->
this
.
input
.
discardExact
(
8
)
STRING1
->
this
.
input
.
discardExact
(
this
.
input
.
readUByte
().
toInt
())
STRING4
->
this
.
input
.
discardExact
(
this
.
input
.
readInt
())
MAP
->
{
// map
Jce
.
BYTE
->
this
.
input
.
discardExact
(
1
)
Jce
.
SHORT
->
this
.
input
.
discardExact
(
2
)
Jce
.
INT
->
this
.
input
.
discardExact
(
4
)
Jce
.
LONG
->
this
.
input
.
discardExact
(
8
)
Jce
.
FLOAT
->
this
.
input
.
discardExact
(
4
)
Jce
.
DOUBLE
->
this
.
input
.
discardExact
(
8
)
Jce
.
STRING1
->
this
.
input
.
discardExact
(
this
.
input
.
readUByte
().
toInt
())
Jce
.
STRING4
->
this
.
input
.
discardExact
(
this
.
input
.
readInt
())
Jce
.
MAP
->
{
// map
repeat
(
skipToTagAndUseIfPossibleOrFail
(
0
)
{
readJceIntValue
(
it
)
}
*
2
)
{
useHead
{
skipField
(
it
.
type
)
}
}
}
LIST
->
{
// list
Jce
.
LIST
->
{
// list
repeat
(
skipToTagAndUseIfPossibleOrFail
(
0
)
{
readJceIntValue
(
it
)
})
{
useHead
{
skipField
(
it
.
type
)
}
}
}
STRUCT_BEGIN
->
{
Jce
.
STRUCT_BEGIN
->
{
fun
skipToStructEnd
()
{
var
head
:
JceHead
do
{
...
...
@@ -258,10 +148,10 @@ private class JceInput(
}
skipToStructEnd
()
}
STRUCT_END
,
ZERO_TYPE
->
{
Jce
.
STRUCT_END
,
Jce
.
ZERO_TYPE
->
{
}
SIMPLE_LIST
->
{
Jce
.
SIMPLE_LIST
->
{
val
head
=
nextHead
()
check
(
head
.
type
.
toInt
()
==
0
)
{
"skipField with invalid type, type value: "
+
type
+
", "
+
head
.
type
}
this
.
input
.
discardExact
(
...
...
@@ -276,46 +166,46 @@ private class JceInput(
// region readers
fun
readJceIntValue
(
head
:
JceHead
):
Int
{
return
when
(
head
.
type
)
{
ZERO_TYPE
->
0
BYTE
->
input
.
readByte
().
toInt
()
SHORT
->
input
.
readShort
().
toInt
()
INT
->
input
.
readInt
()
Jce
.
ZERO_TYPE
->
0
Jce
.
BYTE
->
input
.
readByte
().
toInt
()
Jce
.
SHORT
->
input
.
readShort
().
toInt
()
Jce
.
INT
->
input
.
readInt
()
else
->
error
(
"type mismatch: ${head.type}"
)
}
}
fun
readJceShortValue
(
head
:
JceHead
):
Short
{
return
when
(
head
.
type
)
{
ZERO_TYPE
->
0
BYTE
->
input
.
readByte
().
toShort
()
SHORT
->
input
.
readShort
()
Jce
.
ZERO_TYPE
->
0
Jce
.
BYTE
->
input
.
readByte
().
toShort
()
Jce
.
SHORT
->
input
.
readShort
()
else
->
error
(
"type mismatch: ${head.type}"
)
}
}
fun
readJceLongValue
(
head
:
JceHead
):
Long
{
return
when
(
head
.
type
)
{
ZERO_TYPE
->
0
BYTE
->
input
.
readByte
().
toLong
()
SHORT
->
input
.
readShort
().
toLong
()
INT
->
input
.
readInt
().
toLong
()
LONG
->
input
.
readLong
()
Jce
.
ZERO_TYPE
->
0
Jce
.
BYTE
->
input
.
readByte
().
toLong
()
Jce
.
SHORT
->
input
.
readShort
().
toLong
()
Jce
.
INT
->
input
.
readInt
().
toLong
()
Jce
.
LONG
->
input
.
readLong
()
else
->
error
(
"type mismatch ${head.type}"
)
}
}
fun
readJceByteValue
(
head
:
JceHead
):
Byte
{
return
when
(
head
.
type
)
{
ZERO_TYPE
->
0
BYTE
->
input
.
readByte
()
Jce
.
ZERO_TYPE
->
0
Jce
.
BYTE
->
input
.
readByte
()
else
->
error
(
"type mismatch: ${head.type}"
)
}
}
fun
readJceFloatValue
(
head
:
JceHead
):
Float
{
return
when
(
head
.
type
)
{
ZERO_TYPE
->
0f
FLOAT
->
input
.
readFloat
()
Jce
.
ZERO_TYPE
->
0f
Jce
.
FLOAT
->
input
.
readFloat
()
else
->
error
(
"type mismatch: ${head.type}"
)
}
}
...
...
@@ -323,8 +213,8 @@ private class JceInput(
@OptIn
(
ExperimentalUnsignedTypes
::
class
)
fun
readJceStringValue
(
head
:
JceHead
):
String
{
return
when
(
head
.
type
)
{
STRING1
->
input
.
readString
(
input
.
readUByte
().
toInt
(),
charset
=
charset
.
kotlinCharset
)
STRING4
->
input
.
readString
(
Jce
.
STRING1
->
input
.
readString
(
input
.
readUByte
().
toInt
(),
charset
=
charset
.
kotlinCharset
)
Jce
.
STRING4
->
input
.
readString
(
input
.
readUInt
().
toInt
().
also
{
require
(
it
in
1
until
104857600
)
{
"bad string length: $it"
}
},
charset
=
charset
.
kotlinCharset
)
...
...
@@ -345,32 +235,3 @@ private class JceInput(
return
readJceByteValue
(
head
)
==
0
.
toByte
()
}
}
@OptIn
(
ExperimentalUnsignedTypes
::
class
)
inline
class
JceHead
(
private
val
value
:
Long
)
{
constructor
(
tag
:
Int
,
type
:
Byte
)
:
this
(
tag
.
toLong
().
shl
(
32
)
or
type
.
toLong
())
val
tag
:
Int
get
()
=
(
value
ushr
32
).
toInt
()
val
type
:
Byte
get
()
=
value
.
toUInt
().
toByte
()
override
fun
toString
():
String
{
val
typeString
=
when
(
type
)
{
Jce
.
BYTE
->
"Byte"
Jce
.
DOUBLE
->
"Double"
Jce
.
FLOAT
->
"Float"
Jce
.
INT
->
"Int"
Jce
.
LIST
->
"List"
Jce
.
LONG
->
"Long"
Jce
.
MAP
->
"Map"
Jce
.
SHORT
->
"Short"
Jce
.
SIMPLE_LIST
->
"SimpleList"
Jce
.
STRING1
->
"String1"
Jce
.
STRING4
->
"String4"
Jce
.
STRUCT_BEGIN
->
"StructBegin"
Jce
.
STRUCT_END
->
"StructEnd"
Jce
.
ZERO_TYPE
->
"Zero"
else
->
error
(
"illegal jce type: $type"
)
}
return
"JceHead(tag=$tag, type=$type($typeString))"
}
}
\ No newline at end of file
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceNew.kt
0 → 100644
View file @
115ccec9
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package
net.mamoe.mirai.qqandroid.io.serialization.jce
import
io.ktor.utils.io.core.*
import
kotlinx.serialization.*
import
kotlinx.serialization.internal.TaggedDecoder
import
kotlinx.serialization.modules.EmptyModule
import
kotlinx.serialization.modules.SerialModule
import
kotlinx.serialization.protobuf.ProtoId
import
net.mamoe.mirai.qqandroid.io.serialization.IOFormat
import
net.mamoe.mirai.qqandroid.io.serialization.Jce
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.BYTE
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.DOUBLE
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.FLOAT
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.INT
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.LIST
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.LONG
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.MAP
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.SHORT
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.SIMPLE_LIST
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.STRING1
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.STRING4
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.STRUCT_BEGIN
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.STRUCT_END
import
net.mamoe.mirai.qqandroid.io.serialization.Jce.Companion.ZERO_TYPE
import
net.mamoe.mirai.qqandroid.io.serialization.JceCharset
import
net.mamoe.mirai.utils.io.readString
/**
* Jce 数据结构序列化和反序列化器.
*
* @author Him188
*/
class
JceNew
(
override
val
context
:
SerialModule
)
:
SerialFormat
,
IOFormat
{
companion
object
Default
:
IOFormat
by
JceNew
(
EmptyModule
)
override
fun
<
T
>
dump
(
serializer
:
SerializationStrategy
<
T
>,
input
:
Input
):
ByteArray
{
TODO
(
"Not yet implemented"
)
}
override
fun
<
T
>
load
(
deserializer
:
DeserializationStrategy
<
T
>,
output
:
Output
):
T
{
TODO
(
"Not yet implemented"
)
}
}
\ No newline at end of file
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/common.kt
0 → 100644
View file @
115ccec9
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package
net.mamoe.mirai.qqandroid.io.serialization.jce
import
kotlinx.serialization.SerialInfo
import
net.mamoe.mirai.qqandroid.io.serialization.Jce
/**
* 标注 JCE 序列化时使用的 ID
*/
@SerialInfo
annotation
class
JceId
(
val
id
:
Int
)
/**
* 类中元素的 tag
*
* 保留这个结构, 为将来增加功能的兼容性.
*/
internal
data class
JceTag
(
val
id
:
Int
,
val
isNullable
:
Boolean
)
@OptIn
(
ExperimentalUnsignedTypes
::
class
)
inline
class
JceHead
(
private
val
value
:
Long
)
{
constructor
(
tag
:
Int
,
type
:
Byte
)
:
this
(
tag
.
toLong
().
shl
(
32
)
or
type
.
toLong
())
val
tag
:
Int
get
()
=
(
value
ushr
32
).
toInt
()
val
type
:
Byte
get
()
=
value
.
toUInt
().
toByte
()
override
fun
toString
():
String
{
val
typeString
=
when
(
type
)
{
Jce
.
BYTE
->
"Byte"
Jce
.
DOUBLE
->
"Double"
Jce
.
FLOAT
->
"Float"
Jce
.
INT
->
"Int"
Jce
.
LIST
->
"List"
Jce
.
LONG
->
"Long"
Jce
.
MAP
->
"Map"
Jce
.
SHORT
->
"Short"
Jce
.
SIMPLE_LIST
->
"SimpleList"
Jce
.
STRING1
->
"String1"
Jce
.
STRING4
->
"String4"
Jce
.
STRUCT_BEGIN
->
"StructBegin"
Jce
.
STRUCT_END
->
"StructEnd"
Jce
.
ZERO_TYPE
->
"Zero"
else
->
error
(
"illegal jce type: $type"
)
}
return
"JceHead(tag=$tag, type=$type($typeString))"
}
}
\ No newline at end of file
mirai-core-qqandroid/src/commonTest/kotlin/net.mamoe.mirai.qqandroid.io.serialization/JceInputTest.kt
0 → 100644
View file @
115ccec9
@
file
:
Suppress
(
"unused"
)
package
net.mamoe.mirai.qqandroid.io.serialization
import
io.ktor.utils.io.core.Output
import
io.ktor.utils.io.core.buildPacket
import
io.ktor.utils.io.core.writeInt
import
net.mamoe.mirai.qqandroid.io.serialization.jce.JceInput
import
kotlin.test.Test
import
kotlin.test.assertEquals
internal
const
val
BYTE
:
Byte
=
0
internal
const
val
DOUBLE
:
Byte
=
5
internal
const
val
FLOAT
:
Byte
=
4
internal
const
val
INT
:
Byte
=
2
internal
const
val
JCE_MAX_STRING_LENGTH
=
104857600
internal
const
val
LIST
:
Byte
=
9
internal
const
val
LONG
:
Byte
=
3
internal
const
val
MAP
:
Byte
=
8
internal
const
val
SHORT
:
Byte
=
1
internal
const
val
SIMPLE_LIST
:
Byte
=
13
internal
const
val
STRING1
:
Byte
=
6
internal
const
val
STRING4
:
Byte
=
7
internal
const
val
STRUCT_BEGIN
:
Byte
=
10
internal
const
val
STRUCT_END
:
Byte
=
11
internal
const
val
ZERO_TYPE
:
Byte
=
12
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@Suppress
(
"INVISIBLE_MEMBER"
)
// bug
internal
class
JceInputTest
{
@Test
fun
testHeadStack
()
{
}
@Test
fun
testReadInt
()
{
val
input
=
JceInput
(
buildPacket
{
writeHead
(
INT
,
0
)
writeInt
(
123456
)
},
JceCharset
.
UTF8
)
assertEquals
(
123456
,
input
.
readJceIntValue
(
input
.
nextHead
()))
}
@PublishedApi
internal
fun
Output
.
writeHead
(
type
:
Byte
,
tag
:
Int
)
{
if
(
tag
<
15
)
{
writeByte
(((
tag
shl
4
)
or
type
.
toInt
()).
toByte
())
return
}
if
(
tag
<
256
)
{
writeByte
((
type
.
toInt
()
or
0
xF0
).
toByte
())
writeByte
(
tag
.
toByte
())
return
}
error
(
"tag is too large: $tag"
)
}
}
\ No newline at end of file
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