2

I have a Java application that persists byte[] structures to a DB (using Hibernate). I'm writing a C++ application that reads these structures at a later time.

As you might expect, I'm having problems.... The byte[] structure written to the DB is longer than the original number of bytes - 27 bytes, by the looks of things.

Is there a way of determining (in C++) the byte[] header structure to properly find the beginning of the true data?

I spent some time looking at the JNI source code (GetArrayLength(jbytearray), etc.) to determine how that works, but got quickly mired in the vagaries of JVM code. yuck...

Ideas?

5
  • 3
    You provide neither the real size of the byte[] nor any idea of any structure. It would help if you'd give us a better description of the array, any regularities it might have, and a sample before and after. Commented Jun 9, 2011 at 20:58
  • If your DB structure needs to be cross-platform in a general ongoing sense, you might consider attacking it from the side of changing how the data is persisted/serialized. Commented Jun 9, 2011 at 21:34
  • The length of the data varies by the type of data being persisted. Some is short (65 bytes written by Java and 38 bytes of real data) and some is long (1789 bytes in the DB and 1762 of real data). In the end, the total length doesn't matter, right? Commented Jun 10, 2011 at 15:24
  • The data itself is encrypted data. Some of the data is originally Strings and some is compiled object stuff. But again, that shouldn't matter. Commented Jun 10, 2011 at 15:25
  • Darien, you are correct about considering attacking it from how the data is persisted/serialized. Unfortunately, we didn't even envision this data being read by anything other than some Java/Hibernate-based application when originally designed and developed. Oops. Commented Jun 10, 2011 at 15:29

1 Answer 1

4

The object is probably being serialized using the Java Object Serialization Protocol. You can verify this by looking for the magic number 0xACED at the beginning. If this is the case, it's just wrapped with some meta information about the class and length, and you can easily parse the actual byte values off the end.

In particular, you would see 0xAC 0xED 0x00 0x05 for the header followed by a classDesc element that would be 0x75 ...bytes... 0x70, followed by a 4 byte length, and the then the bytes themselves. Java serializes the length and other multibyte values in big-endian format.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, thank you, thank you. That's exactly what I'm seeing. I looked for something like that and didn't find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.