View Javadoc
1   package emissary.transform.decode;
2   
3   import emissary.util.shell.Executrix;
4   
5   import java.io.ByteArrayOutputStream;
6   import java.io.IOException;
7   
8   @Deprecated
9   public class JavascriptEscape {
10  
11      /**
12       * Unescape javascript unicode characters in the form backslash-u-nnnn. Browser tests show that only lowercase "u" and
13       * only four digits work. Javascript also has normal unix escapes like \n and \r.
14       */
15      public static byte[] unescape(byte[] data) {
16          ByteArrayOutputStream out = new ByteArrayOutputStream();
17          for (int i = 0; i < data.length; i++) {
18              if (data[i] == '\\' && (i + 5) < data.length && data[i + 1] == 'u') {
19                  // process unicode escape
20                  try {
21                      String s = new String(data, i + 2, 4);
22                      char[] c = HtmlEscape.unescapeHtmlChar(s, true);
23                      if (c != null && c.length > 0) {
24                          out.write(new String(c).getBytes());
25                          // logger.debug("Unicode '{}' ==> '{}'", s, new String(c));
26                          i += 5;
27                      } else {
28                          out.write(data[i]);
29                      }
30                  } catch (IOException iox) {
31                      out.write(data[i]);
32                  }
33              } else if (data[i] == '\\' && (i + 1) < data.length && (data[i + 1] == 'n' || data[i + 1] == 'r')) {
34                  out.write('\n');
35              } else {
36                  out.write(data[i]);
37              }
38          }
39  
40          return out.toByteArray();
41      }
42  
43      /** This class is not meant to be instantiated. */
44      private JavascriptEscape() {}
45  
46      @SuppressWarnings("SystemOut")
47      public static void main(String[] args) {
48          int i = 0;
49  
50          for (; i < args.length; i++) {
51              byte[] content = Executrix.readDataFromFile(args[i]);
52              if (content == null) {
53                  System.out.println(args[i] + ": Unreadable");
54                  continue;
55              }
56  
57              System.out.println(args[i]);
58              byte[] escaped = JavascriptEscape.unescape(content);
59              System.out.write(escaped, 0, escaped.length);
60              System.out.println();
61          }
62      }
63  }