<commit_before>#include <libgnome/libgnome.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

typedef enum {
  T_END /* end of array */, T_BYTE, T_SHORT, T_LONG, T_STR, T_DATE, 
  T_BESHORT, T_BELONG, T_BEDATE,
  T_LESHORT, T_LELONG, T_LEDATE
} GnomeMagicType;

typedef struct _GnomeMagicEntry {
  guint32 mask;
  GnomeMagicType type;
  guint16 offset, level;

  char test[48];
  guchar test_len;
  enum { CHECK_EQUAL, CHECK_LT, CHECK_GT, CHECK_AND, CHECK_XOR,
	 CHECK_ANY } comptype;
  guint32 compval;

  char mimetype[48];
} GnomeMagicEntry;

#ifndef GEN_MIMEDB
/****** misc lame parsing routines *******/
static guchar
read_octal_str(char **pos)
{
  guchar retval = 0;

  if(**pos >= '0' && **pos <= '7') {
    retval += **pos - '0';
  } else
    return retval;

  retval *= 8;
  (*pos)++;

  if(**pos >= '0' && **pos <= '7') {
    retval += **pos - '0';
  } else
    return retval/8;

  (*pos)++;

  retval *= 8;

  if(**pos >= '0' && **pos <= '7') {
    retval += **pos - '0';
  } else
    return retval/8;

  (*pos)++;

  return retval;
}

static char
read_hex_str(char **pos)
{
  char retval = 0;

  if(**pos >= '0' && **pos <= '9') {
    retval = **pos - '0';
  } else if(**pos >= 'a' && **pos <= 'f') {
    retval = **pos - 'a' + 10;
  } else if(**pos >= 'A' && **pos <= 'A') {
    retval = **pos - 'A' + 10;
  } else
    g_error("bad hex digit %c", **pos);

  (*pos)++;
  retval *= 16;

  if(**pos >= '0' && **pos <= '9') {
    retval += **pos - '0';
  } else if(**pos >= 'a' && **pos <= 'f') {
    retval += **pos - 'a' + 10;
  } else if(**pos >= 'A' && **pos <= 'A') {
    retval += **pos - 'A' + 10;
  } else
    g_error("bad hex digit %c", **pos);

  (*pos)++;

  return retval;
}

static char *
read_string_val(char *curpos, char *intobuf, guchar *into_len)
{
  *into_len = 0;

  while(1) {
    if(!*curpos || isspace(*curpos)) {
      *intobuf = '\0';
      return curpos;
    }

    switch(*curpos) {
    case '\\':
      curpos++;
      switch(*curpos) {
      case 'x': /* read hex value */
	curpos++;
	*(intobuf++) = read_hex_str(&curpos);
	break;
      case '0': case '1':
	/* read octal value */
	*(intobuf++) = read_octal_str(&curpos);
	break;
      case 'n': *(intobuf++) = '\n'; curpos++; break;
      case ' ': *(intobuf++) = ' '; curpos++; break;
      case '\\': *(intobuf++) = '\\'; curpos++; break;
      }
      break;
    default:
      *(intobuf++) = *curpos;
      curpos++;
    }
    (*into_len)++;
  }
}

static char *read_num_val(char *curpos, int bsize, char *intobuf)
{
  char fmttype, fmtstr[4];
  short itmp;

  if(*curpos == '0') {
    if(tolower(*(curpos+1)) == 'x') fmttype = 'x';
    else fmttype = 'o';
  } else
    fmttype = 'u';

  switch(bsize) {
  case 1:<commit_msg>Changed temporary variable to an int from a short, otherwise we seem to overwrite the stack if the fmtstr is a "%o" (octal) on my box. Thanks to Owen for seeing this bug.<commit_after>#include <libgnome/libgnome.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

typedef enum {
  T_END /* end of array */, T_BYTE, T_SHORT, T_LONG, T_STR, T_DATE, 
  T_BESHORT, T_BELONG, T_BEDATE,
  T_LESHORT, T_LELONG, T_LEDATE
} GnomeMagicType;

typedef struct _GnomeMagicEntry {
  guint32 mask;
  GnomeMagicType type;
  guint16 offset, level;

  char test[48];
  guchar test_len;
  enum { CHECK_EQUAL, CHECK_LT, CHECK_GT, CHECK_AND, CHECK_XOR,
	 CHECK_ANY } comptype;
  guint32 compval;

  char mimetype[48];
} GnomeMagicEntry;

#ifndef GEN_MIMEDB
/****** misc lame parsing routines *******/
static guchar
read_octal_str(char **pos)
{
  guchar retval = 0;

  if(**pos >= '0' && **pos <= '7') {
    retval += **pos - '0';
  } else
    return retval;

  retval *= 8;
  (*pos)++;

  if(**pos >= '0' && **pos <= '7') {
    retval += **pos - '0';
  } else
    return retval/8;

  (*pos)++;

  retval *= 8;

  if(**pos >= '0' && **pos <= '7') {
    retval += **pos - '0';
  } else
    return retval/8;

  (*pos)++;

  return retval;
}

static char
read_hex_str(char **pos)
{
  char retval = 0;

  if(**pos >= '0' && **pos <= '9') {
    retval = **pos - '0';
  } else if(**pos >= 'a' && **pos <= 'f') {
    retval = **pos - 'a' + 10;
  } else if(**pos >= 'A' && **pos <= 'A') {
    retval = **pos - 'A' + 10;
  } else
    g_error("bad hex digit %c", **pos);

  (*pos)++;
  retval *= 16;

  if(**pos >= '0' && **pos <= '9') {
    retval += **pos - '0';
  } else if(**pos >= 'a' && **pos <= 'f') {
    retval += **pos - 'a' + 10;
  } else if(**pos >= 'A' && **pos <= 'A') {
    retval += **pos - 'A' + 10;
  } else
    g_error("bad hex digit %c", **pos);

  (*pos)++;

  return retval;
}

static char *
read_string_val(char *curpos, char *intobuf, guchar *into_len)
{
  *into_len = 0;

  while(1) {
    if(!*curpos || isspace(*curpos)) {
      *intobuf = '\0';
      return curpos;
    }

    switch(*curpos) {
    case '\\':
      curpos++;
      switch(*curpos) {
      case 'x': /* read hex value */
	curpos++;
	*(intobuf++) = read_hex_str(&curpos);
	break;
      case '0': case '1':
	/* read octal value */
	*(intobuf++) = read_octal_str(&curpos);
	break;
      case 'n': *(intobuf++) = '\n'; curpos++; break;
      case ' ': *(intobuf++) = ' '; curpos++; break;
      case '\\': *(intobuf++) = '\\'; curpos++; break;
      }
      break;
    default:
      *(intobuf++) = *curpos;
      curpos++;
    }
    (*into_len)++;
  }
}

static char *read_num_val(char *curpos, int bsize, char *intobuf)
{
  char fmttype, fmtstr[4];
  int itmp;

  if(*curpos == '0') {
    if(tolower(*(curpos+1)) == 'x') fmttype = 'x';
    else fmttype = 'o';
  } else
    fmttype = 'u';

  switch(bsize) {
  case 1:
{'fraction_non_alphanumeric': 0.21836909871244636, 'fraction_numerical': 0.017167381974248927, 'mean_word_length': 2.8303747534516766, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7449680', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2330, 'n_words': 587}