Skip to content

Commit e0990e8

Browse files
committed
8.2.6 RC
Changed - better coverage of BinaryCalc Security - fj-bom updated to to 1.3.5
1 parent a4adcda commit e0990e8

File tree

5 files changed

+110
-52
lines changed

5 files changed

+110
-52
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- better coverage of BinaryCalc
13+
14+
### Security
15+
16+
- fj-bom updated to to 1.3.5
17+
1018
## [8.2.5] - 2023-09-04
1119

1220
### Added

docgen/require/katexInjector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $(document).ready( function () {
1515
katex.render(addDisp+texTxt, el);
1616
}
1717
catch(err) {
18-
$(this).html("<span class='err'>"+err);
18+
$(this).html("<span class='err'>error");
1919
}
2020
});
2121

fj-core/src/main/java/org/fugerit/java/core/util/BinaryCalc.java

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.fugerit.java.core.util;
22

3+
import java.math.BigDecimal;
34
import java.util.Arrays;
45
import java.util.List;
6+
import java.util.function.Function;
57

68
/*
79
*
@@ -13,13 +15,39 @@ public class BinaryCalc {
1315

1416
private BinaryCalc() {}
1517

16-
public static long hexToLong( String hexString ) {
17-
long result = 0;
18-
hexString = hexString.toUpperCase();
19-
for ( int k=hexString.length()-1, esp=0; k>=0; k--, esp++ ) {
20-
result+= BinaryNumber.hexToInt( hexString.charAt( k ) )*Math.pow( 16 , esp );
18+
private static final BigDecimal ZERO = new BigDecimal( "0" ).setScale( 0 );
19+
20+
private static long convertToLong( String stringVal, Function<Character, Integer> fun, int pow ) {
21+
BigDecimal result = ZERO;
22+
stringVal = stringVal.toUpperCase();
23+
for ( int k=stringVal.length()-1, esp=0; k>=0; k--, esp++ ) {
24+
result = result.add( new BigDecimal( fun.apply( stringVal.charAt( k ) )*Math.pow( pow , esp ) ) );
2125
}
22-
return result;
26+
return result.longValue();
27+
}
28+
29+
public static long hexToLong( String stringVal ) {
30+
return convertToLong( stringVal , c -> BinaryNumber.hexToInt( c ) , 16 );
31+
}
32+
33+
public static long octToLong( String stringVal ) {
34+
return convertToLong( stringVal , c -> BinaryNumber.octToInt( c ) , 8 );
35+
}
36+
37+
public static long binToLong( String stringVal ) {
38+
return convertToLong( stringVal , c -> BinaryNumber.binToInt( c ) , 2 );
39+
}
40+
41+
public static String longToHex( long value ) {
42+
return new BinaryNumber( value ).getHexString();
43+
}
44+
45+
public static String longToOct( long value ) {
46+
return new BinaryNumber( value ).getOctString();
47+
}
48+
49+
public static String longToBin( long value ) {
50+
return new BinaryNumber( value ).getBinString();
2351
}
2452

2553
}
@@ -66,6 +94,11 @@ public BinaryNumber() {
6694
this.size = 0;
6795
}
6896

97+
public BinaryNumber( long value ) {
98+
this();
99+
this.setValue(value);
100+
}
101+
69102
private byte[] data;
70103

71104
private int size;
@@ -94,45 +127,10 @@ public int get( int index, int radix ) {
94127
return result;
95128
}
96129

97-
public int binSize() {
98-
return this.size;
99-
}
100-
101-
public int octSize() {
102-
return this.size( OCT_RADIX );
103-
}
104-
105-
public int hextSize() {
106-
return this.size( HEX_RADIX );
107-
}
108-
109-
public int getBin( int index ) {
110-
return this.data[index];
111-
}
112-
113-
public int getOct( int index ) {
114-
return this.get( index, OCT_RADIX );
115-
}
116-
117-
public int getHex( int index ) {
118-
return this.get( index, HEX_RADIX );
119-
}
120-
121-
public char getBinValue( int index ) {
122-
return VALUES_BIN[ this.getBin( index ) ];
123-
}
124-
125-
public char getOctValue( int index ) {
126-
return VALUES_OCT[ this.getOct( index ) ];
127-
}
128-
129-
public char getHexValue( int index ) {
130-
return VALUES_HEX[ this.getHex( index ) ];
131-
}
132-
133130
private String getString( char[] values, int radix ) {
134131
StringBuilder buffer = new StringBuilder();
135-
for ( int k=0; k<this.size( radix ); k++ ) {
132+
int currSize = this.size( radix );
133+
for ( int k=currSize-1; k>=0; k-- ) {
136134
buffer.append( values[ this.get( k , radix ) ] );
137135
}
138136
return buffer.toString();
@@ -150,9 +148,4 @@ public String getHexString() {
150148
return this.getString( VALUES_HEX, HEX_RADIX );
151149
}
152150

153-
@Override
154-
public String toString() {
155-
return this.getBinString();
156-
}
157-
158151
}

fj-core/src/test/java/test/org/fugerit/java/core/util/TestBinaryCalc.java

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,67 @@
1010
public class TestBinaryCalc extends BasicTest {
1111

1212
@Test
13-
public void hexToLong() {
13+
public void hexToLongFF() {
1414
long res = BinaryCalc.hexToLong( "FF" );
15-
logger.info( "TEST : {}", res );
15+
logger.info( "TEST hex 1 : {}", res );
1616
assertEquals( 255L , res );
1717
}
18+
19+
@Test
20+
public void hexToLongF() {
21+
long res = BinaryCalc.hexToLong( "F" );
22+
logger.info( "TEST hex 2 : {}", res );
23+
assertEquals( 15L , res );
24+
}
25+
26+
@Test
27+
public void hexToLong7BB() {
28+
long res = BinaryCalc.hexToLong( "7BB" );
29+
logger.info( "TEST hex 3 : {}", res );
30+
assertEquals( 1979L , res );
31+
}
32+
33+
@Test
34+
public void octToLong255() {
35+
long res = BinaryCalc.octToLong( "377" );
36+
logger.info( "TEST oct 1 : {}", res );
37+
assertEquals( 255L , res );
38+
}
39+
40+
41+
@Test
42+
public void binToLong55() {
43+
long res = BinaryCalc.binToLong( "11111111" );
44+
logger.info( "TEST bin 1 : {}", res );
45+
assertEquals( 255L , res );
46+
}
47+
48+
@Test
49+
public void longToHex255() {
50+
String res = BinaryCalc.longToHex( 255 );
51+
logger.info( "TEST long 1 : {}", res );
52+
assertEquals( "FF" , res );
53+
}
54+
55+
@Test
56+
public void longToOct255() {
57+
String res = BinaryCalc.longToOct( 255 );
58+
logger.info( "TEST long 2 : {}", res );
59+
assertEquals( "377" , res );
60+
}
61+
62+
@Test
63+
public void longToBin255() {
64+
String res = BinaryCalc.longToBin( 255 );
65+
logger.info( "TEST long 3 : {}", res );
66+
assertEquals( "11111111" , res );
67+
}
68+
69+
@Test
70+
public void longToHex43133() {
71+
String res = BinaryCalc.longToHex( 43133 );
72+
logger.info( "TEST long 4 : {}", res );
73+
assertEquals( "A87D" , res );
74+
}
1875

1976
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.fugerit.java</groupId>
99
<artifactId>fj-bom</artifactId>
10-
<version>1.3.4</version>
10+
<version>1.3.5</version>
1111
<relativePath></relativePath>
1212
</parent>
1313

0 commit comments

Comments
 (0)