Coverage Report

Created: 2025-08-29 06:10

/src/open62541/plugins/crypto/ua_filestore_common.c
Line
Count
Source (jump to first uncovered line)
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
    char *lastSlash = strrchr(path, '/');
20
    *lastSlash = 0;
21
    return path;
22
}
23
#endif /* UA_ARCHITECTURE_WIN32 */
24
25
UA_StatusCode
26
0
readFileToByteString(const char *const path, UA_ByteString *data) {
27
0
    if(path == NULL || data == NULL)
28
0
        return UA_STATUSCODE_BADINTERNALERROR;
29
30
    /* Open the file */
31
0
    UA_FILE *fp = UA_fopen(path, "rb");
32
0
    if(!fp)
33
0
        return UA_STATUSCODE_BADNOTFOUND;
34
35
    /* Get the file length, allocate the data and read */
36
0
    UA_fseek(fp, 0, UA_SEEK_END);
37
0
    UA_StatusCode retval = UA_ByteString_allocBuffer(data, (size_t)UA_ftell(fp));
38
0
    if(retval == UA_STATUSCODE_GOOD) {
39
0
        UA_fseek(fp, 0, UA_SEEK_SET);
40
0
        size_t read = UA_fread(data->data, sizeof(UA_Byte), data->length * sizeof(UA_Byte), fp);
41
0
        if(read != data->length) {
42
0
            UA_ByteString_clear(data);
43
0
        }
44
0
    } else {
45
0
        data->length = 0;
46
0
    }
47
0
    UA_fclose(fp);
48
49
0
    return UA_STATUSCODE_GOOD;
50
0
}
51
52
UA_StatusCode
53
0
writeByteStringToFile(const char *const path, const UA_ByteString *data) {
54
0
    UA_StatusCode retval = UA_STATUSCODE_GOOD;
55
56
    /* Open the file */
57
0
    UA_FILE *fp = UA_fopen(path, "wb");
58
0
    if(!fp)
59
0
        return UA_STATUSCODE_BADINTERNALERROR;
60
61
    /* Write byte string to file */
62
0
    size_t len = UA_fwrite(data->data, sizeof(UA_Byte), data->length * sizeof(UA_Byte), fp);
63
0
    if(len != data->length) {
64
0
        UA_fclose(fp);
65
0
        retval = UA_STATUSCODE_BADINTERNALERROR;
66
0
    }
67
68
0
    UA_fclose(fp);
69
0
    return retval;
70
0
}
71
72
#endif /* defined(UA_ARCHITECTURE_POSIX) || defined(UA_ARCHITECTURE_WIN32) || defined(__APPLE__) */
73
74
#endif /* UA_ENABLE_ENCRYPTION */