Skip to content

Commit 5b6f423

Browse files
committed
Wasm
1 parent c977ccc commit 5b6f423

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed

src/main/java/net/siisise/lang/Bin.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,29 @@ public static byte[] ltob(final long[] src) {
805805
return ds;
806806
}
807807

808+
/**
809+
* Little Endian long列をbyte列に変換する
810+
*
811+
* @param src long列
812+
* @return byte列
813+
*/
814+
public static byte[] lltob(final long[] src) {
815+
byte[] ds = new byte[src.length * 8];
816+
for (int i = 0; i < src.length; i++) {
817+
long s = src[i];
818+
int l = i * 8;
819+
ds[l ] = (byte) (s );
820+
ds[l + 1] = (byte) (s >> 8);
821+
ds[l + 2] = (byte) (s >> 16);
822+
ds[l + 3] = (byte) (s >> 24);
823+
ds[l + 4] = (byte) (s >> 32);
824+
ds[l + 5] = (byte) (s >> 40);
825+
ds[l + 6] = (byte) (s >> 48);
826+
ds[l + 7] = (byte) (s >> 56);
827+
}
828+
return ds;
829+
}
830+
808831
/**
809832
* long列をint列に変換する
810833
*
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2025 okome.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.siisise.lang;
17+
18+
import java.math.BigInteger;
19+
import net.siisise.block.ReadableBlock;
20+
import net.siisise.io.Input;
21+
import net.siisise.io.Packet;
22+
import net.siisise.io.PacketA;
23+
24+
/**
25+
* 符号なしとして符号化.
26+
* Wasm用仮
27+
*/
28+
public class LEB128 {
29+
30+
/**
31+
* 正の整数限定
32+
* @param val
33+
* @return
34+
*/
35+
public static byte[] toLEB128(BigInteger val) {
36+
Packet pac = new PacketA();
37+
38+
do {
39+
byte v = (byte)(val.intValue() & 0x7f);
40+
val = val.shiftRight(7);
41+
if (!val.equals(BigInteger.ZERO)) {
42+
v |= 0x80;
43+
}
44+
pac.write(v);
45+
} while (val.compareTo(BigInteger.ZERO) > 0);
46+
return pac.toByteArray();
47+
}
48+
49+
public static BigInteger toBigInteger(byte[] val) {
50+
return toBigInteger(ReadableBlock.wrap(val));
51+
/*
52+
int shift = 0;
53+
BigInteger r = BigInteger.ZERO;
54+
for (byte v : val) {
55+
BigInteger b = BigInteger.valueOf(v & 0x7f);
56+
57+
r = r.or(b.shiftLeft(shift));
58+
if ( (v & 0x80) == 0) {
59+
return r;
60+
}
61+
shift += 7;
62+
}
63+
throw new IllegalStateException();
64+
*/
65+
}
66+
67+
public static BigInteger toBigInteger(Input in) {
68+
int shift = 0;
69+
BigInteger r = BigInteger.ZERO;
70+
int v = in.read();
71+
while (v >= 0) {
72+
BigInteger b = BigInteger.valueOf(v & 0x7f);
73+
74+
r = r.or(b.shiftLeft(shift));
75+
if ( (v & 0x80) == 0) {
76+
return r;
77+
}
78+
shift += 7;
79+
v = in.read();
80+
}
81+
throw new IllegalStateException();
82+
}
83+
84+
public static long toLong(Input in) {
85+
int shift = 0;
86+
long r = 0;
87+
int v = in.read();
88+
while (v >= 0) {
89+
long b = v & 0x7f;
90+
91+
r |= b << shift;
92+
if ( (v & 0x80) == 0) {
93+
return r;
94+
}
95+
shift += 7;
96+
v = in.read();
97+
}
98+
throw new IllegalStateException();
99+
}
100+
101+
/**
102+
* 符号なし
103+
* @param sign 符号なしとみなす
104+
* @return
105+
*/
106+
public static byte[] toLEB128(long sign) {
107+
Packet pac = new PacketA();
108+
do {
109+
byte v = (byte)(sign & 0x7f);
110+
sign >>>= 7;
111+
if ( sign != 0) {
112+
v |= 0x80;
113+
}
114+
pac.write(v);
115+
} while (sign > 0);
116+
return pac.toByteArray();
117+
}
118+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2025 okome.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.siisise.lang;
17+
18+
import java.math.BigInteger;
19+
import net.siisise.io.Packet;
20+
import net.siisise.io.PacketA;
21+
22+
/**
23+
* 符号なし可変長整数符号化.
24+
* Little Endian
25+
* Wasm用仮
26+
*/
27+
public class ULEB128 {
28+
29+
static final BigInteger MINUS1 = BigInteger.valueOf(-1);
30+
31+
/**
32+
* 正の整数限定
33+
* @param val
34+
* @return
35+
*/
36+
public static byte[] toULEB128(BigInteger val) {
37+
Packet pac = new PacketA();
38+
39+
do {
40+
byte v = (byte)(val.intValue() & 0x7f);
41+
val = val.shiftRight(7);
42+
if (!(val.equals(BigInteger.ZERO) || val.equals(MINUS1))) {
43+
v |= 0x80;
44+
}
45+
pac.write(v);
46+
} while (val.compareTo(MINUS1) != 0);
47+
return pac.toByteArray();
48+
}
49+
}

0 commit comments

Comments
 (0)