Exemplo n.º 1
0
Arquivo: creator.c Projeto: gcc-o/PMSI
int main(int argc, char *argv[])
{
	struct image_t img_1, img_2;
	int diff;

	image_read(argv[1], &img_1);
	if(argv[2][0] == '1')
		image_lsbr_rndcode(&img_1, 0.01);

	image_clone(&img_1, &img_2);
	image_plusone(&img_2);
	image_offset(&img_1);
	image_offset(&img_2);

	image_write("image_ideal.bmp", &img_1);
	image_write("image_plusone.bmp", &img_2);
	return 0;
}
Exemplo n.º 2
0
int loop_bind(FILE *image_fp, char **loop_dev, int autoclear) {
    struct loop_info64 lo64 = {0};
    int i;

    message(DEBUG, "Called loop_bind(image_fp, **{loop_dev)\n");

    if ( autoclear > 0 ) {
        lo64.lo_flags = LO_FLAGS_AUTOCLEAR;
    }
    lo64.lo_offset = image_offset(image_fp);

    for( i=0; i < MAX_LOOP_DEVS; i++ ) {
        char *test_loopdev = strjoin("/dev/loop", int2str(i));
        FILE *loop_fp;

        if ( is_blk(test_loopdev) < 0 ) {
            message(VERBOSE, "Creating loop device: %s\n", test_loopdev);
            if ( mknod(test_loopdev, S_IFBLK | 0644, makedev(7, i)) < 0 ) {
                message(ERROR, "Could not create %s: %s\n", test_loopdev, strerror(errno));
                ABORT(255);
            }
        }

        if ( ( loop_fp = fopen(test_loopdev, "r+") ) == NULL ) { // Flawfinder: ignore (not user modifyable)
            message(VERBOSE, "Could not open loop device %s: %s\n", test_loopdev, strerror(errno));
            continue;
        }

        message(VERBOSE2, "Attempting to associate image pointer to loop device\n");
        if ( ioctl(fileno(loop_fp), LOOP_SET_FD, fileno(image_fp)) < 0 ) {
            if ( errno == 16 ) {
                message(VERBOSE3, "Loop device is in use: %s\n", test_loopdev);
                fclose(loop_fp);
                continue;
            } else {
                message(WARNING, "Could not associate image to loop %s: %s\n", test_loopdev, strerror(errno));
                fclose(loop_fp);
                continue;
            }
        }

        message(VERBOSE, "Found valid loop device: %s\n", test_loopdev);

        message(VERBOSE2, "Setting loop device flags\n");
        if ( ioctl(fileno(loop_fp), LOOP_SET_STATUS64, &lo64) < 0 ) {
            fprintf(stderr, "ERROR: Failed to set loop flags on loop device: %s\n", strerror(errno));
            (void)ioctl(fileno(loop_fp), LOOP_CLR_FD, 0);
            (void)loop_free(*loop_dev);
            ABORT(255);
        }
        *loop_dev = strdup(test_loopdev);

        message(VERBOSE, "Using loop device: %s\n", *loop_dev);

        message(DEBUG, "Returning loop_bind(image_fp) = 0\n");

        return(0);
    }

    message(ERROR, "No valid loop devices available\n");
    ABORT(255);

    return(-1);
}