Cyan4973 / FiniteStateEntropy

New generation entropy codecs : Finite State Entropy and Huff0

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FSE_isError and FSE_getErrorName

darrenrhea opened this issue · comments

I feel like I might be running into an issue with the error codes.

When FiniteStateEntropy finds that it is simply one symbol is repeated many times, it should send back a code suggesting Run Length Encoding.

Also when it does not manage to make it smaller, it should send back another code.

But FSE_isError and FSE_getErrorName don't do what I would expect here:

What is intended?

#include "fse.h"
#include <stdio.h>
#include <stdlib.h>

int
main()
{
  char* out_buffer = (char*)malloc(1000);
  if (out_buffer == NULL)
    exit(1);
  size_t maxlength = 1000;

  // gives an error code of 1?
  char* input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  size_t in_length = 30;

  // gives an error code of 0?
  // char* input = "ab";
  // size_t in_length = 2;

  // works without error
  // char* input = "abaaaaaaaaaaaaaaaaaabaaaaaaaaa";
  // size_t in_length = 30;

  size_t size = FSE_compress(out_buffer, maxlength, input, in_length);

  printf("size = %d\n", (int)size);

  if (size <= 1) { // sometimes the size is an error code

    printf("\n[%s]\n\n", FSE_getErrorName(size));

    printf("We infer there was probably some kind of error since size = %zu\n",
           size);
    if (size == 0) {
      printf("\n\n*********************************\nFailed to get "
             "smaller?\n+++++++++++++++++++++++++++++++++\n\n");
    }
    if (size == 1) {
      printf("\n\n*********************************\nsource is a single symbol "
             "a bunch of times?\n+++++++++++++++++++++++++++++++++\n\n");
    }
  }
  if (FSE_isError(size)) { // should detect when size is an error code
    printf("Considered to be an error\n");
  } else {
    printf("Not considered to be an error\n");
  }

  return 0;
}

As described in fse.h :

@return : size of compressed data (<= dstCapacity).
Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!
                 if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead.

Special values are not considered as "errors".
As a consequence, using FSE_isError() on a special value will return 0 (false).

Another potential outcome is to get error code FSE_error_dstSize_tooSmall, when, as the name implies, dstSize is too small to contain the compressed input. This may happen whenever provided dstSize < FSE_compressBound(srcSize), and input is not compressible enough to fit into intended size.