Skip first level if it exists

This commit is contained in:
Laurent Vivier 2007-08-24 09:27:29 +00:00
parent b9431c6cf7
commit 23de41a929
2 changed files with 30 additions and 11 deletions

View File

@ -1,38 +1,49 @@
/* /*
* *
* (c) 2005 Laurent Vivier <Laurent@lvivier.info> * (c) 2005-2007 Laurent Vivier <Laurent@lvivier.info>
* *
*/ */
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "libemile.h" #include "libemile.h"
#include "emile.h" #include "emile.h"
char* emile_second_get_configuration(int fd) char* emile_second_get_configuration(int fd)
{ {
char *conf; char *conf = NULL;
emile_l2_header_t header; emile_l2_header_t header;
int ret; int ret;
int size; int size;
off_t offset;
memset(&header, 0, sizeof(header));
ret = read(fd, &header, sizeof(header)); ret = read(fd, &header, sizeof(header));
if (ret != sizeof(header))
return NULL;
if (!EMILE_COMPAT(EMILE_06_SIGNATURE, read_long(&header.signature))) if (!EMILE_COMPAT(EMILE_06_SIGNATURE, read_long(&header.signature)))
return NULL; {
offset = lseek(fd, FIRST_LEVEL_SIZE, SEEK_SET);
ret = read(fd, &header, sizeof(header));
if (!EMILE_COMPAT(EMILE_06_SIGNATURE, read_long(&header.signature)))
goto exit;
}
if (ret != sizeof(header))
goto exit;
size = read_short(&header.conf_size); size = read_short(&header.conf_size);
conf = (char*)malloc(size); conf = (char*)malloc(size);
if (conf == NULL) if (conf == NULL)
return NULL; goto exit;
ret = read(fd, conf, size); ret = read(fd, conf, size);
if (ret != size) if (ret != size)
return NULL; return NULL;
exit:
lseek(fd, offset, SEEK_SET);
return conf; return conf;
} }

View File

@ -1,6 +1,6 @@
/* /*
* *
* (c) 2005 Laurent Vivier <Laurent@lvivier.info> * (c) 2005-2007 Laurent Vivier <Laurent@lvivier.info>
* *
*/ */
@ -20,19 +20,27 @@ int emile_second_set_configuration(int fd, char *configuration)
int ret; int ret;
int size; int size;
int len; int len;
off_t offset;
if (configuration == NULL) if (configuration == NULL)
return EEMILE_CANNOT_READ_SECOND; return EEMILE_CANNOT_READ_SECOND;
len = strlen (configuration) + 1; /* + 1 for ending 0 */ len = strlen (configuration) + 1; /* + 1 for ending 0 */
memset(&header, 0, sizeof(header));
ret = read(fd, &header, sizeof(header)); ret = read(fd, &header, sizeof(header));
if (!EMILE_COMPAT(EMILE_06_SIGNATURE, read_long(&header.signature)))
{
offset = lseek(fd, FIRST_LEVEL_SIZE, SEEK_SET);
ret = read(fd, &header, sizeof(header));
if (!EMILE_COMPAT(EMILE_06_SIGNATURE, read_long(&header.signature)))
return EEMILE_INVALID_SECOND;
}
if (ret != sizeof(header)) if (ret != sizeof(header))
return EEMILE_CANNOT_READ_SECOND; return EEMILE_CANNOT_READ_SECOND;
if (!EMILE_COMPAT(EMILE_06_SIGNATURE, read_long(&header.signature)))
return EEMILE_INVALID_SECOND;
size = read_short(&header.conf_size); size = read_short(&header.conf_size);
if (len > size) if (len > size)
return EEMILE_INVALID_SECOND; return EEMILE_INVALID_SECOND;