src/C/FreeBSD/FreeBSD-10.0/opencrypto/cryptosoft.c
/*
 * Apply a compression/decompression algorithm
 */
Apply a compression/decompression algorithm
Apply a compression / decompression algorithm
static int
swcr_compdec(struct cryptodesc *crd, struct swcr_data *sw,
    caddr_t buf, int flags)
{
	u_int8_t *data, *out;
	struct comp_algo *cxf;
	int adj;
	u_int32_t result;
	cxf = sw->sw_cxf;
	data = malloc(crd->crd_len, M_CRYPTO_DATA,  M_NOWAIT);
	if (data == NULL)
		return (EINVAL);
	crypto_copydata(flags, buf, crd->crd_skip, crd->crd_len, data);
	if (crd->crd_flags & CRD_F_COMP)
		result = cxf->compress(data, crd->crd_len, &out);
	else
		result = cxf->decompress(data, crd->crd_len, &out);
	free(data, M_CRYPTO_DATA);
	if (result == 0)
		return EINVAL;
	sw->sw_size = result;
	if (crd->crd_flags & CRD_F_COMP) {
		if (result >= crd->crd_len) {
			free(out, M_CRYPTO_DATA);
			return 0;
		}
	}
	crypto_copyback(flags, buf, crd->crd_skip, result, out);
	if (result < crd->crd_len) {
		adj = result - crd->crd_len;
		if (flags & CRYPTO_F_IMBUF) {
			adj = result - crd->crd_len;
			m_adj((struct mbuf *)buf, adj);
		} else if (flags & CRYPTO_F_IOV) {
			struct uio *uio = (struct uio *)buf;
			int ind;
			adj = crd->crd_len - result;
			ind = uio->uio_iovcnt - 1;
			while (adj > 0 && ind >= 0) {
				if (adj < uio->uio_iov[ind].iov_len) {
					uio->uio_iov[ind].iov_len -= adj;
					break;
				}
				adj -= uio->uio_iov[ind].iov_len;
				uio->uio_iov[ind].iov_len = 0;
				ind--;
				uio->uio_iovcnt--;
			}
		}
	}
	free(out, M_CRYPTO_DATA);
	return 0;
}
static int swcr_compdec ( struct cryptodesc * crd , struct swcr_data * sw , caddr_t buf , int flags ) { u_int8_t * data , * out ; struct comp_algo * cxf ; int adj ; u_int32_t result ; cxf = sw -> sw_cxf ; data = malloc ( crd -> crd_len , M_CRYPTO_DATA , M_NOWAIT ) ; if ( data == NULL ) return ( EINVAL ) ; crypto_copydata ( flags , buf , crd -> crd_skip , crd -> crd_len , data ) ; if ( crd -> crd_flags & CRD_F_COMP ) result = cxf -> compress ( data , crd -> crd_len , & out ) ; else result = cxf -> decompress ( data , crd -> crd_len , & out ) ; free ( data , M_CRYPTO_DATA ) ; if ( result == 0 ) return EINVAL ; sw -> sw_size = result ; if ( crd -> crd_flags & CRD_F_COMP ) { if ( result >= crd -> crd_len ) { free ( out , M_CRYPTO_DATA ) ; return 0 ; } } crypto_copyback ( flags , buf , crd -> crd_skip , result , out ) ; if ( result < crd -> crd_len ) { adj = result - crd -> crd_len ; if ( flags & CRYPTO_F_IMBUF ) { adj = result - crd -> crd_len ; m_adj ( ( struct mbuf * ) buf , adj ) ; } else if ( flags & CRYPTO_F_IOV ) { struct uio * uio = ( struct uio * ) buf ; int ind ; adj = crd -> crd_len - result ; ind = uio -> uio_iovcnt - 1 ; while ( adj > 0 && ind >= 0 ) { if ( adj < uio -> uio_iov [ ind ] . iov_len ) { uio -> uio_iov [ ind ] . iov_len -= adj ; break ; } adj -= uio -> uio_iov [ ind ] . iov_len ; uio -> uio_iov [ ind ] . iov_len = 0 ; ind -- ; uio -> uio_iovcnt -- ; } } } free ( out , M_CRYPTO_DATA ) ; return 0 ; }
Apply a compression/decompression algorithm
Apply a compression / decompression algorithm
/* We must handle the whole buffer of data in one time
	 * then if there is not all the data in the mbuf, we must
	 * copy in a buffer.
	 */ /* Copy back the (de)compressed data. m_copyback is
	 * extending the mbuf as necessary.
	 */ /* Check the compressed size when doing compression */ /* Compression was useless, we lost time */