Beispiel #1
0
void skein256_init(skein_t* ctx, W64* key, W64 outlen) {
  W64 config[4] = {0x0000000133414853, outlen, 0, 0};
  static W64 zeroes[4] = {0,0,0,0};

  /* Set up key if needed */
  if(key != NULL) {
    init_tweak(T_KEY, ctx->tweak);
    set_last(1, ctx->tweak);
    add_bytes(32, ctx->tweak);
    encrypt256(zeroes, ctx->tweak[0], ctx->tweak[1], key, ctx->key);
    ctx->key[0] ^= key[0];
    ctx->key[1] ^= key[1];
    ctx->key[2] ^= key[2];
    ctx->key[3] ^= key[3];
  } else {
    ctx->key[0] = 0; ctx->key[1] = 0; ctx->key[2] = 0; ctx->key[3] = 0;
  }

  /* Process config string */
  mk_config_tweak(ctx->tweak);
  add_bytes(32, ctx->tweak);
  encrypt256(ctx->key, ctx->tweak[0], ctx->tweak[1], config, ctx->key);
  ctx->key[0] ^= config[0];
  ctx->key[1] ^= config[1];
  ctx->key[2] ^= config[2];
  ctx->key[3] ^= config[3];
}
Beispiel #2
0
int
make_brun_chunk
(
UBYTE *image				/* first image */
)
{
    int chunk_count, j, help;
    unsigned char *brun_line;
    float compression;

    chunk_count=6;			/* 4 bytes for chunk size */
					/* 2 bytes for chunk type */
    for (j=0; j < fli_height; j++)
    {
	brun_line = &pixel_chunk_buffer[chunk_count];
	chunk_count += make_brun_line(image, brun_line);
	image += fli_width;
    }

    if ((chunk_count % 2) == 1)
	add_bytes(pixel_chunk_buffer, &chunk_count, 0x0000, IOM_UBYTE);

    help=0;
    add_bytes(pixel_chunk_buffer, &help, chunk_count, IOM_LONG);
    add_bytes(pixel_chunk_buffer, &help, FLI_BRUN, IOM_UWORD);

    compression=fli_size/((float)chunk_count);

    fprintf(stdout," Brun chunk: %d bytes    compression: %f\n",
    	chunk_count,compression);

    return(chunk_count);
}
Beispiel #3
0
static struct convtable *
use_both_charmaps (struct charmap_t *from_charmap,
		   struct charmap_t *to_charmap)
{
  /* In this case we iterate over all the entries in the from_charmap,
     determine the internal name, and find an appropriate entry in the
     to_charmap (if it exists).  */
  struct convtable *rettbl = allocate_table ();
  void *ptr = NULL;
  const void *key;
  size_t keylen;
  void *data;

  while (iterate_table (&from_charmap->char_table, &ptr, &key, &keylen, &data)
	 >= 0)
    {
      struct charseq *in = (struct charseq *) data;
      struct charseq *out = charmap_find_value (to_charmap, key, keylen);

      if (out != NULL)
	add_bytes (rettbl, in, out);
    }

  return rettbl;
}
Beispiel #4
0
static struct convtable *
use_from_charmap (struct charmap_t *from_charmap, const char *to_code)
{
  /* We iterate over all entries in the from_charmap and for those which
     have a known UCS4 representation we use an iconv() call to determine
     the mapping to the to_code charset.  */
  struct convtable *rettbl;
  iconv_t cd;
  void *ptr = NULL;
  const void *key;
  size_t keylen;
  void *data;

  cd = iconv_open (to_code, "WCHAR_T");
  if (cd == (iconv_t) -1)
    /* We cannot do anything.  */
    return NULL;

  rettbl = allocate_table ();

  while (iterate_table (&from_charmap->char_table, &ptr, &key, &keylen, &data)
	 >= 0)
    {
      struct charseq *in = data;
      struct charseq *newp = convert_charseq (cd, in);
      if (newp != NULL)
        add_bytes (rettbl, in, newp);
    }

  iconv_close (cd);

  return rettbl;
}
Beispiel #5
0
void skein256_update(skein_t* ctx, int firstlast, UBIType type, W64 len, W64* data) {
  W64 buf[4] = {0,0,0,0};
  W64* k = ctx->key;
  W64* tweak = ctx->tweak;
  int lastlen;

  /* Process message */
  if(firstlast & 1) {
    init_tweak(type, tweak);
  }
  /* If this is not the last update, don't do last block processing  */
  if(!(firstlast & 2) && len % 32 == 0) {
    ++len;
  }
  while(len > 32) {
    add_bytes(32, tweak);
    encrypt256(k, tweak[0], tweak[1], data, k);
    set_first(0, tweak);
    k[0] ^= *data; ++data;
    k[1] ^= *data; ++data;
    k[2] ^= *data; ++data;
    k[3] ^= *data; ++data;
    len -= 32;
  }

  /* Process last block */
  if(firstlast & 2) {
    lastlen = len % 32;
    if(lastlen == 0 && len > 0) {
      lastlen = 32;
    }
    add_bytes(lastlen, tweak);
    set_last(1, tweak);
    memcpy(buf, data, lastlen);
    encrypt256(k, tweak[0], tweak[1], buf, k);
    k[0] ^= buf[0];
    k[1] ^= buf[1];
    k[2] ^= buf[2];
    k[3] ^= buf[3];
  }
}
Beispiel #6
0
void skein256_output(skein_t* ctx, int from, int to, W64* out) {
  W64 buf[4] = {from,0,0,0};
  W64 *k = ctx->key;
  W64 *tweak = ctx->tweak;
  for(; from <= to; ++from) {
    init_tweak(T_OUT, tweak);
    set_last(1, tweak);
    add_bytes(8, tweak);
    encrypt256(k, tweak[0], tweak[1], buf, out);
    out += 4;
    ++buf[0];
  }
}
Beispiel #7
0
/*************************************************
* Gather Entropy From A File                     *
*************************************************/
void FTW_EntropySource::gather_from_file(const std::string& filename)
   {
   int fd = ::open(filename.c_str(), O_RDONLY | O_NOCTTY);
   if(fd == -1)
      return;

   SecureVector<byte> read_buf(1024);
   ssize_t got = ::read(fd, (byte*)read_buf.begin(), read_buf.size());
   close(fd);

   if(got > 0)
      {
      add_bytes(read_buf, got);
      files_read++;
      }
   }
Beispiel #8
0
static struct convtable *
use_to_charmap (const char *from_code, struct charmap_t *to_charmap)
{
  /* We iterate over all entries in the to_charmap and for those which
     have a known UCS4 representation we use an iconv() call to determine
     the mapping to the from_code charset.  */
  struct convtable *rettbl;
  iconv_t cd;
  void *ptr = NULL;
  const void *key;
  size_t keylen;
  void *data;

  /* Note that the conversion we use here is the reverse direction.  Without
     exhaustive search we cannot figure out which input yields the UCS4
     character we are looking for.  Therefore we determine it the other
     way round.  */
  cd = iconv_open (from_code, "WCHAR_T");
  if (cd == (iconv_t) -1)
    /* We cannot do anything.  */
    return NULL;

  rettbl = allocate_table ();

  while (iterate_table (&to_charmap->char_table, &ptr, &key, &keylen, &data)
	 >= 0)
    {
      struct charseq *out = data;
      struct charseq *newp = convert_charseq (cd, out);
      if (newp != NULL)
        {
          add_bytes (rettbl, newp, out);
          free (newp);
        }
    }

  iconv_close (cd);

  return rettbl;
}
Beispiel #9
0
void* send_msg(void* p)
{
	int sock = *(int*)p;
	char* buf = malloc(send_buf_size);
	for(;;)
	{
		int written;
		if((written = write(sock,buf,send_buf_size)) ==-1)
		{
			err("write");
		}
		else if(written == 0)
		{
			printf("Server has closed the connection!\n");
		}
		else
		{
			// has successfully written
			add_bytes(written);
		}
	}
	return 0;
}
Beispiel #10
0
static void
disassemble(Program * prog, Printer p, void *data)
{
    Stream *s = new_stream(100);
    Stream *insn = new_stream(50);
    int i, l;
    unsigned pc;
    Bytecodes bc;
    const char *ptr;
    const char **names = prog->var_names;
    unsigned tmp, num_names = prog->num_var_names;
#   define NAMES(i)	(tmp = i,					\
			 tmp < num_names ? names[tmp]			\
					 : "*** Unknown variable ***")
    Var *literals = prog->literals;

    initialize_tables();
    print = p;
    print_data = data;
    stream_printf(s, "Language version number: %d", (int) prog->version);
    output(s);
    stream_printf(s, "First line number: %d", prog->first_lineno);
    output(s);

    for (i = -1; i < 0 || i < prog->fork_vectors_size; i++) {
	output(s);
	if (i == -1) {
	    stream_printf(s, "Main code vector:");
	    output(s);
	    stream_printf(s, "=================");
	    output(s);
	    bc = prog->main_vector;
	} else {
	    stream_printf(s, "Forked code vector %d:", i);
	    l = stream_length(s);
	    output(s);
	    while (l--)
		stream_add_char(s, '=');
	    output(s);
	    bc = prog->fork_vectors[i];
	}

	stream_printf(s, "[Bytes for labels = %d, literals = %d, ",
		      bc.numbytes_label, bc.numbytes_literal);
	stream_printf(s, "forks = %d, variables = %d, stack refs = %d]",
		      bc.numbytes_fork, bc.numbytes_var_name,
		      bc.numbytes_stack);
	output(s);
	stream_printf(s, "[Maximum stack size = %d]", bc.max_stack);
	output(s);

	max_bytes_width = 5;

	for (pc = 0; pc < bc.size;) {
	    Byte b;
	    unsigned arg;
#	    define ADD_BYTES(n)	(arg = add_bytes(s, bc.vector, pc, n),	\
				 pc += n,				\
				 arg)
	    unsigned a1, a2;

	    new_insn(s, pc);
	    b = add_bytes(s, bc.vector, pc++, 1);
	    if (b != OP_EXTENDED)
		stream_add_string(insn, COUNT_TICK(b) ? " * " : "   ");
	    if (IS_OPTIM_NUM_OPCODE(b))
		stream_printf(insn, "NUM %d", OPCODE_TO_OPTIM_NUM(b));
#ifdef BYTECODE_REDUCE_REF
	    else if (IS_PUSH_CLEAR_n(b))
		stream_printf(insn, "PUSH_CLEAR %s", NAMES(PUSH_CLEAR_n_INDEX(b)));
#endif /* BYTECODE_REDUCE_REF */
	    else if (IS_PUSH_n(b))
		stream_printf(insn, "PUSH %s", NAMES(PUSH_n_INDEX(b)));
	    else if (IS_PUT_n(b))
		stream_printf(insn, "PUT %s", NAMES(PUT_n_INDEX(b)));
	    else if (b == OP_EXTENDED) {
		b = ADD_BYTES(1);
		stream_add_string(insn, COUNT_EOP_TICK(b) ? " * " : "   ");
		stream_add_string(insn, ext_mnemonics[b]);
		switch ((Extended_Opcode) b) {
		case EOP_WHILE_ID:
		    a1 = ADD_BYTES(bc.numbytes_var_name);
		    a2 = ADD_BYTES(bc.numbytes_label);
		    stream_printf(insn, " %s %d", NAMES(a1), a2);
		    break;
		case EOP_EXIT_ID:
		    stream_printf(insn, " %s",
				  NAMES(ADD_BYTES(bc.numbytes_var_name)));
		    /* fall thru */
		case EOP_EXIT:
		    a1 = ADD_BYTES(bc.numbytes_stack);
		    a2 = ADD_BYTES(bc.numbytes_label);
		    stream_printf(insn, " %d %d", a1, a2);
		    break;
		case EOP_PUSH_LABEL:
		case EOP_END_CATCH:
		case EOP_END_EXCEPT:
		case EOP_TRY_FINALLY:
		    stream_printf(insn, " %d", ADD_BYTES(bc.numbytes_label));
		    break;
		case EOP_TRY_EXCEPT:
		    stream_printf(insn, " %d", ADD_BYTES(1));
		    break;
		case EOP_LENGTH:
		    stream_printf(insn, " %d", ADD_BYTES(bc.numbytes_stack));
		    break;
		case EOP_SCATTER:
		    {
			int i, nargs = ADD_BYTES(1);

			a1 = ADD_BYTES(1);
			a2 = ADD_BYTES(1);
			stream_printf(insn, " %d/%d/%d:", nargs, a1, a2);
			for (i = 0; i < nargs; i++) {
			    a1 = ADD_BYTES(bc.numbytes_var_name);
			    a2 = ADD_BYTES(bc.numbytes_label);
			    stream_printf(insn, " %s/%d", NAMES(a1), a2);
			}
			stream_printf(insn, " %d",
				      ADD_BYTES(bc.numbytes_label));
		    }
		    break;
		default:
		    break;
		}
	    } else {
		stream_add_string(insn, mnemonics[b]);
		switch ((Opcode) b) {
		case OP_IF:
		case OP_IF_QUES:
		case OP_EIF:
		case OP_AND:
		case OP_OR:
		case OP_JUMP:
		case OP_WHILE:
		    stream_printf(insn, " %d", ADD_BYTES(bc.numbytes_label));
		    break;
		case OP_FORK:
		    stream_printf(insn, " %d", ADD_BYTES(bc.numbytes_fork));
		    break;
		case OP_FORK_WITH_ID:
		    a1 = ADD_BYTES(bc.numbytes_fork);
		    a2 = ADD_BYTES(bc.numbytes_var_name);
		    stream_printf(insn, " %d %s", a1, NAMES(a2));
		    break;
		case OP_FOR_LIST:
		case OP_FOR_RANGE:
		    a1 = ADD_BYTES(bc.numbytes_var_name);
		    a2 = ADD_BYTES(bc.numbytes_label);
		    stream_printf(insn, " %s %d", NAMES(a1), a2);
		    break;
		case OP_G_PUSH:
#ifdef BYTECODE_REDUCE_REF
		case OP_G_PUSH_CLEAR:
#endif /* BYTECODE_REDUCE_REF */
		case OP_G_PUT:
		    stream_printf(insn, " %s",
				  NAMES(ADD_BYTES(bc.numbytes_var_name)));
		    break;
		case OP_IMM:
		    {
			Var v;

			v = literals[ADD_BYTES(bc.numbytes_literal)];
			switch (v.type) {
			case TYPE_OBJ:
			    stream_printf(insn, " #%d", v.v.obj);
			    break;
			case TYPE_INT:
			    stream_printf(insn, " %d", v.v.num);
			    break;
			case TYPE_STR:
			    stream_add_string(insn, " \"");
			    for (ptr = v.v.str; *ptr; ptr++) {
				if (*ptr == '"' || *ptr == '\\')
				    stream_add_char(insn, '\\');
				stream_add_char(insn, *ptr);
			    }
			    stream_add_char(insn, '"');
			    break;
			case TYPE_ERR:
			    stream_printf(insn, " %s", error_name(v.v.err));
			    break;
			default:
			    stream_printf(insn, " <literal type = %d>",
					  v.type);
			    break;
			}
		    }
		    break;
		case OP_BI_FUNC_CALL:
		    stream_printf(insn, " %s", name_func_by_num(ADD_BYTES(1)));
		default:
		    break;
		}
	    }

	    finish_insn(s, insn);
	}
    }

    free_stream(s);
    free_stream(insn);
}
Beispiel #11
0
void write_CML_dstring
 (JNIEnv *env, jobject graph, jobject elementRule,
  void *nGraph, int dimension, dstring *encoding)
{
  char buffer [100], *sep, *element;
  dstring from, to;
  int i, k, n;
  void *iterator;
  g_float **coordinate;
  jclass elementRuleClass;
  jmethodID getElement, getBytes;
  prepare_get_element
   (env, elementRule, &elementRuleClass, &getElement, &getBytes);
  clear_dstring (encoding);
  n = get_graph_size (nGraph);
  add_string (encoding, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
  add_string (encoding, "<!DOCTYPE molecule SYSTEM \"cml.dtd\" []>\n");
  add_string (encoding, "<molecule convention=\"MathGraph\">\n");
  add_string (encoding, "  <atomArray>\n");
  add_string (encoding, "    <stringArray builtin=\"id\">");
  sep = "";
  for (i = 1; i <= n; ++i)
  {
    add_string (encoding, sep);
    add_string (encoding, "a");
    sprintf (buffer, "%d", i);
    add_string (encoding, buffer);
    sep = " ";
  }
  add_string (encoding, "</stringArray>\n");
  add_string (encoding, "    <stringArray builtin=\"elementType\">");
  sep = "";
  for (i = 1; i <= n; ++i)
  {
    element = get_element
     (env, elementRule, elementRuleClass, getElement, getBytes, graph, i);
    if (element == NULL) element = "X";
    add_string (encoding, sep);
    add_string (encoding, element);
    sep = " ";
  }
  add_string (encoding, "</stringArray>\n");
  coordinate = malloc (n * sizeof (*coordinate));
  for (i = 0, k = 1; i < n; i = k++)
  {
    coordinate [i] = dimension == 2 ?
     get_2d_coordinates (nGraph, k) : get_3d_coordinates (nGraph, k);
  }
  for (k = 0; k < dimension; ++k)
  {
    add_string (encoding, "    <floatArray builtin=\"");
    add_char (encoding, 'x' + k);
    sprintf (buffer, "%d", dimension);
    add_string (encoding, buffer);
    add_string (encoding, "\">");
    sep = "";
    for (i = 0; i < n; ++i)
    {
      add_string (encoding, sep);
      if (sizeof (g_float) == sizeof (float)) {
	  sprintf (buffer,  "%-g", coordinate [i][k]);
      } else {
	  sprintf (buffer, "%-lg", coordinate [i][k]);
      }
      add_string (encoding, buffer);
      sep = " ";
    }
    add_string (encoding, "</floatArray>\n");
  }
  add_string (encoding, "  </atomArray>\n");
  add_string (encoding, "  <bondArray>\n");
  from = new_dstring;
  to = new_dstring;
  sep = "";
  for (i = 1; i <= n; ++i)
  {
    void *iterator;
    iterator = get_edge_iterator (nGraph, i);
    while (has_next_edge (iterator))
    {
      int j = get_next_edge (iterator);
      if (j <= i) continue;
      sprintf (buffer, "%sa", sep);
      add_string (&from, buffer);
      add_string (&to, buffer);
      sprintf (buffer, "%d", i);
      add_string (&from, buffer);
      sprintf (buffer, "%d", j);
      add_string (&to, buffer);
      sep = " ";
    }
  }
  add_string (encoding, "    <stringArray builtin=\"atomRef\">");
  add_bytes (encoding, from.base, from.length);
  add_string (encoding, "</stringArray>\n");
  add_string (encoding, "    <stringArray builtin=\"atomRef\">");
  add_bytes (encoding, to.base, to.length);
  add_string (encoding, "</stringArray>\n");
  add_string (encoding, "  </bondArray>\n");
  add_string (encoding, "</molecule>\n");
  clear_dstring (&from);
  clear_dstring (&to);
}
Beispiel #12
0
static int make_brun_line
(
unsigned char *image_line,
unsigned char *brun_line
)
{
    int i, ipos, n, packets;
    int len, help;
    int inext;

    for (i=0; i < fli_width; i++)
        val[i]=image_line[i];

    i=0;
    while (i < fli_width)
      {
	len = test_const(i);
	if (len > 1)
	  {
	    for (n=0; n < len; n++)
	      {
		modus[i] = MCONST;
		bingo[i] = len;
		i++;
	      }
	  }
	else
	  {
	    modus[i] = MLOAD;
	    bingo[i] = 1;
	    i++;
	  }
      }

    i=0;
    while (i < fli_width)
      {
	if (modus[i] == MCONST)
	  {
	    i += bingo[i];
	  }
	else
	  {
	    len = test_mload(i);
	    for (n=0; n < len; n++)
	      {
		bingo[i] = len;
		i++;
	      }
	  }
      }

    i=bingo[0];
    while (i < fli_width)
      {
	inext = i + bingo[i];
	if ((((modus[i] == MCONST) && (bingo[i] <= 2)) || (modus[i] == MLOAD))
	    && (modus[i-1] == MLOAD)) merge_packets(i);
	i = inext;
      }

    i=fli_width - bingo[fli_width - 1];
    while (i > 0)
      {
	inext = i - bingo[i-1];
	if ((((modus[i-1] == MCONST) && (bingo[i-1] <= 2))
	     || (modus[i-1] == MLOAD)) && (modus[i] == MLOAD))
	  merge_packets(i);
	i = inext;
      }

    while (test_packets() > 255)
      {reduce_packets();}

    ipos=1;

    packets=0;
    i=0;
    while (i < fli_width)
    {
        len=bingo[i];
	/* fprintf(stdout," %d  %d\n",i,len); */

        if (modus[i] == MCONST)
        {
	  add_bytes(brun_line, &ipos, len, IOM_UBYTE);
	  add_bytes(brun_line, &ipos, val[i], IOM_UBYTE);
	  i += len;
        }
        else
        {
	  add_bytes(brun_line, &ipos, -len, IOM_UBYTE);
	  for (n=0; n < len; n++)
	    {
	      add_bytes(brun_line, &ipos, val[i++], IOM_UBYTE);
            }
	}
	packets++;
    }
    /* fprintf(stdout," packets: %d    ipos: %d\n\n",packets,ipos); */

    help=0;
    add_bytes(brun_line, &help, packets, IOM_UBYTE);

    return(ipos);
}