- /* Checksum whole block except for the checksum */
- CheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
- block_len-BLKHDR_CS_LENGTH);
- Dmsg1(1390, "ser_bloc_header: checksum=%x\n", CheckSum);
- ser_begin(block->buf, BLKHDR2_LENGTH);
- ser_uint32(CheckSum); /* now add checksum to block header */
-}
-
-/*
- * Unserialize the block header for reading block.
- * This includes setting all the buffer pointers correctly.
- *
- * Returns: false on failure (not a block)
- * true on success
- */
-static bool unser_block_header(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
-{
- ser_declare;
- char Id[BLKHDR_ID_LENGTH+1];
- uint32_t CheckSum, BlockCheckSum;
- uint32_t block_len;
- uint32_t block_end;
- uint32_t BlockNumber;
- int bhl;
-
- unser_begin(block->buf, BLKHDR_LENGTH);
- unser_uint32(CheckSum);
- unser_uint32(block_len);
- unser_uint32(BlockNumber);
- unser_bytes(Id, BLKHDR_ID_LENGTH);
- ASSERT(unser_length(block->buf) == BLKHDR1_LENGTH);
-
- Id[BLKHDR_ID_LENGTH] = 0;
- if (Id[3] == '1') {
- bhl = BLKHDR1_LENGTH;
- block->BlockVer = 1;
- block->bufp = block->buf + bhl;
- if (strncmp(Id, BLKHDR1_ID, BLKHDR_ID_LENGTH) != 0) {
- dev->dev_errno = EIO;
- Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
- dev->file, dev->block_num, BLKHDR1_ID, Id);
- if (block->read_errors == 0 || verbose >= 2) {
- Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
- }
- block->read_errors++;
- return false;
- }
- } else if (Id[3] == '2') {
- unser_uint32(block->VolSessionId);
- unser_uint32(block->VolSessionTime);
- bhl = BLKHDR2_LENGTH;
- block->BlockVer = 2;
- block->bufp = block->buf + bhl;
- if (strncmp(Id, BLKHDR2_ID, BLKHDR_ID_LENGTH) != 0) {
- dev->dev_errno = EIO;
- Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
- dev->file, dev->block_num, BLKHDR2_ID, Id);
- if (block->read_errors == 0 || verbose >= 2) {
- Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
- }
- block->read_errors++;
- return false;
- }
- } else {
- dev->dev_errno = EIO;
- Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
- dev->file, dev->block_num, BLKHDR2_ID, Id);
- if (block->read_errors == 0 || verbose >= 2) {
- Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
- }
- block->read_errors++;
- unser_uint32(block->VolSessionId);
- unser_uint32(block->VolSessionTime);
- return false;
- }
-
- /* Sanity check */
- if (block_len > MAX_BLOCK_LENGTH) {
- dev->dev_errno = EIO;
- Mmsg3(dev->errmsg, _("Volume data error at %u:%u! Block length %u is insane (too large), probably due to a bad archive.\n"),
- dev->file, dev->block_num, block_len);
- if (block->read_errors == 0 || verbose >= 2) {
- Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
- }
- block->read_errors++;
- return false;
- }
-
- Dmsg1(390, "unser_block_header block_len=%d\n", block_len);
- /* Find end of block or end of buffer whichever is smaller */
- if (block_len > block->read_len) {
- block_end = block->read_len;
- } else {
- block_end = block_len;
- }
- block->binbuf = block_end - bhl;
- block->block_len = block_len;
- block->BlockNumber = BlockNumber;
- Dmsg3(390, "Read binbuf = %d %d block_len=%d\n", block->binbuf,
- bhl, block_len);
- if (block_len <= block->read_len) {
- BlockCheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
- block_len-BLKHDR_CS_LENGTH);
- if (BlockCheckSum != CheckSum) {
- dev->dev_errno = EIO;
- Mmsg6(dev->errmsg, _("Volume data error at %u:%u!\n"
- "Block checksum mismatch in block=%u len=%d: calc=%x blk=%x\n"),
- dev->file, dev->block_num, (unsigned)BlockNumber,
- block_len, BlockCheckSum, CheckSum);
- if (block->read_errors == 0 || verbose >= 2) {
- Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
- }
- block->read_errors++;
- if (!forge_on) {
- return false;
- }
- }
- }
- return true;
-}