-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring_functions.c
More file actions
267 lines (253 loc) · 8.05 KB
/
string_functions.c
File metadata and controls
267 lines (253 loc) · 8.05 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* This file is part of the ttyterm project.
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include "string_functions.h"
// end of line - for text mode
static char *eol = "\n";
static int eollen = 1;
// read text string and throw out all < 31 and > 126
static inline const char *omit_nonletters(disptype input_type, const char *line){
int start = (input_type == DISP_TEXT) ? 31 : 32; // remove spaces for non-TEXT modes
while(*line){
char c = *line;
if(c > start && c < 127) break;
++line;
}
return line;
}
// return hex of given sybol (0..9, a..f or A..F) or -1
static inline int hex2i(char c){
if(c >= '0' && c <= '9') return c - '0';
else if(c >= 'A' && c <= 'F') return c - 'A' + 10;
else if(c >= 'a' && c <= 'f') return c - 'a' + 10;
return -1;
}
static inline const char *getbin(const char *line, int *ch){
char c = *line;
if(c < '0' || c > '1'){
*ch = -1;
return line+1;
}
int num = 0, ctr = 8;
do{
if(--ctr < 0) break; // nineth symbol
if(c < '0' || c > '1'){
break;
}
num <<= 1;
if(c == '1') num |= 1;
++line;
}while((c = *line));
*ch = num < 256 ? num : -1;
return line;
}
/**
* @brief getoct read octal number without first '0' (up to three symbols)
* @param line - pointer to string
* @param ch - data read (or -1 if error)
* @return pointer to next symbol after this (or first incorrect symbol)
*/
static inline const char *getoct(const char *line, int *ch){
char c = *line;
if(c < '0' || c > '7'){
*ch = -1;
return line+1;
}
int num = 0, ctr = 3;
do{
if(--ctr < 0) break; // fourth symbol
if(c < '0' || c > '7'){ // other symbol
break;
}
num <<= 3;
num += c - '0';
++line;
}while((c = *line));
*ch = num < 256 ? num : -1;
return line;
}
static inline const char *getdec(const char *line, int *ch){
char c = *line;
if(c < '0' || c > '9'){
*ch = -1;
return line+1;
}
int num = 0, ctr = 3;
do{
if(--ctr < 0) break; // fourth symbol
if(c < '0' || c > '9'){ // other symbol
break;
}
num *= 10;
num += c - '0';
++line;
}while((c = *line));
*ch = num < 256 ? num : -1;
return line;
}
/**
* @brief gethex - read hex number without first '\x'
* @param line - pointer to string
* @param ch - data read (or -1 if error)
* @return pointer to next symbol after hex number (x or xx)
*/
static inline const char *gethex(const char *line, int *ch){
int i = hex2i(*line++);
if(i > -1){
int j = hex2i(*line);
if(j > -1){
i = (i<<4) | j;
++line;
}
}
*ch = i;
return line;
}
/**
* @brief getspec - get special symbol (after '\') (without unicode support!)
* @param line - pointer to string
* @param ch - data read (or -1 if error)
* @return pointer to next symbol after this
*/
static inline const char *getspec(const char *line, int *ch){
if(!*line){ *ch = -1; return line; }
int got = -1, s = *line++; // next symbol after '\'
if(s >= '0' && s <= '7'){ // octal symbol
line = getoct(line-1, &got);
}else switch(s){
case 'a': got = '\a'; break;
case 'b': got = '\b'; break;
case 'e': got = '\e'; break;
case 'f': got = '\f'; break;
case 'n': got = '\n'; break;
case 'r': got = '\r'; break;
case 't': got = '\t'; break;
case 'v': got = '\v'; break;
case 'x': line = gethex(line, &got); break; // hex symbol
default:// return "as is" all other printable symbols
if(s > 31 && s < 127) got = s;
break;
}
*ch = got;
return line;
}
/*
static inline const char* getu32(const char *str, uint32_t *val){
char *eptr;
if(!str) return NULL;
long ll = strtol(str, &eptr, 0);
if(ll < 0 || ll > UINT32_MAX){ // wrong number
return NULL;
}
if(val) *val = (uint32_t)ll;
return omit_nonletters(DISP_RTU, eptr);
}*/
/**
* @brief convert_and_send - convert input line and send it (in text mode add `eol`)
* @param line - line with data
* @return amount of bytes sent, 0 if error or -1 if disconnect
*/
int convert_and_send(disptype input_type, const char *line){
static uint8_t *buf = NULL;
static size_t bufsiz = 0;
size_t curpos = 0; // position in `buf`
line = omit_nonletters(input_type, line);
DBG("got: '%s' to send", line);
void CHKbufsiz(size_t sz){
if(curpos + sz >= bufsiz){ // out ouptut buffer can't be larger than input
bufsiz += BUFSIZ;
buf = realloc(buf, bufsiz);
}
}
while(*line){
CHKbufsiz(1);
int ch = -1;
switch(input_type){
case DISP_TEXT: // only check for '\'
ch = *line++;
if(ch == '\\') line = getspec(line, &ch);
break;
case DISP_RAW: // read next uint8_t and put into buffer
case DISP_RTURAW: // the same (but calculate CRC at the end)
ch = *line++;
if(ch == '0'){ // number: 0, 0xHH, 0OOO, 0bBBBBBBBB
ch = *line;
switch(ch){
case 'x': // hexadecimal
case 'X':
line = gethex(line + 1, &ch);
break;
case 'b':
case 'B':
line = getbin(line + 1, &ch);
break;
default: // zero or octal
if(ch >= '0' && ch <= '7') line = getoct(line, &ch);
else ch = 0;
break;
}
}else if(ch > '0' && ch <= '9'){ // decimal number
line = getdec(line-1, &ch);
} // else - letter (without escape-symbols!)
break;
case DISP_HEX: // read next 2 hex bytes and put into buffer
case DISP_RTUHEX: // the same (but calculate CRC at the end)
line = gethex(line, &ch);
break;
default:
return 0; // unknown display type
}
if(ch > -1) buf[curpos++] = ch;
line = omit_nonletters(input_type, line);
}
// now insert EOL in text mode
if(input_type == DISP_TEXT){
if(curpos+eollen >= bufsiz){
bufsiz += BUFSIZ;
buf = realloc(buf, bufsiz);
}
memcpy(buf+curpos, eol, eollen);
curpos += eollen;
DBG("Add EOL");
}else if(input_type == DISP_RTURAW || input_type == DISP_RTUHEX){ // calculate CRC
CHKbufsiz(2);
uint16_t crc = 0xFFFF;
for(size_t pos = 0; pos < curpos; ++pos){
crc ^= (uint16_t)buf[pos];
for(int i = 8; i; --i){
if((crc & 1)){
crc >>= 1;
crc ^= 0xA001;
}else crc >>= 1;
}
}
buf[curpos++] = crc & 0xff; // Lo
buf[curpos++] = crc >> 8; // Hi
}
return SendData(buf, curpos);
}
/**
* @brief changeeol - set EOL to given
* @param e - new end of line for text mode
* WARNING! This function doesn't call free(), so don't call it more than once
*/
void changeeol(const char *e){
eollen = strlen(e);
eol = strdup(e);
}