⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 builddec.c

📁 图形加密的算法 用c语言实现 【解压没有密码】
💻 C
📖 第 1 页 / 共 2 页
字号:
short horiz_mode_code_indigestible (void);
short horiz_mode_code_EOL (void);
short horiz_mode_code_white (short);
void write_tables (void);

int main (int arg_count, char **p_args, char **p_env)
{
  printf ("g4build\n");
  /* build the null mode decoding tables */
    build_null_mode_tables ();
    printf ("  %hd null mode prefixes defined\n", null_mode_prefix_count);
  /* build the 1D decoding tables */
    printf ("  building 1D scripts...\n");
    process_horiz_mode_prefixes ();
    printf ("  %hd indigestible prefixes defined\n", horiz_mode_prefix_count);
  /* create tables.h */
    write_tables ();
  exit (0);
}

void build_null_mode_tables ()
{
  short prefix_number;
  /* note: the first eight entries correspond to a null prefix and starting
    bit numbers 0, 1, ... 7 */
  null_mode_prefix_count = 8;
  for (prefix_number = 0; prefix_number < null_mode_prefix_count;
   prefix_number++)
  {
    short byte_value;
    for (byte_value = 0; byte_value < 256; byte_value++)
    {
      short beginning_bit_number, bit_number, mode;
      long working_prefix;
      char found_code = 0;
      if (prefix_number < 8)
      {
        working_prefix = 0L;
        beginning_bit_number = prefix_number;
      }
      else
      {
        working_prefix = null_mode_prefix [prefix_number];
        beginning_bit_number = 0;
      }
      for (bit_number = beginning_bit_number; bit_number < 8 && !found_code;
       bit_number++)
      {
        if (bit_mask [bit_number] & byte_value)
	  working_prefix = append_1 (working_prefix);
	else working_prefix = append_0 (working_prefix);
        mode = null_mode_type (working_prefix);
        switch (mode)
        {
	  case PASS_MODE:
	  case HORIZONTAL_MODE:
	  case VERTICAL_V0_MODE:
	  case VERTICAL_VR1_MODE:
	  case VERTICAL_VR2_MODE:
	  case VERTICAL_VR3_MODE:
	  case VERTICAL_VL1_MODE:
	  case VERTICAL_VL2_MODE:
	  case VERTICAL_VL3_MODE:
	  case EXT_MODE_UNCOMPRESSED:
	  case ERROR_MODE:
	  case ERROR_MODE_1:
	    found_code = 1;
	    null_mode [prefix_number] [byte_value] = (unsigned char) mode;
	    null_mode_next_state [prefix_number] [byte_value]
	      = (unsigned char) ((bit_number + 1) & 0x7);
	      /* note: if the bit number is 8, then
	        the table entry will be zero, which indicates a new byte
	        is to be fetched during the decoding process */
	    break;
	  default:
	    break;
        }
      }
      if (!found_code)
      {
	null_mode_next_state [prefix_number] [byte_value]
	  = (unsigned char) find_null_mode_prefix (working_prefix);
	null_mode [prefix_number] [byte_value] = 0; /* indicating to the
	  decoder that no digestible state was found */
      }
    }
  }
}

short find_null_mode_prefix (long prefix)
{
  short j1;
  if (prefix == 0L) return (0);
  for (j1 = 8; j1 < null_mode_prefix_count; j1++)
    if (prefix == null_mode_prefix [j1]) return (j1);
  if (null_mode_prefix_count == NULL_MODE_PREFIX_LIMIT)
  {
    printf ("ERROR: null mode prefix table overflow\n");
    exit (1);
  }
  null_mode_prefix [null_mode_prefix_count] = prefix;
printf ("adding null mode prefix [%hd] 0x%lx\n", null_mode_prefix_count,
prefix);
  null_mode_prefix_count++;
  return (null_mode_prefix_count - 1);
}

short find_horiz_mode_prefix (long prefix, char color)
{
  short j1;
  for (j1 = 0; j1 < horiz_mode_prefix_count; j1++)
    if (prefix == horiz_mode_prefix [j1] && horiz_mode_color [j1] == color)
      return (j1);
  /* it wasn't found, so add it to the tables */
  /* but first, is there room? */
    if (horiz_mode_prefix_count == HORIZ_MODE_PREFIX_LIMIT) /* we're full */
    {
      printf ("ERROR: 1D prefix table overflow\n");
      exit (1);
    }
  /* OK, there's room... */
  horiz_mode_prefix [horiz_mode_prefix_count] = prefix;
  horiz_mode_color [horiz_mode_prefix_count] = color;
  horiz_mode_prefix_count++;
printf ("\n horiz mode prefix %hd, color %c = 0x%lx ",
(short) (horiz_mode_prefix_count - 1), "WB" [color], prefix);
  return (horiz_mode_prefix_count - 1);
}

short null_mode_type (long prefix)
{
  if (prefix == 0x18000L) return (VERTICAL_V0_MODE);      /* 1 */
  if (prefix == 0x36000L) return (VERTICAL_VR1_MODE);     /* 011 */
  if (prefix == 0x34000L) return (VERTICAL_VL1_MODE);     /* 010 */
  if (prefix == 0x32000L) return (HORIZONTAL_MODE);       /* 001 */
  if (prefix == 0x41000L) return (PASS_MODE);             /* 0001 */
  if (prefix == 0x60C00L) return (VERTICAL_VR2_MODE);     /* 0000 11 */
  if (prefix == 0x60800L) return (VERTICAL_VL2_MODE);     /* 0000 10 */
  if (prefix == 0x70600L) return (VERTICAL_VR3_MODE);     /* 0000 011 */
  if (prefix == 0x70400L) return (VERTICAL_VL3_MODE);     /* 0000 010 */
  if (prefix == 0x80200L) return (ERROR_MODE);            /* 0000 0010 */
  if (prefix == 0x90300L) return (ERROR_MODE);            /* 0000 0011 0 */
  if (prefix == 0xA0380L) return (ERROR_MODE);            /* 0000 0011 10 */
  if (prefix == 0xA03C0L) return (EXT_MODE_UNCOMPRESSED); /* 0000 0011 11 */
  if (prefix == 0x70000L) return (ERROR_MODE_1);          /* 0000 000 */
    /* under the assumption that there are no errors in the file, then this
       bit string can only be the beginning of an EOFB (end-of-facsimile-block)
       code */
  return (-1);
}

void process_horiz_mode_prefixes ()
{
  unsigned short code_byte_value;
  short prefix_number;
  horiz_mode_prefix_count = 16; /* the first 8 are for white, the second 8 are
    for black, beginning with bits 0, 1, ... 7 */
  for (prefix_number = 0; prefix_number < horiz_mode_prefix_count;
    prefix_number++)
    for (code_byte_value = 0; code_byte_value < 256; code_byte_value++)
    {
      short bits_digested = 0;
      short bit_number, beginning_bit_number;
      char working_color;
      long working_prefix;
      if (prefix_number < 8)
      {
	working_color = WHITE;
	working_prefix = 0L;
	beginning_bit_number = prefix_number;
      }
      else if (prefix_number < 16)
      {
	working_color = BLACK;
	working_prefix = 0L;
	beginning_bit_number = prefix_number - 8;
      }
      else
      {
        working_color = horiz_mode_color [prefix_number];
        working_prefix = horiz_mode_prefix [prefix_number];
	beginning_bit_number = 0;
      }
      for (bit_number = beginning_bit_number; bit_number < 8 && !bits_digested;
	bit_number++)
      {
	if (bit_mask [bit_number] & code_byte_value)
	  working_prefix = append_1 (working_prefix);
	else working_prefix = append_0 (working_prefix);
	if (working_prefix == 0xC0000L) working_prefix = 0xB0000L;
	  /* This conversion allows for arbitrary strings of zeroes to precede
	     the end-of-line code 0000 0000 0001.  It assumes no errors in the
	     data, and is based on the assumption that the code replaced (12
	     consecutive zeroes) can only be "legally" encountered before the
	     end-of-line code.  This assumption is valid only for a Group 3
	     image; the combination will never occur in horizontal mode in a
	     proper Group 4 image. */
	if (working_color == WHITE)
	{
	  short runlength;
	  runlength = white_run_length (working_prefix);
	  if (runlength == INVALID_CODE)
	  {
	    horiz_mode [prefix_number] [code_byte_value]
	      = (unsigned char) horiz_mode_code_invalid ();
	    horiz_mode_next_state [prefix_number] [code_byte_value]
	      = (unsigned char) bit_number;
	    bits_digested = bit_number + 1;
	  }
	  else if (runlength == EOL_CODE) /* Group 3 only */
	  {
	    horiz_mode [prefix_number] [code_byte_value]
	      = (unsigned char) horiz_mode_code_EOL ();
	    horiz_mode_next_state [prefix_number] [code_byte_value]
	      = (unsigned char) ((bit_number + 1) & 0x7);
	    bits_digested = bit_number + 1;
	  }
	  else if (runlength != INCOMPLETE_CODE)
	  {
	    horiz_mode [prefix_number] [code_byte_value]
	      = (unsigned char) horiz_mode_code_white (runlength);
	    horiz_mode_next_state [prefix_number] [code_byte_value]
	      = (unsigned char) ((bit_number + 1) & 0x7);
	    bits_digested = bit_number + 1;
	  }
	  /* else incomplete code */
	}
	else /* working_color == BLACK */
	{
	  short runlength;
	  runlength = black_run_length (working_prefix);
	  if (runlength == INVALID_CODE)
	  {
	    horiz_mode [prefix_number] [code_byte_value]
	      = (unsigned char) horiz_mode_code_invalid ();
	    horiz_mode_next_state [prefix_number] [code_byte_value]
	      = (unsigned char) (bit_number + 8);
	    bits_digested = bit_number + 1;
	  }
	  else if (runlength == EOL_CODE) /* Group 3 only */
	  {
	    horiz_mode [prefix_number] [code_byte_value]
	      = (unsigned char) horiz_mode_code_EOL ();
	    horiz_mode_next_state [prefix_number] [code_byte_value]
	      = (unsigned char) ((bit_number + 1) & 0x7);
	    bits_digested = bit_number + 1;
	  }
	  else if (runlength != INCOMPLETE_CODE)
	  {
	    horiz_mode [prefix_number] [code_byte_value]
	      = (unsigned char) horiz_mode_code_black (runlength);
	    horiz_mode_next_state [prefix_number] [code_byte_value]
	      = (unsigned char) ((bit_number + 1) & 0x7);
	    bits_digested = bit_number + 1;
	  }
	  /* else incomplete code */
	}
      }
      if (!bits_digested) /* if no codewords after examining byte */
      {
	horiz_mode [prefix_number] [code_byte_value]
	  = (unsigned char) horiz_mode_code_indigestible ();
	horiz_mode_next_state [prefix_number] [code_byte_value]
	  = (unsigned char) find_horiz_mode_prefix (working_prefix,
	    working_color);
      }
    }
}

short horiz_mode_code_black (short runlength)
{
  /*
	0	106
	1	107
	...	...
	63	169
	64	170
	128	171
	...	...
	2560	209
  */
  if (runlength < 64) return (runlength + 106);
  else return ((runlength / 64) + 169);
}

short horiz_mode_code_invalid ()
{
  return (1);
}

short horiz_mode_code_indigestible ()
{
  return (0);
}

short horiz_mode_code_EOL ()
{
  return (210);
}

short horiz_mode_code_white (short runlength)
{
  /*
	0	2
	1	3
	...	...
	63	65
	64	66
	128	67
	...	...
	2560	105
  */
  if (runlength < 64) return (runlength + 2);
  else return ((runlength / 64) + 65);
}

void write_tables ()
{
  FILE *p_table_file;
  short j1, j2, j3;
  p_table_file = fopen ("tables.c", "w+");
  if (p_table_file == NULL)
  {
    printf ("can't open \"tables.c\"\n");
    exit (1);
  }
  /* ---------------------------------------- null_mode [] [] */
  fprintf (p_table_file,
    "unsigned char null_mode [%hd] [256] = {\n",
    null_mode_prefix_count);
  for (j1 = 0; j1 < null_mode_prefix_count; j1++)
  {
    fprintf (p_table_file, "/* %hd */", j1);
    j3 = 1;
    fprintf (p_table_file, "  { ");
    for (j2 = 0; j2 < 256; j2++)
    {
      fprintf (p_table_file, "%hd", (short) null_mode [j1] [j2]);
      if (j3++ == 14)
      {
	j3 = 0; fprintf (p_table_file, ",\n    ");
      }
      else if (j2 != 255) fprintf (p_table_file, ", ");
    }
    if (j1 == horiz_mode_prefix_count - 1) fprintf (p_table_file, "  }\n");
    else fprintf (p_table_file, "  },\n");
  }
  fprintf (p_table_file, "};\n");
  /* ---------------------------------------- null_mode_next_state [] [] */
  fprintf (p_table_file,
    "unsigned char null_mode_next_state [%hd] [256] = {\n",
    null_mode_prefix_count);
  for (j1 = 0; j1 < null_mode_prefix_count; j1++)
  {
    fprintf (p_table_file, "/* %hd */", j1);
    j3 = 1;
    fprintf (p_table_file, "  { ");
    for (j2 = 0; j2 < 256; j2++)
    {
      fprintf (p_table_file, "%hd", (short) null_mode_next_state [j1] [j2]);
      if (j3++ == 14)
      {
	j3 = 0; fprintf (p_table_file, ",\n    ");
      }
      else if (j2 != 255) fprintf (p_table_file, ", ");
    }
    if (j1 == horiz_mode_prefix_count - 1) fprintf (p_table_file, "  }\n");
    else fprintf (p_table_file, "  },\n");
  }
  fprintf (p_table_file, "};\n");
  /* ---------------------------------------- horiz_mode [] [] */
  fprintf (p_table_file,
    "unsigned char horiz_mode [%hd] [256] = {\n",
    horiz_mode_prefix_count);
  for (j1 = 0; j1 < horiz_mode_prefix_count; j1++)
  {
    j3 = 1;
    fprintf (p_table_file, "/* %hd */", j1);
    fprintf (p_table_file, "  { ");
    for (j2 = 0; j2 < 256; j2++)
    {
      fprintf (p_table_file, "%hd", (short) horiz_mode [j1] [j2]);
      if (j3++ == 11)
      {
	j3 = 0; fprintf (p_table_file, ",\n    ");
      }
      else if (j2 != 255) fprintf (p_table_file, ", ");
    }
    if (j1 == horiz_mode_prefix_count - 1) fprintf (p_table_file, "  }\n");
    else fprintf (p_table_file, "  },\n");
  }
  fprintf (p_table_file, "};\n");
  /* ---------------------------------------- horiz_mode_next_state [] [] */
  fprintf (p_table_file,
    "unsigned char horiz_mode_next_state [%hd] [256] = {\n",
    horiz_mode_prefix_count);
  for (j1 = 0; j1 < horiz_mode_prefix_count; j1++)
  {
    fprintf (p_table_file, "/* %hd */", j1);
    j3 = 1;
    fprintf (p_table_file, "  { ");
    for (j2 = 0; j2 < 256; j2++)
    {
      fprintf (p_table_file, "%hd", (short) horiz_mode_next_state [j1] [j2]);
      if (j3++ == 14)
      {
	j3 = 0; fprintf (p_table_file, ",\n    ");
      }
      else if (j2 != 255) fprintf (p_table_file, ", ");
    }
    if (j1 == horiz_mode_prefix_count - 1) fprintf (p_table_file, "  }\n");
    else fprintf (p_table_file, "  },\n");
  }
  fprintf (p_table_file, "};\n");
  fclose (p_table_file);
}

/*	end $RCSfile: builddec.c $ */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -