Skip to content

Commit

Permalink
fix: clear definition of the specific encoding type
Browse files Browse the repository at this point in the history
  • Loading branch information
emptyOVO committed Sep 9, 2024
1 parent 24c1306 commit d63c9e1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,32 @@
import net.sf.jsqlparser.expression.Function;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@TransformFunction(names = {"decode"})
public class DecodeFunction implements ValueParser {

private ValueParser binaryParser;

private ValueParser characterSetParser;

private static final Set<String> SUPPORTED_CHARSETS;

static {
Set<String> charsets = new HashSet<>();
charsets.add(StandardCharsets.US_ASCII.name());
charsets.add(StandardCharsets.ISO_8859_1.name());
charsets.add(StandardCharsets.UTF_8.name());
charsets.add(StandardCharsets.UTF_16.name());
charsets.add(StandardCharsets.UTF_16BE.name());
charsets.add(StandardCharsets.UTF_16LE.name());
SUPPORTED_CHARSETS = Collections.unmodifiableSet(charsets);
}

public DecodeFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
if (expressions != null && expressions.size() == 2) {
Expand Down Expand Up @@ -63,7 +81,7 @@ private String decode(String binaryString, String charsetName) {
for (int i = 0; i < byteValues.length; i++) {
byteArray[i] = (byte) Integer.parseInt(byteValues[i]);
}
if (Charset.isSupported(charsetName)) {
if (Charset.isSupported(charsetName) && SUPPORTED_CHARSETS.contains(charsetName)) {
Charset charset = Charset.forName(charsetName);
return new String(byteArray, charset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,32 @@
import net.sf.jsqlparser.expression.Function;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@TransformFunction(names = {"encode"})
public class EncodeFunction implements ValueParser {

private ValueParser stringParser;

private ValueParser characterSetParser;

private static final Set<String> SUPPORTED_CHARSETS;

static {
Set<String> charsets = new HashSet<>();
charsets.add(StandardCharsets.US_ASCII.name());
charsets.add(StandardCharsets.ISO_8859_1.name());
charsets.add(StandardCharsets.UTF_8.name());
charsets.add(StandardCharsets.UTF_16.name());
charsets.add(StandardCharsets.UTF_16BE.name());
charsets.add(StandardCharsets.UTF_16LE.name());
SUPPORTED_CHARSETS = Collections.unmodifiableSet(charsets);
}

public EncodeFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
if (expressions != null && expressions.size() == 2) {
Expand Down Expand Up @@ -65,7 +83,7 @@ private byte[] encode(String stringValue, String characterSetValue) {
if (stringValue == null || stringValue.isEmpty() || characterSetValue == null || characterSetValue.isEmpty()) {
return new byte[0];
}
if (Charset.isSupported(characterSetValue)) {
if (Charset.isSupported(characterSetValue) && SUPPORTED_CHARSETS.contains(characterSetValue)) {
Charset charset = Charset.forName(characterSetValue);
return stringValue.getBytes(charset);
}
Expand Down

0 comments on commit d63c9e1

Please sign in to comment.