Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  1
Language: JAVA
Code:
public class IeViDgbX {

    public static void main(String[] args) {
        String[] ret = splitNetworkString("network={\n\tssid=\"work\" scan_ssid=1\n\tpsk=\"12345678\"\nkey_mgmt=WPA-EAP\npairwise=CCMP TKIP\n  group=CCMP TKIP\n   eap=TLS\n identity=\"[email protected]\"\n   ca_cert=\"/etc/cert/ca.pem\"\n  client_cert=\"/etc/cert/user.pem\"\n  private_key=\"/etc/cert/user.prv\"\n   private_key_passwd=\"password\"\n}");
        int count = ret.size();
        for(int i = 0; i < count; i++) {
           System.out.println(ret[i]);
     }
    }

public static String[] splitNetworkString(String str) {
//check psk is hashed
     final int START = 0;
     final int PSK_P = 1;
     final int PSK_S = 2;
     final int PSK_K = 3;
     final int PSK_EQUAL = 4;
     final int PSK_QUOTE = 5;
     final int END = 100;
     
     int count = str.length;
     char token;
     int i = 0;
     int state = START;
     int p1_end = 0, psk_start = 0, psk_end = 0, p2_start = 0;
     
     while(i < count) {
          token = str.charAt(i);
          
          switch(state) {
               case START: {
                    switch(token) {
                         case 'p':
                              state = PSK_P;
                              break;
                         default:
                              state = START;
                              break;
                    }
                    break;
               }
               case PSK_P: {
                    switch(token) {
                         case 's':
                              state = PSK_S;
                              break;
                         default:
                              state = START;
                              break;
                    }
                    break;
               }
               case PSK_S: {
                    switch(token) {
                         case 'k':
                              state = PSK_K;
                              p1_end = i - 3;
                              break;
                         default:
                              state = START;
                              break;
                    }
                    break;
               }
               case PSK_K: {
                    switch(token) {
                         case '=':
                              state = PSK_EQUAL;
                              break;
                         case ' ':
                         case '\t':
                         case '\n':
                              state = PSK_K;
                              break;
                         default:
                              state = START;
                              p1_end = 0;
                              break;
                    }
                    break;
               }
               case PSK_EQUAL: {
                    switch(token) {
                         case ' ':
                         case '\t':
                         case '\n':
                              state = PSK_EQUAL;
                              break;
                         case '"':
                              state = PSK_QUOTE;
                              psk_start = i + 1;
                              break;
                         default:
                              state = START;
                              p1_end = 0;
                              psk_start = 0;
                              break;
                    }
                    break;
               }
               case PSK_QUOTE: {
                    switch(token) {
                         case '"':
                              psk_end = i - 1;
                              p2_start = i + 1;
                              i = count; //break while
                              break;
                         default:
                              state = PSK_QUOTE;  //in psk
                              break;
                    }
                    break;
               }
               case END: {
                    break;
               }
          }
          
          i++;
     }

     List<String> result = new ArrayList<>();
     if(p1_end != 0)
          result.add(str.substring(0, p1_end + 1));
     
     if(psk_start != 0 && psk_end != 0)
          result.add(str.substring(psk_start, psk_end + 1));
     
     if(p2_start != 0)
          result.add(str.substring(p2_start));
     
     return (String[])result.toArray();
}

}          
Comments: