+}
+
+static void unser_crypto_size(JCR *jcr)
+{
+ unser_declare;
+ if (jcr->crypto_size == 0 && jcr->crypto_count >= CRYPTO_LEN_SIZE) {
+ unser_begin(&jcr->crypto_buf[0], CRYPTO_LEN_SIZE);
+ unser_uint32(jcr->crypto_size);
+ jcr->crypto_size += CRYPTO_LEN_SIZE;
+ }
+}
+
+bool store_data(JCR *jcr, BFILE *bfd, char *data, const int32_t length, bool win32_decomp)
+{
+ if (win32_decomp) {
+ if (!processWin32BackupAPIBlock(bfd, data, length)) {
+ berrno be;
+ Jmsg2(jcr, M_ERROR, 0, _("Write error in Win32 Block Decomposition on %s: %s\n"),
+ jcr->last_fname, be.strerror(bfd->berrno));
+ return false;
+ }
+ } else if (bwrite(bfd, data, length) != (ssize_t)length) {
+ berrno be;
+ Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"),
+ jcr->last_fname, be.strerror(bfd->berrno));
+ return false;
+ }
+
+ return true;
+}
+
+/*
+ * In the context of jcr, write data to bfd.
+ * We write buflen bytes in buf at addr. addr is updated in place.
+ * The flags specify whether to use sparse files or compression.
+ * Return value is the number of bytes written, or -1 on errors.
+ */
+int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
+ uint64_t *addr, int flags, CIPHER_CONTEXT *cipher, uint32_t cipher_block_size)
+{
+ char *wbuf; /* write buffer */
+ uint32_t wsize; /* write size */
+ uint32_t rsize; /* read size */
+ uint32_t decrypted_len = 0; /* Decryption output length */
+ char ec1[50]; /* Buffer printing huge values */
+
+ rsize = buflen;
+ jcr->ReadBytes += rsize;
+ wsize = rsize;
+ wbuf = buf;
+
+ if (flags & FO_ENCRYPT) {
+ ASSERT(cipher);
+
+ while (jcr->crypto_size > 0 && jcr->crypto_count > 0 && wsize > 0) {
+ uint32_t chunk_size = 16;
+
+ if (chunk_size > wsize) {
+ chunk_size = wsize;
+ }
+
+ /*
+ * Grow the crypto buffer, if necessary.
+ * crypto_cipher_update() will process only whole blocks,
+ * buffering the remaining input.
+ */
+ jcr->crypto_buf = check_pool_memory_size(jcr->crypto_buf, jcr->crypto_count + chunk_size + cipher_block_size);
+
+ /* Decrypt the input block */
+ if (!crypto_cipher_update(cipher,
+ (const u_int8_t *)wbuf,
+ chunk_size,
+ (u_int8_t *)&jcr->crypto_buf[jcr->crypto_count],
+ &decrypted_len)) {
+ /* Decryption failed. Shouldn't happen. */
+ Jmsg(jcr, M_FATAL, 0, _("Decryption error\n"));
+ return -1;
+ }
+
+ jcr->crypto_count += decrypted_len;
+ wbuf += chunk_size;
+ wsize -= chunk_size;
+
+ if (jcr->crypto_count >= jcr->crypto_size) {
+ char *packet = &jcr->crypto_buf[CRYPTO_LEN_SIZE]; /* Decrypted, possibly decompressed output here. */
+ uint32_t packet_size = jcr->crypto_size - CRYPTO_LEN_SIZE;
+
+ if (flags & FO_GZIP) {
+ if (!decompress_data(jcr, &packet, &packet_size)) {
+ return -1;
+ }
+ } else {
+ Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
+ }
+
+ if (!store_data(jcr, bfd, packet, packet_size, (flags & FO_WIN32DECOMP) != 0)) {
+ return -1;
+ }
+
+ jcr->JobBytes += packet_size;
+ *addr += packet_size;
+
+ memmove(&jcr->crypto_buf[0], &jcr->crypto_buf[jcr->crypto_size], jcr->crypto_count - jcr->crypto_size);
+ jcr->crypto_count -= jcr->crypto_size;
+ jcr->crypto_size = 0;
+ }
+ }
+
+ /*
+ * Grow the crypto buffer, if necessary.
+ * crypto_cipher_update() will process only whole blocks,
+ * buffering the remaining input.
+ */
+ jcr->crypto_buf = check_pool_memory_size(jcr->crypto_buf, jcr->crypto_count + wsize + cipher_block_size);
+
+ /* Decrypt the input block */
+ if (!crypto_cipher_update(cipher,
+ (const u_int8_t *)wbuf,
+ wsize,
+ (u_int8_t *)&jcr->crypto_buf[jcr->crypto_count],
+ &decrypted_len)) {
+ /* Decryption failed. Shouldn't happen. */
+ Jmsg(jcr, M_FATAL, 0, _("Decryption error\n"));
+ return -1;
+ }
+
+ Dmsg2(100, "decrypted len=%d encrypted len=%d\n", decrypted_len, wsize);
+
+ if (decrypted_len == 0) {
+ /* No full block of data available, write more data */
+ return 0;
+ }
+
+ jcr->crypto_count += decrypted_len;
+
+ unser_crypto_size(jcr);
+ wsize = jcr->crypto_size - CRYPTO_LEN_SIZE;
+ Dmsg1(10, "Decrypt size=%d\n", wsize);
+ wbuf = &jcr->crypto_buf[CRYPTO_LEN_SIZE]; /* Decrypted, possibly decompressed output here. */
+
+ if (jcr->crypto_size == 0 || jcr->crypto_count < jcr->crypto_size) {
+ return 0;
+ }
+
+ }
+
+ if (flags & FO_SPARSE) {
+ unser_declare;
+ uint64_t faddr;
+ char ec1[50];
+ unser_begin(wbuf, SPARSE_FADDR_SIZE);
+ unser_uint64(faddr);
+ if (*addr != faddr) {
+ *addr = faddr;
+ if (blseek(bfd, (boffset_t)*addr, SEEK_SET) < 0) {
+ berrno be;
+ Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
+ edit_uint64(*addr, ec1), jcr->last_fname,
+ be.strerror(bfd->berrno));
+ return -1;
+ }
+ }
+ wbuf += SPARSE_FADDR_SIZE;
+ wsize -= SPARSE_FADDR_SIZE;
+ }
+
+ if (flags & FO_GZIP) {
+ if (!decompress_data(jcr, &wbuf, &wsize)) {
+ return -1;
+ }
+ } else {
+ Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
+ }
+
+ if (!store_data(jcr, bfd, wbuf, wsize, (flags & FO_WIN32DECOMP) != 0)) {
+ return -1;
+ }
+
+ jcr->JobBytes += wsize;
+ *addr += wsize;
+
+ return wsize;
+}
+
+/*
+ * In the context of jcr, flush any remaining data from the cipher context,
+ * writing it to bfd.
+ * Return value is true on success, false on failure.
+ */
+bool flush_cipher(JCR *jcr, BFILE *bfd, int flags, CIPHER_CONTEXT *cipher, uint32_t cipher_block_size)
+{
+ uint32_t decrypted_len;
+ char *wbuf; /* write buffer */
+ uint32_t wsize; /* write size */
+ char ec1[50]; /* Buffer printing huge values */
+
+ /* Write out the remaining block and free the cipher context */
+ jcr->crypto_buf = check_pool_memory_size(jcr->crypto_buf, jcr->crypto_count +
+ cipher_block_size);
+
+ if (!crypto_cipher_finalize(cipher, (uint8_t *)&jcr->crypto_buf[jcr->crypto_count],
+ &decrypted_len)) {
+ /* Writing out the final, buffered block failed. Shouldn't happen. */
+ Jmsg1(jcr, M_FATAL, 0, _("Decryption error for %s\n"), jcr->last_fname);
+ }
+
+ /* If nothing new was decrypted, and our output buffer is empty, return */
+ if (decrypted_len == 0 && jcr->crypto_count == 0) {
+ return true;
+ }
+
+ jcr->crypto_count += decrypted_len;
+
+ unser_crypto_size(jcr);
+ wsize = jcr->crypto_size - CRYPTO_LEN_SIZE;
+ Dmsg1(10, "Unser size=%d\n", wsize);
+ wbuf = &jcr->crypto_buf[CRYPTO_LEN_SIZE]; /* Decrypted, possibly decompressed output here. */
+
+ ASSERT(jcr->crypto_count == jcr->crypto_size);
+
+ if (flags & FO_GZIP) {
+ decompress_data(jcr, &wbuf, &wsize);
+ } else {
+ Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
+ }
+
+ if (!store_data(jcr, bfd, wbuf, wsize, (flags & FO_WIN32DECOMP) != 0)) {
+ return false;
+ }
+
+ jcr->JobBytes += wsize;
+
+ return true;
+}