Coverage Report

Created: 2025-11-11 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541/deps/itoa.c
Line
Count
Source
1
/*
2
 * Copyright 2017 Techie Delight
3
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
4
 * of this software and associated documentation files (the "Software"), to deal
5
 * in the Software without restriction, including without limitation the rights 
6
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
7
 * copies of the Software, and to permit persons to whom the Software is 
8
 * furnished to do so, subject to the following conditions:
9
 * 
10
 * The above copyright notice and this permission notice shall be included 
11
 * in all copies or substantial portions of the Software.
12
 * 
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
14
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
15
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
17
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 
18
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
 */
20
21
/* Originally released by techiedelight.com 
22
 * (http://www.techiedelight.com/implement-itoa-function-in-c/) under the
23
 * MIT license. */
24
25
#include "itoa.h"
26
27
91.5k
static void swap(char *x, char *y) {
28
91.5k
    char t = *x;
29
91.5k
    *x = *y;
30
91.5k
    *y = t;
31
91.5k
}
32
33
/* function to reverse buffer */
34
11.8M
static char* reverse(char *buffer, UA_UInt16 i, UA_UInt16 j) {
35
11.9M
    while (i < j)
36
91.5k
        swap(&buffer[i++], &buffer[j--]);
37
38
11.8M
    return buffer;
39
11.8M
}
40
41
/* adapted from http://www.techiedelight.com/implement-itoa-function-in-c/ to use UA_... types */
42
8.82M
UA_UInt16 itoaUnsigned(UA_UInt64 value, char* buffer, UA_Byte base) {
43
    /* consider absolute value of number */
44
8.82M
    UA_UInt64 n = value;
45
46
8.82M
    UA_UInt16 i = 0;
47
16.1M
    while (n) {
48
7.32M
        UA_UInt64 r = n % base;
49
50
7.32M
        if (r >= 10)
51
0
            buffer[i++] = (char)(65 + (r - 10));
52
7.32M
        else
53
7.32M
            buffer[i++] = (char)(48 + r);
54
55
7.32M
        n = n / base;
56
7.32M
    }
57
    /* if number is 0 */
58
8.82M
    if (i == 0)
59
1.56M
        buffer[i++] = '0';
60
61
8.82M
    buffer[i] = '\0'; /* null terminate string */
62
8.82M
    i--;
63
    /* reverse the string */
64
8.82M
    reverse(buffer, 0, i);
65
8.82M
    i++;
66
8.82M
    return i;
67
8.82M
}
68
69
/* adapted from http://www.techiedelight.com/implement-itoa-function-in-c/ */
70
2.99M
UA_UInt16 itoaSigned(UA_Int64 value, char* buffer) {
71
    /* Special case for UA_INT64_MIN which can not simply be negated */
72
    /* it will cause a signed integer overflow */
73
2.99M
    UA_UInt64 n;
74
2.99M
    if(value == UA_INT64_MIN) {
75
456
        n = (UA_UInt64)UA_INT64_MAX + 1;
76
2.99M
    } else {
77
2.99M
        n = (UA_UInt64)value;
78
2.99M
        if(value < 0){
79
4.26k
            n = (UA_UInt64)-value;
80
4.26k
        }
81
2.99M
    }
82
83
2.99M
    UA_UInt16 i = 0;
84
4.56M
    while(n) {
85
1.56M
        UA_UInt64 r = n % 10;
86
1.56M
        buffer[i++] = (char)('0' + r);
87
1.56M
        n = n / 10;
88
1.56M
    }
89
90
2.99M
    if(i == 0)
91
1.49M
        buffer[i++] = '0'; /* if number is 0 */
92
2.99M
    if(value < 0)
93
4.71k
        buffer[i++] = '-';
94
2.99M
    buffer[i] = '\0'; /* null terminate string */
95
2.99M
    i--;
96
2.99M
    reverse(buffer, 0, i); /* reverse the string and return it */
97
2.99M
    i++;
98
2.99M
    return i;
99
2.99M
}
100