I don't know how this lib works but in general terms, replacing the switch
with a look-up table should help with readability/maintenance and performance both. Since there's just 15 items, we don't need to use much memory for such a table and can afford to leave empty items in it.
Define some struct so that it contains all that's different between the various cases, along the lines of this pseudo code:
typedef struct{ something* rsaContext;} context_t;
Then allocate a look-up table and initialize using designated initializers:
const context_t context [16] = { [1] = { &rsaContext->N }, [3] = { &rsaContext->E }, ... // and so on};
This table needs to be placed at local scope, since rsaContext
isn't a constant expression but another variable.
The way initialization works in C, it means that all context
items in the table that we didn't initialize explicitly, will get set to all zeroes, or NULL in case of pointers. So all "rsaContext" members that weren't initialized will be null pointers.
Now you can replace the whole switch with a table look-up:
something* dst = context[k].rsaContext;if(dst != NULL){ token[size-1]='\0'; mpi_read_string(dst, 16, token);}