MCPcopy Index your code
hub / github.com/Q16G/memory-shell / tlv

Class tlv

src/main/java/org/example/tlv.java:6–193  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import java.nio.charset.StandardCharsets;
5
6public class tlv {
7 // 消息类型定义
8 public static final byte TYPE_INFO = 0x01; // 基础信息请求/响应
9 public static final byte TYPE_COMMAND = 0x02; // 命令执行请求/响应
10 public static final byte TYPE_HEARTBEAT = 0x03; // 存活检测请求/响应
11 private static final byte[] MAGIC_PREFIX = new byte[]{
12 (byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE // 4字节魔数
13 };
14
15 private static final byte[] XOR_KEY = new byte[]{
16 (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD // 自定义异或密钥
17 };
18
19 // XOR加密/解密方法
20 public static byte[] xorProcess(byte[] data) {
21 if (data == null || data.length == 0) {
22 return data;
23 }
24 byte[] result = new byte[data.length];
25 for (int i = 0; i < data.length; i++) {
26 result[i] = (byte) (data[i] ^ XOR_KEY[i % XOR_KEY.length]);
27 }
28 return result;
29 }
30
31 public static void main(String[] args) {
32 byte[] encode = new Request((byte) 0x00, null).encode();
33 Request decode = Request.decode(encode);
34 System.out.println(decode);
35
36 byte[] encode1 = new Response((byte) 0x00, (byte) 0x01, null).encode();
37 Response decode1 = Response.decode(encode1);
38 System.out.println(decode1);
39 }
40
41 public static boolean validate(byte[] data) {
42 if (data.length < MAGIC_PREFIX.length) {
43 return false;
44 }
45 for (int i = 0; i < MAGIC_PREFIX.length; i++) {
46 if (data[i] != MAGIC_PREFIX[i]) {
47 return false;
48 }
49 }
50 return true;
51 }
52
53 // 请求包结构
54 public static class Request {
55 private byte type; // 1字节类型
56 private int length; // 4字节长度
57 private byte[] data; // 数据部分
58
59 public Request(byte type, byte[] data) {
60 this.type = type;
61 this.data = data;
62 this.length = data != null ? data.length : 0;
63 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected