Skip to content
This repository has been archived by the owner on Nov 24, 2024. It is now read-only.

Commit

Permalink
Reformat code with spotless:apply
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Jul 30, 2023
1 parent 557f9db commit 9b09322
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 351 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<scm>
<connection>scm:git:https://github.com/codehaus-plexus/plexus-cipher.git</connection>
<developerConnection>scm:git:https://github.com/codehaus-plexus/plexus-cipher.git</developerConnection>
<url>http://github.com/codehaus-plexus/plexus-cipher</url>
<tag>HEAD</tag>
<url>http://github.com/codehaus-plexus/plexus-cipher</url>
</scm>
<issueManagement>
<system>jira</system>
Expand Down Expand Up @@ -99,20 +99,20 @@
<executions>
<execution>
<id>utf8</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
<configuration>
<argLine>-Dfile.encoding=utf8</argLine>
</configuration>
</execution>
<execution>
<id>iso8859-1</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
<configuration>
<argLine>-Dfile.encoding=iso8859-1</argLine>
</configuration>
Expand Down
86 changes: 33 additions & 53 deletions src/main/java/org/sonatype/plexus/components/cipher/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

/**
* <p>Provides Base64 encoding and decoding as defined by RFC 2045.</p>
*
*
* <p>This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite>
* from RFC 2045 <cite>Multipurpose Internet Mail Extensions (MIME) Part One:
* Format of Internet Message Bodies</cite> by Freed and Borenstein.</p>
Expand All @@ -43,8 +43,7 @@
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
* @since 1.0-dev
*/
public class Base64
{
public class Base64 {

/**
* Chunk size per RFC 2045 section 6.8.
Expand Down Expand Up @@ -204,7 +203,6 @@ public static byte[] encodeBase64Chunked(byte[] binaryData) {
return encodeBase64(binaryData, true);
}


/**
* Decodes an Object using the base64 algorithm. This method
* is provided in order to satisfy the requirements of the
Expand Down Expand Up @@ -253,7 +251,7 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
int nbrChunks = 0;

if (fewerThan24bits != 0) {
//data not divisible by 24 bit
// data not divisible by 24 bit
encodedDataLength = (numberTriplets + 1) * 4;
} else {
// 16 or 8 bit
Expand All @@ -265,8 +263,7 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
// allow for extra length to account for the separator(s)
if (isChunked) {

nbrChunks =
(CHUNK_SEPARATOR.length == 0 ? 0 : (int) Math.ceil((float) encodedDataLength / CHUNK_SIZE));
nbrChunks = (CHUNK_SEPARATOR.length == 0 ? 0 : (int) Math.ceil((float) encodedDataLength / CHUNK_SIZE));
encodedDataLength += nbrChunks * CHUNK_SEPARATOR.length;
}

Expand All @@ -279,33 +276,28 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
int nextSeparatorIndex = CHUNK_SIZE;
int chunksSoFar = 0;

//log.debug("number of triplets = " + numberTriplets);
// log.debug("number of triplets = " + numberTriplets);
for (i = 0; i < numberTriplets; i++) {
dataIndex = i * 3;
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex + 1];
b3 = binaryData[dataIndex + 2];

//log.debug("b1= " + b1 +", b2= " + b2 + ", b3= " + b3);
// log.debug("b1= " + b1 +", b2= " + b2 + ", b3= " + b3);

l = (byte) (b2 & 0x0f);
k = (byte) (b1 & 0x03);

byte val1 =
((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
byte val2 =
((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
byte val3 =
((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc);
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc);

encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
//log.debug( "val2 = " + val2 );
//log.debug( "k4 = " + (k<<4) );
//log.debug( "vak = " + (val2 | (k<<4)) );
encodedData[encodedIndex + 1] =
lookUpBase64Alphabet[val2 | (k << 4)];
encodedData[encodedIndex + 2] =
lookUpBase64Alphabet[(l << 2) | val3];
// log.debug( "val2 = " + val2 );
// log.debug( "k4 = " + (k<<4) );
// log.debug( "vak = " + (val2 | (k<<4)) );
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | (k << 4)];
encodedData[encodedIndex + 2] = lookUpBase64Alphabet[(l << 2) | val3];
encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3f];

encodedIndex += 4;
Expand All @@ -314,15 +306,9 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
if (isChunked) {
// this assumes that CHUNK_SIZE % 4 == 0
if (encodedIndex == nextSeparatorIndex) {
System.arraycopy(CHUNK_SEPARATOR,
0,
encodedData,
encodedIndex,
CHUNK_SEPARATOR.length);
System.arraycopy(CHUNK_SEPARATOR, 0, encodedData, encodedIndex, CHUNK_SEPARATOR.length);
chunksSoFar++;
nextSeparatorIndex =
(CHUNK_SIZE * (chunksSoFar + 1)) +
(chunksSoFar * CHUNK_SEPARATOR.length);
nextSeparatorIndex = (CHUNK_SIZE * (chunksSoFar + 1)) + (chunksSoFar * CHUNK_SEPARATOR.length);
encodedIndex += CHUNK_SEPARATOR.length;
}
}
Expand All @@ -334,10 +320,9 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
if (fewerThan24bits == EIGHTBIT) {
b1 = binaryData[dataIndex];
k = (byte) (b1 & 0x03);
//log.debug("b1=" + b1);
//log.debug("b1<<2 = " + (b1>>2) );
byte val1 =
((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
// log.debug("b1=" + b1);
// log.debug("b1<<2 = " + (b1>>2) );
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4];
encodedData[encodedIndex + 2] = PAD;
Expand All @@ -349,26 +334,24 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) {
l = (byte) (b2 & 0x0f);
k = (byte) (b1 & 0x03);

byte val1 =
((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
byte val2 =
((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);

encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
encodedData[encodedIndex + 1] =
lookUpBase64Alphabet[val2 | (k << 4)];
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | (k << 4)];
encodedData[encodedIndex + 2] = lookUpBase64Alphabet[l << 2];
encodedData[encodedIndex + 3] = PAD;
}

if (isChunked) {
// we also add a separator to the end of the final chunk.
if (chunksSoFar < nbrChunks) {
System.arraycopy(CHUNK_SEPARATOR,
0,
encodedData,
encodedDataLength - CHUNK_SEPARATOR.length,
CHUNK_SEPARATOR.length);
System.arraycopy(
CHUNK_SEPARATOR,
0,
encodedData,
encodedDataLength - CHUNK_SEPARATOR.length,
CHUNK_SEPARATOR.length);
}
}

Expand Down Expand Up @@ -418,25 +401,23 @@ public static byte[] decodeBase64(byte[] base64Data) {
b2 = base64Alphabet[base64Data[dataIndex + 1]];

if (marker0 != PAD && marker1 != PAD) {
//No PAD e.g 3cQl
// No PAD e.g 3cQl
b3 = base64Alphabet[marker0];
b4 = base64Alphabet[marker1];

decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
decodedData[encodedIndex + 1] =
(byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
decodedData[encodedIndex + 2] = (byte) (b3 << 6 | b4);
} else if (marker0 == PAD) {
//Two PAD e.g. 3c[Pad][Pad]
// Two PAD e.g. 3c[Pad][Pad]
decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
} else // if ( marker1 == PAD ) (always true at this point)
{
//One PAD e.g. 3cQ[Pad]
// One PAD e.g. 3cQ[Pad]
b3 = base64Alphabet[marker0];

decodedData[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
decodedData[encodedIndex + 1] =
(byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
decodedData[encodedIndex + 1] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
}
encodedIndex += 3;
}
Expand Down Expand Up @@ -530,5 +511,4 @@ public Object encode(Object pObject) throws IllegalArgumentException {
public byte[] encode(byte[] pArray) {
return encodeBase64(pArray, false);
}

}
Loading

0 comments on commit 9b09322

Please sign in to comment.