Skip to content

Commit

Permalink
Detect OS reserved names
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Aug 25, 2024
1 parent 49176d6 commit 5f5b3d3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
28 changes: 28 additions & 0 deletions java/src/main/java/kanzi/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ private Global()
65536
};

private static final String[] WIN_RESERVED =
{
// Sorted list
"AUX", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6",
"COM7", "COM8", "COM9", "COM¹", "COM²", "COM³", "CON", "LPT0", "LPT1", "LPT2",
"LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "NUL", "PRN"
};

private static final byte[] BASE64_SYMBOLS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes();

Expand Down Expand Up @@ -551,4 +559,24 @@ public static DataType detectSimpleType(int count, int[] freqs0)

return DataType.UNDEFINED;
}


public static boolean isReservedName(String fileName)
{
if (!System.getProperty("os.name").toLowerCase().contains("windows"))
return false;

for (int i=0; i<WIN_RESERVED.length; i++)
{
final int res = fileName.compareTo(WIN_RESERVED[i]);

if (res == 0)
return true;

if (res < 0)
break;
}

return false;
}
}
8 changes: 8 additions & 0 deletions java/src/main/java/kanzi/app/BlockCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,16 @@ public BlockCompressor(Map<String, Object> map)
this.overwrite = Boolean.TRUE.equals(map.remove("overwrite"));
this.skipBlocks = Boolean.TRUE.equals(map.remove("skipBlocks"));
String iName = (String) map.remove("inputName");

if (Global.isReservedName(iName))
throw new IllegalArgumentException("'"+iName+"' is a reserved name");

this.inputName = iName.isEmpty() ? STDIN : iName;
String oName = (String) map.remove("outputName");

if (Global.isReservedName(oName))
throw new IllegalArgumentException("'"+oName+"' is a reserved name");

this.outputName = (oName.isEmpty() && STDIN.equalsIgnoreCase(iName)) ? STDOUT : oName;
Integer iBlockSize = (Integer) map.remove("block");

Expand Down
8 changes: 8 additions & 0 deletions java/src/main/java/kanzi/app/BlockDecompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,16 @@ public BlockDecompressor(Map<String, Object> map)
this.noDotFiles = Boolean.TRUE.equals(map.remove("noDotFiles"));
this.noLinks = Boolean.TRUE.equals(map.remove("noLinks"));
String iName = (String) map.remove("inputName");

if (Global.isReservedName(iName))
throw new IllegalArgumentException("'"+iName+"' is a reserved name");

this.inputName = iName.isEmpty() ? STDIN : iName;
String oName = (String) map.remove("outputName");

if (Global.isReservedName(oName))
throw new IllegalArgumentException("'"+oName+"' is a reserved name");

this.outputName = (oName.isEmpty() && STDIN.equalsIgnoreCase(iName)) ? STDOUT : oName;
this.verbosity = (Integer) map.remove("verbose");
this.from = (map.containsKey("from") ? (Integer) map.remove("from") : -1);
Expand Down

0 comments on commit 5f5b3d3

Please sign in to comment.