untar - improved EOF marker handling

This commit is contained in:
hyung-hwan 2022-12-05 16:04:11 +00:00
parent 4e04c5044c
commit 9bea60d43a
3 changed files with 17 additions and 10 deletions

View File

@ -34,7 +34,7 @@ int main (int argc, char* argv[])
if (!fp)
{
fprintf (stderr, "Error: unable to open file %s\n", argv[1]);
hio_tar_close (hio);
hio_tar_close (untar);
hio_close (hio);
return -1;
}
@ -43,7 +43,7 @@ int main (int argc, char* argv[])
while (!feof(fp) && !ferror(fp))
{
int n;
char buf[99]; /* TODO: use a different buffer size???*/
char buf[512]; /* TODO: use a different buffer size???*/
n = fread(buf, 1, sizeof(buf), fp);
if (n > 0)
{

View File

@ -75,7 +75,8 @@ enum hio_tar_state_t
{
HIO_TAR_STATE_START,
HIO_TAR_STATE_FILE,
HIO_TAR_STATE_END
HIO_TAR_STATE_END_1,
HIO_TAR_STATE_END_2
};
typedef enum hio_tar_state_t hio_tar_state_t;

View File

@ -245,9 +245,8 @@ printf("process_header...\n");
/* all-zero byte block ends the archive */
if (HIO_MEMCMP(hdr, _end_block, HIO_TAR_BLKSIZE) == 0)
{
/* TODO: is it correct? */
printf ("end of input\n");
tar->state = HIO_TAR_STATE_END;
/* two all-zero blocks are expected as the EOF indicator */
tar->state = HIO_TAR_STATE_END_1;
}
else
{
@ -362,11 +361,10 @@ static int process_content (hio_tar_t* tar)
int hio_tar_feed (hio_tar_t* tar, const void* ptr, hio_oow_t len)
{
printf ("feed len = %d\n", len);
if (!ptr)
{
/* EOF indicator */
if (tar->state != HIO_TAR_STATE_END || tar->blk.len > 0)
if ((tar->state != HIO_TAR_STATE_END_1 && tar->state != HIO_TAR_STATE_END_2) || tar->blk.len > 0)
{
/* ERROR - premature end of file */
hio_seterrbfmt (tar->hio, HIO_EINVAL, "premature end of feed");
@ -393,17 +391,25 @@ printf ("feed len = %d\n", len);
{
case HIO_TAR_STATE_START:
if (process_header(tar) <= -1) return -1;
if (tar->state == HIO_TAR_STATE_END) printf ("remaining data len %d\n", len);
break;
case HIO_TAR_STATE_FILE:
if (process_content(tar) <= -1) return -1;
break;
case HIO_TAR_STATE_END:
case HIO_TAR_STATE_END_1:
if (HIO_MEMCMP(tar->blk.buf, _end_block, HIO_TAR_BLKSIZE) == 0)
{
tar->state = HIO_TAR_STATE_END_2;
break;
}
/* fall thru here for the trailing garbage error */
case HIO_TAR_STATE_END_2:
/* garbage after the final ending block */
hio_seterrbfmt (tar->hio, HIO_EINVAL, "trailing garbage at the end of feed");
return -1;
break;
}
tar->blk.len = 0;