Skip to content

Commit e954506

Browse files
redact_pii: memcpy-based impl with format mode split in cfg.y
1 parent 4c924ae commit e954506

3 files changed

Lines changed: 85 additions & 20 deletions

File tree

cfg.y

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,20 @@ assign_stm: LOGLEVEL EQUAL snumber { IFOR();
15981598
| REDACT_PII_ error { yyerror("boolean value expected"); }
15991599
| REDACT_TEMPLATE EQUAL STRING { IFOR();
16001600
redact_template=$3;
1601+
if (redact_mode == REDACT_FORMAT) {
1602+
char *pct = strstr(redact_template, "%s");
1603+
if (pct) {
1604+
redact_fmt.left.s = redact_template;
1605+
redact_fmt.left.len = pct - redact_template;
1606+
redact_fmt.right.s = pct + 2;
1607+
redact_fmt.right.len = strlen(pct + 2);
1608+
} else {
1609+
redact_fmt.left.s = redact_template;
1610+
redact_fmt.left.len = strlen(redact_template);
1611+
redact_fmt.right.s = "";
1612+
redact_fmt.right.len = 0;
1613+
}
1614+
}
16011615
}
16021616
| REDACT_TEMPLATE error { yyerror("string value expected"); }
16031617
| REDACT_MODE EQUAL STRING { IFOR();
@@ -1607,8 +1621,23 @@ assign_stm: LOGLEVEL EQUAL snumber { IFOR();
16071621
redact_mode=REDACT_APPEND;
16081622
else if (strcasecmp($3, "prepend")==0)
16091623
redact_mode=REDACT_PREPEND;
1610-
else if (strcasecmp($3, "format")==0)
1624+
else if (strcasecmp($3, "format")==0) {
16111625
redact_mode=REDACT_FORMAT;
1626+
if (redact_template) {
1627+
char *pct = strstr(redact_template, "%s");
1628+
if (pct) {
1629+
redact_fmt.left.s = redact_template;
1630+
redact_fmt.left.len = pct - redact_template;
1631+
redact_fmt.right.s = pct + 2;
1632+
redact_fmt.right.len = strlen(pct + 2);
1633+
} else {
1634+
redact_fmt.left.s = redact_template;
1635+
redact_fmt.left.len = strlen(redact_template);
1636+
redact_fmt.right.s = "";
1637+
redact_fmt.right.len = 0;
1638+
}
1639+
}
1640+
}
16121641
else
16131642
yyerror("redact_mode must be: replace|append|prepend|format");
16141643
}

redact_pii.c

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,59 @@
11
#include <string.h>
2-
#include <stdio.h>
32
#include "ut.h"
43
#include "redact_pii.h"
54

65
int redact_pii_ = 0;
76
char *redact_template = "****";
87
int redact_mode = REDACT_REPLACE;
8+
redact_log_format_t redact_fmt = {{NULL, 0}, {NULL, 0}};
9+
10+
#define REDACT_BUF_SIZE 512
911

1012
inline const char* redact_pii(const char* input) {
11-
static char buf[512];
12-
const char *safe = ZSW(input);
13+
static char buf[REDACT_BUF_SIZE];
14+
const char *safe = ZSW(input);
15+
size_t input_len, idx;
1316

14-
if (!redact_pii_)
15-
return safe;
17+
if (!redact_pii_)
18+
return safe;
1619

17-
switch (redact_mode) {
18-
case REDACT_APPEND:
19-
snprintf(buf, sizeof(buf), "%s%s", safe, redact_template);
20-
return buf;
21-
case REDACT_PREPEND:
22-
snprintf(buf, sizeof(buf), "%s%s", redact_template, safe);
23-
return buf;
24-
case REDACT_FORMAT:
25-
snprintf(buf, sizeof(buf), redact_template, safe);
26-
return buf;
27-
case REDACT_REPLACE:
28-
default:
29-
return redact_template;
30-
}
20+
switch (redact_mode) {
21+
case REDACT_REPLACE:
22+
return redact_template;
23+
case REDACT_APPEND:
24+
input_len = strlen(safe);
25+
idx = 0;
26+
memcpy(buf + idx, safe, input_len);
27+
idx += input_len;
28+
memcpy(buf + idx, redact_template, strlen(redact_template));
29+
idx += strlen(redact_template);
30+
buf[idx] = '\0';
31+
return buf;
32+
case REDACT_PREPEND:
33+
input_len = strlen(safe);
34+
idx = 0;
35+
memcpy(buf + idx, redact_template, strlen(redact_template));
36+
idx += strlen(redact_template);
37+
memcpy(buf + idx, safe, input_len);
38+
idx += input_len;
39+
buf[idx] = '\0';
40+
return buf;
41+
case REDACT_FORMAT:
42+
input_len = strlen(safe);
43+
idx = 0;
44+
if (redact_fmt.left.len > 0) {
45+
memcpy(buf + idx, redact_fmt.left.s, redact_fmt.left.len);
46+
idx += redact_fmt.left.len;
47+
}
48+
memcpy(buf + idx, safe, input_len);
49+
idx += input_len;
50+
if (redact_fmt.right.len > 0) {
51+
memcpy(buf + idx, redact_fmt.right.s, redact_fmt.right.len);
52+
idx += redact_fmt.right.len;
53+
}
54+
buf[idx] = '\0';
55+
return buf;
56+
default:
57+
return redact_template;
58+
}
3159
}

redact_pii.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,24 @@
2020
#ifndef redact_pii_h
2121
#define redact_pii_h
2222

23+
#include "str.h"
24+
2325
enum {
2426
REDACT_REPLACE = 0,
2527
REDACT_APPEND,
2628
REDACT_PREPEND,
2729
REDACT_FORMAT
2830
};
2931

32+
typedef struct {
33+
str left;
34+
str right;
35+
} redact_log_format_t;
36+
3037
extern int redact_pii_;
3138
extern char *redact_template;
3239
extern int redact_mode;
40+
extern redact_log_format_t redact_fmt;
3341

3442
const char* redact_pii(const char* input);
3543

0 commit comments

Comments
 (0)