/src/libtiff/libtiff/tif_write.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 1988-1997 Sam Leffler |
3 | | * Copyright (c) 1991-1997 Silicon Graphics, Inc. |
4 | | * |
5 | | * Permission to use, copy, modify, distribute, and sell this software and |
6 | | * its documentation for any purpose is hereby granted without fee, provided |
7 | | * that (i) the above copyright notices and this permission notice appear in |
8 | | * all copies of the software and related documentation, and (ii) the names of |
9 | | * Sam Leffler and Silicon Graphics may not be used in any advertising or |
10 | | * publicity relating to the software without the specific, prior written |
11 | | * permission of Sam Leffler and Silicon Graphics. |
12 | | * |
13 | | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
14 | | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
15 | | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
16 | | * |
17 | | * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
18 | | * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
19 | | * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
20 | | * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
21 | | * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
22 | | * OF THIS SOFTWARE. |
23 | | */ |
24 | | |
25 | | /* |
26 | | * TIFF Library. |
27 | | * |
28 | | * Scanline-oriented Write Support |
29 | | */ |
30 | | #include "tiffiop.h" |
31 | | #include <stdio.h> |
32 | | |
33 | 47.2k | #define NOSTRIP ((uint32_t)(-1)) /* undefined state */ |
34 | | |
35 | | #define WRITECHECKSTRIPS(tif, module) \ |
36 | 13.8M | (((tif)->tif_flags & TIFF_BEENWRITING) || TIFFWriteCheck((tif), 0, module)) |
37 | | #define WRITECHECKTILES(tif, module) \ |
38 | 0 | (((tif)->tif_flags & TIFF_BEENWRITING) || TIFFWriteCheck((tif), 1, module)) |
39 | | #define BUFFERCHECK(tif) \ |
40 | 13.8M | ((((tif)->tif_flags & TIFF_BUFFERSETUP) && tif->tif_rawdata) || \ |
41 | 13.8M | TIFFWriteBufferSetup((tif), NULL, (tmsize_t)(-1))) |
42 | | |
43 | | static int TIFFGrowStrips(TIFF *tif, uint32_t delta, const char *module); |
44 | | static int TIFFAppendToStrip(TIFF *tif, uint32_t strip, uint8_t *data, |
45 | | tmsize_t cc); |
46 | | |
47 | | int TIFFWriteScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample) |
48 | 13.8M | { |
49 | 13.8M | static const char module[] = "TIFFWriteScanline"; |
50 | 13.8M | TIFFDirectory *td; |
51 | 13.8M | int status, imagegrew = 0; |
52 | 13.8M | uint32_t strip; |
53 | | |
54 | 13.8M | if (!WRITECHECKSTRIPS(tif, module)) |
55 | 0 | return (-1); |
56 | | /* |
57 | | * Handle delayed allocation of data buffer. This |
58 | | * permits it to be sized more intelligently (using |
59 | | * directory information). |
60 | | */ |
61 | 13.8M | if (!BUFFERCHECK(tif)) |
62 | 0 | return (-1); |
63 | 13.8M | tif->tif_flags |= TIFF_BUF4WRITE; /* not strictly sure this is right*/ |
64 | | |
65 | 13.8M | td = &tif->tif_dir; |
66 | | /* |
67 | | * Extend image length if needed |
68 | | * (but only for PlanarConfig=1). |
69 | | */ |
70 | 13.8M | if (row >= td->td_imagelength) |
71 | 0 | { /* extend image */ |
72 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
73 | 0 | { |
74 | 0 | TIFFErrorExtR( |
75 | 0 | tif, module, |
76 | 0 | "Can not change \"ImageLength\" when using separate planes"); |
77 | 0 | return (-1); |
78 | 0 | } |
79 | 0 | td->td_imagelength = row + 1; |
80 | 0 | imagegrew = 1; |
81 | 0 | } |
82 | | /* |
83 | | * Calculate strip and check for crossings. |
84 | | */ |
85 | 13.8M | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
86 | 847k | { |
87 | 847k | if (sample >= td->td_samplesperpixel) |
88 | 0 | { |
89 | 0 | TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu", |
90 | 0 | (unsigned long)sample, |
91 | 0 | (unsigned long)td->td_samplesperpixel); |
92 | 0 | return (-1); |
93 | 0 | } |
94 | 847k | strip = sample * td->td_stripsperimage + row / td->td_rowsperstrip; |
95 | 847k | } |
96 | 13.0M | else |
97 | 13.0M | strip = row / td->td_rowsperstrip; |
98 | | /* |
99 | | * Check strip array to make sure there's space. We don't support |
100 | | * dynamically growing files that have data organized in separate |
101 | | * bitplanes because it's too painful. In that case we require that |
102 | | * the imagelength be set properly before the first write (so that the |
103 | | * strips array will be fully allocated above). |
104 | | */ |
105 | 13.8M | if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module)) |
106 | 0 | return (-1); |
107 | 13.8M | if (strip != tif->tif_dir.td_curstrip) |
108 | 47.2k | { |
109 | | /* |
110 | | * Changing strips -- flush any data present. |
111 | | */ |
112 | 47.2k | if (!TIFFFlushData(tif)) |
113 | 31 | return (-1); |
114 | 47.2k | tif->tif_dir.td_curstrip = strip; |
115 | | /* |
116 | | * Watch out for a growing image. The value of strips/image |
117 | | * will initially be 1 (since it can't be deduced until the |
118 | | * imagelength is known). |
119 | | */ |
120 | 47.2k | if (strip >= td->td_stripsperimage && imagegrew) |
121 | 0 | td->td_stripsperimage = |
122 | 0 | TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip); |
123 | 47.2k | if (td->td_stripsperimage == 0) |
124 | 0 | { |
125 | 0 | TIFFErrorExtR(tif, module, "Zero strips per image"); |
126 | 0 | return (-1); |
127 | 0 | } |
128 | 47.2k | tif->tif_dir.td_row = |
129 | 47.2k | (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
130 | 47.2k | if ((tif->tif_flags & TIFF_CODERSETUP) == 0) |
131 | 31.7k | { |
132 | 31.7k | if (!(*tif->tif_setupencode)(tif)) |
133 | 0 | return (-1); |
134 | 31.7k | tif->tif_flags |= TIFF_CODERSETUP; |
135 | 31.7k | } |
136 | | |
137 | 47.2k | tif->tif_rawcc = 0; |
138 | 47.2k | tif->tif_rawcp = tif->tif_rawdata; |
139 | | |
140 | | /* this informs TIFFAppendToStrip() we have changed strip */ |
141 | 47.2k | tif->tif_curoff = 0; |
142 | | |
143 | 47.2k | if (!(*tif->tif_preencode)(tif, sample)) |
144 | 0 | return (-1); |
145 | 47.2k | tif->tif_flags |= TIFF_POSTENCODE; |
146 | 47.2k | } |
147 | | /* |
148 | | * Ensure the write is either sequential or at the |
149 | | * beginning of a strip (or that we can randomly |
150 | | * access the data -- i.e. no encoding). |
151 | | */ |
152 | 13.8M | if (row != tif->tif_dir.td_row) |
153 | 0 | { |
154 | 0 | if (row < tif->tif_dir.td_row) |
155 | 0 | { |
156 | | /* |
157 | | * Moving backwards within the same strip: |
158 | | * backup to the start and then decode |
159 | | * forward (below). |
160 | | */ |
161 | 0 | tif->tif_dir.td_row = |
162 | 0 | (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
163 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
164 | 0 | } |
165 | | /* |
166 | | * Seek forward to the desired row. |
167 | | */ |
168 | 0 | if (!(*tif->tif_seek)(tif, row - tif->tif_dir.td_row)) |
169 | 0 | return (-1); |
170 | 0 | tif->tif_dir.td_row = row; |
171 | 0 | } |
172 | | |
173 | | /* swab if needed - note that source buffer will be altered */ |
174 | 13.8M | tif->tif_postdecode(tif, (uint8_t *)buf, tif->tif_dir.td_scanlinesize); |
175 | | |
176 | 13.8M | status = (*tif->tif_encoderow)(tif, (uint8_t *)buf, |
177 | 13.8M | tif->tif_dir.td_scanlinesize, sample); |
178 | | |
179 | | /* we are now poised at the beginning of the next row */ |
180 | 13.8M | tif->tif_dir.td_row = row + 1; |
181 | 13.8M | return (status); |
182 | 13.8M | } |
183 | | |
184 | | /* Make sure that at the first attempt of rewriting a tile/strip, we will have |
185 | | */ |
186 | | /* more bytes available in the output buffer than the previous byte count, */ |
187 | | /* so that TIFFAppendToStrip() will detect the overflow when it is called the |
188 | | * first */ |
189 | | /* time if the new compressed tile is bigger than the older one. (GDAL #4771) */ |
190 | | static int _TIFFReserveLargeEnoughWriteBuffer(TIFF *tif, uint32_t strip_or_tile) |
191 | 0 | { |
192 | 0 | static const char module[] = "_TIFFReserveLargeEnoughWriteBuffer"; |
193 | 0 | TIFFDirectory *td = &tif->tif_dir; |
194 | |
|
195 | 0 | if (td->td_stripbytecount_p == NULL) |
196 | 0 | { |
197 | 0 | TIFFErrorExtR(tif, module, "Strip bytecount array pointer is NULL"); |
198 | 0 | return 0; |
199 | 0 | } |
200 | | |
201 | 0 | if (strip_or_tile == NOSTRIP || strip_or_tile >= td->td_nstrips) |
202 | 0 | { |
203 | 0 | TIFFErrorExtR(tif, module, "Strip/tile number not valid"); |
204 | 0 | return 0; |
205 | 0 | } |
206 | | |
207 | 0 | if (td->td_stripbytecount_p[strip_or_tile] > 0) |
208 | 0 | { |
209 | | /* The +1 is to ensure at least one extra bytes */ |
210 | | /* The +4 is because the LZW encoder flushes 4 bytes before the limit */ |
211 | 0 | uint64_t safe_buffer_size = |
212 | 0 | (uint64_t)(td->td_stripbytecount_p[strip_or_tile] + 1 + 4); |
213 | 0 | if (tif->tif_rawdatasize <= (tmsize_t)safe_buffer_size) |
214 | 0 | { |
215 | 0 | if (!(TIFFWriteBufferSetup( |
216 | 0 | tif, NULL, |
217 | 0 | (tmsize_t)TIFFroundup_64(safe_buffer_size, 1024)))) |
218 | 0 | return 0; |
219 | 0 | } |
220 | 0 | } |
221 | 0 | return 1; |
222 | 0 | } |
223 | | |
224 | | /* |
225 | | * Encode the supplied data and write it to the |
226 | | * specified strip. |
227 | | * |
228 | | * NB: Image length must be setup before writing. |
229 | | */ |
230 | | tmsize_t TIFFWriteEncodedStrip(TIFF *tif, uint32_t strip, void *data, |
231 | | tmsize_t cc) |
232 | 0 | { |
233 | 0 | static const char module[] = "TIFFWriteEncodedStrip"; |
234 | 0 | TIFFDirectory *td = &tif->tif_dir; |
235 | 0 | uint16_t sample; |
236 | |
|
237 | 0 | if (!WRITECHECKSTRIPS(tif, module)) |
238 | 0 | return ((tmsize_t)-1); |
239 | | /* |
240 | | * Check strip array to make sure there's space. |
241 | | * We don't support dynamically growing files that |
242 | | * have data organized in separate bitplanes because |
243 | | * it's too painful. In that case we require that |
244 | | * the imagelength be set properly before the first |
245 | | * write (so that the strips array will be fully |
246 | | * allocated above). |
247 | | */ |
248 | 0 | if (strip >= td->td_nstrips) |
249 | 0 | { |
250 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
251 | 0 | { |
252 | 0 | TIFFErrorExtR( |
253 | 0 | tif, module, |
254 | 0 | "Can not grow image by strips when using separate planes"); |
255 | 0 | return ((tmsize_t)-1); |
256 | 0 | } |
257 | 0 | if (!TIFFGrowStrips(tif, 1, module)) |
258 | 0 | return ((tmsize_t)-1); |
259 | 0 | td->td_stripsperimage = |
260 | 0 | TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip); |
261 | 0 | } |
262 | | /* |
263 | | * Handle delayed allocation of data buffer. This |
264 | | * permits it to be sized according to the directory |
265 | | * info. |
266 | | */ |
267 | 0 | if (!BUFFERCHECK(tif)) |
268 | 0 | return ((tmsize_t)-1); |
269 | | |
270 | 0 | tif->tif_flags |= TIFF_BUF4WRITE; |
271 | |
|
272 | 0 | tif->tif_dir.td_curstrip = strip; |
273 | | |
274 | | /* this informs TIFFAppendToStrip() we have changed or reset strip */ |
275 | 0 | tif->tif_curoff = 0; |
276 | |
|
277 | 0 | if (!_TIFFReserveLargeEnoughWriteBuffer(tif, strip)) |
278 | 0 | { |
279 | 0 | return ((tmsize_t)(-1)); |
280 | 0 | } |
281 | | |
282 | 0 | tif->tif_rawcc = 0; |
283 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
284 | |
|
285 | 0 | if (td->td_stripsperimage == 0) |
286 | 0 | { |
287 | 0 | TIFFErrorExtR(tif, module, "Zero strips per image"); |
288 | 0 | return ((tmsize_t)-1); |
289 | 0 | } |
290 | | |
291 | 0 | tif->tif_dir.td_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
292 | 0 | if ((tif->tif_flags & TIFF_CODERSETUP) == 0) |
293 | 0 | { |
294 | 0 | if (!(*tif->tif_setupencode)(tif)) |
295 | 0 | return ((tmsize_t)-1); |
296 | 0 | tif->tif_flags |= TIFF_CODERSETUP; |
297 | 0 | } |
298 | | |
299 | 0 | tif->tif_flags &= ~TIFF_POSTENCODE; |
300 | | |
301 | | /* shortcut to avoid an extra memcpy() */ |
302 | 0 | if (td->td_compression == COMPRESSION_NONE) |
303 | 0 | { |
304 | | /* swab if needed - note that source buffer will be altered */ |
305 | 0 | tif->tif_postdecode(tif, (uint8_t *)data, cc); |
306 | |
|
307 | 0 | if (!isFillOrder(tif, td->td_fillorder) && |
308 | 0 | (tif->tif_flags & TIFF_NOBITREV) == 0) |
309 | 0 | TIFFReverseBits((uint8_t *)data, cc); |
310 | |
|
311 | 0 | if (cc > 0 && !TIFFAppendToStrip(tif, strip, (uint8_t *)data, cc)) |
312 | 0 | return ((tmsize_t)-1); |
313 | 0 | return (cc); |
314 | 0 | } |
315 | | |
316 | 0 | sample = (uint16_t)(strip / td->td_stripsperimage); |
317 | 0 | if (!(*tif->tif_preencode)(tif, sample)) |
318 | 0 | return ((tmsize_t)-1); |
319 | | |
320 | | /* swab if needed - note that source buffer will be altered */ |
321 | 0 | tif->tif_postdecode(tif, (uint8_t *)data, cc); |
322 | |
|
323 | 0 | if (!(*tif->tif_encodestrip)(tif, (uint8_t *)data, cc, sample)) |
324 | 0 | return ((tmsize_t)-1); |
325 | 0 | if (!(*tif->tif_postencode)(tif)) |
326 | 0 | return ((tmsize_t)-1); |
327 | 0 | if (!isFillOrder(tif, td->td_fillorder) && |
328 | 0 | (tif->tif_flags & TIFF_NOBITREV) == 0) |
329 | 0 | TIFFReverseBits(tif->tif_rawdata, tif->tif_rawcc); |
330 | 0 | if (tif->tif_rawcc > 0 && |
331 | 0 | !TIFFAppendToStrip(tif, strip, tif->tif_rawdata, tif->tif_rawcc)) |
332 | 0 | return ((tmsize_t)-1); |
333 | 0 | tif->tif_rawcc = 0; |
334 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
335 | 0 | return (cc); |
336 | 0 | } |
337 | | |
338 | | /* |
339 | | * Write the supplied data to the specified strip. |
340 | | * |
341 | | * NB: Image length must be setup before writing. |
342 | | */ |
343 | | tmsize_t TIFFWriteRawStrip(TIFF *tif, uint32_t strip, void *data, tmsize_t cc) |
344 | 0 | { |
345 | 0 | static const char module[] = "TIFFWriteRawStrip"; |
346 | 0 | TIFFDirectory *td = &tif->tif_dir; |
347 | |
|
348 | 0 | if (!WRITECHECKSTRIPS(tif, module)) |
349 | 0 | return ((tmsize_t)-1); |
350 | | /* |
351 | | * Check strip array to make sure there's space. |
352 | | * We don't support dynamically growing files that |
353 | | * have data organized in separate bitplanes because |
354 | | * it's too painful. In that case we require that |
355 | | * the imagelength be set properly before the first |
356 | | * write (so that the strips array will be fully |
357 | | * allocated above). |
358 | | */ |
359 | 0 | if (strip >= td->td_nstrips) |
360 | 0 | { |
361 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
362 | 0 | { |
363 | 0 | TIFFErrorExtR( |
364 | 0 | tif, module, |
365 | 0 | "Can not grow image by strips when using separate planes"); |
366 | 0 | return ((tmsize_t)-1); |
367 | 0 | } |
368 | | /* |
369 | | * Watch out for a growing image. The value of |
370 | | * strips/image will initially be 1 (since it |
371 | | * can't be deduced until the imagelength is known). |
372 | | */ |
373 | 0 | if (strip >= td->td_stripsperimage) |
374 | 0 | td->td_stripsperimage = |
375 | 0 | TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip); |
376 | 0 | if (!TIFFGrowStrips(tif, 1, module)) |
377 | 0 | return ((tmsize_t)-1); |
378 | 0 | } |
379 | | |
380 | 0 | if (tif->tif_dir.td_curstrip != strip) |
381 | 0 | { |
382 | 0 | tif->tif_dir.td_curstrip = strip; |
383 | | |
384 | | /* this informs TIFFAppendToStrip() we have changed or reset strip */ |
385 | 0 | tif->tif_curoff = 0; |
386 | 0 | } |
387 | |
|
388 | 0 | if (td->td_stripsperimage == 0) |
389 | 0 | { |
390 | 0 | TIFFErrorExtR(tif, module, "Zero strips per image"); |
391 | 0 | return ((tmsize_t)-1); |
392 | 0 | } |
393 | 0 | tif->tif_dir.td_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
394 | 0 | return (TIFFAppendToStrip(tif, strip, (uint8_t *)data, cc) ? cc |
395 | 0 | : (tmsize_t)-1); |
396 | 0 | } |
397 | | |
398 | | /* |
399 | | * Write and compress a tile of data. The |
400 | | * tile is selected by the (x,y,z,s) coordinates. |
401 | | */ |
402 | | tmsize_t TIFFWriteTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z, |
403 | | uint16_t s) |
404 | 0 | { |
405 | 0 | if (!TIFFCheckTile(tif, x, y, z, s)) |
406 | 0 | return ((tmsize_t)(-1)); |
407 | | /* |
408 | | * NB: A tile size of -1 is used instead of tif_tilesize knowing |
409 | | * that TIFFWriteEncodedTile will clamp this to the tile size. |
410 | | * This is done because the tile size may not be defined until |
411 | | * after the output buffer is setup in TIFFWriteBufferSetup. |
412 | | */ |
413 | 0 | return (TIFFWriteEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf, |
414 | 0 | (tmsize_t)(-1))); |
415 | 0 | } |
416 | | |
417 | | /* |
418 | | * Encode the supplied data and write it to the |
419 | | * specified tile. There must be space for the |
420 | | * data. The function clamps individual writes |
421 | | * to a tile to the tile size, but does not (and |
422 | | * can not) check that multiple writes to the same |
423 | | * tile do not write more than tile size data. |
424 | | * |
425 | | * NB: Image length must be setup before writing; this |
426 | | * interface does not support automatically growing |
427 | | * the image on each write (as TIFFWriteScanline does). |
428 | | */ |
429 | | tmsize_t TIFFWriteEncodedTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc) |
430 | 0 | { |
431 | 0 | static const char module[] = "TIFFWriteEncodedTile"; |
432 | 0 | TIFFDirectory *td; |
433 | 0 | uint16_t sample; |
434 | 0 | uint32_t howmany32; |
435 | |
|
436 | 0 | if (!WRITECHECKTILES(tif, module)) |
437 | 0 | return ((tmsize_t)(-1)); |
438 | 0 | td = &tif->tif_dir; |
439 | 0 | if (tile >= td->td_nstrips) |
440 | 0 | { |
441 | 0 | TIFFErrorExtR(tif, module, "Tile %lu out of range, max %lu", |
442 | 0 | (unsigned long)tile, (unsigned long)td->td_nstrips); |
443 | 0 | return ((tmsize_t)(-1)); |
444 | 0 | } |
445 | | /* |
446 | | * Handle delayed allocation of data buffer. This |
447 | | * permits it to be sized more intelligently (using |
448 | | * directory information). |
449 | | */ |
450 | 0 | if (!BUFFERCHECK(tif)) |
451 | 0 | return ((tmsize_t)(-1)); |
452 | | |
453 | 0 | tif->tif_flags |= TIFF_BUF4WRITE; |
454 | |
|
455 | 0 | tif->tif_dir.td_curtile = tile; |
456 | | |
457 | | /* this informs TIFFAppendToStrip() we have changed or reset tile */ |
458 | 0 | tif->tif_curoff = 0; |
459 | |
|
460 | 0 | if (!_TIFFReserveLargeEnoughWriteBuffer(tif, tile)) |
461 | 0 | { |
462 | 0 | return ((tmsize_t)(-1)); |
463 | 0 | } |
464 | | |
465 | 0 | tif->tif_rawcc = 0; |
466 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
467 | | |
468 | | /* |
469 | | * Compute tiles per row & per column to compute |
470 | | * current row and column |
471 | | */ |
472 | 0 | howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength); |
473 | 0 | if (howmany32 == 0) |
474 | 0 | { |
475 | 0 | TIFFErrorExtR(tif, module, "Zero tiles"); |
476 | 0 | return ((tmsize_t)(-1)); |
477 | 0 | } |
478 | 0 | tif->tif_dir.td_row = (tile % howmany32) * td->td_tilelength; |
479 | 0 | howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth); |
480 | 0 | if (howmany32 == 0) |
481 | 0 | { |
482 | 0 | TIFFErrorExtR(tif, module, "Zero tiles"); |
483 | 0 | return ((tmsize_t)(-1)); |
484 | 0 | } |
485 | 0 | tif->tif_dir.td_col = (tile % howmany32) * td->td_tilewidth; |
486 | |
|
487 | 0 | if ((tif->tif_flags & TIFF_CODERSETUP) == 0) |
488 | 0 | { |
489 | 0 | if (!(*tif->tif_setupencode)(tif)) |
490 | 0 | return ((tmsize_t)(-1)); |
491 | 0 | tif->tif_flags |= TIFF_CODERSETUP; |
492 | 0 | } |
493 | 0 | tif->tif_flags &= ~TIFF_POSTENCODE; |
494 | | |
495 | | /* |
496 | | * Clamp write amount to the tile size. This is mostly |
497 | | * done so that callers can pass in some large number |
498 | | * (e.g. -1) and have the tile size used instead. |
499 | | */ |
500 | 0 | if (cc < 1 || cc > tif->tif_dir.td_tilesize) |
501 | 0 | cc = tif->tif_dir.td_tilesize; |
502 | | |
503 | | /* shortcut to avoid an extra memcpy() */ |
504 | 0 | if (td->td_compression == COMPRESSION_NONE) |
505 | 0 | { |
506 | | /* swab if needed - note that source buffer will be altered */ |
507 | 0 | tif->tif_postdecode(tif, (uint8_t *)data, cc); |
508 | |
|
509 | 0 | if (!isFillOrder(tif, td->td_fillorder) && |
510 | 0 | (tif->tif_flags & TIFF_NOBITREV) == 0) |
511 | 0 | TIFFReverseBits((uint8_t *)data, cc); |
512 | |
|
513 | 0 | if (cc > 0 && !TIFFAppendToStrip(tif, tile, (uint8_t *)data, cc)) |
514 | 0 | return ((tmsize_t)-1); |
515 | 0 | return (cc); |
516 | 0 | } |
517 | | |
518 | 0 | sample = (uint16_t)(tile / td->td_stripsperimage); |
519 | 0 | if (!(*tif->tif_preencode)(tif, sample)) |
520 | 0 | return ((tmsize_t)(-1)); |
521 | | /* swab if needed - note that source buffer will be altered */ |
522 | 0 | tif->tif_postdecode(tif, (uint8_t *)data, cc); |
523 | |
|
524 | 0 | if (!(*tif->tif_encodetile)(tif, (uint8_t *)data, cc, sample)) |
525 | 0 | return ((tmsize_t)-1); |
526 | 0 | if (!(*tif->tif_postencode)(tif)) |
527 | 0 | return ((tmsize_t)(-1)); |
528 | 0 | if (!isFillOrder(tif, td->td_fillorder) && |
529 | 0 | (tif->tif_flags & TIFF_NOBITREV) == 0) |
530 | 0 | TIFFReverseBits((uint8_t *)tif->tif_rawdata, tif->tif_rawcc); |
531 | 0 | if (tif->tif_rawcc > 0 && |
532 | 0 | !TIFFAppendToStrip(tif, tile, tif->tif_rawdata, tif->tif_rawcc)) |
533 | 0 | return ((tmsize_t)(-1)); |
534 | 0 | tif->tif_rawcc = 0; |
535 | 0 | tif->tif_rawcp = tif->tif_rawdata; |
536 | 0 | return (cc); |
537 | 0 | } |
538 | | |
539 | | /* |
540 | | * Write the supplied data to the specified strip. |
541 | | * There must be space for the data; we don't check |
542 | | * if strips overlap! |
543 | | * |
544 | | * NB: Image length must be setup before writing; this |
545 | | * interface does not support automatically growing |
546 | | * the image on each write (as TIFFWriteScanline does). |
547 | | */ |
548 | | tmsize_t TIFFWriteRawTile(TIFF *tif, uint32_t tile, void *data, tmsize_t cc) |
549 | 0 | { |
550 | 0 | static const char module[] = "TIFFWriteRawTile"; |
551 | |
|
552 | 0 | if (!WRITECHECKTILES(tif, module)) |
553 | 0 | return ((tmsize_t)(-1)); |
554 | 0 | if (tile >= tif->tif_dir.td_nstrips) |
555 | 0 | { |
556 | 0 | TIFFErrorExtR(tif, module, "Tile %lu out of range, max %lu", |
557 | 0 | (unsigned long)tile, |
558 | 0 | (unsigned long)tif->tif_dir.td_nstrips); |
559 | 0 | return ((tmsize_t)(-1)); |
560 | 0 | } |
561 | 0 | return (TIFFAppendToStrip(tif, tile, (uint8_t *)data, cc) ? cc |
562 | 0 | : (tmsize_t)(-1)); |
563 | 0 | } |
564 | | |
565 | | #define isUnspecified(tif, f) \ |
566 | 34.3k | (TIFFFieldSet(tif, f) && (tif)->tif_dir.td_imagelength == 0) |
567 | | |
568 | | int TIFFSetupStrips(TIFF *tif) |
569 | 34.3k | { |
570 | 34.3k | TIFFDirectory *td = &tif->tif_dir; |
571 | | |
572 | 34.3k | if (isTiled(tif)) |
573 | 0 | td->td_stripsperimage = isUnspecified(tif, FIELD_TILEDIMENSIONS) |
574 | 0 | ? td->td_samplesperpixel |
575 | 0 | : TIFFNumberOfTiles(tif); |
576 | 34.3k | else |
577 | 34.3k | td->td_stripsperimage = isUnspecified(tif, FIELD_ROWSPERSTRIP) |
578 | 34.3k | ? td->td_samplesperpixel |
579 | 34.3k | : TIFFNumberOfStrips(tif); |
580 | 34.3k | td->td_nstrips = td->td_stripsperimage; |
581 | | /* TIFFWriteDirectoryTagData has a limitation to 0x80000000U bytes */ |
582 | 34.3k | if (td->td_nstrips >= |
583 | 34.3k | 0x80000000U / ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U)) |
584 | 0 | { |
585 | 0 | TIFFErrorExtR(tif, "TIFFSetupStrips", |
586 | 0 | "Too large Strip/Tile Offsets/ByteCounts arrays"); |
587 | 0 | return 0; |
588 | 0 | } |
589 | 34.3k | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
590 | 2.06k | td->td_stripsperimage /= td->td_samplesperpixel; |
591 | | |
592 | 34.3k | if (td->td_stripoffset_p != NULL) |
593 | 0 | _TIFFfreeExt(tif, td->td_stripoffset_p); |
594 | 34.3k | td->td_stripoffset_p = (uint64_t *)_TIFFCheckMalloc( |
595 | 34.3k | tif, td->td_nstrips, sizeof(uint64_t), "for \"StripOffsets\" array"); |
596 | 34.3k | if (td->td_stripbytecount_p != NULL) |
597 | 0 | _TIFFfreeExt(tif, td->td_stripbytecount_p); |
598 | 34.3k | td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc( |
599 | 34.3k | tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array"); |
600 | 34.3k | if (td->td_stripoffset_p == NULL || td->td_stripbytecount_p == NULL) |
601 | 0 | return (0); |
602 | | /* |
603 | | * Place data at the end-of-file |
604 | | * (by setting offsets to zero). |
605 | | */ |
606 | 34.3k | _TIFFmemset(td->td_stripoffset_p, 0, |
607 | 34.3k | (tmsize_t)((size_t)td->td_nstrips * sizeof(uint64_t))); |
608 | 34.3k | _TIFFmemset(td->td_stripbytecount_p, 0, |
609 | 34.3k | (tmsize_t)((size_t)td->td_nstrips * sizeof(uint64_t))); |
610 | 34.3k | TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS); |
611 | 34.3k | TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS); |
612 | 34.3k | return (1); |
613 | 34.3k | } |
614 | | #undef isUnspecified |
615 | | |
616 | | /* |
617 | | * Verify file is writable and that the directory |
618 | | * information is setup properly. In doing the latter |
619 | | * we also "freeze" the state of the directory so |
620 | | * that important information is not changed. |
621 | | */ |
622 | | int TIFFWriteCheck(TIFF *tif, int tiles, const char *module) |
623 | 34.3k | { |
624 | 34.3k | if (tif->tif_mode == O_RDONLY) |
625 | 0 | { |
626 | 0 | TIFFErrorExtR(tif, module, "File not open for writing"); |
627 | 0 | return (0); |
628 | 0 | } |
629 | 34.3k | if (tiles ^ isTiled(tif)) |
630 | 0 | { |
631 | 0 | TIFFErrorExtR(tif, module, |
632 | 0 | tiles ? "Can not write tiles to a striped image" |
633 | 0 | : "Can not write scanlines to a tiled image"); |
634 | 0 | return (0); |
635 | 0 | } |
636 | | |
637 | 34.3k | _TIFFFillStriles(tif); |
638 | | |
639 | | /* |
640 | | * On the first write verify all the required information |
641 | | * has been setup and initialize any data structures that |
642 | | * had to wait until directory information was set. |
643 | | * Note that a lot of our work is assumed to remain valid |
644 | | * because we disallow any of the important parameters |
645 | | * from changing after we start writing (i.e. once |
646 | | * TIFF_BEENWRITING is set, TIFFSetField will only allow |
647 | | * the image's length to be changed). |
648 | | */ |
649 | 34.3k | if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) |
650 | 0 | { |
651 | 0 | TIFFErrorExtR(tif, module, |
652 | 0 | "Must set \"ImageWidth\" before writing data"); |
653 | 0 | return (0); |
654 | 0 | } |
655 | 34.3k | if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif)) |
656 | 0 | { |
657 | 0 | tif->tif_dir.td_nstrips = 0; |
658 | 0 | TIFFErrorExtR(tif, module, "No space for %s arrays", |
659 | 0 | isTiled(tif) ? "tile" : "strip"); |
660 | 0 | return (0); |
661 | 0 | } |
662 | 34.3k | if (isTiled(tif)) |
663 | 0 | { |
664 | 0 | tif->tif_dir.td_tilesize = TIFFTileSize(tif); |
665 | 0 | if (tif->tif_dir.td_tilesize == 0) |
666 | 0 | return (0); |
667 | 0 | } |
668 | 34.3k | else |
669 | 34.3k | tif->tif_dir.td_tilesize = (tmsize_t)(-1); |
670 | 34.3k | tif->tif_dir.td_scanlinesize = TIFFScanlineSize(tif); |
671 | 34.3k | if (tif->tif_dir.td_scanlinesize == 0) |
672 | 0 | return (0); |
673 | 34.3k | tif->tif_flags |= TIFF_BEENWRITING; |
674 | | |
675 | 34.3k | if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 && |
676 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_count == 0 && |
677 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_type == 0 && |
678 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 && |
679 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 && |
680 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 && |
681 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 && |
682 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0 && |
683 | 0 | !(tif->tif_flags & TIFF_DIRTYDIRECT)) |
684 | 0 | { |
685 | 0 | TIFFForceStrileArrayWriting(tif); |
686 | 0 | } |
687 | | |
688 | 34.3k | return (1); |
689 | 34.3k | } |
690 | | |
691 | | /* |
692 | | * Setup the raw data buffer used for encoding. |
693 | | */ |
694 | | int TIFFWriteBufferSetup(TIFF *tif, void *bp, tmsize_t size) |
695 | 34.3k | { |
696 | 34.3k | static const char module[] = "TIFFWriteBufferSetup"; |
697 | | |
698 | 34.3k | if (tif->tif_rawdata) |
699 | 0 | { |
700 | 0 | if (tif->tif_flags & TIFF_MYBUFFER) |
701 | 0 | { |
702 | 0 | _TIFFfreeExt(tif, tif->tif_rawdata); |
703 | 0 | tif->tif_flags &= ~TIFF_MYBUFFER; |
704 | 0 | } |
705 | 0 | tif->tif_rawdata = NULL; |
706 | 0 | } |
707 | 34.3k | if (size == (tmsize_t)(-1)) |
708 | 34.3k | { |
709 | 34.3k | size = (isTiled(tif) ? tif->tif_dir.td_tilesize : TIFFStripSize(tif)); |
710 | | |
711 | | /* Adds 10% margin for cases where compression would expand a bit */ |
712 | 34.3k | if (size < TIFF_TMSIZE_T_MAX - size / 10) |
713 | 34.3k | size += size / 10; |
714 | | /* |
715 | | * Make raw data buffer at least 8K |
716 | | */ |
717 | 34.3k | if (size < 8 * 1024) |
718 | 21.6k | size = 8 * 1024; |
719 | 34.3k | bp = NULL; /* NB: force malloc */ |
720 | 34.3k | } |
721 | 34.3k | if (bp == NULL) |
722 | 34.3k | { |
723 | 34.3k | bp = _TIFFmallocExt(tif, size); |
724 | 34.3k | if (bp == NULL) |
725 | 0 | { |
726 | 0 | TIFFErrorExtR(tif, module, "No space for output buffer"); |
727 | 0 | return (0); |
728 | 0 | } |
729 | 34.3k | tif->tif_flags |= TIFF_MYBUFFER; |
730 | 34.3k | } |
731 | 0 | else |
732 | 0 | tif->tif_flags &= ~TIFF_MYBUFFER; |
733 | 34.3k | tif->tif_rawdata = (uint8_t *)bp; |
734 | 34.3k | tif->tif_rawdatasize = size; |
735 | 34.3k | tif->tif_rawcc = 0; |
736 | 34.3k | tif->tif_rawcp = tif->tif_rawdata; |
737 | 34.3k | tif->tif_flags |= TIFF_BUFFERSETUP; |
738 | 34.3k | return (1); |
739 | 34.3k | } |
740 | | |
741 | | /* |
742 | | * Grow the strip data structures by delta strips. |
743 | | */ |
744 | | static int TIFFGrowStrips(TIFF *tif, uint32_t delta, const char *module) |
745 | 0 | { |
746 | 0 | TIFFDirectory *td = &tif->tif_dir; |
747 | 0 | uint64_t *new_stripoffset; |
748 | 0 | uint64_t *new_stripbytecount; |
749 | |
|
750 | 0 | assert(td->td_planarconfig == PLANARCONFIG_CONTIG); |
751 | 0 | new_stripoffset = (uint64_t *)_TIFFreallocExt( |
752 | 0 | tif, td->td_stripoffset_p, |
753 | 0 | (tmsize_t)(((size_t)td->td_nstrips + (size_t)delta) * |
754 | 0 | sizeof(uint64_t))); |
755 | 0 | new_stripbytecount = (uint64_t *)_TIFFreallocExt( |
756 | 0 | tif, td->td_stripbytecount_p, |
757 | 0 | (tmsize_t)(((size_t)td->td_nstrips + (size_t)delta) * |
758 | 0 | sizeof(uint64_t))); |
759 | 0 | if (new_stripoffset == NULL || new_stripbytecount == NULL) |
760 | 0 | { |
761 | 0 | if (new_stripoffset) |
762 | 0 | _TIFFfreeExt(tif, new_stripoffset); |
763 | 0 | if (new_stripbytecount) |
764 | 0 | _TIFFfreeExt(tif, new_stripbytecount); |
765 | 0 | td->td_nstrips = 0; |
766 | 0 | TIFFErrorExtR(tif, module, "No space to expand strip arrays"); |
767 | 0 | return (0); |
768 | 0 | } |
769 | 0 | td->td_stripoffset_p = new_stripoffset; |
770 | 0 | td->td_stripbytecount_p = new_stripbytecount; |
771 | 0 | _TIFFmemset(td->td_stripoffset_p + td->td_nstrips, 0, |
772 | 0 | (tmsize_t)((size_t)delta * sizeof(uint64_t))); |
773 | 0 | _TIFFmemset(td->td_stripbytecount_p + td->td_nstrips, 0, |
774 | 0 | (tmsize_t)((size_t)delta * sizeof(uint64_t))); |
775 | 0 | td->td_nstrips += delta; |
776 | 0 | tif->tif_flags |= TIFF_DIRTYDIRECT; |
777 | |
|
778 | 0 | return (1); |
779 | 0 | } |
780 | | |
781 | | /* |
782 | | * Append the data to the specified strip. |
783 | | */ |
784 | | static int TIFFAppendToStrip(TIFF *tif, uint32_t strip, uint8_t *data, |
785 | | tmsize_t cc) |
786 | 47.2k | { |
787 | 47.2k | static const char module[] = "TIFFAppendToStrip"; |
788 | 47.2k | TIFFDirectory *td = &tif->tif_dir; |
789 | 47.2k | uint64_t m; |
790 | 47.2k | int64_t old_byte_count = -1; |
791 | | |
792 | | /* Some security checks */ |
793 | 47.2k | if (td->td_stripoffset_p == NULL) |
794 | 0 | { |
795 | 0 | TIFFErrorExtR(tif, module, "Strip offset array pointer is NULL"); |
796 | 0 | return (0); |
797 | 0 | } |
798 | 47.2k | if (td->td_stripbytecount_p == NULL) |
799 | 0 | { |
800 | 0 | TIFFErrorExtR(tif, module, "Strip bytecount array pointer is NULL"); |
801 | 0 | return (0); |
802 | 0 | } |
803 | 47.2k | if (strip == NOSTRIP) |
804 | 0 | { |
805 | 0 | TIFFErrorExtR(tif, module, "Strip number not valid (NOSTRIP)"); |
806 | 0 | return (0); |
807 | 0 | } |
808 | | |
809 | 47.2k | if (tif->tif_curoff == 0) |
810 | 47.2k | tif->tif_lastvalidoff = 0; |
811 | | |
812 | 47.2k | if (td->td_stripoffset_p[strip] == 0 || tif->tif_curoff == 0) |
813 | 47.2k | { |
814 | 47.2k | assert(td->td_nstrips > 0); |
815 | | |
816 | 47.2k | if (td->td_stripbytecount_p[strip] != 0 && |
817 | 0 | td->td_stripoffset_p[strip] != 0 && |
818 | 0 | td->td_stripbytecount_p[strip] >= (uint64_t)cc) |
819 | 0 | { |
820 | | /* |
821 | | * There is already tile data on disk, and the new tile |
822 | | * data we have will fit in the same space. The only |
823 | | * aspect of this that is risky is that there could be |
824 | | * more data to append to this strip before we are done |
825 | | * depending on how we are getting called. |
826 | | */ |
827 | 0 | if (!SeekOK(tif, td->td_stripoffset_p[strip])) |
828 | 0 | { |
829 | 0 | TIFFErrorExtR(tif, module, "Seek error at scanline %lu", |
830 | 0 | (unsigned long)tif->tif_dir.td_row); |
831 | 0 | return (0); |
832 | 0 | } |
833 | | |
834 | 0 | tif->tif_lastvalidoff = |
835 | 0 | td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip]; |
836 | 0 | } |
837 | 47.2k | else |
838 | 47.2k | { |
839 | | /* |
840 | | * Seek to end of file, and set that as our location to |
841 | | * write this strip. |
842 | | */ |
843 | 47.2k | td->td_stripoffset_p[strip] = TIFFSeekFile(tif, 0, SEEK_END); |
844 | 47.2k | tif->tif_flags |= TIFF_DIRTYSTRIP; |
845 | 47.2k | } |
846 | | |
847 | 47.2k | tif->tif_curoff = td->td_stripoffset_p[strip]; |
848 | | |
849 | | /* |
850 | | * We are starting a fresh strip/tile, so set the size to zero. |
851 | | */ |
852 | 47.2k | old_byte_count = (int64_t)td->td_stripbytecount_p[strip]; |
853 | 47.2k | td->td_stripbytecount_p[strip] = 0; |
854 | 47.2k | } |
855 | | |
856 | 47.2k | m = tif->tif_curoff + (uint64_t)cc; |
857 | 47.2k | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
858 | 34.8k | m = (uint32_t)m; |
859 | 47.2k | if ((m < tif->tif_curoff) || (m < (uint64_t)cc)) |
860 | 0 | { |
861 | 0 | TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded"); |
862 | 0 | return (0); |
863 | 0 | } |
864 | | |
865 | 47.2k | if (tif->tif_lastvalidoff != 0 && m > tif->tif_lastvalidoff && |
866 | 0 | td->td_stripbytecount_p[strip] > 0) |
867 | 0 | { |
868 | | /* Ouch: we have detected that we are rewriting in place a strip/tile */ |
869 | | /* with several calls to TIFFAppendToStrip(). The first call was with */ |
870 | | /* a size smaller than the previous size of the strip/tile, so we */ |
871 | | /* opted to rewrite in place, but a following call causes us to go */ |
872 | | /* outsize of the strip/tile area, so we have to finally go for a */ |
873 | | /* append-at-end-of-file strategy, and start by moving what we already |
874 | | */ |
875 | | /* wrote. */ |
876 | 0 | tmsize_t tempSize; |
877 | 0 | void *temp; |
878 | 0 | uint64_t offsetRead; |
879 | 0 | uint64_t offsetWrite; |
880 | 0 | uint64_t toCopy = td->td_stripbytecount_p[strip]; |
881 | |
|
882 | 0 | if (toCopy < 1024 * 1024) |
883 | 0 | tempSize = (tmsize_t)toCopy; |
884 | 0 | else |
885 | 0 | tempSize = 1024 * 1024; |
886 | |
|
887 | 0 | offsetRead = td->td_stripoffset_p[strip]; |
888 | 0 | offsetWrite = TIFFSeekFile(tif, 0, SEEK_END); |
889 | |
|
890 | 0 | m = offsetWrite + (uint64_t)toCopy + (uint64_t)cc; |
891 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF) && m != (uint32_t)m) |
892 | 0 | { |
893 | 0 | TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded"); |
894 | 0 | return (0); |
895 | 0 | } |
896 | | |
897 | 0 | temp = _TIFFmallocExt(tif, tempSize); |
898 | 0 | if (temp == NULL) |
899 | 0 | { |
900 | 0 | TIFFErrorExtR(tif, module, "No space for output buffer"); |
901 | 0 | return (0); |
902 | 0 | } |
903 | | |
904 | 0 | tif->tif_flags |= TIFF_DIRTYSTRIP; |
905 | |
|
906 | 0 | td->td_stripoffset_p[strip] = offsetWrite; |
907 | 0 | td->td_stripbytecount_p[strip] = 0; |
908 | | |
909 | | /* Move data written by previous calls to us at end of file */ |
910 | 0 | while (toCopy > 0) |
911 | 0 | { |
912 | 0 | if (!SeekOK(tif, offsetRead)) |
913 | 0 | { |
914 | 0 | TIFFErrorExtR(tif, module, "Seek error"); |
915 | 0 | _TIFFfreeExt(tif, temp); |
916 | 0 | return (0); |
917 | 0 | } |
918 | 0 | if (!ReadOK(tif, temp, tempSize)) |
919 | 0 | { |
920 | 0 | TIFFErrorExtR(tif, module, "Cannot read"); |
921 | 0 | _TIFFfreeExt(tif, temp); |
922 | 0 | return (0); |
923 | 0 | } |
924 | 0 | if (!SeekOK(tif, offsetWrite)) |
925 | 0 | { |
926 | 0 | TIFFErrorExtR(tif, module, "Seek error"); |
927 | 0 | _TIFFfreeExt(tif, temp); |
928 | 0 | return (0); |
929 | 0 | } |
930 | 0 | if (!WriteOK(tif, temp, tempSize)) |
931 | 0 | { |
932 | 0 | TIFFErrorExtR(tif, module, "Cannot write"); |
933 | 0 | _TIFFfreeExt(tif, temp); |
934 | 0 | return (0); |
935 | 0 | } |
936 | 0 | offsetRead += (uint64_t)tempSize; |
937 | 0 | offsetWrite += (uint64_t)tempSize; |
938 | 0 | td->td_stripbytecount_p[strip] += (uint64_t)tempSize; |
939 | 0 | toCopy -= (uint64_t)tempSize; |
940 | 0 | } |
941 | 0 | _TIFFfreeExt(tif, temp); |
942 | | |
943 | | /* Append the data of this call */ |
944 | 0 | offsetWrite += (uint64_t)cc; |
945 | 0 | m = offsetWrite; |
946 | 0 | } |
947 | | |
948 | 47.2k | if (!WriteOK(tif, data, cc)) |
949 | 90 | { |
950 | 90 | TIFFErrorExtR(tif, module, "Write error at scanline %lu", |
951 | 90 | (unsigned long)tif->tif_dir.td_row); |
952 | 90 | return (0); |
953 | 90 | } |
954 | 47.2k | tif->tif_curoff = m; |
955 | 47.2k | td->td_stripbytecount_p[strip] += (uint64_t)cc; |
956 | | |
957 | 47.2k | if ((int64_t)td->td_stripbytecount_p[strip] != old_byte_count) |
958 | 47.2k | tif->tif_flags |= TIFF_DIRTYSTRIP; |
959 | | |
960 | 47.2k | return (1); |
961 | 47.2k | } |
962 | | |
963 | | /* |
964 | | * Internal version of TIFFFlushData that can be |
965 | | * called by ``encodestrip routines'' w/o concern |
966 | | * for infinite recursion. |
967 | | */ |
968 | | int TIFFFlushData1(TIFF *tif) |
969 | 84.0k | { |
970 | 84.0k | if (tif->tif_rawcc > 0 && tif->tif_flags & TIFF_BUF4WRITE) |
971 | 47.2k | { |
972 | 47.2k | if (!isFillOrder(tif, tif->tif_dir.td_fillorder) && |
973 | 4.88k | (tif->tif_flags & TIFF_NOBITREV) == 0) |
974 | 4.88k | TIFFReverseBits((uint8_t *)tif->tif_rawdata, tif->tif_rawcc); |
975 | 47.2k | if (!TIFFAppendToStrip(tif, |
976 | 47.2k | isTiled(tif) ? tif->tif_dir.td_curtile |
977 | 47.2k | : tif->tif_dir.td_curstrip, |
978 | 47.2k | tif->tif_rawdata, tif->tif_rawcc)) |
979 | 90 | { |
980 | | /* We update those variables even in case of error since there's */ |
981 | | /* code that doesn't really check the return code of this */ |
982 | | /* function */ |
983 | 90 | tif->tif_rawcc = 0; |
984 | 90 | tif->tif_rawcp = tif->tif_rawdata; |
985 | 90 | return (0); |
986 | 90 | } |
987 | 47.2k | tif->tif_rawcc = 0; |
988 | 47.2k | tif->tif_rawcp = tif->tif_rawdata; |
989 | 47.2k | } |
990 | 84.0k | return (1); |
991 | 84.0k | } |
992 | | |
993 | | /* |
994 | | * Set the current write offset. This should only be |
995 | | * used to set the offset to a known previous location |
996 | | * (very carefully), or to 0 so that the next write gets |
997 | | * appended to the end of the file. |
998 | | */ |
999 | | void TIFFSetWriteOffset(TIFF *tif, toff_t off) |
1000 | 0 | { |
1001 | 0 | tif->tif_curoff = off; |
1002 | 0 | tif->tif_lastvalidoff = 0; |
1003 | 0 | } |