Wednesday, April 10, 2013

Happy as a bald Baby in Red Overalls

Firstly, great thanks and props to Chinedu for shedding the light on this very dark problem, but I finally manged to fix it!

My small rant
"The guys that did JWebSocket went out of their way to port the whole implementation to j2me(even though they made some errors with reading the byte 0xff)." here had me stomped when I had to redo the codebase to work exclusively with websockets. The problem was figuring where the start(0x00) and end(0xff) of the json websocket packets were. And since j2me was finding it almost impossible to read these two values, Chinedu found out something based on this documentation here

5.6. Data Frames

Data frames (e.g., non-control frames) are identified by opcodes where the most significant bit of the opcode is 0. Currently defined opcodes for data frames include 0x1 (Text), 0x2 (Binary). Opcodes 0x3-0x7 are reserved for further non-control frames yet to be defined. Data frames carry application-layer and/or extension-layer data. The opcode determines the interpretation of the data: Text The "Payload data" is text data encoded as UTF-8. Note that a particular text frame might include a partial UTF-8 sequence; however, the whole message MUST contain valid UTF-8. Invalid UTF-8 in reassembled messages is handled as described in Section 8.1. Binary The "Payload data" is arbitrary binary data whose interpretation is solely up to the application layer.

5.7. Examples

o A single-frame unmasked text message * 0x81 0x05 0x48 0x65 0x6c 0x6c 0x6f (contains "Hello") o A single-frame masked text message * 0x81 0x85 0x37 0xfa 0x21 0x3d 0x7f 0x9f 0x4d 0x51 0x58 (contains "Hello") o A fragmented unmasked text message * 0x01 0x03 0x48 0x65 0x6c (contains "Hel") * 0x80 0x02 0x6c 0x6f (contains "lo")


To break it down, this is what it entails.

  1. Read the incoming data stream like this b = is.read()
  2. Check for this value '129' which signifies the start of the package. "129 is 0x81 which is the marker for a single-frame unmasked text message"
  3. Read the next byte which represents the length of the packet.
  4. Iterate as many times as the length acquired to read the whole packet

Here's my method:
public StringBuffer readStream(InputStream is){
                 StringBuffer sb = null;              
                int length = -1;
                 try {
                    int b = is.read();                
                    if (b != -1) {
                        if (b == 129) {//Set length
                            length = is.read();                        
                        }
                        sb = new StringBuffer();
                     
                            for (int i = 0; i < length;) {
                                int c=is.read();                              
                                sb.append((char) c);
                                i++;    //Increment after /*do*/
                            }                      
                     
                    }

                } catch (IOException ex) {
                    isRunning = false;
                    ex.printStackTrace();
                }
               
                 return sb;
             }
UPDATE: This actually failed. Server underwent a major rebuild with websockets as the ONLY communication channel.

Wednesday, April 3, 2013

Error during Obfuscation

Trying to Obfuscate the saya code base, kept bumping into this error

Warning: there were 1 instances of library classes depending on program classes.
         You must avoid such dependencies, since the program classes will
         be processed, while the library classes will remain unchanged.
Error: Please correct the above warnings first.
The issue it seems was caused by these two classes
Warning: library class com.sun.ukit.jaxp.Parser extends or implements program class org.xml.sax.Locator 
The solution was take apart the whole code and piece it together, class by class(yes-tedious I know). But finally, it worked.!

Thanks to below link for guidance :)


http://proguard.sourceforge.net/manual/troubleshooting.html#dependency