title: Programmatically LZMA decompressing a static LZMA-compressed file
body: <p>I'm LZMA-decompressing a resource file which I earlier compressed using <code>lzma e &lt;infile&gt; &lt;outfile&gt; -lc0 -lp2</code> from the terminal and imported to my project. However when applied to this file <code>LzmaDec_DecodeToBuf</code> returns <code>1</code> in the first iteration, i.e. LZMA data error. (Also by which time <code>inLen</code> is always <code>5</code>, <code>outLen</code> is <code>0</code>.)</p>
  
  <p>Why is that?</p>
  
  <p>My code reads:</p>
  
  <pre><code>SRes static decompress(FILE *inFile, FILE *outFile)
  {
    // Position the inFile pointer at the start.
    fseek(inFile, 0, SEEK_SET);
  
    // Read in LZMA properties (5 bytes) and uncompressed size (8 bytes, little-endian) to header.
    char unsigned header[LZMA_PROPS_SIZE+8];
    fgets(header, sizeof(header), inFile);
    CLzmaDec state;
    LzmaDec_Construct(&amp;state);
    SRes res = LzmaDec_Allocate(&amp;state, header, LZMA_PROPS_SIZE, &amp;SzAllocForLzma);
  
    if (res != SZ_OK) {
      // Free all allocated structures.
      LzmaDec_Free(&amp;state, &amp;SzAllocForLzma);
      return res;
    }
  
    char unsigned inBuf[IN_BUF_SIZE];
    char unsigned outBuf[OUT_BUF_SIZE];
    LzmaDec_Init(&amp;state);
  
    ELzmaStatus status;
    long unsigned outLen = sizeof(outBuf);
    long unsigned inLen = sizeof(inBuf);
    long unsigned inPos = ftell(inFile);
  
    while (fgets(inBuf, sizeof(inBuf), inFile) != NULL) {
      inLen = MIN(sizeof(inBuf), MAX(ftell(inFile)-inPos, 0));
      outLen = sizeof(outBuf);
  
      SRes res = LzmaDec_DecodeToBuf(&amp;state,
                                     outBuf,
                                     &amp;outLen,
                                     inBuf,
                                     &amp;inLen,
                                     LZMA_FINISH_ANY,
                                     &amp;status);
  // continues...
  </code></pre>