diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2014-11-15 00:34:32 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2014-11-15 00:34:32 (GMT) |
commit | f7add23a8bcb6d4e0c594ce13fe224829759041c (patch) | |
tree | 4b903cfa3f64bac5c21a270f9fbe2db7d7a6adbf /tools/d2c/helpers.c | |
parent | 44e6aa9039585ad95fb9c6f21535d89457563297 (diff) |
Given the d2c compiler its own directory.
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@420 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'tools/d2c/helpers.c')
-rw-r--r-- | tools/d2c/helpers.c | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/tools/d2c/helpers.c b/tools/d2c/helpers.c new file mode 100644 index 0000000..1353ba2 --- /dev/null +++ b/tools/d2c/helpers.c @@ -0,0 +1,144 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * helpers.c - fonctionnalités d'assitance à la compilation + * + * Copyright (C) 2014 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 "helpers.h" + + +#include <malloc.h> +#include <string.h> + + + +/* ---------------------------------------------------------------------------------- */ +/* MANIPULATIONS DE CHAINES */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : str = chaîne de caractères à manipuler. [OUT] * +* * +* Description : Bascule toute une chaîne de caractères en (min|maj)uscules. * +* * +* Retour : Pointeur sur la chaîne fournie. * +* * +* Remarques : - * +* * +******************************************************************************/ + +char *_make_string_xxx(char *str, int (* fn) (int)) +{ + size_t max; /* Empleur du parcours */ + size_t i; /* Boucle de parcours */ + + max = strlen(str); + + for (i = 0; i < max; i++) + str[i] = fn(str[i]); + + return str; + +} + + +/****************************************************************************** +* * +* Paramètres : raw = données brutes en provenance de l'analyseur. * +* details = indique la nature de la chaîne à traiter. * +* * +* Description : Traduit une chaîne en élément de fonction C. * +* * +* Retour : Chaîne à libérer de la mémoire après usage. * +* * +* Remarques : - * +* * +******************************************************************************/ + +char *make_callable(const char *raw, bool details) +{ + char *result; /* Nom formaté à retourner */ + size_t max; /* Empleur du parcours */ + size_t i; /* Boucle de parcours */ + + result = strdup(raw); + + max = strlen(result); + + /* Première passe : on vire les virgules */ + + for (i = 0; i < max; i++) + if (result[i] == ',') + { + memmove(result + i, result + i + 1, max - i - 1); + max--; + } + + result[max] = '\0'; + + /* Deuxième passe : on bascule en minuscules */ + + result = make_string_lower(result); + + /* Troisième passe : on remplace les mauvais caractères */ + + for (i = 0; i < max; i++) + switch (result[i]) + { + case 'a' ... 'z': + case '0' ... '9': + break; + case '-': + result[i] = '_'; + break; + default: + result[i] = (i + 1 == max ? '\0' : '_'); + break; + } + + /** + * Dernière passe : on s'assure que le premier caractère n'est pas une lettre. + * On ajoute ici le préfixe '_' utilisé lors de la génération de prototypes ; + * en cas d'absence de détails, on ne se retrouve ainsi pas avec un '_' isolé. + */ + + if (details && result[0] != '_') + { + max = strlen(result) + 1; + result = (char *)realloc(result, max); + memmove(result + 1, result, max - 1); + result[0] = '_'; + } + + /* Ultime passe : supression des '_' groupés */ + + for (i = 0; i < max; i++) + if (result[i] == '_' && result[i + 1] == '_') + { + memmove(result + i + 1, result + i + 2, max - i - 1); + max--; + i--; + } + + return result; + +} |