| OLD | NEW |
| 1 /////////////////////////////////////////////////////////////////////////////// | 1 /////////////////////////////////////////////////////////////////////////////// |
| 2 // | 2 // |
| 3 /// \file stream_buffer_encoder.c | 3 /// \file stream_buffer_encoder.c |
| 4 /// \brief Single-call .xz Stream encoder | 4 /// \brief Single-call .xz Stream 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 lzma_stream_buffer_encode(lzma_filter *filters, lzma_check check, | 44 lzma_stream_buffer_encode(lzma_filter *filters, lzma_check check, |
| 45 lzma_allocator *allocator, const uint8_t *in, size_t in_size, | 45 lzma_allocator *allocator, const uint8_t *in, size_t in_size, |
| 46 uint8_t *out, size_t *out_pos_ptr, size_t out_size) | 46 uint8_t *out, size_t *out_pos_ptr, size_t out_size) |
| 47 { | 47 { |
| 48 // Sanity checks | 48 // Sanity checks |
| 49 if (filters == NULL || (unsigned int)(check) > LZMA_CHECK_ID_MAX | 49 if (filters == NULL || (unsigned int)(check) > LZMA_CHECK_ID_MAX |
| 50 || (in == NULL && in_size != 0) || out == NULL | 50 || (in == NULL && in_size != 0) || out == NULL |
| 51 || out_pos_ptr == NULL || *out_pos_ptr > out_size) | 51 || out_pos_ptr == NULL || *out_pos_ptr > out_size) |
| 52 return LZMA_PROG_ERROR; | 52 return LZMA_PROG_ERROR; |
| 53 | 53 |
| 54 if (!lzma_check_is_supported(check)) |
| 55 return LZMA_UNSUPPORTED_CHECK; |
| 56 |
| 54 // Note for the paranoids: Index encoder prevents the Stream from | 57 // Note for the paranoids: Index encoder prevents the Stream from |
| 55 // getting too big and still being accepted with LZMA_OK, and Block | 58 // getting too big and still being accepted with LZMA_OK, and Block |
| 56 // encoder catches if the input is too big. So we don't need to | 59 // encoder catches if the input is too big. So we don't need to |
| 57 // separately check if the buffers are too big. | 60 // separately check if the buffers are too big. |
| 58 | 61 |
| 59 // Use a local copy. We update *out_pos_ptr only if everything | 62 // Use a local copy. We update *out_pos_ptr only if everything |
| 60 // succeeds. | 63 // succeeds. |
| 61 size_t out_pos = *out_pos_ptr; | 64 size_t out_pos = *out_pos_ptr; |
| 62 | 65 |
| 63 // Check that there's enough space for both Stream Header and | 66 // Check that there's enough space for both Stream Header and |
| (...skipping 10 matching lines...) Expand all Loading... |
| 74 .version = 0, | 77 .version = 0, |
| 75 .check = check, | 78 .check = check, |
| 76 }; | 79 }; |
| 77 | 80 |
| 78 if (lzma_stream_header_encode(&stream_flags, out + out_pos) | 81 if (lzma_stream_header_encode(&stream_flags, out + out_pos) |
| 79 != LZMA_OK) | 82 != LZMA_OK) |
| 80 return LZMA_PROG_ERROR; | 83 return LZMA_PROG_ERROR; |
| 81 | 84 |
| 82 out_pos += LZMA_STREAM_HEADER_SIZE; | 85 out_pos += LZMA_STREAM_HEADER_SIZE; |
| 83 | 86 |
| 84 » // Block | 87 » // Encode a Block but only if there is at least one byte of input. |
| 85 lzma_block block = { | 88 lzma_block block = { |
| 86 .version = 0, | 89 .version = 0, |
| 87 .check = check, | 90 .check = check, |
| 88 .filters = filters, | 91 .filters = filters, |
| 89 }; | 92 }; |
| 90 | 93 |
| 91 » return_if_error(lzma_block_buffer_encode(&block, allocator, | 94 » if (in_size > 0) |
| 92 » » » in, in_size, out, &out_pos, out_size)); | 95 » » return_if_error(lzma_block_buffer_encode(&block, allocator, |
| 96 » » » » in, in_size, out, &out_pos, out_size)); |
| 93 | 97 |
| 94 // Index | 98 // Index |
| 95 { | 99 { |
| 96 » » // Create an Index with one Record. | 100 » » // Create an Index. It will have one Record if there was |
| 101 » » // at least one byte of input to encode. Otherwise the |
| 102 » » // Index will be empty. |
| 97 lzma_index *i = lzma_index_init(allocator); | 103 lzma_index *i = lzma_index_init(allocator); |
| 98 if (i == NULL) | 104 if (i == NULL) |
| 99 return LZMA_MEM_ERROR; | 105 return LZMA_MEM_ERROR; |
| 100 | 106 |
| 101 » » lzma_ret ret = lzma_index_append(i, allocator, | 107 » » lzma_ret ret = LZMA_OK; |
| 102 » » » » lzma_block_unpadded_size(&block), | 108 |
| 103 » » » » block.uncompressed_size); | 109 » » if (in_size > 0) |
| 110 » » » ret = lzma_index_append(i, allocator, |
| 111 » » » » » lzma_block_unpadded_size(&block), |
| 112 » » » » » block.uncompressed_size); |
| 104 | 113 |
| 105 // If adding the Record was successful, encode the Index | 114 // If adding the Record was successful, encode the Index |
| 106 // and get its size which will be stored into Stream Footer. | 115 // and get its size which will be stored into Stream Footer. |
| 107 if (ret == LZMA_OK) { | 116 if (ret == LZMA_OK) { |
| 108 ret = lzma_index_buffer_encode( | 117 ret = lzma_index_buffer_encode( |
| 109 i, out, &out_pos, out_size); | 118 i, out, &out_pos, out_size); |
| 110 | 119 |
| 111 stream_flags.backward_size = lzma_index_size(i); | 120 stream_flags.backward_size = lzma_index_size(i); |
| 112 } | 121 } |
| 113 | 122 |
| 114 lzma_index_end(i, allocator); | 123 lzma_index_end(i, allocator); |
| 115 | 124 |
| 116 if (ret != LZMA_OK) | 125 if (ret != LZMA_OK) |
| 117 return ret; | 126 return ret; |
| 118 } | 127 } |
| 119 | 128 |
| 120 // Stream Footer. We have already reserved space for this. | 129 // Stream Footer. We have already reserved space for this. |
| 121 if (lzma_stream_footer_encode(&stream_flags, out + out_pos) | 130 if (lzma_stream_footer_encode(&stream_flags, out + out_pos) |
| 122 != LZMA_OK) | 131 != LZMA_OK) |
| 123 return LZMA_PROG_ERROR; | 132 return LZMA_PROG_ERROR; |
| 124 | 133 |
| 125 out_pos += LZMA_STREAM_HEADER_SIZE; | 134 out_pos += LZMA_STREAM_HEADER_SIZE; |
| 126 | 135 |
| 127 // Everything went fine, make the new output position available | 136 // Everything went fine, make the new output position available |
| 128 // to the application. | 137 // to the application. |
| 129 *out_pos_ptr = out_pos; | 138 *out_pos_ptr = out_pos; |
| 130 return LZMA_OK; | 139 return LZMA_OK; |
| 131 } | 140 } |
| OLD | NEW |