Coverage Report

Created: 2023-06-07 06:06

/src/open62541/deps/itoa.c
Line
Count
Source (jump to first uncovered line)
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
0
static void swap(char *x, char *y) {
28
0
    char t = *x;
29
0
    *x = *y;
30
0
    *y = t;
31
0
}
32
33
/* function to reverse buffer */
34
0
static char* reverse(char *buffer, UA_UInt16 i, UA_UInt16 j) {
35
0
    while (i < j)
36
0
        swap(&buffer[i++], &buffer[j--]);
37
38
0
    return buffer;
39
0
}
40
41
/* adapted from http://www.techiedelight.com/implement-itoa-function-in-c/ to use UA_... types */
42
0
UA_UInt16 itoaUnsigned(UA_UInt64 value, char* buffer, UA_Byte base) {
43
    /* consider absolute value of number */
44
0
    UA_UInt64 n = value;
45
46
0
    UA_UInt16 i = 0;
47
0
    while (n) {
48
0
        UA_UInt64 r = n % base;
49
50
0
        if (r >= 10)
51
0
            buffer[i++] = (char)(65 + (r - 10));
52
0
        else
53
0
            buffer[i++] = (char)(48 + r);
54
55
0
        n = n / base;
56
0
    }
57
    /* if number is 0 */
58
0
    if (i == 0)
59
0
        buffer[i++] = '0';
60
61
0
    buffer[i] = '\0'; /* null terminate string */
62
0
    i--;
63
    /* reverse the string */
64
0
    reverse(buffer, 0, i);
65
0
    i++;
66
0
    return i;
67
0
}
68
69
/* adapted from http://www.techiedelight.com/implement-itoa-function-in-c/ to use UA_... types */
70
0
UA_UInt16 itoaSigned(UA_Int64 value, char* buffer) {
71
    /* consider absolute value of number */
72
73
74
0
    UA_UInt64 n;
75
76
    /* Special case for UA_INT64_MIN which can not simply be negated */
77
    /* it will cause a signed integer overflow */
78
0
    if (value == UA_INT64_MIN) {
79
0
        n = (UA_UInt64)UA_INT64_MAX + 1;
80
0
    }
81
0
    else {
82
0
        n = (UA_UInt64)value;
83
84
0
        if(value < 0){
85
0
            n = (UA_UInt64)-value;
86
0
        }
87
0
    }
88
89
0
    UA_UInt16 i = 0;
90
0
    while (n) {
91
0
        UA_UInt64 r = n % 10;
92
93
0
        if (r >= 10)
94
0
            buffer[i++] = (char)(65 + (r - 10));
95
0
        else
96
0
            buffer[i++] = (char)(48 + r);
97
98
0
        n = n / 10;
99
0
    }
100
101
    /* if number is 0 */
102
0
    if (i == 0)
103
0
        buffer[i++] = '0';
104
105
0
    if (value < 0)
106
0
        buffer[i++] = '-';
107
108
0
    buffer[i] = '\0'; /* null terminate string */
109
0
    i--;
110
    /* reverse the string and return it */
111
0
    reverse(buffer, 0, i);
112
0
    i++;
113
0
    return i;
114
0
}
115