From: Dirk Lemstra Date: Thu, 11 Jul 2019 18:30:18 +0000 (+0200) Subject: Moved byte buffer code to a separate header so it can be used in other coders. X-Git-Tag: 7.0.8-54~65 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=641953ec26a3c4b72b72b087fbbd101157cdbf00;p=imagemagick Moved byte buffer code to a separate header so it can be used in other coders. --- diff --git a/coders/byte-buffer-private.h b/coders/byte-buffer-private.h new file mode 100644 index 000000000..919c546d3 --- /dev/null +++ b/coders/byte-buffer-private.h @@ -0,0 +1,89 @@ +/* + Copyright 1999-2019 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 + obtain a copy of the License at + + https://imagemagick.org/script/license.php + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +#ifndef MAGICK_BYTE_BUFFER_PRIVATE_H +#define MAGICK_BYTE_BUFFER_PRIVATE_H + +typedef struct _ByteBuffer +{ + Image + *image; + + ssize_t + offset, + count; + + unsigned char + data[8192]; +} ByteBuffer; + +static int ReadByteBuffer(ByteBuffer *buffer) +{ + if ((buffer->offset == buffer->count) && (buffer->offset > 0)) + { + if (buffer->count != (ssize_t) sizeof(buffer->data)) + return(EOF); + buffer->offset=0; + } + if (buffer->offset == 0) + { + buffer->count=ReadBlob(buffer->image,sizeof(buffer->data),buffer->data); + if (buffer->count < 1) + return(EOF); + } + return(buffer->data[buffer->offset++]); +} + +static char *GetByteBufferDatum(ByteBuffer *buffer) +{ + ssize_t + i; + + i=1; /* Skip first to avoid reload of buffer; */ + while (buffer->offset < buffer->count) + buffer->data[i++]=buffer->data[buffer->offset++]; + buffer->count=ReadBlob(buffer->image,sizeof(buffer->data)-i,buffer->data+i); + buffer->count+=i; + buffer->offset=1; + return((char *) buffer->data+1); +} + +static inline void CheckRemainingByteBuffer(ByteBuffer *buffer,size_t length) +{ + if ((buffer->offset+length) > (ssize_t) sizeof(buffer->data)) + (void) GetByteBufferDatum(buffer); +} + +static inline void SkipByteBuffer(ByteBuffer *buffer,size_t length) +{ + CheckRemainingByteBuffer(buffer,length); + if ((buffer->offset+length) < buffer->count) + buffer->offset+=length; +} + +static MagickBooleanType CompareByteBuffer(const char *p,ByteBuffer *buffer, + const size_t length) +{ + const char + *q; + + CheckRemainingByteBuffer(buffer,length); + q=(const char *) buffer->data+buffer->offset; + if (LocaleNCompare(p,q,length) != 0) + return(MagickFalse); + return(MagickTrue); +} + +#endif \ No newline at end of file diff --git a/coders/pdf.c b/coders/pdf.c index 52fae6b92..a00d38987 100644 --- a/coders/pdf.c +++ b/coders/pdf.c @@ -81,6 +81,7 @@ #include "MagickCore/transform.h" #include "MagickCore/utility.h" #include "MagickCore/module.h" +#include "byte-buffer-private.h" /* Define declarations. @@ -110,19 +111,6 @@ typedef struct _PDFInfo StringInfo *profile; } PDFInfo; - -typedef struct _PDFBuffer -{ - Image - *image; - - ssize_t - offset, - count; - - unsigned char - data[8192]; -} PDFBuffer; /* Forward declarations. @@ -404,64 +392,7 @@ static MagickBooleanType IsPDFRendered(const char *path) return(MagickFalse); } -static inline int ReadPDFByte(PDFBuffer *buffer) -{ - if ((buffer->offset == buffer->count) && (buffer->offset > 0)) - { - if (buffer->count != (ssize_t) sizeof(buffer->data)) - return(EOF); - buffer->offset=0; - } - if (buffer->offset == 0) - { - buffer->count=ReadBlob(buffer->image,sizeof(buffer->data),buffer->data); - if (buffer->count < 1) - return(EOF); - } - return(buffer->data[buffer->offset++]); -} - -static char *MovePDFBuffer(PDFBuffer *buffer) -{ - ssize_t - i; - - i=1; /* Skip first to avoid reload of buffer; */ - while (buffer->offset < buffer->count) - buffer->data[i++]=buffer->data[buffer->offset++]; - buffer->count=ReadBlob(buffer->image,sizeof(buffer->data)-i,buffer->data+i); - buffer->count+=i; - buffer->offset=1; - return((char *) buffer->data+1); -} - -static inline void CheckRemainingPDFBuffer(PDFBuffer *buffer,size_t length) -{ - if ((buffer->offset+length) > (ssize_t) sizeof(buffer->data)) - (void) MovePDFBuffer(buffer); -} - -static inline void SkipPDFBytes(PDFBuffer *buffer,size_t length) -{ - CheckRemainingPDFBuffer(buffer,length); - if ((buffer->offset+length) < buffer->count) - buffer->offset+=length; -} - -static inline MagickBooleanType ComparePDFBuffer(const char *p, - PDFBuffer *buffer,const size_t length) -{ - const char - *q; - - CheckRemainingPDFBuffer(buffer,length); - q=(const char *) buffer->data+buffer->offset; - if (LocaleNCompare(p,q,length) != 0) - return(MagickFalse); - return(MagickTrue); -} - -static void ReadPDFXMPProfile(PDFInfo *pdf_info,PDFBuffer *buffer) +static void ReadPDFXMPProfile(PDFInfo *pdf_info,ByteBuffer *buffer) { #define BeginXMPPacket "?xpacket begin=" #define EndXMPPacket "profile != (StringInfo *) NULL) return; - if (ComparePDFBuffer(BeginXMPPacket,buffer,strlen(BeginXMPPacket)) == MagickFalse) + if (CompareByteBuffer(BeginXMPPacket,buffer,strlen(BeginXMPPacket)) == MagickFalse) return; length=8192; pdf_info->profile=AcquireStringInfo(length); @@ -491,7 +422,7 @@ static void ReadPDFXMPProfile(PDFInfo *pdf_info,PDFBuffer *buffer) p=(char *) GetStringInfoDatum(pdf_info->profile); *p++='<'; count=1; - for (c=ReadPDFByte(buffer); c != EOF; c=ReadPDFByte(buffer)) + for (c=ReadByteBuffer(buffer); c != EOF; c=ReadByteBuffer(buffer)) { if (count == (ssize_t) length) { @@ -502,7 +433,7 @@ static void ReadPDFXMPProfile(PDFInfo *pdf_info,PDFBuffer *buffer) count++; *p++=(char) c; if (found_end == MagickFalse) - found_end=ComparePDFBuffer(EndXMPPacket,buffer,strlen(EndXMPPacket)); + found_end=CompareByteBuffer(EndXMPPacket,buffer,strlen(EndXMPPacket)); else { if (c == (int) '>') @@ -531,7 +462,7 @@ static void ReadPDFInfo(const ImageInfo *image_info,Image *image, int c; - PDFBuffer + ByteBuffer buffer; register char @@ -559,7 +490,7 @@ static void ReadPDFInfo(const ImageInfo *image_info,Image *image, spotcolor=0; (void) memset(&buffer,0,sizeof(buffer)); buffer.image=image; - for (c=ReadPDFByte(&buffer); c != EOF; c=ReadPDFByte(&buffer)) + for (c=ReadByteBuffer(&buffer); c != EOF; c=ReadByteBuffer(&buffer)) { switch(c) { @@ -568,7 +499,7 @@ static void ReadPDFInfo(const ImageInfo *image_info,Image *image, if (*version == '\0') { i=0; - for (c=ReadPDFByte(&buffer); c != EOF; c=ReadPDFByte(&buffer)) + for (c=ReadByteBuffer(&buffer); c != EOF; c=ReadByteBuffer(&buffer)) { if ((c == '\r') || (c == '\n') || ((i+1) == MagickPathExtent)) break; @@ -588,22 +519,22 @@ static void ReadPDFInfo(const ImageInfo *image_info,Image *image, default: continue; } - if (ComparePDFBuffer(PDFRotate,&buffer,strlen(PDFRotate)) != MagickFalse) + if (CompareByteBuffer(PDFRotate,&buffer,strlen(PDFRotate)) != MagickFalse) { - p=MovePDFBuffer(&buffer); + p=GetByteBufferDatum(&buffer); (void) sscanf(p,PDFRotate" %lf",&pdf_info->angle); } if (pdf_info->cmyk == MagickFalse) { - if ((ComparePDFBuffer(DefaultCMYK,&buffer,strlen(DefaultCMYK)) != MagickFalse) || - (ComparePDFBuffer(DeviceCMYK,&buffer,strlen(DeviceCMYK)) != MagickFalse) || - (ComparePDFBuffer(CMYKProcessColor,&buffer,strlen(CMYKProcessColor)) != MagickFalse)) + if ((CompareByteBuffer(DefaultCMYK,&buffer,strlen(DefaultCMYK)) != MagickFalse) || + (CompareByteBuffer(DeviceCMYK,&buffer,strlen(DeviceCMYK)) != MagickFalse) || + (CompareByteBuffer(CMYKProcessColor,&buffer,strlen(CMYKProcessColor)) != MagickFalse)) { pdf_info->cmyk=MagickTrue; continue; } } - if (ComparePDFBuffer(SpotColor,&buffer,strlen(SpotColor)) != MagickFalse) + if (CompareByteBuffer(SpotColor,&buffer,strlen(SpotColor)) != MagickFalse) { char name[MagickPathExtent], @@ -616,8 +547,8 @@ static void ReadPDFInfo(const ImageInfo *image_info,Image *image, (void) FormatLocaleString(property,MagickPathExtent, "pdf:SpotColor-%.20g",(double) spotcolor++); i=0; - SkipPDFBytes(&buffer,strlen(SpotColor)+1); - for (c=ReadPDFByte(&buffer); c != EOF; c=ReadPDFByte(&buffer)) + SkipByteBuffer(&buffer,strlen(SpotColor)+1); + for (c=ReadByteBuffer(&buffer); c != EOF; c=ReadByteBuffer(&buffer)) { if ((isspace(c) != 0) || (c == '/') || ((i+1) == MagickPathExtent)) break; @@ -636,12 +567,12 @@ static void ReadPDFInfo(const ImageInfo *image_info,Image *image, count=0; if (pdf_info->cropbox != MagickFalse) { - if (ComparePDFBuffer(CropBox,&buffer,strlen(CropBox)) != MagickFalse) + if (CompareByteBuffer(CropBox,&buffer,strlen(CropBox)) != MagickFalse) { /* Note region defined by crop box. */ - p=MovePDFBuffer(&buffer); + p=GetByteBufferDatum(&buffer); count=(ssize_t) sscanf(p,"CropBox [%lf %lf %lf %lf",&bounds.x1, &bounds.y1,&bounds.x2,&bounds.y2); if (count != 4) @@ -652,12 +583,12 @@ static void ReadPDFInfo(const ImageInfo *image_info,Image *image, else if (pdf_info->trimbox != MagickFalse) { - if (ComparePDFBuffer(TrimBox,&buffer,strlen(TrimBox)) != MagickFalse) + if (CompareByteBuffer(TrimBox,&buffer,strlen(TrimBox)) != MagickFalse) { /* Note region defined by trim box. */ - p=MovePDFBuffer(&buffer); + p=GetByteBufferDatum(&buffer); count=(ssize_t) sscanf(p,"TrimBox [%lf %lf %lf %lf",&bounds.x1, &bounds.y1,&bounds.x2,&bounds.y2); if (count != 4) @@ -666,12 +597,12 @@ static void ReadPDFInfo(const ImageInfo *image_info,Image *image, } } else - if (ComparePDFBuffer(MediaBox,&buffer,strlen(MediaBox)) != MagickFalse) + if (CompareByteBuffer(MediaBox,&buffer,strlen(MediaBox)) != MagickFalse) { /* Note region defined by media box. */ - p=MovePDFBuffer(&buffer); + p=GetByteBufferDatum(&buffer); count=(ssize_t) sscanf(p,"MediaBox [%lf %lf %lf %lf",&bounds.x1, &bounds.y1,&bounds.x2,&bounds.y2); if (count != 4)