forked from sysprog21/fibdrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringNum.h
99 lines (81 loc) · 1.99 KB
/
stringNum.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <linux/device.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
typedef struct {
char *number;
unsigned int size;
} stringNum;
void reverse_str(char *str, int size)
{
for (int i = 0; i < (size >> 1); i++) {
char tmp = str[i];
str[i] = str[size - i - 1];
str[size - i - 1] = tmp;
}
}
stringNum *stringNumInit(int size)
{
stringNum *new = kmalloc(sizeof(*new), GFP_KERNEL);
char *tmp = kmalloc(sizeof(char) * (size + 1), GFP_KERNEL);
memset(tmp, '\0', size + 1);
new->number = tmp;
new->size = 0;
return new;
}
void stringNumFree(stringNum *data)
{
kfree(data->number);
kfree(data);
}
int stringToData(char *str, stringNum *data)
{
int size = strlen(str);
memset(data->number, '\0', size + 1);
memcpy(data->number, str, size);
data->size = size;
return size;
}
void stringAdd(stringNum *a, stringNum *b, stringNum *result)
{
char *num_a, *num_b;
int size_a, size_b;
int i, sum, carry = 0;
if (a->size < b->size) {
num_a = b->number;
size_a = b->size;
num_b = a->number;
size_b = a->size;
} else {
num_a = a->number;
size_a = a->size;
num_b = b->number;
size_b = b->size;
}
reverse_str(num_a, size_a);
reverse_str(num_b, size_b);
char *buf = kmalloc(sizeof(char) * (size_a + 2), GFP_KERNEL);
for (i = 0; i < size_b; i++) {
sum = (num_a[i] - '0') + (num_b[i] - '0') + carry;
buf[i] = '0' + sum % 10;
carry = sum / 10;
}
for (i = size_b; i < size_a; i++) {
sum = (num_a[i] - '0') + carry;
buf[i] = '0' + sum % 10;
carry = sum / 10;
}
if (carry) {
buf[i] = '1';
i++;
} else {
buf[i] = 0;
}
reverse_str(buf, i);
reverse_str(num_a, size_a);
reverse_str(num_b, size_b);
memcpy(result->number, buf, i);
result->size = strlen(result->number);
kfree(buf);
}