コード例 #1
0
ファイル: vncfunc.c プロジェクト: libvirt/libvirt-php
/*
 * Private function name:   vnc_raw_to_bitmap
 * Since version:           0.4.5
 * Description:             Function to get the bitmap from raw encoding
 * Arguments:               @infile [string]: input file
 *                          @outfile [string]: output file
 *                          @width [int]: width of the output image (for bitmap headers)
 *                          @height [int]: height of the output image (for bitmap headers)
 * Returns:                 0 on success, -errno otherwise
 */
static int
vnc_raw_to_bmp(char *infile, char *outfile, int width, int height)
{
    int i, ix, fd, fd2;
    tBMPFile fBMP = { 0 };
    long size = -1;
    long len, hsize = 0;
    uint32_t *pixels = NULL;
    unsigned char buf[8192] = { 0 };
    unsigned char tbuf[4] = { 0 };
    long total = 0;
    int start, end;

    fd = open(infile, O_RDONLY);
    if (fd == -1)
        return -EACCES;

    size = lseek(fd, 0, SEEK_END);
    lseek(fd, 0, SEEK_SET);

    hsize = sizeof(tBMPFile);
    fBMP.filesz = size + hsize + 2;
    fBMP.bmp_offset = hsize + 2;
    fBMP.header_sz = 40;
    fBMP.height = width;
    fBMP.width = height;
    fBMP.nplanes = 1;
    fBMP.bitspp = 32;
    fBMP.compress_type = 0;
    fBMP.bmp_bytesz = 32;
    fBMP.hres = 2835;
    fBMP.vres = 2835;
    fBMP.ncolors = 0;
    fBMP.nimpcolors = 0;

    fd2 = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd2 == -1)
        return -EPERM;

    if (write(fd2, "BM", 2) < 0 ||
        write(fd2, &fBMP, hsize) < 0)
        perror("Error on write");

    ix = 0;
    pixels = malloc(width * height * sizeof(uint32_t));
    if (pixels == NULL)
        return -ENOMEM;

    total = 0;
    while ((len = read(fd, buf, sizeof(buf))) > 0) {
        for (i = 0; i < len; i += 4) {
            tbuf[0] = buf[i];
            tbuf[1] = buf[i + 1];
            tbuf[2] = buf[i + 2];
            tbuf[3] = buf[i + 3];
            pixels[ix++] = GETUINT32(tbuf);

            total++;
        }

        memset(buf, 0, sizeof(buf));
    }

    /* Flip the image to get the real image */
    for (i = height - 1; i >= 0; i--) {
        start = (i * width) + 1;
        end = ((i + 1) * width) + 1;

        for (ix = start; ix < end; ix++) {
            UINT32STR(tbuf, pixels[ix]);
            if (write(fd2, tbuf, 4) < 0)
                perror("Error on write");
        }
    }

    VIR_FREE(pixels);
    close(fd2);
    close(fd);
    return 0;
}
コード例 #2
0
ファイル: archive.c プロジェクト: MigNov/archiver
int archive_write_header(int fd, tArchiveFile *files, int numFiles)
{
	int i, ret = 0;
	char *name = NULL;
	unsigned char dataByte[1] = { 0 }, dataWord[2] = { 0 };
	unsigned char data32[4] = { 0 }, data64[8] = { 0 };

	write(fd, SIGNATURE, sizeof(SIGNATURE));
	ret = sizeof(SIGNATURE);

	WORDSTR(dataWord, ARCHIVER_VERSION);
	write(fd, dataWord, sizeof(dataWord));
	ret += sizeof(dataWord);

	/* Extra header size */
	WORDSTR(dataWord, 0);
	write(fd, dataWord, sizeof(dataWord));
	ret += sizeof(dataWord);

	WORDSTR(dataWord, numFiles);
	write(fd, dataWord, sizeof(dataWord));
	ret += sizeof(dataWord);

	WORDSTR(dataWord, 0x0000);
	write(fd, dataWord, sizeof(dataWord));
	ret += sizeof(dataWord);

	for (i = 0; i < numFiles; i++) {
		BYTESTR(dataByte, strlen(files[i].name));
		write(fd, dataByte, sizeof(dataByte));
		ret += sizeof(dataByte);

		name = strdup( basename(files[i].name) );
		write(fd, name, strlen(name));
		ret += strlen(name);
		free(name);

		UINT32STR(data32, files[i].crc);
		write(fd, data32, sizeof(data32));
		ret += sizeof(data32);

		UINT64STR(data64, files[i].size);
		write(fd, data64, sizeof(data64));
		ret += sizeof(data64);

		UINT64STR(data64, files[i].compressed_size);
		write(fd, data64, sizeof(data64));
		ret += sizeof(data64);

		BYTESTR(dataByte, files[i].encrypted ? 1 : 0);
		write(fd, dataByte, sizeof(dataByte));
		ret += sizeof(dataByte);

		WORDSTR(dataWord, 0x4e45);
		write(fd, dataWord, sizeof(dataWord));
		ret += sizeof(dataWord);
	}
	WORDSTR(dataWord, 0x4e46);
	write(fd, dataWord, sizeof(dataWord));
	ret += sizeof(dataWord);

	return ret;
}