Esempio n. 1
0
void squeeze2(char s1[] /* mutable string only */, char* s2 /* mutable or immutable string would do */)
{
    // create a binary hash out of (unique) characters from s2
    Bool binaryhash[NOOFCHARS] = {0};
    createhash(binaryhash, s2);

    // in a single iteration on the length of the source string, copy all the characters which are not present in the binary hash back into the source string.
    int i, j;
    char x;
    for(i = j = 0; x = *(s1 + i); i++)
        if(!*(binaryhash + x))
            *(s1 + j++) = x;
    *(s1 + j) = '\0';
}
Esempio n. 2
0
int
main(int argc, char **argv)
{
	int ch, version = 0;
	extern char build_info[];
	struct hashinfo *hashinfo = 0;

	while ((ch = getopt(argc, argv, "cdvhnD:")) != -1)
		switch(ch) {
		case 'c':
			create++;
			break;
		case 'D':
			if (strcmp(optarg, "md5") == 0)
				hashtype = HASH_TYPE_MD5;
			else if (strcmp(optarg, "sha1") == 0)
				hashtype = HASH_TYPE_SHA1;
			else {
				fprintf(stderr, "Invalid digest type `%s'\n",
					optarg);
				usage();
			}
			break;
		case 'd':
			detail++;
			break;
		case 'n':
			nothreads++;
			break;
		case 'v':
			version++;
			break;
		case 'h':
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (version || detail) {
		fprintf(stderr, "%s\n", build_info);
		if (version)
			exit(0);
	}

	if ((create && argc < 1) || (!create && argc < 2))
		usage();

	/*
	 * Ensure we can open both files before we do the expensive stuff.
	 */
	if (strcmp(argv[0], "-") != 0 && access(argv[0], R_OK) != 0) {
		perror("image file");
		exit(1);
	}
	if (!create && access(argv[1], R_OK) != 0) {
		perror("device file");
		exit(1);
	}

	/*
	 * Create a hash file
	 */
	if (create) {
		if (createhash(argv[0], &hashinfo))
			exit(2);
		dumphash(argv[0], hashinfo);
		exit(0);
	}

	/*
	 * Compare the hash file versus a device
	 */
	if (gethashinfo(argv[0], &hashinfo))
		exit(2);
	dumphash(argv[0], hashinfo);
	(void) checkhash(argv[1], hashinfo);

	exit(0);
}