Openwall GNU/*/Linux - a small security-enhanced Linux distro for servers
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sun, 17 Mar 2013 22:54:55 +0100
From: Lukas Odzioba <lukas.odzioba@...il.com>
To: john-dev@...ts.openwall.com
Subject: Re: All *2john programs should use basename of filename when put as “login” field

2013/3/17 magnum <john.magnum@...hmail.com>:
> As far as I know there are two problems:
> 1. Some versions of basename() modify the original string, some do not. This is only a problem if we want to keep the original full name as well.
> 2. Some versions of basename() may return NULL on error. This problem might be more or less academic for this use.
>
> The current gpg2john code handles both cases. A generic version in misc.c or maybe in path.c is a good idea. Supporting an arbitrary list of extensions to strip would be powerful of course.

misc.s seems to be better place.
What do you think about this version:

#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <assert.h>
#include <string.h>


char *get_basename(const char *path, const char *extensions[], int count)
{
        assert(path);
        assert(*path);

        static char retmem[64];
        char *pathdup = NULL;
        char *base = NULL;
        int retsize,i,extlen,baselen = 0;
        pathdup = strdup(path);
        if(!pathdup)
                goto error;
        base = basename(pathdup);
        if(!base || !*base)
                goto error;
        baselen = strlen(base);

        while(count--) {
                extlen=strlen(extensions[count]);
                if ( baselen > extlen &&
!strcmp(&base[baselen-extlen],extensions[count]) ) {
                        base[baselen-extlen]=0;
                        break;
                }
        }

        retsize = strlen(base) + 1;
        memcpy(retmem, base, retsize<64?retsize:64);
        free(pathdup);
        return retmem;

        error:
                if(pathdup)
                        free(pathdup);
                return path;
}

int main(void) {
    char *test="test.txt";
    char *ext[]={".pdf",".txt",".gpg"};

    printf("[%s]",get_basename(test,ext,3));
    printf("[%s]",get_basename("/root/test.pdf",ext,3));
    printf("[%s]",get_basename("/root/test.odt",ext,3));
    printf("[%s]",get_basename("../test.pdf",ext,3));
    printf("[%s]",get_basename("../test.gpg",ext,3));
    printf("[%s]",get_basename("x",ext,3));
    printf("[%s]",get_basename("xtxt",ext,3));

    return 0;
}

Powered by blists - more mailing lists

Your e-mail address:

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.