+static int crypto_write(URLContext *h, const unsigned char *buf, int size)
+{
+ CryptoContext *c = h->priv_data;
+ int total_size, blocks, pad_len, out_size;
+ uint8_t *out_buf;
+ int ret = 0;
+
+ total_size = size + c->pad_len;
+ pad_len = total_size % BLOCKSIZE;
+ out_size = total_size - pad_len;
+ blocks = out_size / BLOCKSIZE;
+
+ if (out_size) {
+ out_buf = av_malloc(out_size);
+ if (!out_buf)
+ return AVERROR(ENOMEM);
+
+ if (c->pad_len) {
+ memcpy(&c->pad[c->pad_len], buf, BLOCKSIZE - c->pad_len);
+ av_aes_crypt(c->aes_encrypt, out_buf, c->pad, 1, c->encrypt_iv, 0);
+ blocks--;
+ }
+
+ av_aes_crypt(c->aes_encrypt, &out_buf[c->pad_len ? BLOCKSIZE : 0],
+ &buf[c->pad_len ? BLOCKSIZE - c->pad_len: 0],
+ blocks, c->encrypt_iv, 0);
+
+ ret = ffurl_write(c->hd, out_buf, out_size);
+ av_free(out_buf);
+ if (ret < 0)
+ return ret;
+
+ memcpy(c->pad, &buf[size - pad_len], pad_len);
+ } else
+ memcpy(&c->pad[c->pad_len], buf, size);
+
+ c->pad_len = pad_len;
+
+ return size;
+}
+