-
- size_t bytes = size * nmemb;
- input->curl_callback(ptr, bytes);
- return bytes;
-}
-
-void Input::curl_callback(char *ptr, size_t bytes)
-{
- pending_data.insert(pending_data.end(), ptr, ptr + bytes);
-
- for ( ;; ) {
- // If we don't have enough data (yet) for even the Metacube header, just return.
- if (pending_data.size() < sizeof(metacube_block_header)) {
- return;
- }
-
- // Make sure we have the Metacube sync header at the start.
- // We may need to skip over junk data (it _should_ not happen, though).
- if (!has_metacube_header) {
- char *ptr = static_cast<char *>(
- memmem(pending_data.data(), pending_data.size(),
- METACUBE_SYNC, strlen(METACUBE_SYNC)));
- if (ptr == NULL) {
- // OK, so we didn't find the sync marker. We know then that
- // we do not have the _full_ marker in the buffer, but we
- // could have N-1 bytes. Drop everything before that,
- // and then give up.
- drop_pending_data(pending_data.size() - (strlen(METACUBE_SYNC) - 1));
- return;
- } else {
- // Yay, we found the header. Drop everything (if anything) before it.
- drop_pending_data(ptr - pending_data.data());
- has_metacube_header = true;
-
- // Re-check that we have the entire header; we could have dropped data.
- if (pending_data.size() < sizeof(metacube_block_header)) {
- return;
- }
- }
- }
-
- // Now it's safe to read the header.
- metacube_block_header *hdr = reinterpret_cast<metacube_block_header *>(pending_data.data());
- assert(memcmp(hdr->sync, METACUBE_SYNC, sizeof(hdr->sync)) == 0);
- uint32_t size = ntohl(hdr->size);
- uint32_t flags = ntohl(hdr->flags);
-
- // See if we have the entire block. If not, wait for more data.
- if (pending_data.size() < sizeof(metacube_block_header) + size) {
- return;
- }
-
- process_block(pending_data.data() + sizeof(metacube_block_header), size, flags);
-
- // Consume this block. This isn't the most efficient way of dealing with things
- // should we have many blocks, but these routines don't need to be too efficient
- // anyway.
- pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube_block_header) + size);
- has_metacube_header = false;