---
title: "compression_encode_buffer(_:_:_:_:_:_:)"
framework: compression
role: symbol
role_heading: Function
path: "compression/compression_encode_buffer(_:_:_:_:_:_:)"
---

# compression_encode_buffer(_:_:_:_:_:_:)

Compresses the contents of a source buffer into a destination buffer.

## Declaration

```swift
func compression_encode_buffer(_ dst_buffer: UnsafeMutablePointer<UInt8>, _ dst_size: Int, _ src_buffer: UnsafePointer<UInt8>, _ src_size: Int, _ scratch_buffer: UnsafeMutableRawPointer?, _ algorithm: compression_algorithm) -> Int
```

## Parameters

- `dst_buffer`: Pointer to the buffer that receives the compressed data.
- `dst_size`: Size of the destination buffer in bytes.
- `src_buffer`: Pointer to a buffer containing all of the source data.
- `src_size`: Size of the data in the source buffer in bytes.
- `scratch_buffer`: If scratch_buffer is not nil, this parameter is a pointer to a buffer that the function uses for scratch purposes. The size of this buffer must be at least the size returned by a previous call to doc://com.apple.compression/documentation/Compression/compression_encode_scratch_buffer_size(_:). If scratch_buffer is nil, the function creates and manages its own scratch space, but with a possible performance hit.
- `algorithm`: A constant of type doc://com.apple.compression/documentation/Compression/compression_algorithm that specifies the compression algorithm.

## Return Value

Return Value The number of bytes written to the destination buffer after compressing the input. If the funtion can’t compress the entire input to fit into the provided destination buffer, or an error occurs, 0 is returned.

## Discussion

Discussion If the input is successfully compressed, the function writes the compressed data to dst_buffer.

## See Also

### Single-step compression

- [Compressing and decompressing data with buffer compression](accelerate/compressing-and-decompressing-data-with-buffer-compression.md)
- [compression_encode_scratch_buffer_size(_:)](compression/compression_encode_scratch_buffer_size(_:).md)
- [compression_decode_scratch_buffer_size(_:)](compression/compression_decode_scratch_buffer_size(_:).md)
- [compression_decode_buffer(_:_:_:_:_:_:)](compression/compression_decode_buffer(_:_:_:_:_:_:).md)
- [compression_algorithm](compression/compression_algorithm.md)
