- ~CountedBuffer()
- {
- delete[] buffer;
- }
-
- void AddData(unsigned char* data, int data_length)
- {
- SI->Log(DEBUG,"AddData, %d bytes to add", data_length);
- if ((data_length + bufptr) > bufsz)
- {
- SI->Log(DEBUG,"Need to extend buffer to %d, is now %d", data_length + bufptr, bufsz);
- /* Buffer is too small, enlarge it and copy contents */
- int old_bufsz = bufsz;
- unsigned char* temp = buffer;
-
- bufsz += data_length;
- buffer = new unsigned char[bufsz + 1];
-
- memcpy(buffer, temp, old_bufsz);
-
- delete[] temp;
- }
-
- SI->Log(DEBUG,"Copy data in at pos %d", bufptr);
-
- memcpy(buffer + bufptr, data, data_length);
- bufptr += data_length;
- amount_read += data_length;
-
- SI->Log(DEBUG,"Amount read is now %d, bufptr is now %d", amount_read, bufptr);
-
- if ((!amount_expected) && (amount_read >= 4))
- {
- SI->Log(DEBUG,"We dont yet have an expected amount");
- /* We have enough to read an int */
- int* size = (int*)buffer;
- amount_expected = ntohl(*size);
- SI->Log(DEBUG,"Expected amount is %d", amount_expected);
- }
- }
-
- int GetFrame(unsigned char* frame, int maxsize)
- {
- if (amount_expected)
- {
- SI->Log(DEBUG,"Were expecting a frame of size %d", amount_expected);
- /* We know how much we're expecting...
- * Do we have enough yet?
- */
- if ((amount_read - 4) >= amount_expected)
- {
- SI->Log(DEBUG,"We have enough for the frame (have %d)", (amount_read - 4));
- int amt_ex = amount_expected;
- /* Yes, we have enough now */
- memcpy(frame, buffer + 4, amount_expected > maxsize ? maxsize : amount_expected);
- RemoveFirstFrame();
- return (amt_ex > maxsize) ? maxsize : amt_ex;
- }
- }
- /* Not enough for a frame yet, COME AGAIN! */
- return 0;
- }
-
- void RemoveFirstFrame()
- {
- SI->Log(DEBUG,"Removing first frame from buffer sized %d", amount_expected);
- unsigned char* temp = buffer;
-
- bufsz -= (amount_expected + 4);
- buffer = new unsigned char[bufsz + 1];
-
- SI->Log(DEBUG,"Shrunk buffer to %d", bufsz);
-
- memcpy(buffer, temp + amount_expected + 4, bufsz);
-
- amount_read -= (amount_expected + 4);
- SI->Log(DEBUG,"Amount read now %d", amount_read);
-
- if (amount_read >= 4)
- {
- /* We have enough to read an int */
- int* size = (int*)buffer;
- amount_expected = ntohl(*size);
- }
- else
- amount_expected = 0;
-
- SI->Log(DEBUG,"Amount expected now %d", amount_expected);
-
- bufptr = 0;
-
- delete[] temp;
- }
-};
-
-/** Represents an ZIP user's extra data