
int parsebuffer_spel(void *b)
{
  int n;
  int mloc;

#if 0
  printf("Skipping\n");
  mloc = buffer_getloc(b);
  buffer_skip_bytes(b,4);
  n = buffer_read_int(b);
  buffer_setloc(b,mloc);
  buffer_skip_bytes(b,20);
  buffer_skip_bytes(b,n);
#else

  int loc;
  int i,nn;
  char *s,*s2;
  void *btmp;
  unsigned char *buffer;
  unsigned char *obuffer = NULL;
  float f;
  unsigned int flags1,flags2,flags;
  int m,nuncomp,nin,nout,err;
  int ndata;

  mloc = buffer_getloc(b);
  buffer_skip_bytes(b,4);
  ndata = buffer_read_int(b);
  flags1 = buffer_read_uint(b);
  printf("flags 1 = ");
  printflags(flags1);
  m = buffer_read_int(b);
  printf("formid = %i\n",m);
  flags2 = buffer_read_uint(b);
  printf("flags 2 = ");
  printflags(flags2);

  buffer_setloc(b,mloc);
  buffer_skip_bytes(b,20);

  if (flags1 & 0x00040000) {
    printf("Data is compressed\n");
    nuncomp = buffer_read_int(b);
    buffer = buffer_getbuf(b);
    nin = ndata-4;
    nout = 10*nin;
    obuffer = (unsigned char *)malloc(nout*sizeof(unsigned char));
    err = uncompress(obuffer,(uLongf *)(&nout),&(buffer[buffer_getloc(b)]),nin);
    if (err == Z_OK) {
      // printf("err = OK\n");
    } else if (err == Z_MEM_ERROR) {
      printf("err = MEM\n");
    } else if (err == Z_BUF_ERROR) {
      printf("err = BUF\n");
    } else if (err == Z_DATA_ERROR) {
      printf("err = DATA\n");
    } else {
      printf("Unknown error\n");
    }
    if (nout != nuncomp) {
      printf("Uncompressed data is of wrong size\n");
      exit(-1);
    }
    btmp = b;
    b = buffer_new(obuffer,(int)nout);
  } else {
    buffer = buffer_getbuf(b);
    btmp = b;
    b = buffer_new(&(buffer[buffer_getloc(btmp)]),ndata);
  }

  while (buffer_eof(b) != 1) {
    loc = buffer_getloc(b);
    s = buffer_read_string4(b);
    printf("Chunk name: %s\n",s);
    if (strcmp(s,"EDID") == 0) {
      n = buffer_read_short(b);
      printf("SPEL/EDID %i\n",n);
      s2 = buffer_read_nstring(b,n);
      printf("%s\n",s2);
      free(s2);
    } else if (strcmp(s,"FULL") == 0) {
      n = buffer_read_short(b);
      printf("SPEL/FULL %i\n",n);
      s2 = buffer_read_nstring(b,n);
      printf("%s\n",s2);
      free(s2);
    } else if (strcmp(s,"SPIT") == 0) {
      n = buffer_read_short(b);
      printf("SPEL/SPIT %i\n",n);
      n = buffer_read_int(b);
      printf("%i\n",n);
      n = buffer_read_int(b);
      printf("%i\n",n);
      n = buffer_read_int(b);
      printf("%i\n",n);
      flags = buffer_read_uint(b);
      printflags(flags);
    } else if (strcmp(s,"EFID") == 0) {
      n = buffer_read_short(b);
      printf("SPEL/EFID %i\n",n);
      s2 = buffer_read_nstring(b,n);
      printf("%s\n",s2);
      free(s2);
    } else if (strcmp(s,"EFIT") == 0) {
      n = buffer_read_short(b);
      printf("SPEL/EFIT %i\n",n);
      s2 = buffer_read_nstring(b,4);
      printf("%s\n",s2);
      free(s2);
      n = buffer_read_int(b);
      printf("%i\n",n);
      n = buffer_read_int(b);
      printf("%i\n",n);
      n = buffer_read_int(b);
      printf("%i\n",n);
      n = buffer_read_int(b);
      printf("%i\n",n);
      n = buffer_read_int(b);
      printf("%i\n",n);
    } else if (strcmp(s,"SCIT") == 0) {
      n = buffer_read_short(b);
      printf("SPEL/SCIT %i\n",n);
      n = buffer_read_int(b);
      printf("%i\n",n);
      n = buffer_read_int(b);
      printf("%i\n",n);
      n = buffer_read_int(b);
      printf("%i\n",n);
      flags = buffer_read_uint(b);
      printflags(flags);
    } else {
      printf("Unknown chunk name\n");
      exit(-1);
    }
    free(s);
  }

  printf("End of SPEL\n");

  buffer_free(b);
  b = btmp;
  buffer_setloc(b,mloc);
  buffer_skip_bytes(b,20);
  buffer_skip_bytes(b,ndata);
  if (flags1 & 0x00040000) {
    free(obuffer);
  }

#endif

  return -1;
}
