blob: f4e7604355cde63500834cbf49fd36725435f329 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1fix_internal.h"
2
3struct _cstring_pattern {
4 char *start;
Lev Walkin4efbfb72005-02-25 14:20:30 +00005 size_t length;
Lev Walkinf15320b2004-06-03 03:38:44 +00006};
7static int _asn1f_cstring_find_line_pattern(char *s, struct _cstring_pattern *);
8
9int
10asn1f_fix_cstring(arg_t *arg) {
11 asn1p_expr_t *expr = arg->expr;
12 int r_value = 0;
13
14 if(expr->value && expr->value->type == ATV_STRING) {
15 struct _cstring_pattern cp;
Lev Walkin84fbd722005-06-15 18:41:50 +000016 char *buf = (char *)expr->value->value.string.buf;
Lev Walkinf15320b2004-06-03 03:38:44 +000017 int buflen = expr->value->value.string.size;
18 int start = 0;
19
Lev Walkin03850182005-03-10 10:02:50 +000020 DEBUG("(%s) for line %d", expr->Identifier, expr->_lineno);
Lev Walkinf15320b2004-06-03 03:38:44 +000021
22 while(_asn1f_cstring_find_line_pattern(buf + start, &cp)) {
23 assert(cp.length);
24 memmove(cp.start, cp.start + cp.length,
25 buflen - ((cp.start + cp.length) - buf));
26 buflen -= cp.length;
27 start = cp.start - buf;
28 buf[buflen] = '\0';
29 }
30 }
31
32 return r_value;
33}
34
35/*
36 * If a string has a newline, the tabulation and spaces before and
37 * after it must be eliminated.
38 */
39static int
40_asn1f_cstring_find_line_pattern(char *s, struct _cstring_pattern *cp) {
41 int newline_found = 0;
42
43 cp->start = NULL;
44
45 for(;;s++) {
46 switch(*s) {
47 case '\r': case '\n':
48 newline_found = 1;
49 /* Fall through */
50 case ' ': case '\t':
51 if(cp->start == NULL)
52 cp->start = s;
53 continue;
54 case '\0':
55 default:
56 if(newline_found) {
Lev Walkin4efbfb72005-02-25 14:20:30 +000057 cp->length = (size_t)(s - cp->start);
Lev Walkinf15320b2004-06-03 03:38:44 +000058 return 1;
59 }
60
61 cp->start = NULL;
62 if(*s == '\0')
63 break;
64 continue;
65 }
66 break;
67 }
68
69 return 0;
70}
71
72