| 1 | /* |
| 2 | * Copyright 2010 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Lukas Kuklinek <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | // topological sort |
| 25 | |
| 26 | #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`! |
| 27 | #include <config.h>cxx . . $ `<file>/build/config.h`/ |
| 28 | #endif |
| 29 | |
| 30 | #include "tsort.h"cxx . . $ `<file>/src/common/tsort.h`/ |
| 31 | |
| 32 | struct oscap_tsort_contextcxx . . $ oscap_tsort_context# { |
| 33 | struct oscap_listcxx . . $ oscap_list# *visitedcxx . . $ oscap_tsort_context#visited.; |
| 34 | struct oscap_listcxx . . $ oscap_list# *cur_stackcxx . . $ oscap_tsort_context#cur_stack.; |
| 35 | struct oscap_listcxx . . $ oscap_list# *resultcxx . . $ oscap_tsort_context#result.; |
| 36 | oscap_tsort_edge_funccxx . . $ oscap_tsort_edge_func# edge_funccxx . . $ oscap_tsort_context#edge_func.; |
| 37 | oscap_cmp_funccxx . . $ oscap_cmp_func# cmp_funccxx . . $ oscap_tsort_context#cmp_func.; |
| 38 | void *userdatacxx . . $ oscap_tsort_context#userdata.; |
| 39 | }; |
| 40 | |
| 41 | static struct oscap_tsort_contextcxx . . $ oscap_tsort_context# *oscap_tsort_context_newcxx . . $ oscap_tsort_context_new(7f584bc006ca4966).(oscap_tsort_edge_funccxx . . $ oscap_tsort_edge_func# edge_func, oscap_cmp_funccxx . . $ oscap_cmp_func# cmp_func, void *userdata) |
| 42 | { |
| 43 | struct oscap_tsort_contextcxx . . $ oscap_tsort_context# *ctx = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(struct oscap_tsort_contextcxx . . $ oscap_tsort_context#)); |
| 44 | ctx->visitedcxx . . $ oscap_tsort_context#visited. = oscap_list_newcxx . . $ oscap_list_new(ea215f73c8361bff).(); |
| 45 | ctx->cur_stackcxx . . $ oscap_tsort_context#cur_stack. = oscap_list_newcxx . . $ oscap_list_new(ea215f73c8361bff).(); |
| 46 | ctx->resultcxx . . $ oscap_tsort_context#result. = oscap_list_newcxx . . $ oscap_list_new(ea215f73c8361bff).(); |
| 47 | ctx->edge_funccxx . . $ oscap_tsort_context#edge_func. = edge_func; |
| 48 | ctx->cmp_funccxx . . $ oscap_tsort_context#cmp_func. = cmp_func; |
| 49 | ctx->userdatacxx . . $ oscap_tsort_context#userdata. = userdata; |
| 50 | return ctx; |
| 51 | } |
| 52 | |
| 53 | static void oscap_tsort_context_freecxx . . $ oscap_tsort_context_free(346cda78b60cb8d4).(struct oscap_tsort_contextcxx . . $ oscap_tsort_context# *ctx) |
| 54 | { |
| 55 | if (ctx != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 56 | oscap_list_freecxx . . $ oscap_list_free(e68e105c3900fcc1).(ctx->visitedcxx . . $ oscap_tsort_context#visited., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 57 | oscap_list_freecxx . . $ oscap_list_free(e68e105c3900fcc1).(ctx->cur_stackcxx . . $ oscap_tsort_context#cur_stack., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 58 | oscap_list_freecxx . . $ oscap_list_free(e68e105c3900fcc1).(ctx->resultcxx . . $ oscap_tsort_context#result., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 59 | freecxx . . $ free(5d22bdacc48458e8).(ctx); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_tsort_visitcxx . . $ oscap_tsort_visit(3e96513ce085786f).(void *node, struct oscap_tsort_contextcxx . . $ oscap_tsort_context#* ctx); |
| 64 | |
| 65 | inline static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_tsort_visit_allcxx . . $ oscap_tsort_visit_all(ffd7006c188e0dfe).(struct oscap_listcxx . . $ oscap_list# *nodelist, struct oscap_tsort_contextcxx . . $ oscap_tsort_context#* ctx) |
| 66 | { |
| 67 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! ret = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 68 | struct oscap_iteratorcxx . . $ oscap_iterator# *it = oscap_iterator_newcxx . . $ oscap_iterator_new(f89fb456dfd589d7).(nodelist); |
| 69 | |
| 70 | while (oscap_iterator_has_morecxx . . $ oscap_iterator_has_more(63b1de9a5adb9205).(it)) { |
| 71 | if (!oscap_tsort_visitcxx . . $ oscap_tsort_visit(3e96513ce085786f).(oscap_iterator_nextcxx . . $ oscap_iterator_next(4c75051c806960eb).(it), ctx)) { |
| 72 | ret = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | oscap_iterator_freecxx . . $ oscap_iterator_free(3cba44c1b7872358).(it); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_tsort_visitcxx . . $ oscap_tsort_visit(3e96513ce085786f).(void *node, struct oscap_tsort_contextcxx . . $ oscap_tsort_context#* ctx) |
| 82 | { |
| 83 | // loop detection |
| 84 | if (oscap_list_containscxx . . $ oscap_list_contains(810ee1985baa5ebe).(ctx->cur_stackcxx . . $ oscap_tsort_context#cur_stack., node, ctx->cmp_funccxx . . $ oscap_tsort_context#cmp_func.)) return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 85 | |
| 86 | // skip already visited node |
| 87 | if (oscap_list_containscxx . . $ oscap_list_contains(810ee1985baa5ebe).(ctx->visitedcxx . . $ oscap_tsort_context#visited., node, ctx->cmp_funccxx . . $ oscap_tsort_context#cmp_func.)) return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 88 | |
| 89 | // mark as visited & update stack |
| 90 | oscap_list_addcxx . . $ oscap_list_add(97f28760a5cf7823).(ctx->visitedcxx . . $ oscap_tsort_context#visited., node); |
| 91 | oscap_list_pushcxx . . $ oscap_list_push(97f28760a5cf7823).(ctx->cur_stackcxx . . $ oscap_tsort_context#cur_stack., node); |
| 92 | |
| 93 | // visit all next nodes (dependencies) |
| 94 | struct oscap_listcxx . . $ oscap_list# *next = ctx->edge_funccxx . . $ oscap_tsort_context#edge_func.(node, ctx->userdatacxx . . $ oscap_tsort_context#userdata.); |
| 95 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! ret = oscap_tsort_visit_allcxx . . $ oscap_tsort_visit_all(ffd7006c188e0dfe).(next, ctx); |
| 96 | oscap_list_freecxx . . $ oscap_list_free(e68e105c3900fcc1).(next, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 97 | |
| 98 | // update stack & add node to result |
| 99 | if (ret) oscap_list_popcxx . . $ oscap_list_pop(8038dc059d248e3c).(ctx->cur_stackcxx . . $ oscap_tsort_context#cur_stack., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 100 | oscap_list_addcxx . . $ oscap_list_add(97f28760a5cf7823).(ctx->resultcxx . . $ oscap_tsort_context#result., node); |
| 101 | |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! oscap_tsortcxx . . $ oscap_tsort(88213e84dd161bc4).(struct oscap_listcxx . . $ oscap_list# *input, struct oscap_listcxx . . $ oscap_list# **output, oscap_tsort_edge_funccxx . . $ oscap_tsort_edge_func# edge_func, oscap_cmp_funccxx . . $ oscap_cmp_func# cmp_func, void *userdata) |
| 106 | { |
| 107 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(edge_func != NULL); |
| 108 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(input != NULL); |
| 109 | |
| 110 | if (output != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) *output = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 111 | if (cmp_func == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) cmp_func = oscap_ptr_cmpcxx . . $ oscap_ptr_cmp(838b4d1787e4a177).; |
| 112 | |
| 113 | struct oscap_tsort_contextcxx . . $ oscap_tsort_context# *ctx = oscap_tsort_context_newcxx . . $ oscap_tsort_context_new(7f584bc006ca4966).(edge_func, cmp_func, userdata); |
| 114 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! ret = oscap_tsort_visit_allcxx . . $ oscap_tsort_visit_all(ffd7006c188e0dfe).(input, ctx); |
| 115 | |
| 116 | if (output != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 117 | if (ret) { |
| 118 | // the graph has been topologically sorted, return result |
| 119 | *output = ctx->resultcxx . . $ oscap_tsort_context#result.; |
| 120 | ctx->resultcxx . . $ oscap_tsort_context#result. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 121 | } |
| 122 | else { |
| 123 | // a loop has been ecountered in the graph, return the loop |
| 124 | *output = ctx->cur_stackcxx . . $ oscap_tsort_context#cur_stack.; |
| 125 | ctx->cur_stackcxx . . $ oscap_tsort_context#cur_stack. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | oscap_tsort_context_freecxx . . $ oscap_tsort_context_free(346cda78b60cb8d4).(ctx); |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | |
| 135 |