1 /*
2 Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
4
5 You may not use this file except in compliance with the License. You may
6 obtain a copy of the License at
7
8 https://imagemagick.org/script/license.php
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16 #ifndef MAGICK_BYTE_BUFFER_PRIVATE_H
17 #define MAGICK_BYTE_BUFFER_PRIVATE_H
18
19 #include "MagickCore/blob.h"
20 #include "MagickCore/blob-private.h"
21
22 typedef struct _MagickByteBuffer
23 {
24 Image
25 *image;
26
27 ssize_t
28 offset,
29 count;
30
31 unsigned char
32 data[MagickMinBufferExtent];
33 } MagickByteBuffer;
34
ReadMagickByteBuffer(MagickByteBuffer * buffer)35 static inline int ReadMagickByteBuffer(MagickByteBuffer *buffer)
36 {
37 if ((buffer->offset == buffer->count) && (buffer->offset > 0))
38 {
39 if (buffer->count != (ssize_t) sizeof(buffer->data)-1)
40 return(EOF);
41 buffer->offset=0;
42 buffer->count=0;
43 }
44 if ((buffer->offset == 0) && (buffer->count == 0))
45 {
46 buffer->count=ReadBlob(buffer->image,sizeof(buffer->data)-1,
47 buffer->data);
48 if (buffer->count < 1)
49 return(EOF);
50 }
51 return(buffer->data[buffer->offset++]);
52 }
53
GetMagickByteBufferDatum(MagickByteBuffer * buffer)54 static inline char *GetMagickByteBufferDatum(MagickByteBuffer *buffer)
55 {
56 ssize_t
57 count,
58 i;
59
60 if (buffer->offset != 0)
61 {
62 i=0;
63 while (buffer->offset < buffer->count)
64 buffer->data[i++]=buffer->data[buffer->offset++];
65 count=ReadBlob(buffer->image,sizeof(buffer->data)-1-i,buffer->data+i);
66 buffer->count=i;
67 if (count > 0)
68 buffer->count+=count;
69 buffer->offset=0;
70 }
71 return((char *) buffer->data);
72 }
73
CheckMagickByteBuffer(MagickByteBuffer * buffer,const size_t length)74 static void CheckMagickByteBuffer(MagickByteBuffer *buffer,
75 const size_t length)
76 {
77 if ((buffer->offset+length) > (ssize_t) sizeof(buffer->data))
78 (void) GetMagickByteBufferDatum(buffer);
79 }
80
CompareMagickByteBuffer(MagickByteBuffer * buffer,const char * p,const size_t length)81 static MagickBooleanType CompareMagickByteBuffer(MagickByteBuffer *buffer,
82 const char *p,const size_t length)
83 {
84 const char
85 *q;
86
87 CheckMagickByteBuffer(buffer,length);
88 q=(const char *) buffer->data+buffer->offset;
89 if (LocaleNCompare(p,q,length) != 0)
90 return(MagickFalse);
91 return(MagickTrue);
92 }
93
SkipMagickByteBuffer(MagickByteBuffer * buffer,const size_t length)94 static inline void SkipMagickByteBuffer(MagickByteBuffer *buffer,
95 const size_t length)
96 {
97 CheckMagickByteBuffer(buffer,length);
98 if ((ssize_t) (buffer->offset+length) < buffer->count)
99 buffer->offset+=length;
100 }
101
SkipMagickByteBufferUntilNewline(MagickByteBuffer * buffer)102 static inline MagickBooleanType SkipMagickByteBufferUntilNewline(
103 MagickByteBuffer *buffer)
104 {
105 int
106 c;
107
108 c=ReadMagickByteBuffer(buffer);
109 while ((c != '\n') && (c != '\r'))
110 {
111 c=ReadMagickByteBuffer(buffer);
112 if (c == EOF)
113 return(MagickFalse);
114 }
115 return(MagickTrue);
116 }
117
118 #endif
119