| OLD | NEW |
| 1 /////////////////////////////////////////////////////////////////////////////// | 1 /////////////////////////////////////////////////////////////////////////////// |
| 2 // | 2 // |
| 3 /// \file block_buffer_encoder.c | 3 /// \file block_buffer_encoder.c |
| 4 /// \brief Single-call .xz Block encoder | 4 /// \brief Single-call .xz Block encoder |
| 5 // | 5 // |
| 6 // Author: Lasse Collin | 6 // Author: Lasse Collin |
| 7 // | 7 // |
| 8 // This file has been put into the public domain. | 8 // This file has been put into the public domain. |
| 9 // You can do whatever you want with this file. | 9 // You can do whatever you want with this file. |
| 10 // | 10 // |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 | 219 |
| 220 return ret; | 220 return ret; |
| 221 } | 221 } |
| 222 | 222 |
| 223 | 223 |
| 224 extern LZMA_API(lzma_ret) | 224 extern LZMA_API(lzma_ret) |
| 225 lzma_block_buffer_encode(lzma_block *block, lzma_allocator *allocator, | 225 lzma_block_buffer_encode(lzma_block *block, lzma_allocator *allocator, |
| 226 const uint8_t *in, size_t in_size, | 226 const uint8_t *in, size_t in_size, |
| 227 uint8_t *out, size_t *out_pos, size_t out_size) | 227 uint8_t *out, size_t *out_pos, size_t out_size) |
| 228 { | 228 { |
| 229 » // Sanity checks | 229 » // Validate the arguments. |
| 230 » if (block == NULL || block->filters == NULL | 230 » if (block == NULL || (in == NULL && in_size != 0) || out == NULL |
| 231 » » » || (in == NULL && in_size != 0) || out == NULL | |
| 232 || out_pos == NULL || *out_pos > out_size) | 231 || out_pos == NULL || *out_pos > out_size) |
| 233 return LZMA_PROG_ERROR; | 232 return LZMA_PROG_ERROR; |
| 234 | 233 |
| 235 » // Check the version field. | 234 » // The contents of the structure may depend on the version so |
| 235 » // check the version before validating the contents of *block. |
| 236 if (block->version != 0) | 236 if (block->version != 0) |
| 237 return LZMA_OPTIONS_ERROR; | 237 return LZMA_OPTIONS_ERROR; |
| 238 | 238 |
| 239 if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX |
| 240 || block->filters == NULL) |
| 241 return LZMA_PROG_ERROR; |
| 242 |
| 243 if (!lzma_check_is_supported(block->check)) |
| 244 return LZMA_UNSUPPORTED_CHECK; |
| 245 |
| 239 // Size of a Block has to be a multiple of four, so limit the size | 246 // Size of a Block has to be a multiple of four, so limit the size |
| 240 // here already. This way we don't need to check it again when adding | 247 // here already. This way we don't need to check it again when adding |
| 241 // Block Padding. | 248 // Block Padding. |
| 242 out_size -= (out_size - *out_pos) & 3; | 249 out_size -= (out_size - *out_pos) & 3; |
| 243 | 250 |
| 244 // Get the size of the Check field. | 251 // Get the size of the Check field. |
| 245 const size_t check_size = lzma_check_size(block->check); | 252 const size_t check_size = lzma_check_size(block->check); |
| 246 » if (check_size == UINT32_MAX) | 253 » assert(check_size != UINT32_MAX); |
| 247 » » return LZMA_PROG_ERROR; | |
| 248 | 254 |
| 249 // Reserve space for the Check field. | 255 // Reserve space for the Check field. |
| 250 if (out_size - *out_pos <= check_size) | 256 if (out_size - *out_pos <= check_size) |
| 251 return LZMA_BUF_ERROR; | 257 return LZMA_BUF_ERROR; |
| 252 | 258 |
| 253 out_size -= check_size; | 259 out_size -= check_size; |
| 254 | 260 |
| 255 // Do the actual compression. | 261 // Do the actual compression. |
| 256 const lzma_ret ret = block_encode_normal(block, allocator, | 262 const lzma_ret ret = block_encode_normal(block, allocator, |
| 257 in, in_size, out, out_pos, out_size); | 263 in, in_size, out, out_pos, out_size); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 lzma_check_update(&check, block->check, in, in_size); | 296 lzma_check_update(&check, block->check, in, in_size); |
| 291 lzma_check_finish(&check, block->check); | 297 lzma_check_finish(&check, block->check); |
| 292 | 298 |
| 293 memcpy(block->raw_check, check.buffer.u8, check_size); | 299 memcpy(block->raw_check, check.buffer.u8, check_size); |
| 294 memcpy(out + *out_pos, check.buffer.u8, check_size); | 300 memcpy(out + *out_pos, check.buffer.u8, check_size); |
| 295 *out_pos += check_size; | 301 *out_pos += check_size; |
| 296 } | 302 } |
| 297 | 303 |
| 298 return LZMA_OK; | 304 return LZMA_OK; |
| 299 } | 305 } |
| OLD | NEW |