void randomizeBuffer(byte_t * buf, uint_t len) { uint_t i; for (i = 0; i < len; ++i) { buf[i] = getRandomByte(); } }
LOCAL bool parseDefinition(const char *s, Definition *definition, uint64 maxPosition) { StringTokenizer stringTokenizer; String t; String w; uint length; char buffer[1024]; assert(definition != NULL); t = String_newCString(s); String_initTokenizer(&stringTokenizer,t,STRING_BEGIN,":",NULL,FALSE); /* get type */ if (String_getNextToken(&stringTokenizer,&w,NULL)) { if (String_equalsCString(w,"m")) definition->type = DEFINITION_TYPE_MODIFY; else if (String_equalsCString(w,"r")) definition->type = DEFINITION_TYPE_RANDOMIZE; else if (String_equalsCString(w,"i")) definition->type = DEFINITION_TYPE_INSERT; else if (String_equalsCString(w,"d")) definition->type = DEFINITION_TYPE_DELETE; else { String_doneTokenizer(&stringTokenizer); String_delete(t); fprintf(stderr,"ERROR: Invalid definition '%s': expected m,i,d!\n",s); return FALSE; } } else { String_doneTokenizer(&stringTokenizer); String_delete(t); fprintf(stderr,"ERROR: Invalid definition '%s'!\n",s); return FALSE; } /* get position */ if (String_getNextToken(&stringTokenizer,&w,NULL)) { if (!String_scan(w,STRING_BEGIN,"%llu",&definition->position)) { String_doneTokenizer(&stringTokenizer); String_delete(t); fprintf(stderr,"ERROR: Invalid position in definition '%s'!\n",s); return FALSE; } } else { definition->position = getRandomInteger64(maxPosition); } /* get value/length */ if (String_getNextToken(&stringTokenizer,&w,NULL)) { switch (definition->type) { case DEFINITION_TYPE_MODIFY: definition->value = String_new(); if (!String_scan(w,STRING_BEGIN,"%S",definition->value)) { String_delete(definition->value); String_doneTokenizer(&stringTokenizer); String_delete(t); fprintf(stderr,"ERROR: Invalid length in definition '%s'!\n",s); return FALSE; } break; case DEFINITION_TYPE_RANDOMIZE: if (!String_scan(w,STRING_BEGIN,"%u",&length)) { String_doneTokenizer(&stringTokenizer); String_delete(t); fprintf(stderr,"ERROR: Invalid length in definition '%s'!\n",s); return FALSE; } if (length > sizeof(buffer)) length = sizeof(buffer); getRandomBuffer(buffer,length); definition->value = String_newBuffer(buffer,length); break; case DEFINITION_TYPE_INSERT: definition->value = String_new(); if (!String_scan(w,STRING_BEGIN,"%S",definition->value)) { String_delete(definition->value); String_doneTokenizer(&stringTokenizer); String_delete(t); fprintf(stderr,"ERROR: Invalid value in definition '%s'!\n",s); return FALSE; } break; case DEFINITION_TYPE_DELETE: if (!String_scan(w,STRING_BEGIN,"%u",&definition->length)) { String_doneTokenizer(&stringTokenizer); String_delete(t); fprintf(stderr,"ERROR: Invalid length in definition '%s'!\n",s); return FALSE; } break; } } else { switch (definition->type) { case DEFINITION_TYPE_MODIFY: definition->value = String_newChar((char)getRandomByte(256)); break; case DEFINITION_TYPE_RANDOMIZE: getRandomBuffer(buffer,sizeof(buffer)); definition->value = String_newBuffer(buffer,getRandomInteger(sizeof(buffer))); break; case DEFINITION_TYPE_INSERT: definition->value = String_new(); if (!String_scan(w,STRING_BEGIN,"%S",&definition->value)) { String_delete(definition->value); String_doneTokenizer(&stringTokenizer); String_delete(t); fprintf(stderr,"ERROR: Invalid value in definition '%s'!\n",s); return FALSE; } break; case DEFINITION_TYPE_DELETE: if (!String_scan(w,STRING_BEGIN,"%u",&definition->length)) { String_doneTokenizer(&stringTokenizer); String_delete(t); fprintf(stderr,"ERROR: Invalid length in definition '%s'!\n",s); return FALSE; } break; } } if (String_getNextToken(&stringTokenizer,&w,NULL)) { String_doneTokenizer(&stringTokenizer); String_delete(t); fprintf(stderr,"ERROR: Invalid definition '%s'!\n",s); return FALSE; } String_doneTokenizer(&stringTokenizer); String_delete(t); return TRUE; }