Пример #1
0
int
macsign(Pkt *p, int seq)
{
	int rc, len;
	uchar *sig, *buf, mac[MAClen];

	sig = p->buf + NBHDRLEN + MACoff;
	buf = p->buf + NBHDRLEN;
	len = (p->pos - p->buf) - NBHDRLEN;

#ifdef DEBUG_MAC
	if(seq & 1)
		dmp("rx", seq, sig, MAClen);
#endif
	rc = 0;
	if(! p->s->seqrun)
		memcpy(mac, "BSRSPYL ", 8);	/* no idea, ask MS */
	else
		rc = genmac(buf, len, seq, p->s->auth->mackey[0], mac);
#ifdef DEBUG_MAC
	if(!(seq & 1))
		dmp("tx", seq, mac, MAClen);
#endif
	memcpy(sig, mac, MAClen);
	return rc;
}
Пример #2
0
MassPoint MassPoint::dstate(double)
{
    MassPoint dmp(v, force * (1 / m));
    force.x = 0;
    force.y = 0;
    force.z = 0;
    return dmp;
}
Пример #3
0
void stp()
{
    programClock++;
    queueArray[currentPriority][0]->isComplete = true;
    cleanUp();
    dmp();
    printf("\n\n---------------------------------\n\tMACHINE HALTED\n---------------------------------\n");
    exit(0);
}
Пример #4
0
spstr changeext(const spstr &src,const spstr &newext) {
   spstr dmp(src);
   int ps=dmp.crpos('.');
   if (dmp.crpos('/')>ps||dmp.crpos('\\')>ps) ps=-1;
   if (!newext) {
      if (ps>=0) dmp.del(ps,dmp.length()-ps);
      return dmp;
   }
   if (ps<0) { dmp+='.'; ps=dmp.length(); }
   if (ps<dmp.length()) dmp.del(ps+1,65536);
   return dmp+=newext;
}
Пример #5
0
// Interactive command-line user interface
void userInterface()
{
    printf("\n\n");
    // User prompt
    if (currentUser == 0) printf("Operating System: ");
    else printf("User %i: ", currentUser);

    // Get command
    fgets(controlCommand, 5, stdin);
    printf("\n");

    if (isValidCommand())
    {
        switch (commandCode)
        {
        case 1: placeInQueue(); break;
        case 2: dmp(); break;
        case 3: placeInQueue(); break;
        case 4: nop(); break;
        }

        // Schedule next execution request
        scheduler();

        // Routine cleanup after every execution; queue manipulation, process removal
        if (queueArray[currentPriority][0] != NULL)
        {
            cleanUp();
        }

        dumpQueues();

        // Cycle to the next user
        if (currentUser == numberOfUsers - 1) currentUser = 0;
        else currentUser++;
    }
    else printf("Invalid command entered\n");
}
Пример #6
0
void snmp_output(u_char *pkt, u_char *ebuf, int j){
	int k;
	struct ip *ipi, *ipo;
	struct udphdr *udpi, *udpo;
	
	/* word align */
	if( j&1 ){
		for(k=0; k<j; k++)
			ebuf[k-j-1] = ebuf[k-j];

		udpo = (struct udphdr *) (ebuf -j - sizeof(struct udphdr));
	}else{
		udpo = (struct udphdr *) (ebuf - j - sizeof(struct udphdr) + 1);
	}

	ipo = (struct ip*) (((char*)udpo) - sizeof(struct ip));

	ipi = (struct ip*)pkt;
	udpi = (struct udphdr*) (pkt + sizeof(struct ip));

	/* fill in udp and ip headers */
	udpo->uh_sport = udpi->uh_dport;
	udpo->uh_dport = udpi->uh_sport;
	udpo->uh_len   = H2NS(j);
	udpo->uh_sum   = 0;

	ipo->ip_p = IPPROTO_UDP;
	bcopy( (char*)&ipi->ip_src, (char*)&ipo->ip_dst, 4);
	bcopy( (char*)&eeprom.myipaddr, (char*)&ipo->ip_src, 4);
	
#ifdef TESTING
	dmp( (u_char*)ipo, sizeof(struct ip)+sizeof(struct udphdr)+j);
#else
	ip_output( (u_char*)ipo, sizeof(struct ip)+sizeof(struct udphdr)+j);
#endif
	return;
}
Пример #7
0
Файл: avl.c Проект: hubugui/ds
static int _node_dump(struct avl_node *node, void *userdata)
{
    dump dmp = (dump) userdata;
    dmp(node->value);
    return 0;
}
Пример #8
0
void trans_graph()
{
   // remember if  we are in batch mode
   Bool_t batch = gROOT->IsBatch();

   // switch to batch mode
   gROOT->SetBatch(kTRUE);

   // execute graph.C macro
   gROOT->Macro("$ROOTSYS/tutorials/graphs/graph.C");

   // create gVirtualPS object
   TImageDump dmp("dummy.png");
   TImage *fore = dmp.GetImage();  // image associated with image_dump

   // resize canvas
   gPad->SetCanvasSize(400, 300);
   gPad->Paint(); // paint gPad on fore image associated with TImageDump

   // open background image
   TImage *back = TImage::Open("$ROOTSYS/tutorials/image/rose512.jpg");

   // choose colors to be transparent
   TColor *bk1 = gROOT->GetColor(gPad->GetFillColor());
   TColor *bk2 = gROOT->GetColor(gPad->GetFrame()->GetFillColor());
   UInt_t rgb1 = color2rgb(bk1);
   UInt_t rgb2 = color2rgb(bk2);

   // get directly accessible ARGB array
   UInt_t *argb = fore->GetArgbArray();
   UInt_t w = fore->GetWidth();
   UInt_t h = fore->GetHeight();

   // scan all pixels in fore image and
   // make rgb1, rgb2 colors transparent.
   for (UInt_t i = 0; i < h; i++) {
      for (UInt_t j = 0; j < w; j++) {
         Int_t idx = i*w + j;

         // RGB part of ARGB color
         UInt_t col = argb[idx] & 0xffffff;

         // 24..31 bits define transparency of the color in the range 0 - 0xff
         // for example, 0x00000000 - black color with 100% transparency
         //              0xff000000 - non-transparent black color

         if ((col == rgb1) || (col == rgb2)) { //
            argb[idx] = 0; // 100% transparent
         } else {
            argb[idx] = 0xff000000 + col;  // make other pixels non-transparent
         }
      }
   }

   // alphablend back and fore images
   back->Merge(fore, "alphablend", 20, 20);

   // write result image in PNG format
   back->WriteImage("trans_graph.png");
   printf("*************** File trans_graph.png created ***************\n");

   delete back;

   // switch back to GUI mode
   if (!batch) gROOT->SetBatch(kFALSE);
}
Пример #9
0
int main(int argc, const char * argv[]) {
    const double pi = std::abs(std::atan2(0,-1));
    Floorplan flp;

    double wall_loss = 2.0;                      // layered drywall
    double angle_loss = 5.0 / (pi/2);            // 5.0dB for 90 degrees
    double exterior_wall_loss = 15.0;            // thick concrete

    int size_x = 15;
    int size_y = 15;

    unsigned seed = 0;
    unsigned st_seed = 0;
    int office = 0;
    double office_x = 3.0;
    double office_y = 4.0;
    double hall_width = 2.0;

    const char *save = 0;
    const char *load = 0;
    const char *saveimage = 0;
    std::vector<int> pathset;

    bool display_paths = 1;
    bool label_floorplan = 1;
    double grid_measurements = 5.0;
    double step = 0.5;

    bool truncate_dijkstra = true;

    // quick hack for various computations
    // mode 0 == s-t breakpoint path computation
    // mode 1 == all destinations
    // mode 2 == approx all dest
    // mode 3 == heatmap
    // mode 4 == random s-t pair breakpoint evaluation
    // mode 5 == coverage
    // mode 6 == ratio_all_measurement
    int mode = 0;

    int num_experiments = 100;

    double p = 20.0/std::log(10.0);

    // This program finds the paths from pts[0] to pts[1]
    Point pts[3];
    pts[0].x = 2.5;
    pts[0].y = 1.5;
    pts[1].x = 9.3;
    pts[1].y = 12.3;
    pts[2].x = 7.9;
    pts[2].y = 11.1;

    int limit = 50;

    int argi = 1;
    while (argi < argc - 1) {
        if (strcmp(argv[argi], "-sx") == 0) size_x = atoi(argv[++argi]);
        else if (strcmp(argv[argi], "-sy") == 0) size_y = atoi(argv[++argi]);
        else if (strcmp(argv[argi], "-wl") == 0) wall_loss = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-al") == 0) angle_loss = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-el") == 0) exterior_wall_loss = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-sd") == 0) seed = atoi(argv[++argi]);
        else if (strcmp(argv[argi], "-st") == 0) st_seed = atoi(argv[++argi]);
        else if (strcmp(argv[argi], "-s") == 0) save = argv[++argi];
        else if (strcmp(argv[argi], "-l") == 0) load = argv[++argi];
        else if (strcmp(argv[argi], "-w") == 0) saveimage = argv[++argi];
        else if (strcmp(argv[argi], "-p0x") == 0) pts[0].x = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-p0y") == 0) pts[0].y = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-p1x") == 0) pts[1].x = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-p1y") == 0) pts[1].y = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-dp") == 0) display_paths = atoi(argv[++argi]);
        else if (strcmp(argv[argi], "-gm") == 0) grid_measurements = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-step") == 0) step = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-mode") == 0) mode = atoi(argv[++argi]);
        else if (strcmp(argv[argi], "-p") == 0) p = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-office") == 0) office = atoi(argv[++argi]);
        else if (strcmp(argv[argi], "-office_x") == 0) office_x = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-office_y") == 0) office_y = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-hall_width") == 0) hall_width = atof(argv[++argi]);
        else if (strcmp(argv[argi], "-n") == 0) num_experiments = atoi(argv[++argi]);
        else if (strcmp(argv[argi], "-path") == 0) pathset.push_back(atoi(argv[++argi]));
        else if (strcmp(argv[argi], "-truncate") == 0) truncate_dijkstra = atoi(argv[++argi]);
        else if (strcmp(argv[argi], "-label") == 0) label_floorplan = atoi(argv[++argi]);

        argi++;
    }

    if (load) {
        double start_time = util::cpu_timer();
        flp.load(load);
        std::cerr << "Floorplan loaded in " << (util::cpu_timer() - start_time)
                  << " cpu seconds." << std::endl;
    } else {
        double start_time = util::cpu_timer();
        if (office == 1) {
            flp.genOffice1(size_x, size_y, office_x, office_y, hall_width,
                           wall_loss, angle_loss, exterior_wall_loss);
        } else if (office == 2) {
            flp.genOffice2(size_x, size_y, office_x, office_y, hall_width,
                           wall_loss, angle_loss, exterior_wall_loss);
        } else if (office == 3) {
            flp.genOffice3(size_x, size_y, office_x, office_y, hall_width,
                           wall_loss, angle_loss, exterior_wall_loss);
        } else if (office == 4) {
            flp.genOffice4(size_x, size_y, office_x, office_y, hall_width,
                           wall_loss, angle_loss, exterior_wall_loss);
        } else {
            flp.genRandomFloorplan(size_x, size_y, wall_loss, angle_loss,
                                   exterior_wall_loss, seed);
        }
        std::cerr << "Floorplan generated in " << (util::cpu_timer() - start_time)
                  << " cpu seconds." << std::endl;
    }

    std::cerr << "Floorplan contains " << flp.getNumCorners() << " corners and "
              << flp.getNumWalls() << " walls." << std::endl;

    if (save) {
        double start_time = util::cpu_timer();
        flp.save(save);
        std::cerr << "Floorplan saved in " << (util::cpu_timer() - start_time)
                  << " cpu seconds." << std::endl;
    }

    int npts = 2;
    if (mode == 1 || mode == 2) npts = 3;

    if (mode == 3) label_floorplan = false;
    DominantPath dmp(&flp, npts, pts, label_floorplan);

    if (mode != 3 && mode != 4 && mode != 6) {
        double geng2_start = util::cpu_timer();
        dmp.generateG2();
        std::cerr << "G2 generated " << dmp.numG2Points() << " points, "
                  << dmp.numG2Links() << " links in "
                  << (util::cpu_timer() - geng2_start) << " cpu seconds."
                  << std::endl;
    }

    Path *paths = new Path[limit];
    int npaths = 2;
    if (mode == 0) {
        double break_start = util::cpu_timer();
        int count = dmp.BreakPoints(0, 1, limit, paths, npaths);
        std::cerr << count << " relaxations performed in "
                  << (util::cpu_timer() - break_start) << " cpu seconds."
                  << std::endl;
        if (display_paths) {
            dmp.printPaths(npaths, paths, p, saveimage, pathset);
        }
        for (int i = 0; i < npaths; i++) {
            printf("%f\n", (paths[i].L+p*std::log(paths[i].D)));
        }
    } else if (mode == 1) {
        double break_start = util::cpu_timer();
        dmp.Dijkstra_all_dest(100, paths);
        std::cerr << "Dijkstra_all_dest finished in "
                  << (util::cpu_timer() - break_start) << " cpu seconds."
                  << std::endl;
    } else if (mode == 2) {
        double break_start = util::cpu_timer();
        dmp.Approx_all_dest(p, step, paths, truncate_dijkstra);
        std::cerr << "Approx_all_dest finished in "
                  << (util::cpu_timer() - break_start) << " cpu seconds."
                  << std::endl;
    } else if (mode == 3) {
        dmp.heatmap(p, step, pts[0].x, pts[0].y,
                    flp.getWidth(), flp.getHeight(),
                    grid_measurements, saveimage, truncate_dijkstra);
    } else if (mode == 4) { // for testing
        Random_st_pairs rsp(&flp, p);
        rsp.size_x = flp.getWidth();
        rsp.size_y = flp.getHeight();
        rsp.test(num_experiments, st_seed);
    } else if (mode == 5) {
        double break_start = util::cpu_timer();
        coverage(dmp);
        std::cerr << "coverage finished in "
        << (util::cpu_timer() - break_start) << " cpu seconds."
        << std::endl;
    } else if (mode == 6) {
        dmp.ratio_all_measurement(step, pts[0].x, pts[0].y,
                                  flp.getWidth(), flp.getHeight(),
                                  grid_measurements, false, false, true);
    }
    
    delete [] paths;

    return 0;
}
Пример #10
0
int
main(int argc, char **argv, char **envp)
{
#if 0
	const char *algorithm	= NULL;
#else
	const char *algorithm	= "md5";
#endif
	const char *key		= NULL;
	const char *msg		= NULL;
	const char *signature	= NULL;
#if 0
	const char *type	= NULL;
#else
	const char *type	= "rsa";
#endif

	xxxflag	= false;

	if(!(prog = basename(argv[0]))) {
		fprintf(stderr, "err: basename: %s -- %s\n",
			strerror(errno), argv[0]);

		return(-1);
		/* NOTREACHED */
	}

	{
		int ch = 0;
#if 0
		while((ch = getopt(argc, argv, "a:k:m:s:t:@")) != -1) {
#else
		while((ch = getopt(argc, argv, "k:m:s:@")) != -1) {
#endif
			switch (ch) {
#if 0
				case 'a':
					algorithm	= optarg;
					break;
#endif
				case 'k':
					key		= optarg;
					break;
				case 'm':
					msg		= optarg;
					break;
				case 's':
					signature	= optarg;
					break;
#if 0
				case 't':
					type		= optarg;
					break;
#endif
				case '@':
					xxxflag		= true;
					break;
				default:
					usage();
					/* NOTREACHED */

					break;
			}
		}

		argc -= optind;
		argv += optind;
	}

	if(argc || !algorithm || !key || !msg || !type) {
		usage();
		/* NOTREACHED */
	}

	return(sigcomp_main(algorithm, key, msg, signature, type)? 0: -1);
	/* NOTREACHED */
}



static bool
sigcomp_main(const char *const algorithm, const char *const key,
	const char *const msg, const char *const signature,
	const char *const type)
{
	const EVP_MD *evp_md	= NULL;
	BIGNUM *bn_key		= NULL;
	BIGNUM *bn_msg		= NULL;
	BIGNUM *bn_signature	= NULL;

	ERR_load_crypto_strings();

	if(!(evp_md = evp_md_with_algorithm(type, algorithm))) {
		fprintf(stderr, "err: algorithm: %s -- "
			"unknown algorithm for %s\n", algorithm, type);

		return(sigcomp_return(false, bn_key, bn_msg, bn_signature));
	}

	/* key -> bn_key */
	if(!(bn_key = BN_new())) {
		openssl_strerror("BN_new");
		return(sigcomp_return(false, bn_key, bn_msg, bn_signature));
	}

	if(!BN_hex2bn(&bn_key, (const char *)key)) {
		openssl_strerror("BN_hex2bn");
		return(sigcomp_return(false, bn_key, bn_msg, bn_signature));
	}

	/* msg -> bn_msg */
	if(!(bn_msg = BN_new())) {
		openssl_strerror("BN_new");
		return(sigcomp_return(false, bn_key, bn_msg, bn_signature));
	}

	if(!BN_hex2bn(&bn_msg, (const char *)msg)) {
		openssl_strerror("BN_hex2bn");
		return(sigcomp_return(false, bn_key, bn_msg, bn_signature));
	}

	if(signature) {
		/* signature -> bn_signature */
		if(!(bn_signature = BN_new())) {
			openssl_strerror("BN_new");
			return(sigcomp_return(false,
				bn_key, bn_msg, bn_signature));
		}

		if(!BN_hex2bn(&bn_signature, (const char *)signature)) {
			openssl_strerror("BN_hex2bn");
			return(sigcomp_return(false,
				bn_key, bn_msg, bn_signature));
		}

		if(!sigcomp_vrfy(evp_md, bn_key, bn_msg, bn_signature, type)) {
			return(sigcomp_return(false,
				bn_key, bn_msg, bn_signature));
		}
	} else {
		if(!sigcomp_sign(evp_md, bn_key, bn_msg, type)) {
			return(sigcomp_return(false,
				bn_key, bn_msg, bn_signature));
		}
	}

	return(sigcomp_return(true, bn_key, bn_msg, bn_signature));
}



static bool
sigcomp_return(const bool code,
	BIGNUM *bn_key, BIGNUM *bn_msg, BIGNUM *bn_signature)
{
	if(bn_signature) {
		BN_free(bn_signature); bn_signature = NULL;
	}

	if(bn_msg) {
		BN_free(bn_msg); bn_msg = NULL;
	}

	if(bn_key) {
		BN_free(bn_key); bn_key = NULL;
	}

	return(code);
}



static bool
sigcomp_sign(const EVP_MD *const evp_md, const BIGNUM *const bn_key,
	const BIGNUM *const bn_msg, const char *const type)
{
	EVP_PKEY *pkey			= NULL;
	unsigned char *key		= NULL;
	unsigned char *msg		= NULL;
	unsigned char *signature	= NULL;
	unsigned char *ptr = NULL;

	int keylen		= 0;
	int msglen		= 0;
	int signaturelen	= 0;
	int padding		= 0;

	/* bn_key -> key */
	if(!(keylen = BN_num_bytes(bn_key))) {
		openssl_strerror("BN_num_bytes");
		return(sigcomp_sign_return(false, key, msg, signature, pkey));
	}

	if(!(key = (unsigned char *)malloc(keylen))) {
		fprintf(stderr, "err: malloc: %s\n", strerror(errno));
		return(sigcomp_sign_return(false, key, msg, signature, pkey));
	}

	if(BN_bn2bin(bn_key, key) != keylen) {
		openssl_strerror("BN_bn2bin");
		return(sigcomp_sign_return(false, key, msg, signature, pkey));
	}
#ifdef DEBUG
	dmp(stderr, "key", key, keylen);
#endif	// DEBUG
	/* bn_msg -> msg */
	if(!(msglen = BN_num_bytes(bn_msg))) {
		openssl_strerror("BN_num_bytes");
		return(sigcomp_sign_return(false, key, msg, signature, pkey));
	}

	if(!(msg = (unsigned char *)malloc(msglen))) {
		fprintf(stderr, "err: malloc: %s\n", strerror(errno));
		return(sigcomp_sign_return(false, key, msg, signature, pkey));
	}

	if(BN_bn2bin(bn_msg, msg) != msglen) {
		openssl_strerror("BN_bn2bin");
		return(sigcomp_sign_return(false, key, msg, signature, pkey));
	}
#ifdef DEBUG
	dmp(stderr, "msg", msg, msglen);
#endif	// DEBUG
	ptr = key;

	for( ; ; ) {
		if(!strcmp(type, "rsa")) {
#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090800fL)
			if(!(pkey = d2i_PrivateKey(EVP_PKEY_RSA,
				NULL, (const unsigned char **)&ptr, keylen))) {
#else
			if(!(pkey = d2i_PrivateKey(EVP_PKEY_RSA,
				NULL, &ptr, keylen))) {
#endif
				openssl_strerror("d2i_PrivateKey");
				return(sigcomp_sign_return(false,
					key, msg, signature, pkey));
			}

			signaturelen	= RSA_size(pkey->pkey.rsa);
			padding		= RSA_PKCS1_PADDING;

			break;
		}

		if(!strcmp(type, "dsa")) {
#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090800fL)
			if(!(pkey = d2i_PrivateKey(EVP_PKEY_DSA,
				NULL, (const unsigned char **)&ptr, keylen))) {
#else
			if(!(pkey = d2i_PrivateKey(EVP_PKEY_DSA,
				NULL, &ptr, keylen))) {
#endif

				openssl_strerror("d2i_PrivateKey");
				return(sigcomp_sign_return(false,
					key, msg, signature, pkey));
			}

			fprintf(stderr, "err: type: %s -- not implemented\n",
				type);

			return(sigcomp_sign_return(false,
				key, msg, signature, pkey));

			break;
		}

		fprintf(stderr, "err: type: %s -- unknown type\n",
			type);

		return(sigcomp_sign_return(false, key, msg, signature, pkey));
		/* NOTREACHED */
	}

	if(!(signature = (unsigned char *)malloc(signaturelen))) {
		fprintf(stderr, "err: malloc: %s\n", strerror(errno));
		return(sigcomp_sign_return(false, key, msg, signature, pkey));
	}

	RSA_private_encrypt(msglen, msg, signature, pkey->pkey.rsa, padding);
#ifdef DEBUG
	dmp(stderr, "signature", signature, signaturelen);
#endif	// DEBUG
	sigcomp_sign_dump(key, keylen, msg, msglen, signature, signaturelen);

	return(sigcomp_sign_return(true, key, msg, signature, pkey));
}



static void
sigcomp_sign_dump(const unsigned char *const key, int keylen,
	const unsigned char *const msg, int msglen,
	const unsigned char *const signature, int signaturelen)
{
	int d = 0;

	if(xxxflag) {
#if 0	/* needless */
		for(d = 0; d < msglen; d ++) {
			printf("%02x", msg[d]);
		}
#endif
		for(d = 0; d < signaturelen; d ++) {
			printf("%02x", signature[d]);
		}

		printf("\n");

		return;
	}

	printf("log:SigCompSign_Results             (length:%d)\n",
		keylen + msglen + signaturelen);

	printf("log:| PrivateKey                      (length:%d)\n", keylen);
	printf("log:| | data                             = ");

	for(d = 0; d < keylen; d ++) {
		printf("%02x", key[d]);
	}

	printf("\n");

	printf("log:| Message                         (length:%d)\n", msglen);
	printf("log:| | data                             = ");

	for(d = 0; d < msglen; d ++) {
		printf("%02x", msg[d]);
	}

	printf("\n");

	printf("log:| Signature                       (length:%d)\n",
		signaturelen);

	printf("log:| | data                             = ");

	for(d = 0; d < signaturelen; d ++) {
		printf("%02x", signature[d]);
	}

	printf("\n");

	return;
}



static bool
sigcomp_sign_return(const bool code,
	unsigned char *key, unsigned char *msg, unsigned char *signature,
	EVP_PKEY *pkey)
{
	if(pkey) {
		EVP_PKEY_free(pkey); pkey = NULL;
	}

	if(signature) {
		free(signature); signature = NULL;
	}

	if(msg) {
		free(msg); msg = NULL;
	}

	if(key) {
		free(key); key = NULL;
	}

	return(code);
}



static bool
sigcomp_vrfy(const EVP_MD *const evp_md, const BIGNUM *const bn_key,
	const BIGNUM *const bn_msg,
	const BIGNUM *const bn_signature, const char *const type)
{
	EVP_PKEY *pkey  = NULL;

	unsigned char *key		= NULL;
	unsigned char *msg		= NULL;
	unsigned char *signature	= NULL;
	unsigned char *to		= NULL;
	unsigned char *ptr		= NULL;

	int keylen		= 0;
	int msglen		= 0;
	int signaturelen	= 0;
	int tolen		= 0;
	int padding		= 0;

	bool compare		= true;

	/* bn_key -> key */
	if(!(keylen = BN_num_bytes(bn_key))) {
		openssl_strerror("BN_num_bytes");
		return(sigcomp_vrfy_return(false,
			key, msg, signature, to, pkey));
	}

	if(!(key = (unsigned char *)malloc(keylen))) {
		fprintf(stderr, "err: malloc: %s\n", strerror(errno));
		return(sigcomp_vrfy_return(false,
			key, msg, signature, to, pkey));
	}

	if(BN_bn2bin(bn_key, key) != keylen) {
		openssl_strerror("BN_bn2bin");
		return(sigcomp_vrfy_return(false,
			key, msg, signature, to, pkey));
	}
#ifdef DEBUG
	dmp(stderr, "key", key, keylen);
#endif  // DEBUG
	/* bn_msg -> msg */
        if(!(msglen = BN_num_bytes(bn_msg))) {
		openssl_strerror("BN_num_bytes");
		return(sigcomp_vrfy_return(false,
			key, msg, signature, to, pkey));
        }

	if(!(msg = (unsigned char *)malloc(msglen))) {
		fprintf(stderr, "err: malloc: %s\n", strerror(errno));
		return(sigcomp_vrfy_return(false,
			key, msg, signature, to, pkey));
	}

	if(BN_bn2bin(bn_msg, msg) != msglen) {
		openssl_strerror("BN_bn2bin");
		return(sigcomp_vrfy_return(false,
			key, msg, signature, to, pkey));
	}
#ifdef DEBUG
	dmp(stderr, "msg", msg, msglen);
#endif  // DEBUG
	/* bn_signature -> signature */
        if(!(signaturelen = BN_num_bytes(bn_signature))) {
		openssl_strerror("BN_num_bytes");
		return(sigcomp_vrfy_return(false,
			key, msg, signature, to, pkey));
        }

	if(!(signature = (unsigned char *)malloc(signaturelen))) {
		fprintf(stderr, "err: malloc: %s\n", strerror(errno));
		return(sigcomp_vrfy_return(false,
			key, msg, signature, to, pkey));
	}

	if(BN_bn2bin(bn_signature, signature) != signaturelen) {
		openssl_strerror("BN_bn2bin");
		return(sigcomp_vrfy_return(false,
			key, msg, signature, to, pkey));
	}
#ifdef DEBUG
	dmp(stderr, "signature", signature, signaturelen);
#endif  // DEBUG
        ptr = key;

	for( ; ; ) {
		if(!strcmp(type, "rsa")) {
#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090800fL)
			if(!(pkey = d2i_PublicKey(EVP_PKEY_RSA,
				NULL, (const unsigned char **)&ptr, keylen))) {
#else
			if(!(pkey = d2i_PublicKey(EVP_PKEY_RSA,
				NULL, &ptr, keylen))) {
#endif

				openssl_strerror("d2i_PublicKey");
				return(sigcomp_vrfy_return(false,
					key, msg, signature, to, pkey));
			}

			tolen		= RSA_size(pkey->pkey.rsa);
			padding		= RSA_PKCS1_PADDING;

			break;
		}

		if(!strcmp(type, "dsa")) {
#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090800fL)
			if(!(pkey = d2i_PublicKey(EVP_PKEY_DSA,
				NULL, (const unsigned char **)&ptr, keylen))) {
#else
			if(!(pkey = d2i_PublicKey(EVP_PKEY_DSA,
				NULL, &ptr, keylen))) {
#endif

				openssl_strerror("d2i_PublicKey");
				return(sigcomp_vrfy_return(false,
					key, msg, signature, to, pkey));
			}

			fprintf(stderr, "err: type: %s -- not implemented\n",
				type);

			return(sigcomp_vrfy_return(false,
				key, msg, signature, to, pkey));
			/* NOTREACHED */

			break;
		}

		fprintf(stderr, "err: type: %s -- unknown type\n",
			type);

		return(sigcomp_vrfy_return(false,
			key, msg, signature, to, pkey));
		/* NOTREACHED */
	}

	if(!(to = (unsigned char *)malloc(tolen))) {
		fprintf(stderr, "err: malloc: %s\n", strerror(errno));
		return(sigcomp_vrfy_return(false,
			key, msg, signature, to, pkey));
	}

	if(RSA_public_decrypt(signaturelen,
		signature, to, pkey->pkey.rsa, padding) < 0) {

		openssl_strerror("RSA_public_decrypt");
		return(sigcomp_vrfy_return(true,
			key, msg, signature, to, pkey));
	}

	if(memcmp(msg, to, msglen)) {
		compare	= false;
	}

	sigcomp_vrfy_dump(compare, key, keylen,
		msg, msglen, signature, signaturelen, to, tolen);

	return(sigcomp_vrfy_return(true, key, msg, signature, to, pkey));
}



static void
sigcomp_vrfy_dump(const bool code, const unsigned char *const key, int keylen,
	const unsigned char *const msg, int msglen,
	const unsigned char *const signature, int signaturelen,
	const unsigned char *const to, int tolen)
{
	int d = 0;

	printf("log:SigCompVrfy_Results             (length:%d)\n",
		keylen + msglen + signaturelen + tolen);

	printf("log:| PublicKey                       (length:%d)\n", keylen);
	printf("log:| | data                             = ");

	for(d = 0; d < keylen; d ++) {
		printf("%02x", key[d]);
	}

	printf("\n");

	printf("log:| Message                         (length:%d)\n", msglen);
	printf("log:| | data                             = ");

	for(d = 0; d < msglen; d ++) {
		printf("%02x", msg[d]);
	}

	printf("\n");

	printf("log:| Signature                       (length:%d)\n",
		signaturelen + tolen);

	printf("log:| | status                           = %s\n",
		code? "true": "false");

	printf("log:| | Encrypted                       (length:%d)\n",
		signaturelen);

	printf("log:| | | data                             = ");

	for(d = 0; d < signaturelen; d ++) {
		printf("%02x", signature[d]);
	}

	printf("\n");

	printf("log:| | Decrypted                       (length:%d)\n", tolen);
	printf("log:| | | data                             = ");

	for(d = 0; d < tolen; d ++) {
		printf("%02x", to[d]);
	}

	printf("\n");

	return;
}



static bool
sigcomp_vrfy_return(const bool code,
	unsigned char *key, unsigned char *msg, unsigned char *signature,
	unsigned char *to, EVP_PKEY *pkey)
{
	if(pkey) {
		EVP_PKEY_free(pkey); pkey = NULL;
	}

	if(to) {
		free(to); to = NULL;
	}

	if(signature) {
		free(signature); signature = NULL;
	}

	if(msg) {
		free(msg); msg = NULL;
	}

	if(key) {
		free(key); key = NULL;
	}

	return(code);
}



static const EVP_MD *
evp_md_with_algorithm(const char *const type, const char *const algorithm)
{
	const EVP_MD *evp_md	= NULL;
	bool dsa	= false;
	bool rsa	= false;

	for( ; ; ) {
		if(!strcmp(type, "dsa")) {
			dsa = true;
			break;
		}

		if(!strcmp(type, "rsa")) {
			rsa = true;
			break;
		}

		break;
	}

	for( ; ; ) {
		if(dsa && !strcmp(algorithm, "dss1")) {
			evp_md = EVP_dss1();
			break;
		}

		if(rsa && !strcmp(algorithm, "md2")) {
			evp_md = EVP_md2();
			break;
		}

		if(rsa && !strcmp(algorithm, "md4")) {
			evp_md = EVP_md4();
			break;
		}

		if(rsa && !strcmp(algorithm, "md5")) {
			evp_md = EVP_md5();
			break;
		}

#if ! defined(__linux__)
		if(rsa && !strcmp(algorithm, "mdc2")) {
			evp_md = EVP_mdc2();
			break;
		}
#endif

		if(rsa && !strcmp(algorithm, "ripemd160")) {
			evp_md = EVP_ripemd160();
			break;
		}

		if(rsa && !strcmp(algorithm, "sha1")) {
			evp_md = EVP_sha1();
			break;
		}

		break;
	}

	return(evp_md);
}



static void
openssl_strerror(const char *const label)
{
	unsigned long code = 0;

	while((code = ERR_get_error())) {
		fprintf(stderr, "err: %s: %s\n",
			label, ERR_error_string(code, NULL));
	}

	return;
}