1  /* PR target/54121 */
       2  /* Reported by Jan Engelhardt <jengelh@inai.de> */
       3  
       4  /* { dg-do compile { target fpic } } */
       5  /* { dg-options "-std=gnu99 -O -fPIC -fprofile-generate" } */
       6  /* { dg-require-profiling "-fprofile-generate" } */
       7  
       8  typedef __SIZE_TYPE__ size_t;
       9  typedef unsigned char uint8_t;
      10  
      11  extern void *memcpy (void *__restrict __dest,
      12         __const void *__restrict __src, size_t __n)
      13       __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
      14  
      15  typedef enum {
      16   LZMA_OK = 0,
      17   LZMA_STREAM_END = 1,
      18   LZMA_NO_CHECK = 2,
      19   LZMA_UNSUPPORTED_CHECK = 3,
      20   LZMA_GET_CHECK = 4,
      21   LZMA_MEM_ERROR = 5,
      22   LZMA_MEMLIMIT_ERROR = 6,
      23   LZMA_FORMAT_ERROR = 7,
      24   LZMA_OPTIONS_ERROR = 8,
      25   LZMA_DATA_ERROR = 9,
      26   LZMA_BUF_ERROR = 10,
      27   LZMA_PROG_ERROR = 11,
      28  } lzma_ret;
      29  
      30  typedef enum {
      31   LZMA_RUN = 0,
      32   LZMA_SYNC_FLUSH = 1,
      33   LZMA_FULL_FLUSH = 2,
      34   LZMA_FINISH = 3
      35  } lzma_action;
      36  
      37  typedef struct {
      38   void *( *alloc)(void *opaque, size_t nmemb, size_t size);
      39   void ( *free)(void *opaque, void *ptr);
      40   void *opaque;
      41  } lzma_allocator;
      42  
      43  typedef struct lzma_coder_s lzma_coder;
      44  
      45  typedef struct lzma_next_coder_s lzma_next_coder;
      46  
      47  typedef struct lzma_filter_info_s lzma_filter_info;
      48  
      49  typedef lzma_ret (*lzma_init_function)(
      50    lzma_next_coder *next, lzma_allocator *allocator,
      51    const lzma_filter_info *filters);
      52  
      53  typedef lzma_ret (*lzma_code_function)(
      54    lzma_coder *coder, lzma_allocator *allocator,
      55    const uint8_t *restrict in, size_t *restrict in_pos,
      56    size_t in_size, uint8_t *restrict out,
      57    size_t *restrict out_pos, size_t out_size,
      58    lzma_action action);
      59  
      60  typedef void (*lzma_end_function)(
      61    lzma_coder *coder, lzma_allocator *allocator);
      62  
      63  typedef struct {
      64   uint8_t *buf;
      65   size_t pos;
      66   size_t size;
      67  } lzma_dict;
      68  
      69  typedef struct {
      70   lzma_coder *coder;
      71   lzma_ret (*code)(lzma_coder *restrict coder,
      72     lzma_dict *restrict dict, const uint8_t *restrict in,
      73     size_t *restrict in_pos, size_t in_size);
      74  } lzma_lz_decoder;
      75  
      76  struct lzma_coder_s {
      77   lzma_dict dict;
      78   lzma_lz_decoder lz;
      79  };
      80  
      81  lzma_ret
      82  decode_buffer(lzma_coder *coder,
      83    const uint8_t *restrict in, size_t *restrict in_pos,
      84    size_t in_size, uint8_t *restrict out, size_t *restrict out_pos)
      85  {
      86   while (1) {
      87    const size_t dict_start = coder->dict.pos;
      88    const lzma_ret ret
      89      = coder->lz.code( coder->lz.coder, &coder->dict, in, in_pos, in_size);
      90    const size_t copy_size = coder->dict.pos - dict_start;
      91    memcpy(out + *out_pos, coder->dict.buf + dict_start, copy_size);
      92    if (ret != LZMA_OK || coder->dict.pos < coder->dict.size)
      93     return ret;
      94   }
      95  }