Skip to content

Commit 074c1cd

Browse files
committed
apparmor: dfa move character match into a macro
Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent 9fcf78c commit 074c1cd

1 file changed

Lines changed: 27 additions & 47 deletions

File tree

‎security/apparmor/match.c‎

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,18 @@ struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
329329
return ERR_PTR(error);
330330
}
331331

332+
#define match_char(state, def, base, next, check, C) \
333+
do { \
334+
u32 b = (base)[(state)]; \
335+
unsigned int pos = base_idx(b) + (C); \
336+
if ((check)[pos] != (state)) { \
337+
(state) = (def)[(state)]; \
338+
break; \
339+
} \
340+
(state) = (next)[pos]; \
341+
break; \
342+
} while (1)
343+
332344
/**
333345
* aa_dfa_match_len - traverse @dfa to find state @str stops at
334346
* @dfa: the dfa to match @str against (NOT NULL)
@@ -352,7 +364,7 @@ unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
352364
u32 *base = BASE_TABLE(dfa);
353365
u16 *next = NEXT_TABLE(dfa);
354366
u16 *check = CHECK_TABLE(dfa);
355-
unsigned int state = start, pos;
367+
unsigned int state = start;
356368

357369
if (state == 0)
358370
return 0;
@@ -361,23 +373,13 @@ unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
361373
if (dfa->tables[YYTD_ID_EC]) {
362374
/* Equivalence class table defined */
363375
u8 *equiv = EQUIV_TABLE(dfa);
364-
/* default is direct to next state */
365-
for (; len; len--) {
366-
pos = base_idx(base[state]) + equiv[(u8) *str++];
367-
if (check[pos] == state)
368-
state = next[pos];
369-
else
370-
state = def[state];
371-
}
376+
for (; len; len--)
377+
match_char(state, def, base, next, check,
378+
equiv[(u8) *str++]);
372379
} else {
373380
/* default is direct to next state */
374-
for (; len; len--) {
375-
pos = base_idx(base[state]) + (u8) *str++;
376-
if (check[pos] == state)
377-
state = next[pos];
378-
else
379-
state = def[state];
380-
}
381+
for (; len; len--)
382+
match_char(state, def, base, next, check, (u8) *str++);
381383
}
382384

383385
return state;
@@ -402,7 +404,7 @@ unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
402404
u32 *base = BASE_TABLE(dfa);
403405
u16 *next = NEXT_TABLE(dfa);
404406
u16 *check = CHECK_TABLE(dfa);
405-
unsigned int state = start, pos;
407+
unsigned int state = start;
406408

407409
if (state == 0)
408410
return 0;
@@ -412,22 +414,13 @@ unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
412414
/* Equivalence class table defined */
413415
u8 *equiv = EQUIV_TABLE(dfa);
414416
/* default is direct to next state */
415-
while (*str) {
416-
pos = base_idx(base[state]) + equiv[(u8) *str++];
417-
if (check[pos] == state)
418-
state = next[pos];
419-
else
420-
state = def[state];
421-
}
417+
while (*str)
418+
match_char(state, def, base, next, check,
419+
equiv[(u8) *str++]);
422420
} else {
423421
/* default is direct to next state */
424-
while (*str) {
425-
pos = base_idx(base[state]) + (u8) *str++;
426-
if (check[pos] == state)
427-
state = next[pos];
428-
else
429-
state = def[state];
430-
}
422+
while (*str)
423+
match_char(state, def, base, next, check, (u8) *str++);
431424
}
432425

433426
return state;
@@ -450,27 +443,14 @@ unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
450443
u32 *base = BASE_TABLE(dfa);
451444
u16 *next = NEXT_TABLE(dfa);
452445
u16 *check = CHECK_TABLE(dfa);
453-
unsigned int pos;
454446

455447
/* current state is <state>, matching character *str */
456448
if (dfa->tables[YYTD_ID_EC]) {
457449
/* Equivalence class table defined */
458450
u8 *equiv = EQUIV_TABLE(dfa);
459-
/* default is direct to next state */
460-
461-
pos = base_idx(base[state]) + equiv[(u8) c];
462-
if (check[pos] == state)
463-
state = next[pos];
464-
else
465-
state = def[state];
466-
} else {
467-
/* default is direct to next state */
468-
pos = base_idx(base[state]) + (u8) c;
469-
if (check[pos] == state)
470-
state = next[pos];
471-
else
472-
state = def[state];
473-
}
451+
match_char(state, def, base, next, check, equiv[(u8) c]);
452+
} else
453+
match_char(state, def, base, next, check, (u8) c);
474454

475455
return state;
476456
}

0 commit comments

Comments
 (0)