summaryrefslogtreecommitdiff
path: root/src/common/pathname.c
blob: 181fd1f991d3f556c7e4583c2b3f89b7575a0dbd (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

/* Chrysalide - Outil d'analyse de fichiers binaires
 * pathname.c - manipulation de chemins de fichiers
 *
 * Copyright (C) 2015 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  OpenIDA is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenIDA is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "pathname.h"


#include <assert.h>
#include <glib.h>
#include <malloc.h>
#include <string.h>


#include "extstr.h"



/******************************************************************************
*                                                                             *
*  Paramètres  : ref    = fichier servant de référence aux calculs.           *
*                target = fichier absolu ciblé par la procédure.              *
*                                                                             *
*  Description : Calcule le chemin relatif entre deux fichiers donnés.        *
*                                                                             *
*  Retour      : Chemin d'accès déterminé.                                    *
*                                                                             *
*  Remarques   : Les chemins de type 'a//b' ne sont pas supportés.            *
*                                                                             *
******************************************************************************/

char *build_relative_filename(const char *ref, const char *target)
{
    char *result;                           /* Chemin à retourner          */
    size_t common;                          /* Taille de la partie commune */
    const char *found;                      /* Séparateur suivant rencontré*/
    size_t ref_next;                        /* Prochain séparateur #1      */
    size_t target_next;                     /* Prochain séparateur #2      */
    int ret;                                /* Bilan d'un appel            */
    unsigned int levels;                    /* Niveaux de décalage         */
    unsigned int i;                         /* Boucle de parcours #1       */

    common = 0;

    /* Recherche d'une base commune */

    while (1)
    {
        found = strchr(ref + common, G_DIR_SEPARATOR);
        if (found == NULL) break;

        ref_next = found - ref;

        found = strchr(target + common, G_DIR_SEPARATOR);
        if (found == NULL) break;

        target_next = found - target;

        /* Comparaison rapide sur la longeur du nom */
        if (ref_next != target_next) break;

        /* Comparaison sur une portion de chemin */
        ret = strncmp(ref + common, target + common, ref_next - common);
        if (ret != 0) break;

        common = ref_next + 1;

    }

    /* Décompte du décalage entre la référence et la cible */

    found = ref + common;

    for (levels = 0; ; levels++)
    {
        found = strchr(found, G_DIR_SEPARATOR);
        if (found == NULL) break;

        found++;

    }

    /* Construction du résultat final */

    result = strdup(target + common);

    for (i = 0; i < levels; i++)
    {
        result = strprep(result, G_DIR_SEPARATOR_S);
        result = strprep(result, "..");
    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ref    = fichier servant de référence aux calculs.           *
*                target = fichier relatif ciblé par la procédure.             *
*                                                                             *
*  Description : Calcule le chemin absolu d'un fichier par rapport à un autre.*
*                                                                             *
*  Retour      : Chemin d'accès déterminé ou NULL en cas d'erreur.            *
*                                                                             *
*  Remarques   : Les chemins de type 'a//b' ne sont pas supportés.            *
*                                                                             *
******************************************************************************/

char *build_absolute_filename(const char *ref, const char *target)
{
    char *result;                           /* Chemin à retourner          */
    char *last_sep;                         /* Dernier séparateur trouvé   */
    const char *target_base;                /* Base de la relativité       */

    static const char upper[4] = { '.', '.', G_DIR_SEPARATOR, '\0' };

    result = strdup(ref);

    last_sep = strrchr(result, G_DIR_SEPARATOR);
    assert(last_sep != NULL);

    target_base = target;

    /* Remontée des répertoires */
    while (1)
    {
        if (strncmp(target_base, upper, 3) != 0)
            break;

        target_base += 3;

        *last_sep = '\0';
        last_sep = strrchr(result, G_DIR_SEPARATOR);

        /* S'il devient impossible de remonter autant... */
        if (last_sep == NULL) break;

    }

    if (last_sep == NULL)
    {
        free(result);
        result = NULL;
    }
    else
    {
        *(last_sep + 1) = '\0';
        result = stradd(result, target_base);
    }

    return result;

}