% MagickCore Binary Large OBjectS Methods %
% %
% Software Design %
-% John Cristy %
+% Cristy %
% July 1999 %
% %
% %
-% Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
+% Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
% dedicated to making software imaging solutions freely available. %
% %
% You may not use this file except in compliance with the License. You may %
Include declarations.
*/
#include "MagickCore/studio.h"
-#include "MagickCore/nt-base-private.h"
#include "MagickCore/blob.h"
#include "MagickCore/blob-private.h"
#include "MagickCore/cache.h"
#include "MagickCore/log.h"
#include "MagickCore/magick.h"
#include "MagickCore/memory_.h"
+#include "MagickCore/nt-base-private.h"
#include "MagickCore/policy.h"
#include "MagickCore/resource_.h"
#include "MagickCore/semaphore.h"
magick_info=GetMagickInfo(blob_info->magick,exception);
if (magick_info == (const MagickInfo *) NULL)
{
- blob_info=DestroyImageInfo(blob_info);
(void) ThrowMagickException(exception,GetMagickModule(),
MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
- image_info->filename);
+ blob_info->magick);
+ blob_info=DestroyImageInfo(blob_info);
return((Image *) NULL);
}
if (GetMagickBlobSupport(magick_info) != MagickFalse)
*images;
/*
- Restore original filenames.
+ Restore original filenames and image format.
*/
for (images=GetFirstImageInList(image); images != (Image *) NULL; )
{
- (void) CopyMagickMemory(images->filename,image_info->filename,
- sizeof(images->filename));
- (void) CopyMagickMemory(images->magick_filename,image_info->filename,
- sizeof(images->magick_filename));
+ (void) CopyMagickString(images->filename,image_info->filename,
+ MaxTextExtent);
+ (void) CopyMagickString(images->magick_filename,image_info->filename,
+ MaxTextExtent);
+ (void) CopyMagickString(images->magick,magick_info->name,
+ MaxTextExtent);
images=GetNextImageInList(images);
}
}
RelinquishMagickResource(MapResource,image->blob->length);
}
if (image->blob->semaphore != (SemaphoreInfo *) NULL)
- DestroySemaphoreInfo(&image->blob->semaphore);
+ RelinquishSemaphoreInfo(&image->blob->semaphore);
image->blob->signature=(~MagickSignature);
image->blob=(BlobInfo *) RelinquishMagickMemory(image->blob);
}
if (blob_info->mapped != MagickFalse)
{
(void) UnmapBlob(blob_info->data,blob_info->length);
+ blob_info->data=(unsigned char *) NULL;
RelinquishMagickResource(MapResource,blob_info->length);
}
blob_info->mapped=MagickFalse;
}
offset=(MagickOffsetType) lseek(file,0,SEEK_END);
count=0;
- if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
+ if ((file == fileno(stdin)) || (offset < 0) ||
+ (offset != (MagickOffsetType) ((ssize_t) offset)))
{
size_t
quantum;
/*
Stream is not seekable.
*/
+ offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
quantum=(size_t) MagickMaxBufferExtent;
if ((fstat(file,&file_stats) == 0) && (file_stats.st_size != 0))
quantum=(size_t) MagickMin((MagickSizeType) file_stats.st_size,
extent=(MagickSizeType) (image->blob->offset+(MagickOffsetType) length);
if (extent >= image->blob->extent)
{
- image->blob->quantum<<=1;
extent=image->blob->extent+image->blob->quantum+length;
+ image->blob->quantum<<=1;
if (SetBlobExtent(image,extent) == MagickFalse)
return(0);
}
blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
if (blob == (unsigned char *) NULL)
{
+ file=close(file);
ThrowFileException(exception,ResourceLimitError,"MemoryAllocationFailed",
filename);
return(MagickFalse);
blob_info->properties.st_ctime=time((time_t *) NULL);
blob_info->debug=IsEventLogging();
blob_info->reference_count=1;
- blob_info->semaphore=AllocateSemaphoreInfo();
+ blob_info->semaphore=AcquireSemaphoreInfo();
blob_info->signature=MagickSignature;
}
\f
%
% o image: the image.
%
-% o length: This pointer to a size_t integer sets the initial length of the
-% blob. On return, it reflects the actual length of the blob.
+% o length: return the actual length of the blob.
%
% o exception: return any errors or warnings in this structure.
%
{
(void) ThrowMagickException(exception,GetMagickModule(),
MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
- image->filename);
+ image->magick);
+ blob_info=DestroyImageInfo(blob_info);
return(blob);
}
(void) CopyMagickString(blob_info->magick,image->magick,MaxTextExtent);
(void) FormatLocaleString(image->filename,MaxTextExtent,"%s:%s",
image->magick,unique);
status=WriteImage(blob_info,image,exception);
+ (void) CloseBlob(image);
(void) fclose(blob_info->file);
if (status != MagickFalse)
- blob=FileToBlob(image->filename,~0UL,length,exception);
+ blob=FileToBlob(unique,~0UL,length,exception);
}
(void) RelinquishUniqueFileResource(unique);
}
buffer=(unsigned char *) RelinquishMagickMemory(buffer);
if ((file == -1) || (i < length))
{
+ if (file != -1)
+ file=close(file);
ThrowFileException(exception,BlobError,"UnableToWriteBlob",filename);
return(MagickFalse);
}
%
% o images: the image list.
%
-% o length: This pointer to a size_t integer sets the initial length of the
-% blob. On return, it reflects the actual length of the blob.
+% o length: return the actual length of the blob.
%
% o exception: return any errors or warnings in this structure.
%
exception);
if (*blob_info->magick != '\0')
(void) CopyMagickString(images->magick,blob_info->magick,MaxTextExtent);
- if (blob_info->adjoin == MagickFalse)
- {
- blob_info=DestroyImageInfo(blob_info);
- return(ImageToBlob(image_info,images,length,exception));
- }
magick_info=GetMagickInfo(images->magick,exception);
if (magick_info == (const MagickInfo *) NULL)
{
(void) ThrowMagickException(exception,GetMagickModule(),
MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
- images->filename);
+ images->magick);
+ blob_info=DestroyImageInfo(blob_info);
return(blob);
}
+ if (GetMagickAdjoin(magick_info) == MagickFalse)
+ {
+ blob_info=DestroyImageInfo(blob_info);
+ return(ImageToBlob(image_info,images,length,exception));
+ }
(void) CopyMagickString(blob_info->magick,images->magick,MaxTextExtent);
if (GetMagickBlobSupport(magick_info) != MagickFalse)
{
ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
else
{
+ (void) CloseBlob(images);
images->blob->exempt=MagickTrue;
*images->filename='\0';
status=WriteImages(blob_info,images,images->filename,exception);
- if ((status != MagickFalse) && (images->blob->length != 0))
- {
- *length=images->blob->length;
- blob=DetachBlob(images->blob);
- blob=(unsigned char *) ResizeQuantumMemory(blob,*length,
- sizeof(*blob));
- }
+ *length=images->blob->length;
+ blob=DetachBlob(images->blob);
+ if (status == MagickFalse)
+ blob=(unsigned char *) RelinquishMagickMemory(blob);
+ else
+ blob=(unsigned char *) ResizeQuantumMemory(blob,*length+1,
+ sizeof(*blob));
}
}
else
(void) FormatLocaleString(filename,MaxTextExtent,"%s:%s",
images->magick,unique);
status=WriteImages(blob_info,images,filename,exception);
+ (void) CloseBlob(images);
(void) fclose(blob_info->file);
if (status != MagickFalse)
- blob=FileToBlob(images->filename,~0UL,length,exception);
+ blob=FileToBlob(unique,~0UL,length,exception);
}
(void) RelinquishUniqueFileResource(unique);
}
MagickExport unsigned char *MapBlob(int file,const MapMode mode,
const MagickOffsetType offset,const size_t length)
{
-#if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
+#if defined(MAGICKCORE_HAVE_MMAP)
int
flags,
protection;
if (file == -1)
#if defined(MAP_ANONYMOUS)
flags|=MAP_ANONYMOUS;
-#if defined(MAGICKCORE_HAVE_HUGEPAGES) && defined(MAP_HUGETLB)
- flags|=MAP_HUGETLB;
-#endif
#else
return((unsigned char *) NULL);
#endif
{
protection=PROT_READ;
flags|=MAP_PRIVATE;
- map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
- (off_t) offset);
break;
}
case WriteMode:
{
protection=PROT_WRITE;
flags|=MAP_SHARED;
- map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
- (off_t) offset);
-#if defined(MAGICKCORE_HAVE_POSIX_MADVISE)
- (void) posix_madvise(map,length,POSIX_MADV_SEQUENTIAL |
- POSIX_MADV_WILLNEED);
-#endif
break;
}
case IOMode:
{
protection=PROT_READ | PROT_WRITE;
flags|=MAP_SHARED;
- map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
- (off_t) offset);
break;
}
}
+#if !defined(MAGICKCORE_HAVE_HUGEPAGES) || !defined(MAP_HUGETLB)
+ map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
+ (off_t) offset);
+#else
+ map=(unsigned char *) mmap((char *) NULL,length,protection,flags |
+ MAP_HUGETLB,file,(off_t) offset);
+ if (map == (unsigned char *) MAP_FAILED)
+ map=(unsigned char *) mmap((char *) NULL,length,protection,flags,file,
+ (off_t) offset);
+#endif
if (map == (unsigned char *) MAP_FAILED)
return((unsigned char *) NULL);
return(map);
#endif
status=GetPathAttributes(filename,&image->blob->properties);
#if defined(S_ISFIFO)
- if ((status == MagickTrue) && S_ISFIFO(image->blob->properties.st_mode))
+ if ((status != MagickFalse) && S_ISFIFO(image->blob->properties.st_mode))
{
image->blob->file_info.file=(FILE *) fopen_utf8(filename,type);
if (image->blob->file_info.file == (FILE *) NULL)
if (((int) magick[0] == 0x1F) && ((int) magick[1] == 0x8B) &&
((int) magick[2] == 0x08))
{
- (void) fclose(image->blob->file_info.file);
+ if (image->blob->file_info.file != (FILE *) NULL)
+ (void) fclose(image->blob->file_info.file);
+ image->blob->file_info.file=(FILE *) NULL;
image->blob->file_info.gzfile=gzopen(filename,type);
if (image->blob->file_info.gzfile != (gzFile) NULL)
image->blob->type=ZipStream;
#if defined(MAGICKCORE_BZLIB_DELEGATE)
if (strncmp((char *) magick,"BZh",3) == 0)
{
- (void) fclose(image->blob->file_info.file);
+ if (image->blob->file_info.file != (FILE *) NULL)
+ (void) fclose(image->blob->file_info.file);
+ image->blob->file_info.file=(FILE *) NULL;
image->blob->file_info.bzfile=BZ2_bzopen(filename,type);
if (image->blob->file_info.bzfile != (BZFILE *) NULL)
image->blob->type=BZipStream;
return(-1);
case FileStream:
{
+ if ((whence == SEEK_SET) && (offset < 0))
+ return(-1);
if (fseek(image->blob->file_info.file,offset,whence) < 0)
return(-1);
image->blob->offset=TellBlob(image);
{
image->blob->extent=(size_t) (image->blob->offset+
image->blob->quantum);
+ image->blob->quantum<<=1;
image->blob->data=(unsigned char *) ResizeQuantumMemory(
image->blob->data,image->blob->extent+1,
sizeof(*image->blob->data));
*/
MagickExport MagickBooleanType UnmapBlob(void *map,const size_t length)
{
-#if defined(MAGICKCORE_HAVE_MMAP_FILEIO)
+#if defined(MAGICKCORE_HAVE_MMAP)
int
status;
{
if (image->blob->mapped != MagickFalse)
return(0);
- image->blob->quantum<<=1;
image->blob->extent+=length+image->blob->quantum;
+ image->blob->quantum<<=1;
image->blob->data=(unsigned char *) ResizeQuantumMemory(
image->blob->data,image->blob->extent+1,sizeof(*image->blob->data));
(void) SyncBlob(image);