Coverage Report

Created: 2026-11-05 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541/plugins/crypto/ua_filestore_common.c
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
 *
5
 *    Copyright 2024 (c) Fraunhofer IOSB (Author: Noel Graf)
6
 */
7
8
#include "ua_filestore_common.h"
9
10
#ifdef UA_ENABLE_ENCRYPTION
11
12
#if defined(UA_ARCHITECTURE_POSIX) || defined(UA_ARCHITECTURE_WIN32) || defined(__APPLE__)
13
14
#ifdef UA_ARCHITECTURE_WIN32
15
/* TODO: Replace with a proper dirname implementation. This is a just minimal
16
 * implementation working with correct input data. */
17
char *
18
_UA_dirname_minimal(char *path) {
19
    if(path == NULL || *path == '\0')
20
        return ".";
21
    char *lastSlash = strrchr(path, '/');
22
    if(lastSlash == NULL)
23
        return ".";
24
    *lastSlash = 0;
25
    return path;
26
}
27
#endif /* UA_ARCHITECTURE_WIN32 */
28
29
UA_StatusCode
30
0
readFileToByteString(const char *const path, UA_ByteString *data) {
31
0
    if(path == NULL || data == NULL)
32
0
        return UA_STATUSCODE_BADINTERNALERROR;
33
34
    /* Open the file */
35
0
    UA_FILE *fp = UA_fopen(path, "rb");
36
0
    if(!fp)
37
0
        return UA_STATUSCODE_BADNOTFOUND;
38
39
    /* Get the file length, allocate the data and read */
40
0
    UA_fseek(fp, 0, UA_SEEK_END);
41
0
    long fileSize = UA_ftell(fp);
42
0
    if(fileSize < 0) {
43
0
        UA_fclose(fp);
44
0
        return UA_STATUSCODE_BADINTERNALERROR;
45
0
    }
46
0
    UA_StatusCode retval = UA_ByteString_allocBuffer(data, (size_t)fileSize);
47
0
    if(retval == UA_STATUSCODE_GOOD) {
48
0
        UA_fseek(fp, 0, UA_SEEK_SET);
49
0
        size_t read = UA_fread(data->data, sizeof(UA_Byte), data->length * sizeof(UA_Byte), fp);
50
0
        if(read != data->length) {
51
0
            UA_ByteString_clear(data);
52
0
        }
53
0
    } else {
54
0
        data->length = 0;
55
0
    }
56
0
    UA_fclose(fp);
57
58
0
    return UA_STATUSCODE_GOOD;
59
0
}
60
61
UA_StatusCode
62
0
writeByteStringToFile(const char *const path, const UA_ByteString *data) {
63
0
    UA_StatusCode retval = UA_STATUSCODE_GOOD;
64
65
    /* Open the file */
66
0
    UA_FILE *fp = UA_fopen(path, "wb");
67
0
    if(!fp)
68
0
        return UA_STATUSCODE_BADINTERNALERROR;
69
70
    /* Write byte string to file */
71
0
    size_t len = UA_fwrite(data->data, sizeof(UA_Byte), data->length * sizeof(UA_Byte), fp);
72
0
    if(len != data->length)
73
0
        retval = UA_STATUSCODE_BADINTERNALERROR;
74
75
0
    UA_fclose(fp);
76
0
    return retval;
77
0
}
78
79
#endif /* defined(UA_ARCHITECTURE_POSIX) || defined(UA_ARCHITECTURE_WIN32) || defined(__APPLE__) */
80
81
#endif /* UA_ENABLE_ENCRYPTION */