summaryrefslogtreecommitdiff
path: root/src/common/leb128.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2010-05-13 12:32:03 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2010-05-13 12:32:03 (GMT)
commit118a668adbf6ca9d4c549618e54f58330f46ce58 (patch)
tree10e75f1a7e83ab48aba82a5a595441a065a6037e /src/common/leb128.c
parente56b4db3aae87f0458319019635dea4968a5c529 (diff)
Supported Dalvik VM / DEX (partially).
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@155 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'src/common/leb128.c')
-rw-r--r--src/common/leb128.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/common/leb128.c b/src/common/leb128.c
new file mode 100644
index 0000000..4a03797
--- /dev/null
+++ b/src/common/leb128.c
@@ -0,0 +1,111 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * leb128.c - support des valeurs encodées au format LEB128.
+ *
+ * Copyright (C) 2010 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 "leb128.h"
+
+
+
+/******************************************************************************
+* *
+* Paramètres : target = lieu d'enregistrement de la lecture. [OUT] *
+* data = flux de données à analyser. *
+* pos = position courante dans ce flux. [OUT] *
+* len = taille totale des données à analyser. *
+* *
+* Description : Lit un nombre non signé encodé au format LEB128. *
+* *
+* Retour : Bilan de l'opération : true en cas de succès, false sinon. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool read_uleb128(uleb128_t *target, const bin_t *data, off_t *pos, off_t len)
+{
+ int shift; /* Décallage à appliquer */
+ off_t i; /* Boucle de parcours */
+
+ shift = 0;
+ *target = 0;
+
+ for (i = 0; i < 8; i++)
+ {
+ /* On évite les débordements... */
+ if (*pos >= len) return false;
+
+ *target |= (data[*pos] & 0x7f) << shift;
+
+ shift += 7;
+ (*pos)++;
+
+ if ((data[*pos - 1] & 0x80) == 0x00) break;
+
+ }
+
+ return (i < 8);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : target = lieu d'enregistrement de la lecture. [OUT] *
+* data = flux de données à analyser. *
+* pos = position courante dans ce flux. [OUT] *
+* len = taille totale des données à analyser. *
+* *
+* Description : Lit un nombre signé encodé au format LEB128. *
+* *
+* Retour : Bilan de l'opération : true en cas de succès, false sinon. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool read_leb128(leb128_t *target, const bin_t *data, off_t *pos, off_t len)
+{
+ int shift; /* Décallage à appliquer */
+ off_t i; /* Boucle de parcours */
+
+ shift = 0;
+ *target = 0;
+
+ for (i = 0; i < 8; i++)
+ {
+ /* On évite les débordements... */
+ if (*pos >= len) return false;
+
+ *target |= (data[*pos] & 0x7f) << shift;
+
+ shift += 7;
+
+ if ((data[(*pos)++] & 0x80) == 0x00) break;
+
+ }
+
+ if (shift < (8 * sizeof(int64_t)) && (data[*pos - 1] & 0x40) == 0x40)
+ *target |= - (1 << shift);
+
+ return (i < 8);
+
+}