Leptonica 1.85.0
Image processing and image analysis suite
Loading...
Searching...
No Matches
bytearray.c
Go to the documentation of this file.
1/*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
67#ifdef HAVE_CONFIG_H
68#include <config_auto.h>
69#endif /* HAVE_CONFIG_H */
70
71#include <string.h>
72#include "allheaders.h"
73#include "array_internal.h"
74
75 /* Bounds on array size */
76static const l_uint32 MaxArraySize = 1000000000; /* 10^9 bytes */
77static const l_int32 InitialArraySize = 200;
79 /* Static function */
80static l_int32 l_byteaExtendArrayToSize(L_BYTEA *ba, size_t size);
81
82/*---------------------------------------------------------------------*
83 * Creation, copy, clone, destruction *
84 *---------------------------------------------------------------------*/
97L_BYTEA *
98l_byteaCreate(size_t nbytes)
99{
100L_BYTEA *ba;
101
102 if (nbytes <= 0 || nbytes > MaxArraySize)
103 nbytes = InitialArraySize;
104 ba = (L_BYTEA *)LEPT_CALLOC(1, sizeof(L_BYTEA));
105 ba->data = (l_uint8 *)LEPT_CALLOC(nbytes + 1, sizeof(l_uint8));
106 if (!ba->data) {
107 l_byteaDestroy(&ba);
108 return (L_BYTEA *)ERROR_PTR("ba array not made", __func__, NULL);
109 }
110 ba->nalloc = nbytes + 1;
111 ba->refcount = 1;
112 return ba;
113}
114
115
123L_BYTEA *
124l_byteaInitFromMem(const l_uint8 *data,
125 size_t size)
126{
127L_BYTEA *ba;
128
129 if (!data)
130 return (L_BYTEA *)ERROR_PTR("data not defined", __func__, NULL);
131 if (size <= 0)
132 return (L_BYTEA *)ERROR_PTR("no bytes to initialize", __func__, NULL);
133 if (size > MaxArraySize)
134 return (L_BYTEA *)ERROR_PTR("size is too big", __func__, NULL);
135
136 if ((ba = l_byteaCreate(size)) == NULL)
137 return (L_BYTEA *)ERROR_PTR("ba not made", __func__, NULL);
138 memcpy(ba->data, data, size);
139 ba->size = size;
140 return ba;
141}
142
143
150L_BYTEA *
151l_byteaInitFromFile(const char *fname)
152{
153FILE *fp;
154L_BYTEA *ba;
155
156 if (!fname)
157 return (L_BYTEA *)ERROR_PTR("fname not defined", __func__, NULL);
158
159 if ((fp = fopenReadStream(fname)) == NULL)
160 return (L_BYTEA *)ERROR_PTR_1("file stream not opened",
161 fname, __func__, NULL);
162 ba = l_byteaInitFromStream(fp);
163 fclose(fp);
164 if (!ba)
165 return (L_BYTEA *)ERROR_PTR_1("ba not made", fname, __func__, NULL);
166 return ba;
167}
168
169
176L_BYTEA *
178{
179l_uint8 *data;
180size_t nbytes;
181L_BYTEA *ba;
182
183 if (!fp)
184 return (L_BYTEA *)ERROR_PTR("stream not defined", __func__, NULL);
185
186 if ((data = l_binaryReadStream(fp, &nbytes)) == NULL)
187 return (L_BYTEA *)ERROR_PTR("data not read", __func__, NULL);
188 if ((ba = l_byteaCreate(nbytes)) == NULL) {
189 LEPT_FREE(data);
190 return (L_BYTEA *)ERROR_PTR("ba not made", __func__, NULL);
191 }
192 memcpy(ba->data, data, nbytes);
193 ba->size = nbytes;
194 LEPT_FREE(data);
195 return ba;
196}
197
198
211L_BYTEA *
213 l_int32 copyflag)
214{
215 if (!bas)
216 return (L_BYTEA *)ERROR_PTR("bas not defined", __func__, NULL);
217
218 if (copyflag == L_CLONE) {
219 bas->refcount++;
220 return bas;
221 }
222
223 return l_byteaInitFromMem(bas->data, bas->size);
224}
225
226
241void
243{
244L_BYTEA *ba;
245
246 if (pba == NULL) {
247 L_WARNING("ptr address is null!\n", __func__);
248 return;
249 }
250
251 if ((ba = *pba) == NULL)
252 return;
253
254 /* Decrement the ref count. If it is 0, destroy the lba. */
255 if (--ba->refcount == 0) {
256 if (ba->data) LEPT_FREE(ba->data);
257 LEPT_FREE(ba);
258 }
259 *pba = NULL;
260}
261
262
263/*---------------------------------------------------------------------*
264 * Accessors *
265 *---------------------------------------------------------------------*/
272size_t
274{
275 if (!ba)
276 return ERROR_INT("ba not defined", __func__, 0);
277 return ba->size;
278}
279
280
293l_uint8 *
295 size_t *psize)
296{
297 if (!ba)
298 return (l_uint8 *)ERROR_PTR("ba not defined", __func__, NULL);
299 if (!psize)
300 return (l_uint8 *)ERROR_PTR("&size not defined", __func__, NULL);
301
302 *psize = ba->size;
303 return ba->data;
304}
305
306
320l_uint8 *
322 size_t *psize)
323{
324l_uint8 *data;
325
326 if (!psize)
327 return (l_uint8 *)ERROR_PTR("&size not defined", __func__, NULL);
328 *psize = 0;
329 if (!ba)
330 return (l_uint8 *)ERROR_PTR("ba not defined", __func__, NULL);
331
332 data = l_byteaGetData(ba, psize);
333 return l_binaryCopy(data, *psize);
334}
335
336
337/*---------------------------------------------------------------------*
338 * Appending *
339 *---------------------------------------------------------------------*/
348l_ok
350 const l_uint8 *newdata,
351 size_t newbytes)
352{
353size_t size, nalloc, reqsize;
354
355 if (!ba)
356 return ERROR_INT("ba not defined", __func__, 1);
357 if (!newdata)
358 return ERROR_INT("newdata not defined", __func__, 1);
359
360 size = l_byteaGetSize(ba);
361 reqsize = size + newbytes + 1;
362 nalloc = ba->nalloc;
363 if (nalloc < reqsize) {
364 if (l_byteaExtendArrayToSize(ba, 2 * reqsize))
365 return ERROR_INT("extension failed", __func__, 1);
366 }
367
368 memcpy(ba->data + size, newdata, newbytes);
369 ba->size += newbytes;
370 return 0;
371}
372
373
381l_ok
383 const char *str)
384{
385size_t size, len, nalloc, reqsize;
386
387 if (!ba)
388 return ERROR_INT("ba not defined", __func__, 1);
389 if (!str)
390 return ERROR_INT("str not defined", __func__, 1);
391
392 size = l_byteaGetSize(ba);
393 len = strlen(str);
394 reqsize = size + len + 1;
395 nalloc = ba->nalloc;
396 if (nalloc < reqsize) {
397 if (l_byteaExtendArrayToSize(ba, 2 * reqsize))
398 return ERROR_INT("extension failed", __func__, 1);
399 }
400
401 memcpy(ba->data + size, str, len);
402 ba->size += len;
403 return 0;
404}
405
406
420static l_int32
422 size_t size)
423{
424 if (!ba)
425 return ERROR_INT("ba not defined", __func__, 1);
426 if (ba->nalloc > MaxArraySize) /* belt & suspenders */
427 return ERROR_INT("ba has too many ptrs", __func__, 1);
428 if (size > MaxArraySize)
429 return ERROR_INT("size > 1 GB; too large", __func__, 1);
430 if (size <= ba->nalloc) {
431 L_INFO("size too small; no extension\n", __func__);
432 return 0;
433 }
434
435 if ((ba->data =
436 (l_uint8 *)reallocNew((void **)&ba->data, ba->nalloc, size)) == NULL)
437 return ERROR_INT("new array not returned", __func__, 1);
438 ba->nalloc = size;
439 return 0;
440}
441
442
443/*---------------------------------------------------------------------*
444 * String join/split *
445 *---------------------------------------------------------------------*/
459l_ok
461 L_BYTEA **pba2)
462{
463l_uint8 *data2;
464size_t nbytes2;
465L_BYTEA *ba2;
466
467 if (!ba1)
468 return ERROR_INT("ba1 not defined", __func__, 1);
469 if (!pba2)
470 return ERROR_INT("&ba2 not defined", __func__, 1);
471 if ((ba2 = *pba2) == NULL) return 0;
472
473 data2 = l_byteaGetData(ba2, &nbytes2);
474 l_byteaAppendData(ba1, data2, nbytes2);
475
476 l_byteaDestroy(pba2);
477 return 0;
478}
479
480
489l_ok
491 size_t splitloc,
492 L_BYTEA **pba2)
493{
494l_uint8 *data1;
495size_t nbytes1, nbytes2;
496
497 if (!pba2)
498 return ERROR_INT("&ba2 not defined", __func__, 1);
499 *pba2 = NULL;
500 if (!ba1)
501 return ERROR_INT("ba1 not defined", __func__, 1);
502
503 data1 = l_byteaGetData(ba1, &nbytes1);
504 if (splitloc >= nbytes1)
505 return ERROR_INT("splitloc invalid", __func__, 1);
506 nbytes2 = nbytes1 - splitloc;
507
508 /* Make the new lba */
509 *pba2 = l_byteaInitFromMem(data1 + splitloc, nbytes2);
510
511 /* Null the removed bytes in the input lba */
512 memset(data1 + splitloc, 0, nbytes2);
513 ba1->size = splitloc;
514 return 0;
515}
516
517
518/*---------------------------------------------------------------------*
519 * Search *
520 *---------------------------------------------------------------------*/
530l_ok
532 const l_uint8 *sequence,
533 size_t seqlen,
534 L_DNA **pda)
535{
536l_uint8 *data;
537size_t size;
538
539 if (!pda)
540 return ERROR_INT("&da not defined", __func__, 1);
541 *pda = NULL;
542 if (!ba)
543 return ERROR_INT("ba not defined", __func__, 1);
544 if (!sequence)
545 return ERROR_INT("sequence not defined", __func__, 1);
546
547 data = l_byteaGetData(ba, &size);
548 *pda = arrayFindEachSequence(data, size, sequence, seqlen);
549 return 0;
550}
551
552
553/*---------------------------------------------------------------------*
554 * Output to file *
555 *---------------------------------------------------------------------*/
566l_ok
567l_byteaWrite(const char *fname,
568 L_BYTEA *ba,
569 size_t startloc,
570 size_t nbytes)
571{
572l_int32 ret;
573FILE *fp;
574
575 if (!fname)
576 return ERROR_INT("fname not defined", __func__, 1);
577 if (!ba)
578 return ERROR_INT("ba not defined", __func__, 1);
579
580 if ((fp = fopenWriteStream(fname, "wb")) == NULL)
581 return ERROR_INT_1("stream not opened", fname, __func__, 1);
582 ret = l_byteaWriteStream(fp, ba, startloc, nbytes);
583 fclose(fp);
584 return ret;
585}
586
587
598l_ok
600 L_BYTEA *ba,
601 size_t startloc,
602 size_t nbytes)
603{
604l_uint8 *data;
605size_t size, maxbytes;
606
607 if (!fp)
608 return ERROR_INT("stream not defined", __func__, 1);
609 if (!ba)
610 return ERROR_INT("ba not defined", __func__, 1);
611
612 data = l_byteaGetData(ba, &size);
613 if (startloc >= size)
614 return ERROR_INT("invalid startloc", __func__, 1);
615 maxbytes = size - startloc;
616 nbytes = (nbytes == 0) ? maxbytes : L_MIN(nbytes, maxbytes);
617
618 fwrite(data + startloc, 1, nbytes, fp);
619 return 0;
620}
L_BYTEA * l_byteaInitFromMem(const l_uint8 *data, size_t size)
l_byteaInitFromMem()
Definition bytearray.c:124
L_BYTEA * l_byteaCreate(size_t nbytes)
l_byteaCreate()
Definition bytearray.c:98
size_t l_byteaGetSize(L_BYTEA *ba)
l_byteaGetSize()
Definition bytearray.c:273
L_BYTEA * l_byteaInitFromFile(const char *fname)
l_byteaInitFromFile()
Definition bytearray.c:151
l_ok l_byteaFindEachSequence(L_BYTEA *ba, const l_uint8 *sequence, size_t seqlen, L_DNA **pda)
l_byteaFindEachSequence()
Definition bytearray.c:531
l_ok l_byteaSplit(L_BYTEA *ba1, size_t splitloc, L_BYTEA **pba2)
l_byteaSplit()
Definition bytearray.c:490
L_BYTEA * l_byteaInitFromStream(FILE *fp)
l_byteaInitFromStream()
Definition bytearray.c:177
l_uint8 * l_byteaCopyData(L_BYTEA *ba, size_t *psize)
l_byteaCopyData()
Definition bytearray.c:321
static l_int32 l_byteaExtendArrayToSize(L_BYTEA *ba, size_t size)
l_byteaExtendArrayToSize()
Definition bytearray.c:421
l_ok l_byteaWrite(const char *fname, L_BYTEA *ba, size_t startloc, size_t nbytes)
l_byteaWrite()
Definition bytearray.c:567
void l_byteaDestroy(L_BYTEA **pba)
l_byteaDestroy()
Definition bytearray.c:242
l_uint8 * l_byteaGetData(L_BYTEA *ba, size_t *psize)
l_byteaGetData()
Definition bytearray.c:294
L_BYTEA * l_byteaCopy(L_BYTEA *bas, l_int32 copyflag)
l_byteaCopy()
Definition bytearray.c:212
l_ok l_byteaWriteStream(FILE *fp, L_BYTEA *ba, size_t startloc, size_t nbytes)
l_byteaWriteStream()
Definition bytearray.c:599
l_ok l_byteaAppendString(L_BYTEA *ba, const char *str)
l_byteaAppendString()
Definition bytearray.c:382
static const l_int32 InitialArraySize
Definition bytearray.c:77
l_ok l_byteaJoin(L_BYTEA *ba1, L_BYTEA **pba2)
l_byteaJoin()
Definition bytearray.c:460
l_ok l_byteaAppendData(L_BYTEA *ba, const l_uint8 *newdata, size_t newbytes)
l_byteaAppendData()
Definition bytearray.c:349
@ L_CLONE
Definition pix.h:506
l_atomic refcount
size_t size
size_t nalloc
l_uint8 * data