| 1 | /* |
| 2 | * Copyright 2013--2014 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 | * Šimon Lukašík |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <libxml/xmlreader.h> |
| 29 | |
| 30 | #include "cpe_ctx_priv.h" |
| 31 | #include "common/_error.h" |
| 32 | #include "common/elements.h" |
| 33 | |
| 34 | struct cpe_parser_ctx { |
| 35 | xmlTextReaderPtr reader; |
| 36 | bool owns_reader; |
| 37 | char *schema_version; |
| 38 | }; |
| 39 | |
| 40 | static inline struct cpe_parser_ctx *_cpe_parser_ctx_new() |
| 41 | { |
| 42 | return calloc(1, sizeof(struct cpe_parser_ctx)); |
| 43 | } |
| 44 | |
| 45 | struct cpe_parser_ctx *cpe_parser_ctx_from_reader(xmlTextReaderPtr reader) |
| 46 | { |
| 47 | struct cpe_parser_ctx *ctx = _cpe_parser_ctx_new(); |
| 48 | ctx->reader = reader; |
| 49 | ctx->owns_reader = false; |
| 50 | return ctx; |
| 51 | } |
| 52 | |
| 53 | void cpe_parser_ctx_free(struct cpe_parser_ctx *ctx) |
| 54 | { |
| 55 | if (ctx) { |
| 56 | if (ctx->reader != NULL && ctx->owns_reader) |
| 57 | xmlFreeTextReader(ctx->reader); |
| 58 | free(ctx->schema_version); |
| 59 | free(ctx); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static inline const char *_cpe_parser_ctx_get_schema_version(struct cpe_parser_ctx *ctx) |
| 64 | { |
| 65 | return (ctx->schema_version == NULL) ? "2.3" : ctx->schema_version; |
| 66 | } |
| 67 | |
| 68 | static int _cpe_parser_ctx_version_cmp(struct cpe_parser_ctx *ctx, const char *version) |
| 69 | { |
| 70 | #ifdef __USE_GNU |
| 71 | return strverscmp(_cpe_parser_ctx_get_schema_version(ctx), version); |
| 72 | #else |
| 73 | return strcmp(_cpe_parser_ctx_get_schema_version(ctx), version); |
| 74 | #endif |
| 75 | } |
| 76 | |
| 77 | bool cpe_parser_ctx_version_gt(struct cpe_parser_ctx *ctx, const char *version) |
| 78 | { |
| 79 | return _cpe_parser_ctx_version_cmp(ctx, version) > 0; |
| 80 | } |
| 81 | |
| 82 | OSCAP_GETTER(xmlTextReaderPtr, cpe_parser_ctx, reader); |
| 83 | OSCAP_SETTER_GENERIC(cpe_parser_ctx, const char *, schema_version, free, oscap_strdup); |