コード例 #1
0
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
Note::SetEntry(NotationFile *value)
{
    Check_Object(this);
    Check_Object(value);

    //
    //----------------------------------------------------
    // If this isn't a nested file, write out the filename
    //----------------------------------------------------
    //
    const char* name = value->GetFileName();
    if (name && name[strlen(name)-1] != ']')
        SetEntry(name);

    //
    //-------------------------------------------------------------------------
    // Otherwise, we need to write the notation file out to a memory stream and
    // assign that to the entry
    //-------------------------------------------------------------------------
    //
    else
    {
        DynamicMemoryStream file_buffer(5);
        file_buffer << "{\r\n";
        value->Write(&file_buffer);
        file_buffer << "}" << '\0';
        file_buffer.Rewind();
        SetEntry(static_cast<const char*>(file_buffer.GetPointer()));
        value->IgnoreChanges();
    }
}
コード例 #2
0
ファイル: compress.c プロジェクト: ericfode/radare2
int file_zmagic(RMagic *ms, int fd, const char *name, const ut8 *buf, size_t nbytes) {
	unsigned char *newbuf = NULL;
	size_t i, nsz;
	int rv = 0;
	int mime = ms->flags & R_MAGIC_MIME;

	if ((ms->flags & R_MAGIC_COMPRESS) == 0)
		return 0;

	for (i = 0; i < ncompr; i++) {
		if (nbytes < compr[i].maglen)
			continue;
		if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0 &&
		    (nsz = uncompressbuf(ms, fd, i, buf, &newbuf,
		    nbytes)) != NODATA) {
			ms->flags &= ~R_MAGIC_COMPRESS;
			rv = -1;
			if (file_buffer(ms, -1, name, newbuf, nsz) == -1)
				goto error;

			if (mime == R_MAGIC_MIME || mime == 0) {
				if (file_printf(ms, mime ?
				    " compressed-encoding=" : " (") == -1)
					goto error;
			}

			if ((mime == 0 || mime & R_MAGIC_MIME_ENCODING) &&
			    file_buffer(ms, -1, NULL, buf, nbytes) == -1)
				goto error;

			if (!mime && file_printf(ms, ")") == -1)
				goto error;
			rv = 1;
			break;
		}
	}
error:
	if (newbuf)
		free(newbuf);
	ms->flags |= R_MAGIC_COMPRESS;
	return rv;
}
コード例 #3
0
ファイル: magic.c プロジェクト: EliaGeretto/radare2
R_API const char * r_magic_buffer(RMagic *ms, const void *buf, size_t nb) {
	if (file_reset (ms) == -1)
		return NULL;
	/*
	 * The main work is done here!
	 * We have the file name and/or the data buffer to be identified. 
	 */
	if (file_buffer (ms, -1, NULL, buf, nb) == -1)
		return NULL;
	return file_getbuffer (ms);
}
コード例 #4
0
void file_socket::handle_msg(const file_buffer& str)
{
	if (str.size() <= ORDER_LEN)
	{
		printf("wrong order length: " size_t_format ".\n", str.size());
		return;
	}

	switch (*str.data())
	{
	case 0:
		if (TRANS_IDLE == state)
		{
			trans_end();

			char buffer[ORDER_LEN + DATA_LEN];
			*buffer = 0; //head

			file = fopen(std::next(str.data(), ORDER_LEN), "rb");
			if (nullptr != file)
			{
				fseeko64(file, 0, SEEK_END);
				auto length = ftello64(file);
				memcpy(std::next(buffer, ORDER_LEN), &length, DATA_LEN);
				state = TRANS_PREPARE;
			}
			else
			{
				*(__off64_t*) std::next(buffer, ORDER_LEN) = -1;
				printf("can't not open file %s!\n", std::next(str.data(), ORDER_LEN));
			}

			send_msg(buffer, sizeof(buffer), true);
		}
		break;
	case 1:
		if (TRANS_PREPARE == state && nullptr != file && ORDER_LEN + OFFSET_LEN + DATA_LEN == str.size())
		{
			auto offset = *(__off64_t*) std::next(str.data(), ORDER_LEN);
			auto length = *(__off64_t*) std::next(str.data(), ORDER_LEN + OFFSET_LEN);
			if (offset >= 0 && length > 0 && offset + length <= ftello64(file))
			{
				state = TRANS_BUSY;
				direct_send_msg(file_buffer(boost::make_shared<data_buffer>(file, offset, length)), true);
			}
		}
		break;
	case 2:
		printf("client says: %s\n", std::next(str.data(), ORDER_LEN));
		break;
	default:
		break;
	}
}
コード例 #5
0
ファイル: demo.c プロジェクト: af-inet/gitdex
void print_index(char *filename){
	size_t len;
	unsigned char data[MAX_SIZE];
	
	// Reads a file to a buffer
	if( !(len=file_buffer(filename, data, MAX_SIZE)) )
		die("error: file not found");

	if( gitdex_check(data) ){
		example_stack(data, len);
		//example_heap(data, len);
	}else{
		die("error: not a git index file");
	}
}
コード例 #6
0
bool FolderDialog::doModalOpen(tstring* file_path,HWND wnd_handle,const TCHAR* title,const TCHAR* init_dir){
	bool result=false;
	std::vector<TCHAR> file_buffer(MAX_PATHW);

	::CoInitialize(NULL);
	LPTSTR buffer=NULL;
	LPITEMIDLIST id_root=NULL;
	LPITEMIDLIST item_id_list=NULL;
	LPMALLOC lpmalloc=NULL;
	if(FAILED(::SHGetMalloc(&lpmalloc)))return false;
	if((buffer=(LPTSTR)lpmalloc->Alloc(MAX_PATHW))==NULL)return false;
	if(!SUCCEEDED(::SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOP,&id_root))){
		lpmalloc->Free(buffer);
		::CoUninitialize();
		return false;
	}

	//デスクトップのパスを取得
	SHFILEINFO shFileInfo={};

	::SHGetFileInfo((LPCTSTR)id_root,0,&shFileInfo,sizeof(SHFILEINFO),SHGFI_DISPLAYNAME|SHGFI_PIDL);

	BROWSEINFO browse_info={};
	browse_info.hwndOwner=wnd_handle;
	browse_info.pidlRoot=id_root;
	browse_info.pszDisplayName=buffer;
	browse_info.lpszTitle=(title)?title:_T("フォルダを選択してください");
	//BIF_DONTGOBELOWDOMAINを有効にすると、デスクトップ上のファイルが列挙されてしまう
	browse_info.ulFlags=BIF_USENEWUI|BIF_NONEWFOLDERBUTTON|BIF_RETURNONLYFSDIRS/*|BIF_DONTGOBELOWDOMAIN*/|BIF_VALIDATE;
	browse_info.lpfn=BrowseForFolderCallbackProc;
	browse_info.lParam=reinterpret_cast<LPARAM>((init_dir)?init_dir:shFileInfo.szDisplayName);

	item_id_list=::SHBrowseForFolder(&browse_info);
	if(item_id_list){
		if(::SHGetPathFromIDList(item_id_list,&file_buffer[0])){
			if(file_path!=NULL){
				file_path->assign(&file_buffer[0]);
				result=true;
			}
		}
		lpmalloc->Free(item_id_list);
	}
	lpmalloc->Free(id_root);
	lpmalloc->Free(buffer);
	lpmalloc->Release();
	::CoUninitialize();
	return result;
}
コード例 #7
0
 std::vector<uint8_t> LoadFromFileAndDecompress(const std::string& path)
 {
     std::ifstream input_file(path, std::ios::binary | std::ios::in | std::ios::ate);
     if (!input_file.is_open())
     {
         throw_arc_exception(std::runtime_error, "Couldn't open file " + path);
     }
     std::streamsize size = input_file.tellg();
     input_file.seekg(0, std::ios::beg);
     std::vector<uint8_t> file_buffer((size_t)size);
     if (!(input_file.read(reinterpret_cast<char*>(file_buffer.data()), size)))
     {
         throw_arc_exception(std::runtime_error, "Unable to read entire contents of file");
     }
     const std::vector<uint8_t> decompressed = DecompressBytes(file_buffer);
     return decompressed;
 }
コード例 #8
0
ファイル: magic.c プロジェクト: montekki/radare2
static const char *file_or_fd(RMagic *ms, const char *inname, int fd) {
	int ispipe = 0, rv = -1;
	unsigned char *buf;
	struct stat sb;
	int  nbytes = 0;	/* number of bytes read from a datafile */

	/*
	 * one extra for terminating '\0', and
	 * some overlapping space for matches near EOF
	 */
#define SLOP (1 + sizeof(union VALUETYPE))
	if (!(buf = malloc (HOWMANY + SLOP))) {
		return NULL;
	}

	if (file_reset (ms) == -1) {
		goto done;
	}

	switch (file_fsmagic (ms, inname, &sb)) {
	case -1: goto done;		/* error */
	case 0:	break;			/* nothing found */
	default: rv = 0; goto done;	/* matched it and printed type */
	}

	if (!inname) {
		if (fstat (fd, &sb) == 0 && S_ISFIFO (sb.st_mode)) {
			ispipe = 1;
		}
	} else {
		int flags = O_RDONLY|O_BINARY;

		if (stat (inname, &sb) == 0 && S_ISFIFO (sb.st_mode)) {
#if O_NONBLOCK
			flags |= O_NONBLOCK;
#endif
			ispipe = 1;
		}
		errno = 0;
		if ((fd = open (inname, flags)) < 0) {
			eprintf ("couldn't open file\n");
			if (info_from_stat (ms, sb.st_mode) == -1) {
				goto done;
			}
			rv = 0;
			goto done;
		}
#ifdef O_NONBLOCK
		if ((flags = fcntl (fd, F_GETFL)) != -1) {
			flags &= ~O_NONBLOCK;
			(void)fcntl (fd, F_SETFL, flags);
		}
#endif
	}

	/*
	 * try looking at the first HOWMANY bytes
	 */
#ifdef O_NONBLOCK
	if (ispipe) {
		ssize_t r = 0;

		//while ((r = sread(fd, (void *)&buf[nbytes],
		while ((r = read(fd, (void *)&buf[nbytes],
		    (size_t)(HOWMANY - nbytes))) > 0) {
			nbytes += r;
			if (r < PIPE_BUF) {
				break;
			}
		}

		if (nbytes == 0) {
			/* We can not read it, but we were able to stat it. */
			if (info_from_stat (ms, sb.st_mode) == -1) {
				goto done;
			}
			rv = 0;
			goto done;
		}
	} else {
#endif
		if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
			file_error(ms, errno, "cannot read `%s'", inname);
			goto done;
		}
#ifdef O_NONBLOCK
	}
#endif

	(void)memset (buf + nbytes, 0, SLOP); /* NUL terminate */
	if (file_buffer (ms, fd, inname, buf, (size_t)nbytes) == -1) {
		goto done;
	}
	rv = 0;
done:
	free (buf);
	close_and_restore (ms, inname, fd, &sb);
	return rv == 0 ? file_getbuffer(ms) : NULL;
}
コード例 #9
0
/*
	MAIN()
	------
*/
int main(int argc, char *argv[])
{
static char destination[1024 * 1024];
ANT_memory file_buffer(1024 * 1024);
ANT_instream_file *file_stream;
ANT_instream_bz2 *decompressor;
ANT_instream_buffer *instream_buffer;
ANT_directory_iterator_tar *source;
ANT_directory_iterator_multiple *disk;
ANT_directory_iterator_object file_object, *current_file;
ANT_file outfile;
long param, length;
char *document, *pos, *link, *slash;
long source_id, destination_id;
long documents_processed;
long broken_link;

if (argc < 3)
	exit(usage(argv[0]));

disk = new ANT_directory_iterator_multiple;
for (param = 2; param < argc; param++)
	{
	file_stream = new ANT_instream_file(&file_buffer, argv[param]);
	decompressor = new ANT_instream_bz2(&file_buffer, file_stream);
	instream_buffer = new ANT_instream_buffer(&file_buffer, decompressor);
	source = new ANT_directory_iterator_tar(instream_buffer, ANT_directory_iterator::READ_FILE);
	disk->add_iterator(source);
	}

outfile.open(argv[1], "wb");

documents_processed = 0;
for (current_file = disk->first(&file_object); current_file != NULL; current_file = disk->next(&file_object))
	{
	documents_processed++;
	if ((documents_processed % 1000) == 0)
		fprintf(stderr, "%ld\n", documents_processed);

	document = file_object.file;
	if ((slash = strrchr(file_object.filename, '/')) != NULL)
		{
		source_id = ANT_atol(slash + 1);
		while ((link = strstr(document, "<link")) != NULL)
			{
			if ((pos = strchr(link, '>')) == NULL)
				break;

			pos++;		// include the '>' in the extraction
			length = pos - link < sizeof(destination) - 1 ? pos - link : sizeof(destination) - 1;
			strncpy(destination, link, length);
			destination[length] = '\0';
			if (strstr(destination, "simple") != NULL)
				if ((slash = strchr(destination, '/')) != NULL)
					{
					broken_link = false;
					slash++;
					while (*slash != '/' && *slash != '\0')
						{
						if (!ANT_isdigit(*slash))
							broken_link = true;
						slash++;
						}
#ifdef NEVER
					if (broken_link)
						printf("BROKEN: %s -> %s\n", current_file->filename, destination);
					else
						printf("%s -> %s\n", current_file->filename, destination);
#endif
					
					if (!broken_link && *slash != '\0')
						{
						destination_id = ANT_atol(slash + 1);

						if (source_id != 0 && destination_id != 0)
							{
							outfile.write((unsigned char *)&source_id, sizeof(source_id));
							outfile.write((unsigned char *)&destination_id, sizeof(destination_id));
							}
#ifdef NEVER
						else
							printf("%ld -> %ld %s -> %s\n", source_id, destination_id, current_file->filename, destination);
#endif

						}
					}
			document = pos;
			}
		}
	delete [] current_file->file;
	delete [] current_file->filename;
	}
fprintf(stderr, "Done:%ld\n", documents_processed);
}
コード例 #10
0
ファイル: mypixmap.c プロジェクト: victorbarna/xfwm4_tiling
/* This function does all the work. */
static GdkPixbuf *
pixbuf_create_from_xpm (gpointer handle, xfwmColorSymbol *color_sym)
{
    gchar pixel_str[32];
    const gchar *buffer;
    gchar *name_buf;
    gint w, h, n_col, cpp, items;
    gint cnt, xcnt, ycnt, wbytes, n;
    GHashTable *color_hash;
    XPMColor *colors, *color, *fallbackcolor;
    guchar *pixtmp;
    GdkPixbuf *pixbuf;

    fallbackcolor = NULL;

    buffer = file_buffer (op_header, handle);
    if (!buffer)
    {
        g_warning ("Cannot read Pixmap header");
        return NULL;
    }
    items = sscanf (buffer, "%d %d %d %d", &w, &h, &n_col, &cpp);

    if (items != 4)
    {
        g_warning ("Pixmap definition contains invalid number attributes (expecting at least 4, got %i)", items);
        return NULL;
    }

    if ((w <= 0) ||
            (h <= 0) ||
            (cpp <= 0) ||
            (cpp >= 32) ||
            (n_col <= 0) ||
            (n_col >= G_MAXINT / (cpp + 1)) ||
            (n_col >= G_MAXINT / (gint) sizeof (XPMColor)))
    {
        g_warning ("Pixmap definition contains invalid attributes");
        return NULL;
    }

    /* The hash is used for fast lookups of color from chars */
    color_hash = g_hash_table_new (g_str_hash, g_str_equal);

    name_buf = g_try_malloc (n_col * (cpp + 1));
    if (!name_buf) {
        g_hash_table_destroy (color_hash);
        g_warning ("Cannot allocate buffer");
        return NULL;
    }

    colors = (XPMColor *) g_try_malloc (sizeof (XPMColor) * n_col);
    if (!colors)
    {
        g_hash_table_destroy (color_hash);
        g_free (name_buf);
        g_warning ("Cannot allocate colors for Pixmap");
        return NULL;
    }

    for (cnt = 0; cnt < n_col; cnt++)
    {
        gchar *color_name;

        buffer = file_buffer (op_cmap, handle);
        if (!buffer)
        {
            g_hash_table_destroy (color_hash);
            g_free (name_buf);
            g_free (colors);
            g_warning ("Cannot load colormap attributes");
            return NULL;
        }

        color = &colors[cnt];
        color->color_string = &name_buf[cnt * (cpp + 1)];
        strncpy (color->color_string, buffer, cpp);
        color->color_string[cpp] = 0;
        buffer += strlen (color->color_string);
        color->transparent = FALSE;

        color_name = xpm_extract_color (buffer, color_sym);

        if ((color_name == NULL) ||
                (g_ascii_strcasecmp (color_name, "None") == 0) ||
                (parse_color (color_name, color) == FALSE))
        {
            color->transparent = TRUE;
            color->red = 0;
            color->green = 0;
            color->blue = 0;
        }

        g_free (color_name);
        g_hash_table_insert (color_hash, color->color_string, color);

        if (cnt == 0)
        {
            fallbackcolor = color;
        }
    }

    pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);

    if (!pixbuf)
    {
        g_hash_table_destroy (color_hash);
        g_free (colors);
        g_free (name_buf);
        g_warning ("Cannot allocate Pixbuf");
        return NULL;
    }

    wbytes = w * cpp;

    for (ycnt = 0; ycnt < h; ycnt++)
    {
        pixtmp = gdk_pixbuf_get_pixels (pixbuf) + ycnt * gdk_pixbuf_get_rowstride(pixbuf);

        buffer = file_buffer (op_body, handle);
        if ((!buffer) || (wbytes > (gint) strlen (buffer)))
        {
            continue;
        }

        for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++)
        {
            strncpy (pixel_str, &buffer[n], cpp);
            pixel_str[cpp] = 0;

            color = g_hash_table_lookup (color_hash, pixel_str);

            /* Bad XPM...punt */
            if (!color)
            {
                color = fallbackcolor;
            }

            *pixtmp++ = color->red   >> 8;
            *pixtmp++ = color->green >> 8;
            *pixtmp++ = color->blue  >> 8;

            if (color->transparent)
            {
                *pixtmp++ = 0;
            }
            else
            {
                *pixtmp++ = 0xFF;
            }
        }
    }

    g_hash_table_destroy (color_hash);
    g_free (colors);
    g_free (name_buf);

    return pixbuf;
}