summaryrefslogtreecommitdiffstats
path: root/src/file_sig.c
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2011-04-03 17:49:17 +0200
committerChristophe Grenier <grenier@cgsecurity.org>2011-04-03 17:49:17 +0200
commit73e8653aa1eb47c0b7dd645d43ddbbb8e2d7a933 (patch)
tree1ae0b7ce068f1deb6f09f29cf4e4c2ebcd5cf2e5 /src/file_sig.c
parentc51b6298f6e24a154dc73dee375ce96b8edcd147 (diff)
PhotoRec: Fix custom signature parser
Diffstat (limited to 'src/file_sig.c')
-rw-r--r--src/file_sig.c210
1 files changed, 112 insertions, 98 deletions
diff --git a/src/file_sig.c b/src/file_sig.c
index f67981a..dc8d340 100644
--- a/src/file_sig.c
+++ b/src/file_sig.c
@@ -46,7 +46,7 @@ static int header_check_sig(const unsigned char *buffer, const unsigned int buff
const file_hint_t file_hint_sig= {
.extension="",
- .description="Own signature",
+ .description="Own custom signatures",
.min_header_distance=0,
.max_filesize=PHOTOREC_MAX_FILE_SIZE,
.recover=1,
@@ -190,82 +190,54 @@ static char *parse_signature_file(file_stat_t *file_stat, char *pos)
{
while(*pos!='\0')
{
- /* each line is composed of "extension offset signature" */
- const char *extension;
- unsigned int offset=0;
- unsigned char *signature=NULL;
- unsigned int signature_max_size=512;
- unsigned int signature_size=0;
+ /* skip comment */
+ if(*pos=='#')
{
- const char *extension_start=pos;
- while(*pos!='\0' && !isspace(*pos))
+ while(*pos!='\0' && *pos!='\n')
pos++;
if(*pos=='\0')
return pos;
- *pos='\0';
- extension=strdup(extension_start);
pos++;
}
- /* skip space */
- while(isspace(*pos))
- pos++;
- /* read offset */
- pos=str_uint(pos, &offset);
- /* read signature */
- signature=(unsigned char *)MALLOC(signature_max_size);
- while(*pos!='\n' && *pos!='\0')
+ /* each line is composed of "extension offset signature" */
{
- if(signature_size==signature_max_size)
+ const char *extension;
+ unsigned int offset=0;
+ unsigned char *signature=NULL;
+ unsigned int signature_max_size=512;
+ unsigned int signature_size=0;
{
- signature_max_size*=2;
- signature=(unsigned char *)realloc(signature, signature_max_size);
+ const char *extension_start=pos;
+ while(*pos!='\0' && !isspace(*pos))
+ pos++;
+ if(*pos=='\0')
+ return pos;
+ *pos='\0';
+ extension=strdup(extension_start);
+ pos++;
}
- if(isspace(*pos) || *pos=='\r' || *pos==',')
+ /* skip space */
+ while(isspace(*pos))
pos++;
- else if(*pos== '\'')
+ /* read offset */
+ pos=str_uint(pos, &offset);
+ /* read signature */
+ signature=(unsigned char *)MALLOC(signature_max_size);
+ while(*pos!='\n' && *pos!='\0')
{
- pos++;
- if(*pos=='\0')
- return pos;
- else if(*pos=='\\')
+ if(signature_size==signature_max_size)
{
- pos++;
- if(*pos=='\0')
- return pos;
- else if(*pos=='b')
- signature[signature_size++]='\b';
- else if(*pos=='n')
- signature[signature_size++]='\n';
- else if(*pos=='t')
- signature[signature_size++]='\t';
- else if(*pos=='r')
- signature[signature_size++]='\r';
- else if(*pos=='0')
- signature[signature_size++]='\0';
- else
- signature[signature_size++]=*pos;
- pos++;
+ signature_max_size*=2;
+ signature=(unsigned char *)realloc(signature, signature_max_size);
}
- else
- {
- signature[signature_size++]=*pos;
+ if(isspace(*pos) || *pos=='\r' || *pos==',')
pos++;
- }
- if(*pos!='\'')
- return pos;
- pos++;
- }
- else if(*pos=='"')
- {
- pos++;
- for(; *pos!='"' && *pos!='\0'; pos++)
+ else if(*pos== '\'')
{
- if(signature_size==signature_max_size)
- {
- signature_max_size*=2;
- signature=(unsigned char *)realloc(signature, signature_max_size);
- }
- if(*pos=='\\')
+ pos++;
+ if(*pos=='\0')
+ return pos;
+ else if(*pos=='\\')
{
pos++;
if(*pos=='\0')
@@ -274,55 +246,97 @@ static char *parse_signature_file(file_stat_t *file_stat, char *pos)
signature[signature_size++]='\b';
else if(*pos=='n')
signature[signature_size++]='\n';
- else if(*pos=='r')
- signature[signature_size++]='\r';
else if(*pos=='t')
signature[signature_size++]='\t';
+ else if(*pos=='r')
+ signature[signature_size++]='\r';
else if(*pos=='0')
signature[signature_size++]='\0';
else
signature[signature_size++]=*pos;
+ pos++;
}
else
- signature[signature_size++]=*pos;;
+ {
+ signature[signature_size++]=*pos;
+ pos++;
+ }
+ if(*pos!='\'')
+ return pos;
+ pos++;
+ }
+ else if(*pos=='"')
+ {
+ pos++;
+ for(; *pos!='"' && *pos!='\0'; pos++)
+ {
+ if(signature_size==signature_max_size)
+ {
+ signature_max_size*=2;
+ signature=(unsigned char *)realloc(signature, signature_max_size);
+ }
+ if(*pos=='\\')
+ {
+ pos++;
+ if(*pos=='\0')
+ return pos;
+ else if(*pos=='b')
+ signature[signature_size++]='\b';
+ else if(*pos=='n')
+ signature[signature_size++]='\n';
+ else if(*pos=='r')
+ signature[signature_size++]='\r';
+ else if(*pos=='t')
+ signature[signature_size++]='\t';
+ else if(*pos=='0')
+ signature[signature_size++]='\0';
+ else
+ signature[signature_size++]=*pos;
+ }
+ else
+ signature[signature_size++]=*pos;;
+ }
+ if(*pos!='"')
+ return pos;
+ pos++;
}
- if(*pos!='"')
+ else if(*pos=='0' && (*(pos+1)=='x' || *(pos+1)=='X'))
+ {
+ pos+=2;
+ while(isxdigit(*pos) && isxdigit(*(pos+1)))
+ {
+ unsigned int val=0;
+ val=(*pos);
+ if(*pos>='0' && *pos<='9')
+ val-='0';
+ else if(*pos>='A' && *pos<='F')
+ val-='A';
+ else if(*pos>='a' && *pos<='f')
+ val-='a';
+ pos++;
+ val*=16;
+ val+=(*pos);
+ if(*pos>='0' && *pos<='9')
+ val-='0';
+ else if(*pos>='A' && *pos<='F')
+ val-='A';
+ else if(*pos>='a' && *pos<='f')
+ val-='a';
+ pos++;
+ signature[signature_size++]=val;
+ }
+ }
+ else
return pos;
- pos++;
}
- else if(*pos=='0' && (*(pos+1)=='x' || *(pos+1)=='X') && isxdigit(*(pos+2)) && isxdigit(*(pos+3)))
- {
- unsigned int val=0;
- pos+=2;
- val=(*pos);
- if(*pos>='0' && *pos<='9')
- val-='0';
- else if(*pos>='A' && *pos<='F')
- val-='A';
- else if(*pos>='a' && *pos<='f')
- val-='a';
- pos++;
- val*=16;
- val+=(*pos);
- if(*pos>='0' && *pos<='9')
- val-='0';
- else if(*pos>='A' && *pos<='F')
- val-='A';
- else if(*pos>='a' && *pos<='f')
- val-='a';
+ if(*pos=='\n')
pos++;
- signature[signature_size++]=val;
+ if(signature_size>0)
+ {
+ log_info("register a signature for %s\n", extension);
+ register_header_check(offset, signature, signature_size, &header_check_sig, file_stat);
+ signature_insert(extension, offset, signature, signature_size);
}
- else
- return pos;
- }
- if(*pos=='\n')
- pos++;
- if(signature_size>0)
- {
- log_info("register a signature for %s\n", extension);
- register_header_check(offset, signature, signature_size, &header_check_sig, file_stat);
- signature_insert(extension, offset, signature, signature_size);
}
}
return pos;