Class Utils

java.lang.Object
org.mariadb.jdbc.internal.util.Utils

public class Utils extends Object
  • Field Details

    • hexArray

      private static final char[] hexArray
    • IP_V4

      private static final Pattern IP_V4
    • IP_V6

      private static final Pattern IP_V6
    • IP_V6_COMPRESSED

      private static final Pattern IP_V6_COMPRESSED
    • socketHandler

      private static final SocketHandlerFunction socketHandler
  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • standardSocket

      public static Socket standardSocket(UrlParser urlParser, String host) throws IOException
      Use standard socket implementation.
      Parameters:
      urlParser - url parser
      host - host to connect
      Returns:
      socket
      Throws:
      IOException - in case of error establishing socket.
    • escapeString

      public static String escapeString(String value, boolean noBackslashEscapes)
      Escape String.
      Parameters:
      value - value to escape
      noBackslashEscapes - must backslash be escaped
      Returns:
      escaped string.
    • encryptPassword

      public static byte[] encryptPassword(String password, byte[] seed, String passwordCharacterEncoding) throws NoSuchAlgorithmException, UnsupportedEncodingException
      Encrypts a password.

      protocol for authentication is like this: 1. Server sends a random array of bytes (the seed) 2. client makes a sha1 digest of the password 3. client hashes the output of 2 4. client digests the seed 5. client updates the digest with the output from 3 6. an xor of the output of 5 and 2 is sent to server 7. server does the same thing and verifies that the scrambled passwords match

      Parameters:
      password - the password to encrypt
      seed - the seed to use
      passwordCharacterEncoding - password character encoding
      Returns:
      a scrambled password
      Throws:
      NoSuchAlgorithmException - if SHA1 is not available on the platform we are using
      UnsupportedEncodingException - if passwordCharacterEncoding is not a valid charset name
    • copyWithLength

      public static byte[] copyWithLength(byte[] orig, int length)
      Copies the original byte array content to a new byte array. The resulting byte array is always "length" size. If length is smaller than the original byte array, the resulting byte array is truncated. If length is bigger than the original byte array, the resulting byte array is filled with zero bytes.
      Parameters:
      orig - the original byte array
      length - how big the resulting byte array will be
      Returns:
      the copied byte array
    • copyRange

      public static byte[] copyRange(byte[] orig, int from, int to)
      Copies from original byte array to a new byte array. The resulting byte array is always "to-from" size.
      Parameters:
      orig - the original byte array
      from - index of first byte in original byte array which will be copied
      to - index of last byte in original byte array which will be copied. This can be outside of the original byte array
      Returns:
      resulting array
    • replaceFunctionParameter

      private static String replaceFunctionParameter(String functionString, Protocol protocol)
      Helper function to replace function parameters in escaped string. 3 functions are handles :
      • CONVERT(value, type): replacing SQL_XXX types to convertible type, i.e SQL_BIGINT to INTEGER
      • TIMESTAMPDIFF(type, ...): replacing type SQL_TSI_XXX in type with XXX, i.e SQL_TSI_HOUR with HOUR
      • TIMESTAMPADD(type, ...): replacing type SQL_TSI_XXX in type with XXX, i.e SQL_TSI_HOUR with HOUR

      caution: this use MariaDB server conversion: 'SELECT CONVERT('2147483648', INTEGER)' will return a BIGINT. MySQL will throw a syntax error.

      Parameters:
      functionString - input string
      protocol - protocol
      Returns:
      unescaped string
    • resolveEscapes

      private static String resolveEscapes(String escaped, Protocol protocol) throws SQLException
      Throws:
      SQLException
    • nativeSql

      public static String nativeSql(String sql, Protocol protocol) throws SQLException
      Escape sql String.
      Parameters:
      sql - initial sql
      protocol - protocol
      Returns:
      escaped sql string
      Throws:
      SQLException - if escape sequence is incorrect.
    • retrieveProxy

      public static Protocol retrieveProxy(UrlParser urlParser, GlobalStateInfo globalInfo) throws SQLException
      Retrieve protocol corresponding to the failover options. if no failover option, protocol will not be proxied. if a failover option is precised, protocol will be proxied so that any connection error will be handle directly.
      Parameters:
      urlParser - urlParser corresponding to connection url string.
      globalInfo - global variable information
      Returns:
      protocol
      Throws:
      SQLException - if any error occur during connection
    • getProxyLoggingIfNeeded

      private static Protocol getProxyLoggingIfNeeded(UrlParser urlParser, Protocol protocol)
    • getTimeZone

      public static TimeZone getTimeZone(String id) throws SQLException
      Get timezone from Id. This differ from java implementation : by default, if timezone Id is unknown, java return GMT timezone. GMT will be return only if explicitly asked.
      Parameters:
      id - timezone id
      Returns:
      timezone.
      Throws:
      SQLException - if no timezone is found for this Id
    • createSocket

      public static Socket createSocket(UrlParser urlParser, String host) throws IOException
      Create socket accordingly to options.
      Parameters:
      urlParser - urlParser
      host - hostName ( mandatory only for named pipe)
      Returns:
      a nex socket
      Throws:
      IOException - if connection error occur
    • hexdump

      public static String hexdump(byte[]... bytes)
      Hexdump.
      Parameters:
      bytes - byte arrays
      Returns:
      String
    • hexdump

      public static String hexdump(int maxQuerySizeToLog, int offset, int length, byte[]... byteArr)
      Hexdump.

      String output example :

      
       7D 00 00 01 C5 00 00                                 ......            <- first byte array
       01 00 00 01 02 33 00 00  02 03 64 65 66 05 74 65     .....3....def.te   <- second byte array
       73 74 6A 0A 74 65 73 74  5F 62 61 74 63 68 0A 74     stj.test_batch.t
       65 73 74 5F 62 61 74 63  68 02 69 64 02 69 64 0C     est_batch.id.id.
       3F 00 0B 00 00 00 03 03  42 00 00 00 37 00 00 03     ?.......B...7...
       03 64 65 66 05 74 65 73  74 6A 0A 74 65 73 74 5F     .def.testj.test_
       62 61 74 63 68 0A 74 65  73 74 5F 62 61 74 63 68     batch.test_batch
       04 74 65 73 74 04 74 65  73 74 0C 21 00 1E 00 00     .test.test.!....
       00 FD 00 00 00 00 00 05  00 00 04 FE 00 00 22 00     ..............".
       06 00 00 05 01 31 03 61  61 61 06 00 00 06 01 32     .....1.aaa.....2
       03 62 62 62 06 00 00 07  01 33 03 63 63 63 06 00     .bbb.....3.ccc..
       00 08 01 34 03 61 61 61  06 00 00 09 01 35 03 62     ...4.aaa.....5.b
       62 62 06 00 00 0A 01 36  03 63 63 63 05 00 00 0B     bb.....6.ccc....
       FE 00 00 22 00                                       ...".
       }
      Parameters:
      maxQuerySizeToLog - max log size
      offset - offset of last byte array
      length - length of last byte array
      byteArr - byte arrays. if many, only the last may have offset and size limitation others will be displayed completely.
      Returns:
      String
    • writeHex

      private static void writeHex(byte[] bytes, int offset, int dataLength, StringBuilder outputBuilder)
      Write bytes/hexadecimal value of a byte array to a StringBuilder.

      String output example :

      
       38 00 00 00 03 63 72 65  61 74 65 20 74 61 62 6C     8....create tabl
       65 20 42 6C 6F 62 54 65  73 74 63 6C 6F 62 74 65     e BlobTestclobte
       73 74 32 20 28 73 74 72  6D 20 74 65 78 74 29 20     st2 (strm text)
       43 48 41 52 53 45 54 20  75 74 66 38                 CHARSET utf8
       
      Parameters:
      bytes - byte array
      offset - offset
      dataLength - byte length to write
      outputBuilder - string builder
    • getHex

      private static String getHex(byte[] raw)
    • byteArrayToHexString

      public static String byteArrayToHexString(byte[] bytes)
    • intToHexString

      public static String intToHexString(int value)
      Convert int value to hexadecimal String.
      Parameters:
      value - value to transform
      Returns:
      Hexadecimal String value of integer.
    • parseSessionVariables

      public static String parseSessionVariables(String sessionVariable)
      Parse the option "sessionVariable" to ensure having no injection. semi-column not in string will be replaced by comma.
      Parameters:
      sessionVariable - option value
      Returns:
      parsed String
    • isIPv4

      public static boolean isIPv4(String ip)
    • isIPv6

      public static boolean isIPv6(String ip)
    • transactionFromString

      public static int transactionFromString(String txIsolation) throws SQLException
      Traduce a String value of transaction isolation to corresponding java value.
      Parameters:
      txIsolation - String value
      Returns:
      java corresponding value (Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_REPEATABLE_READ or Connection.TRANSACTION_SERIALIZABLE)
      Throws:
      SQLException - if String value doesn't correspond to @@tx_isolation/@@transaction_isolation possible value
    • validateFileName

      public static boolean validateFileName(String sql, ParameterHolder[] parameters, String fileName)
      Validate that file name correspond to send query.
      Parameters:
      sql - sql command
      parameters - sql parameter
      fileName - server file name
      Returns:
      true if correspond