| 1 | // SPDX-License-Identifier: BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (c) 2015-2016, Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 8 | * |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer |
| 10 | * in the documentation and/or other materials provided with the distribution. |
| 11 | * |
| 12 | * 3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived |
| 13 | * from this software without specific prior written permission. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 17 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 19 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 20 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 21 | */ |
| 22 | |
| 23 | /* LZFSE decode API */ |
| 24 | |
| 25 | #include <linux/slab.h> |
| 26 | #include "lzfse.h" |
| 27 | #include "lzfse_internal.h" |
| 28 | |
| 29 | size_t lzfse_decode_scratch_size(void) |
| 30 | { |
| 31 | return sizeof(lzfse_decoder_state); |
| 32 | } |
| 33 | |
| 34 | static size_t lzfse_decode_buffer_with_scratch(uint8_t *__restrict dst_buffer, size_t dst_size, |
| 35 | const uint8_t *__restrict src_buffer, |
| 36 | size_t src_size, void *__restrict scratch_buffer) |
| 37 | { |
| 38 | int status; |
| 39 | lzfse_decoder_state *s = (lzfse_decoder_state *)scratch_buffer; |
| 40 | |
| 41 | memset(s, 0x00, sizeof(*s)); |
| 42 | |
| 43 | /* Initialize state */ |
| 44 | s->src = src_buffer; |
| 45 | s->src_begin = src_buffer; |
| 46 | s->src_end = s->src + src_size; |
| 47 | s->dst = dst_buffer; |
| 48 | s->dst_begin = dst_buffer; |
| 49 | s->dst_end = dst_buffer + dst_size; |
| 50 | |
| 51 | /* Decode */ |
| 52 | status = lzfse_decode(s); |
| 53 | if (status == LZFSE_STATUS_DST_FULL) |
| 54 | return dst_size; |
| 55 | if (status != LZFSE_STATUS_OK) |
| 56 | return 0; /* failed */ |
| 57 | return (size_t)(s->dst - dst_buffer); /* bytes written */ |
| 58 | } |
| 59 | |
| 60 | size_t lzfse_decode_buffer(uint8_t *__restrict dst_buffer, size_t dst_size, |
| 61 | const uint8_t *__restrict src_buffer, size_t src_size, |
| 62 | void *__restrict scratch_buffer) |
| 63 | { |
| 64 | int has_malloc = 0; |
| 65 | size_t ret = 0; |
| 66 | |
| 67 | /* Deal with the possible NULL pointer */ |
| 68 | if (scratch_buffer == NULL) { |
| 69 | /* +1 in case scratch size could be zero */ |
| 70 | scratch_buffer = kmalloc(lzfse_decode_scratch_size() + 1, GFP_KERNEL); |
| 71 | has_malloc = 1; |
| 72 | } |
| 73 | if (scratch_buffer == NULL) |
| 74 | return 0; |
| 75 | ret = lzfse_decode_buffer_with_scratch(dst_buffer, dst_size, src_buffer, src_size, |
| 76 | scratch_buffer); |
| 77 | if (has_malloc) |
| 78 | kfree(scratch_buffer); |
| 79 | return ret; |
| 80 | } |