1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
/*
* $Id: ext_hdr.c,v 1.1.1.1 2002/03/28 00:02:52 andrew_belov Exp $
* ---------------------------------------------------------------------------
* Handy functions for dealing with extended headers.
*
*/
#include "arj.h"
DEBUGHDR(__FILE__) /* Debug information block */
/* Allocates a per-file extended header structure */
struct ext_hdr FAR *eh_alloc()
{
struct ext_hdr FAR *rc;
rc=(struct ext_hdr FAR *)farmalloc_msg(sizeof(struct ext_hdr));
far_memset((char FAR *)rc, 0, sizeof(struct ext_hdr));
return(rc);
}
/* Locates a block with the specified tag, returning pointer to it */
struct ext_hdr FAR *eh_lookup(struct ext_hdr FAR *eh, char tag)
{
while(eh->next!=NULL)
{
if(eh->tag==tag)
return(eh);
eh=eh->next;
}
return(NULL);
}
/* Locates an unfinalized block */
struct ext_hdr FAR *eh_find_pending(struct ext_hdr FAR *eh)
{
if(eh==NULL)
return(NULL);
while(eh->next!=NULL)
{
if(EH_STATUS(eh)!=EH_FINALIZED)
return(eh);
eh=eh->next;
}
return(NULL);
}
/* Inserts a new block into an instantiated extended header structure. If the
block is given as NULL, performs reallocation only */
struct ext_hdr FAR *eh_append(struct ext_hdr FAR *eh, char tag, char FAR *block, unsigned int size)
{
struct ext_hdr FAR *p_eh;
if((p_eh=eh_lookup(eh, tag))==NULL)
{
for(p_eh=eh; p_eh->next!=NULL; p_eh=p_eh->next);
p_eh->tag=tag;
p_eh->next=eh_alloc();
}
p_eh->raw=(char FAR *)farrealloc_msg(p_eh->raw, p_eh->size+size);
if(block!=NULL)
far_memmove(p_eh->raw+p_eh->size, block, size);
p_eh->size+=size;
return(p_eh);
}
/* Releases the extended header structure */
void eh_release(struct ext_hdr FAR *eh)
{
struct ext_hdr FAR *p_eh;
while((p_eh=eh->next)!=NULL)
{
if(eh->raw!=NULL)
farfree(eh->raw);
farfree(eh);
eh=p_eh;
}
}
|