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