+ /** Adds arbitrary compressed data to the buffer.
+ * - Binsry safe, of course.
+ */
+ void AddData(unsigned char* data, int data_length)
+ {
+ buffer.append((const char*)data, data_length);
+ this->NextFrameSize();
+ }
+
+ /** Works out the size of the next compressed frame
+ */
+ void NextFrameSize()
+ {
+ if ((!amount_expected) && (buffer.length() >= 4))
+ {
+ /* We have enough to read an int -
+ * Yes, this is safe, but its ugly. Give me
+ * a nicer way to read 4 bytes from a binary
+ * stream, and push them into a 32 bit int,
+ * and i'll consider replacing this.
+ */
+ amount_expected = ntohl((buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0]);
+ buffer = buffer.substr(4);
+ }
+ }
+
+ /** Gets the next frame and returns its size, or returns
+ * zero if there isnt one available yet.
+ * A frame can contain multiple plaintext lines.
+ * - Binary safe.
+ */
+ int GetFrame(unsigned char* frame, int maxsize)
+ {
+ if (amount_expected)
+ {
+ /* We know how much we're expecting...
+ * Do we have enough yet?
+ */
+ if (buffer.length() >= amount_expected)
+ {
+ int j = 0;
+ for (unsigned int i = 0; i < amount_expected; i++, j++)
+ frame[i] = buffer[i];
+
+ buffer = buffer.substr(j);
+ amount_expected = 0;
+ NextFrameSize();
+ return j;
+ }
+ }
+ /* Not enough for a frame yet, COME AGAIN! */
+ return 0;
+ }
+};