From 869f0e85a8aa5ec30cc5f30c14e2a27cfa0485cf Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Sat, 15 Aug 2026 19:42:55 +0300 Subject: [PATCH] liblzma: index.c 3/4: Add index_tree_prev() --- src/liblzma/common/index.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/liblzma/common/index.c b/src/liblzma/common/index.c index 09ee2fed..0baf4e71 100644 --- a/src/liblzma/common/index.c +++ b/src/liblzma/common/index.c @@ -348,6 +348,25 @@ index_tree_next(const index_tree_node *node) } +/// Get the previous node in the tree. Return NULL if there are no more nodes. +static void * +index_tree_prev(const index_tree_node *node) +{ + if (node->left != NULL) { + node = node->left; + while (node->right != NULL) + node = node->right; + + return (void *)(node); + } + + while (node->parent != NULL && node->parent->left == node) + node = node->parent; + + return (void *)(node->parent); +} + + /// Locate a node that contains the given uncompressed offset. It is /// caller's job to check that target is not bigger than the uncompressed /// size of the tree (the last node would be returned in that case still).