Leptonica  1.82.0
Image processing and image analysis suite
bytearray.c File Reference
#include <string.h>
#include "allheaders.h"

Go to the source code of this file.

Functions

static l_int32 l_byteaExtendArrayToSize (L_BYTEA *ba, size_t size)
 
L_BYTEAl_byteaCreate (size_t nbytes)
 
L_BYTEAl_byteaInitFromMem (const l_uint8 *data, size_t size)
 
L_BYTEAl_byteaInitFromFile (const char *fname)
 
L_BYTEAl_byteaInitFromStream (FILE *fp)
 
L_BYTEAl_byteaCopy (L_BYTEA *bas, l_int32 copyflag)
 
void l_byteaDestroy (L_BYTEA **pba)
 
size_t l_byteaGetSize (L_BYTEA *ba)
 
l_uint8 * l_byteaGetData (L_BYTEA *ba, size_t *psize)
 
l_uint8 * l_byteaCopyData (L_BYTEA *ba, size_t *psize)
 
l_ok l_byteaAppendData (L_BYTEA *ba, const l_uint8 *newdata, size_t newbytes)
 
l_ok l_byteaAppendString (L_BYTEA *ba, const char *str)
 
l_ok l_byteaJoin (L_BYTEA *ba1, L_BYTEA **pba2)
 
l_ok l_byteaSplit (L_BYTEA *ba1, size_t splitloc, L_BYTEA **pba2)
 
l_ok l_byteaFindEachSequence (L_BYTEA *ba, const l_uint8 *sequence, size_t seqlen, L_DNA **pda)
 
l_ok l_byteaWrite (const char *fname, L_BYTEA *ba, size_t startloc, size_t nbytes)
 
l_ok l_byteaWriteStream (FILE *fp, L_BYTEA *ba, size_t startloc, size_t nbytes)
 

Variables

static const l_uint32 MaxArraySize = 1000000000
 
static const l_int32 InitialArraySize = 200
 

Detailed Description

  Functions for handling byte arrays, in analogy with C++ 'strings'
     Creation, copy, clone, destruction
          L_BYTEA      *l_byteaCreate()
          L_BYTEA      *l_byteaInitFromMem()
          L_BYTEA      *l_byteaInitFromFile()
          L_BYTEA      *l_byteaInitFromStream()
          L_BYTEA      *l_byteaCopy()
          void          l_byteaDestroy()
     Accessors
          size_t        l_byteaGetSize()
          l_uint8      *l_byteaGetData()
          l_uint8      *l_byteaCopyData()
     Appending
          l_int32       l_byteaAppendData()
          l_int32       l_byteaAppendString()
          static l_int32  l_byteaExtendArrayToSize()
     Join/Split
          l_int32       l_byteaJoin()
          l_int32       l_byteaSplit()
     Search
          l_int32       l_byteaFindEachSequence()
     Output to file
          l_int32       l_byteaWrite()
          l_int32       l_byteaWriteStream()
  The internal data array is always null-terminated, for ease of use
  in the event that it is an ascii string without null bytes.

Definition in file bytearray.c.

Function Documentation

◆ l_byteaAppendData()

l_ok l_byteaAppendData ( L_BYTEA ba,
const l_uint8 *  newdata,
size_t  newbytes 
)

l_byteaAppendData()

Parameters
[in]ba
[in]newdatabyte array to be appended
[in]newbytessize of data array
Returns
0 if OK, 1 on error

Definition at line 366 of file bytearray.c.

References L_Bytea::data, l_byteaExtendArrayToSize(), l_byteaGetSize(), L_Bytea::nalloc, and L_Bytea::size.

Referenced by l_byteaJoin(), and substituteObjectNumbers().

◆ l_byteaAppendString()

l_ok l_byteaAppendString ( L_BYTEA ba,
const char *  str 
)

l_byteaAppendString()

Parameters
[in]ba
[in]strnull-terminated string to be appended
Returns
0 if OK, 1 on error

Definition at line 401 of file bytearray.c.

References L_Bytea::data, l_byteaExtendArrayToSize(), l_byteaGetSize(), L_Bytea::nalloc, and L_Bytea::size.

Referenced by pathJoin(), and substituteObjectNumbers().

◆ l_byteaCopy()

L_BYTEA* l_byteaCopy ( L_BYTEA bas,
l_int32  copyflag 
)

l_byteaCopy()

Parameters
[in]bassource lba
[in]copyflagL_COPY, L_CLONE
Returns
clone or copy of bas, or NULL on error
Notes:
     (1) If cloning, up the refcount and return a ptr to bas.

Definition at line 218 of file bytearray.c.

References L_Bytea::data, l_byteaInitFromMem(), L_CLONE, L_Bytea::refcount, and L_Bytea::size.

◆ l_byteaCopyData()

l_uint8* l_byteaCopyData ( L_BYTEA ba,
size_t *  psize 
)

l_byteaCopyData()

Parameters
[in]ba
[out]psizesize of data in lba
Returns
copy of data in use in the data array, or NULL on error.
Notes:
     (1) The returned data is owned by the caller.  The input ba
         still owns the original data array.

Definition at line 336 of file bytearray.c.

References l_binaryCopy(), and l_byteaGetData().

◆ l_byteaCreate()

L_BYTEA* l_byteaCreate ( size_t  nbytes)

l_byteaCreate()

Parameters
[in]nbytesdetermines initial size of data array
Returns
l_bytea, or NULL on error
Notes:
     (1) The allocated array is n + 1 bytes.  This allows room
         for null termination.

Definition at line 97 of file bytearray.c.

Referenced by l_byteaInitFromStream(), pathJoin(), and substituteObjectNumbers().

◆ l_byteaDestroy()

void l_byteaDestroy ( L_BYTEA **  pba)

l_byteaDestroy()

Parameters
[in,out]pbawill be set to null before returning
Returns
void
Notes:
     (1) Decrements the ref count and, if 0, destroys the lba.
     (2) Always nulls the input ptr.
     (3) If the data has been previously removed, the lba will
         have been nulled, so this will do nothing.

Definition at line 250 of file bytearray.c.

References L_Bytea::data, and L_Bytea::refcount.

Referenced by convertSegmentedFilesToPdf(), l_byteaJoin(), pixaConvertToPdfData(), ptraConcatenatePdfToData(), saConcatenatePdfToData(), saConvertFilesToPdfData(), and saConvertUnscaledFilesToPdfData().

◆ l_byteaExtendArrayToSize()

static l_int32 l_byteaExtendArrayToSize ( L_BYTEA ba,
size_t  size 
)
static

l_byteaExtendArrayToSize()

Parameters
[in]ba
[in]sizenew size of lba data array
Returns
0 if OK; 1 on error
Notes:
     (1) If necessary, reallocs the byte array to size.
     (2) The max buffer size is 1 GB.

Definition at line 442 of file bytearray.c.

References L_Bytea::nalloc.

Referenced by l_byteaAppendData(), and l_byteaAppendString().

◆ l_byteaFindEachSequence()

l_ok l_byteaFindEachSequence ( L_BYTEA ba,
const l_uint8 *  sequence,
size_t  seqlen,
L_DNA **  pda 
)

l_byteaFindEachSequence()

Parameters
[in]ba
[in]sequencesubarray of bytes to find in data
[in]seqlenlength of sequence, in bytes
[out]pdabyte positions of each occurrence of sequence
Returns
0 if OK, 1 on error

Definition at line 558 of file bytearray.c.

References arrayFindEachSequence(), and l_byteaGetData().

Referenced by parseTrailerPdf().

◆ l_byteaGetData()

l_uint8* l_byteaGetData ( L_BYTEA ba,
size_t *  psize 
)

l_byteaGetData()

Parameters
[in]ba
[out]psizesize of data in lba
Returns
ptr to existing data array, or NULL on error
Notes:
     (1) The returned ptr is owned by ba.  Do not free it!

Definition at line 307 of file bytearray.c.

References L_Bytea::data, and L_Bytea::size.

Referenced by l_byteaCopyData(), l_byteaFindEachSequence(), l_byteaJoin(), l_byteaSplit(), l_byteaWriteStream(), parseTrailerPdf(), and substituteObjectNumbers().

◆ l_byteaGetSize()

size_t l_byteaGetSize ( L_BYTEA ba)

l_byteaGetSize()

Parameters
[in]ba
Returns
size of stored byte array, or 0 on error

Definition at line 284 of file bytearray.c.

References L_Bytea::size.

Referenced by l_byteaAppendData(), and l_byteaAppendString().

◆ l_byteaInitFromFile()

L_BYTEA* l_byteaInitFromFile ( const char *  fname)

l_byteaInitFromFile()

Parameters
[in]fname
Returns
l_bytea, or NULL on error

Definition at line 154 of file bytearray.c.

References fopenReadStream(), and l_byteaInitFromStream().

Referenced by saConcatenatePdfToData().

◆ l_byteaInitFromMem()

L_BYTEA* l_byteaInitFromMem ( const l_uint8 *  data,
size_t  size 
)

l_byteaInitFromMem()

Parameters
[in]datato be copied to the array
[in]sizeamount of data
Returns
l_bytea, or NULL on error

Definition at line 125 of file bytearray.c.

Referenced by convertSegmentedFilesToPdf(), l_byteaCopy(), l_byteaSplit(), pixaConvertToPdfData(), saConvertFilesToPdfData(), and saConvertUnscaledFilesToPdfData().

◆ l_byteaInitFromStream()

L_BYTEA* l_byteaInitFromStream ( FILE *  fp)

l_byteaInitFromStream()

Parameters
[in]fpfile stream
Returns
l_bytea, or NULL on error

Definition at line 181 of file bytearray.c.

References L_Bytea::data, l_binaryReadStream(), l_byteaCreate(), and L_Bytea::size.

Referenced by l_byteaInitFromFile().

◆ l_byteaJoin()

l_ok l_byteaJoin ( L_BYTEA ba1,
L_BYTEA **  pba2 
)

l_byteaJoin()

Parameters
[in]ba1
[in,out]pba2data array is added to the one in ba1; then ba2 is destroyed and its pointer is nulled.
Returns
0 if OK, 1 on error
Notes:
     (1) It is a no-op, not an error, for ba2 to be null.

Definition at line 483 of file bytearray.c.

References l_byteaAppendData(), l_byteaDestroy(), and l_byteaGetData().

◆ l_byteaSplit()

l_ok l_byteaSplit ( L_BYTEA ba1,
size_t  splitloc,
L_BYTEA **  pba2 
)

l_byteaSplit()

Parameters
[in]ba1lba to split; array bytes nulled beyond the split loc
[in]splitloclocation in ba1 to split; ba2 begins there
[out]pba2with data starting at splitloc
Returns
0 if OK, 1 on error

Definition at line 515 of file bytearray.c.

References l_byteaGetData(), l_byteaInitFromMem(), and L_Bytea::size.

◆ l_byteaWrite()

l_ok l_byteaWrite ( const char *  fname,
L_BYTEA ba,
size_t  startloc,
size_t  nbytes 
)

l_byteaWrite()

Parameters
[in]fnameoutput file
[in]ba
[in]startlocfirst byte to output
[in]nbytesnumber of bytes to write; use 0 to write to the end of the data array
Returns
0 if OK, 1 on error

Definition at line 596 of file bytearray.c.

References fopenWriteStream(), and l_byteaWriteStream().

◆ l_byteaWriteStream()

l_ok l_byteaWriteStream ( FILE *  fp,
L_BYTEA ba,
size_t  startloc,
size_t  nbytes 
)

l_byteaWriteStream()

Parameters
[in]fpfile stream opened for binary write
[in]ba
[in]startlocfirst byte to output
[in]nbytesnumber of bytes to write; use 0 to write to the end of the data array
Returns
0 if OK, 1 on error

Definition at line 630 of file bytearray.c.

References l_byteaGetData().

Referenced by l_byteaWrite().

Variable Documentation

◆ InitialArraySize

const l_int32 InitialArraySize = 200
static

n'importe quoi

Definition at line 76 of file bytearray.c.