(root)/
xz-5.4.5/
debug/
full_flush.c
       1  ///////////////////////////////////////////////////////////////////////////////
       2  //
       3  /// \file       full_flush.c
       4  /// \brief      Encode files using LZMA_FULL_FLUSH
       5  //
       6  //  Author:     Lasse Collin
       7  //
       8  //  This file has been put into the public domain.
       9  //  You can do whatever you want with this file.
      10  //
      11  ///////////////////////////////////////////////////////////////////////////////
      12  
      13  #include "sysdefs.h"
      14  #include "lzma.h"
      15  #include <stdio.h>
      16  
      17  #define CHUNK 64
      18  
      19  
      20  static lzma_stream strm = LZMA_STREAM_INIT;
      21  static FILE *file_in;
      22  
      23  
      24  static void
      25  encode(size_t size, lzma_action action)
      26  {
      27  	uint8_t in[CHUNK];
      28  	uint8_t out[CHUNK];
      29  	lzma_ret ret;
      30  
      31  	do {
      32  		if (strm.avail_in == 0 && size > 0) {
      33  			const size_t amount = my_min(size, CHUNK);
      34  			strm.avail_in = fread(in, 1, amount, file_in);
      35  			strm.next_in = in;
      36  			size -= amount; // Intentionally not using avail_in.
      37  		}
      38  
      39  		strm.next_out = out;
      40  		strm.avail_out = CHUNK;
      41  
      42  		ret = lzma_code(&strm, size == 0 ? action : LZMA_RUN);
      43  
      44  		if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
      45  			fprintf(stderr, "%s:%u: %s: ret == %d\n",
      46  					__FILE__, __LINE__, __func__, ret);
      47  			exit(1);
      48  		}
      49  
      50  		fwrite(out, 1, CHUNK - strm.avail_out, stdout);
      51  
      52  	} while (size > 0 || strm.avail_out == 0);
      53  
      54  	if ((action == LZMA_RUN && ret != LZMA_OK)
      55  			|| (action != LZMA_RUN && ret != LZMA_STREAM_END)) {
      56  		fprintf(stderr, "%s:%u: %s: ret == %d\n",
      57  				__FILE__, __LINE__, __func__, ret);
      58  		exit(1);
      59  	}
      60  }
      61  
      62  
      63  int
      64  main(int argc, char **argv)
      65  {
      66  	file_in = argc > 1 ? fopen(argv[1], "rb") : stdin;
      67  
      68  
      69  	// Config
      70  	lzma_options_lzma opt_lzma;
      71  	if (lzma_lzma_preset(&opt_lzma, 1)) {
      72  		fprintf(stderr, "preset failed\n");
      73  		exit(1);
      74  	}
      75  	lzma_filter filters[LZMA_FILTERS_MAX + 1];
      76  	filters[0].id = LZMA_FILTER_LZMA2;
      77  	filters[0].options = &opt_lzma;
      78  	filters[1].id = LZMA_VLI_UNKNOWN;
      79  
      80  	// Init
      81  	if (lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC32) != LZMA_OK) {
      82  		fprintf(stderr, "init failed\n");
      83  		exit(1);
      84  	}
      85  
      86  // 	if (lzma_easy_encoder(&strm, 1)) {
      87  // 		fprintf(stderr, "init failed\n");
      88  // 		exit(1);
      89  // 	}
      90  
      91  	// Encoding
      92  	encode(0, LZMA_FULL_FLUSH);
      93  	encode(6, LZMA_FULL_FLUSH);
      94  	encode(0, LZMA_FULL_FLUSH);
      95  	encode(7, LZMA_FULL_FLUSH);
      96  	encode(0, LZMA_FULL_FLUSH);
      97  	encode(0, LZMA_FINISH);
      98  
      99  	// Clean up
     100  	lzma_end(&strm);
     101  
     102  	return 0;
     103  }