Example #1
0
File: memory.c Project: jes/bnbf
/* Write output from the current cell */
void output(Memory *mem) {
  void *cell;
  char *buf;

  if(!(cell = get_cell(mem))) return;

  if(chario) {
    /* character io */
    if(wrap) fputc(*(unsigned char *)cell, stdout);
    else fputc(bigint_to_int(cell), stdout);
  } else {
    /* number io */
    if(wrap) printf("%d\n", *(unsigned char *)cell);
    else {
      buf = malloc(bigint_string_length(cell));

      bigint_to_string(cell, buf);
      fputs(buf, stdout); fputc('\n', stdout);

      free(buf);
    }
  }
}
Example #2
0
// FUNCTION : generate a public key
//
// Input : username, 
//         a4, a6 -> elliptic curve constants
//         xp, yp -> point's coordinate
//				 prime field
//         passphrase
// Output : file username.pkf which contains all
//          of those information
// 
// AUTHOR : TH <*****@*****.**>, Jan 2000
//
void gen_pubkey(const char* pkfilename, int blocksize)
{
	// declaration of variables
	char username[MAX];
	point P,H;
	bigmod a4,a6,xp,yp;
	bigint xdp,ydp,q;

	banner();
	cout<<"Before you can use encryption/decryption you must generate your public key.";
	cout<<"\nThe following process will guide you through generating your public key.";
	cout<<"\n\nTo achieve a cryptographically secure elliptic curve cryptosystem, you must ";
	cout<<"\nsupply a relatively big prime finite field (q), a good a, b";
	cout<<"\nfor elliptic curve equation and a point that lies on the curve.";

	//get EC parameters from user
	cout<<"\n\nGENERATE PUBLIC KEY\n";
	cout<<"-------------------";
	cout<<"\nPlease enter your user name : ";cin>>username;
		
	get_ecparm(a4,a6,xp,yp,q);
	
	// EC initialization
	P.init_curve(a4,a6);
	P.set_point(xp,yp);

	if (pkfilename==" ")
		pkfilename=PKFILENAME;

	// open public key file
	FILE* pkfile;
	pkfile=fopen(pkfilename,"a+");
	if(!pkfile)
	{
		cout<<"\nAZTECC ERROR : Error writing file ["<<pkfilename<<"]"<<endl;
		exit(1);
	}
	
	bigint d;
	get_passphrase(d,blocksize,username);
	
	// multiply the private key with EC point
	mul_point(H,d,P);
	
	// compute hash(d)
	char* sd=new char[MAX];
	bigint_to_string(d,sd);
	char* hash_d=new char[MAX];
	hash_d=MD5_hex_digest(sd);
	
	// convert the EC coordinate to string
	get_x(xdp,H);
	char* sxdp=new char[MAX];
	bigint_to_string(xdp,sxdp);
	get_y(ydp,H);
	char* sydp=new char[MAX];
	bigint_to_string(ydp,sydp);

	// convert a4, a6, xp, yp to string
	char* sa4=new char[MAX];
	bigmod_to_string(a4,sa4);
	char* sa6=new char[MAX];
	bigmod_to_string(a6,sa6);
	char* sxp=new char[MAX];
	bigmod_to_string(xp,sxp);
	char* syp=new char[MAX];
	bigmod_to_string(yp,syp);
	char* sq=new char[MAX];
	bigint_to_string(q,sq);
	
	// save all the info to public key file
   fputc('\n',pkfile);
	fprintf(pkfile,"%s#%s#%s#%s#%s#%s#%s#%s#%s",username,hash_d,sa4,sa6,sxp,syp,sxdp,sydp,sq);
	fclose(pkfile);
	cout<<"\nDone saving your public key to ["<<pkfilename<<"]"<<endl;

	//cleaning up
	delete[] sd;
	delete[] sxdp;
	delete[] sydp;
	delete[] sa4;
	delete[] sa6;
	delete[] sxp;
	delete[] syp;
	delete[] sq;
}
Example #3
0
/*
 * csql_db_value_as_string() - convert DB_VALUE to string
 *   return: formatted string
 *   value(in): value to convert
 *   length(out): length of output string
 *   plain_output(in): refine string for plain output
 */
char *
csql_db_value_as_string (DB_VALUE * value, int *length, bool plain_string)
{
  char *result = NULL;
  int len = 0;

  if (value == NULL)
    {
      return (NULL);
    }

  switch (DB_VALUE_TYPE (value))
    {
    case DB_TYPE_BIGINT:
      result =
	bigint_to_string (DB_GET_BIGINT (value), default_bigint_profile.fieldwidth, default_bigint_profile.leadingzeros,
			  default_bigint_profile.leadingsymbol, default_bigint_profile.commas,
			  default_bigint_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_INTEGER:
      result =
	bigint_to_string (DB_GET_INTEGER (value), default_int_profile.fieldwidth, default_int_profile.leadingzeros,
			  default_int_profile.leadingsymbol, default_int_profile.commas, default_int_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_SHORT:
      result =
	bigint_to_string (SHORT_TO_INT (DB_GET_SHORT (value)), default_short_profile.fieldwidth,
			  default_short_profile.leadingzeros, default_short_profile.leadingsymbol,
			  default_short_profile.commas, default_short_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_FLOAT:
      result =
	double_to_string ((double) DB_GET_FLOAT (value), default_float_profile.fieldwidth,
			  default_float_profile.precision, default_float_profile.leadingsign, NULL, NULL,
			  default_float_profile.leadingzeros, default_float_profile.trailingzeros,
			  default_float_profile.commas, default_float_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_DOUBLE:
      result =
	double_to_string (DB_GET_DOUBLE (value), default_double_profile.fieldwidth, default_double_profile.precision,
			  default_double_profile.leadingsign, NULL, NULL, default_double_profile.leadingzeros,
			  default_double_profile.trailingzeros, default_double_profile.commas,
			  default_double_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_NUMERIC:
      result = numeric_to_string (value, default_numeric_profile.commas);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_VARCHAR:
    case DB_TYPE_CHAR:
      {
	int dummy, bytes_size, decomp_size;
	bool need_decomp = false;
	char *str;
	char *decomposed = NULL;

	str = db_get_char (value, &dummy);
	bytes_size = db_get_string_size (value);
	if (bytes_size > 0 && DB_GET_STRING_CODESET (value) == INTL_CODESET_UTF8)
	  {
	    need_decomp =
	      unicode_string_need_decompose (str, bytes_size, &decomp_size, lang_get_generic_unicode_norm ());
	  }

	if (need_decomp)
	  {
	    decomposed = (char *) malloc (decomp_size * sizeof (char));
	    if (decomposed != NULL)
	      {
		unicode_decompose_string (str, bytes_size, decomposed, &decomp_size, lang_get_generic_unicode_norm ());

		str = decomposed;
		bytes_size = decomp_size;
	      }
	    else
	      {
		return NULL;
	      }
	  }

	result = string_to_string (str, default_string_profile.string_delimiter, '\0', bytes_size, &len, plain_string);

	if (decomposed != NULL)
	  {
	    free_and_init (decomposed);
	  }

      }
      break;
    case DB_TYPE_VARNCHAR:
    case DB_TYPE_NCHAR:
      {
	int dummy, bytes_size, decomp_size;
	bool need_decomp = false;
	char *str;
	char *decomposed = NULL;

	str = db_get_char (value, &dummy);
	bytes_size = db_get_string_size (value);
	if (bytes_size > 0 && DB_GET_STRING_CODESET (value) == INTL_CODESET_UTF8)
	  {
	    need_decomp =
	      unicode_string_need_decompose (str, bytes_size, &decomp_size, lang_get_generic_unicode_norm ());
	  }

	if (need_decomp)
	  {
	    decomposed = (char *) malloc (decomp_size * sizeof (char));
	    if (decomposed != NULL)
	      {
		unicode_decompose_string (str, bytes_size, decomposed, &decomp_size, lang_get_generic_unicode_norm ());

		str = decomposed;
		bytes_size = decomp_size;
	      }
	    else
	      {
		return NULL;
	      }
	  }

	result = string_to_string (str, default_string_profile.string_delimiter, 'N', bytes_size, &len, plain_string);

	if (decomposed != NULL)
	  {
	    free_and_init (decomposed);
	  }
      }
      break;
    case DB_TYPE_VARBIT:
    case DB_TYPE_BIT:
      result = bit_to_string (value, default_string_profile.string_delimiter, plain_string);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_OBJECT:
      result = object_to_string (DB_GET_OBJECT (value), get_object_print_format ());
      if (result == NULL)
	{
	  result = duplicate_string ("NULL");
	}
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_VOBJ:
      result = object_to_string (DB_GET_OBJECT (value), get_object_print_format ());
      if (result == NULL)
	{
	  result = duplicate_string ("NULL");
	}
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_SET:
    case DB_TYPE_MULTISET:
    case DB_TYPE_SEQUENCE:
      result =
	set_to_string (value, default_set_profile.begin_notation, default_set_profile.end_notation,
		       default_set_profile.max_entries, plain_string);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_TIME:
      {
	char buf[TIME_BUF_SIZE];
	if (db_time_to_string (buf, sizeof (buf), DB_GET_TIME (value)))
	  {
	    result = duplicate_string (buf);
	  }
	if (result)
	  {
	    len = strlen (result);
	  }
	break;
      }
    case DB_TYPE_TIMETZ:
      {
	char buf[TIMETZ_BUF_SIZE];
	DB_TIMETZ *time_tz = DB_GET_TIMETZ (value);
	if (db_timetz_to_string (buf, sizeof (buf), &(time_tz->time), &time_tz->tz_id))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_TIMELTZ:
      {
	char buf[TIMETZ_BUF_SIZE];

	if (db_timeltz_to_string (buf, sizeof (buf), DB_GET_TIME (value)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_MONETARY:
      {
	char *leading_str = NULL;
	char *trailing_str = NULL;
	DB_CURRENCY currency = ((DB_GET_MONETARY (value))->type);

	if (default_monetary_profile.currency_symbol)
	  {
	    if (intl_get_currency_symbol_position (currency) == 1)
	      {
		trailing_str = intl_get_money_symbol_console (currency);
	      }
	    else
	      {
		leading_str = intl_get_money_symbol_console (currency);
	      }
	  }

	result =
	  (DB_GET_MONETARY (value) == NULL) ? NULL : double_to_string ((DB_GET_MONETARY (value))->amount,
								       default_monetary_profile.fieldwidth,
								       default_monetary_profile.decimalplaces,
								       default_monetary_profile.leadingsign,
								       leading_str, trailing_str,
								       default_monetary_profile.leadingzeros,
								       default_monetary_profile.trailingzeros,
								       default_monetary_profile.commas,
								       DOUBLE_FORMAT_DECIMAL);

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_DATE:
      /* default format for all locales */
      result = date_as_string (DB_GET_DATE (value), default_date_profile.format);
      if (result)
	{
	  len = strlen (result);
	}
      break;
    case DB_TYPE_UTIME:
      {
	char buf[TIMESTAMP_BUF_SIZE];
	if (db_utime_to_string (buf, sizeof (buf), DB_GET_UTIME (value)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_TIMESTAMPTZ:
      {
	char buf[TIMESTAMPTZ_BUF_SIZE];
	DB_TIMESTAMPTZ *ts_tz = DB_GET_TIMESTAMPTZ (value);
	if (db_timestamptz_to_string (buf, sizeof (buf), &(ts_tz->timestamp), &(ts_tz->tz_id)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_TIMESTAMPLTZ:
      {
	char buf[TIMESTAMPTZ_BUF_SIZE];

	if (db_timestampltz_to_string (buf, sizeof (buf), DB_GET_UTIME (value)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_DATETIME:
      {
	char buf[DATETIME_BUF_SIZE];
	if (db_datetime_to_string (buf, sizeof (buf), DB_GET_DATETIME (value)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_DATETIMETZ:
      {
	char buf[DATETIMETZ_BUF_SIZE];
	DB_DATETIMETZ *dt_tz = DB_GET_DATETIMETZ (value);
	if (db_datetimetz_to_string (buf, sizeof (buf), &(dt_tz->datetime), &(dt_tz->tz_id)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_DATETIMELTZ:
      {
	char buf[DATETIMETZ_BUF_SIZE];

	if (db_datetimeltz_to_string (buf, sizeof (buf), DB_GET_DATETIME (value)))
	  {
	    result = duplicate_string (buf);
	  }

	if (result)
	  {
	    len = strlen (result);
	  }
      }
      break;
    case DB_TYPE_NULL:
      result = duplicate_string ("NULL");
      if (result)
	{
	  len = strlen (result);
	}
      break;

    case DB_TYPE_BLOB:
    case DB_TYPE_CLOB:
      {
	DB_ELO *elo = DB_GET_ELO (value);

	if (elo != NULL)
	  {
	    result = duplicate_string (elo->locator);
	  }

	if (result != NULL)
	  {
	    len = strlen (result);
	  }
      }
      break;

    case DB_TYPE_ENUMERATION:
      {
	int bytes_size, decomp_size;
	bool need_decomp = false;
	const char *str;
	char *decomposed = NULL;

	if (db_get_enum_short (value) == 0 && db_get_enum_string (value) == NULL)
	  {
	    /* ENUM special error value */
	    str = "";
	    bytes_size = 0;
	  }
	else
	  {
	    str = db_get_enum_string (value);
	    bytes_size = db_get_enum_string_size (value);
	  }
	if (bytes_size > 0 && db_get_enum_codeset (value) == INTL_CODESET_UTF8)
	  {
	    need_decomp =
	      unicode_string_need_decompose (str, bytes_size, &decomp_size, lang_get_generic_unicode_norm ());
	  }

	if (need_decomp)
	  {
	    decomposed = (char *) malloc (decomp_size * sizeof (char));
	    if (decomposed != NULL)
	      {
		unicode_decompose_string (str, bytes_size, decomposed, &decomp_size, lang_get_generic_unicode_norm ());

		str = decomposed;
		bytes_size = decomp_size;
	      }
	    else
	      {
		return NULL;
	      }
	  }

	result = string_to_string (str, default_string_profile.string_delimiter, '\0', bytes_size, &len, plain_string);
	if (decomposed != NULL)
	  {
	    free_and_init (decomposed);
	  }
      }
      break;

    default:
      {
	char temp_buffer[256];

	(void) sprintf (temp_buffer, "<%s>", db_get_type_name (DB_VALUE_TYPE (value)));
	result = duplicate_string (temp_buffer);
	if (result)
	  {
	    len = strlen (result);
	  }
      }
    }

  if (length)
    {
      *length = len;
    }
  return result;
}