From cf3a34cbef269506b36331a14c1de352926e08de Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 23 May 2011 17:32:43 +0000 Subject: [PATCH] OpenPGP: re-factor pgp_select_file() * remove unnecessary copy operations with a temporary path object, instead increase the start index. * addd comments Signed-off-by: Peter Marschall git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@5489 c6295689-39f2-0310-b995-f0e70906c6a9 --- src/libopensc/card-openpgp.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/libopensc/card-openpgp.c b/src/libopensc/card-openpgp.c index 55554387..ba1f6c29 100644 --- a/src/libopensc/card-openpgp.c +++ b/src/libopensc/card-openpgp.c @@ -460,14 +460,11 @@ pgp_select_file(sc_card_t *card, const sc_path_t *path, sc_file_t **ret) { struct pgp_priv_data *priv = DRVDATA(card); struct blob *blob; - sc_path_t path_copy; + unsigned int path_start; unsigned int n; - int r; LOG_FUNC_CALLED(card->ctx); - memset(&path_copy, 0, sizeof(path_copy)); - if (path->type == SC_PATH_TYPE_DF_NAME) LOG_FUNC_RETURN(card->ctx, iso_ops->select_file(card, path, ret)); @@ -479,24 +476,23 @@ pgp_select_file(sc_card_t *card, const sc_path_t *path, sc_file_t **ret) LOG_TEST_RET(card->ctx, SC_ERROR_INVALID_ARGUMENTS, "invalid path length"); - if (!memcmp(path->value, "\x3f\x00", 2)) { - memcpy(path_copy.value, path->value + 2, path->len - 2); - path_copy.len = path->len - 2; - path = &path_copy; - } + /* ignore explicitely mentioned MF at the path's beginning */ + path_start = (memcmp(path->value, "\x3f\x00", 2) == 0) ? 2 : 0; + /* starting with the MF ... */ blob = priv->mf; - for (n = 0; n < path->len; n += 2) { - r = pgp_get_blob(card, blob, - (path->value[n] << 8) | path->value[n+1], - &blob); - if (r < 0) { + /* ... recurse through the tree following the path */ + for (n = path_start; n < path->len; n += 2) { + unsigned int id = bebytes2ushort(path->value + n); + int r = pgp_get_blob(card, blob, id, &blob); + + if (r < 0) { /* failure */ priv->current = NULL; LOG_FUNC_RETURN(card->ctx, r); } } - /* select file = set "current" pointer to blob found */ + /* success: select file = set "current" pointer to blob found */ priv->current = blob; if (ret)