END_TEST START_TEST(test_hostnamemap) { unsigned char test = 0; while(1) { if((test >= 48 && test <= 57) // 0-9 || (test >= 65 && test <= 90) // A-Z || (test >= 97 && test <= 122) // a-z || test == '(' || test == ')' || test == '-' || test == '.' || test == '@' || test == '/' || test == '_') { ck_assert_msg(isValidChar(test) == 1, "char %d should be a valid hostname char", test); } else { ck_assert_msg(isValidChar(test) != 1, "char %d should not be a valid hostname char", test); } if(test == 255) { break; } test++; } }
boolean Minitel::textChar(byte c) { byte charByte = getCharByte(c); if (isValidChar(charByte)) { serialprint7(charByte); return true; } return false; }
int validateAndParsePathname (void) { int pathnameIndex = 0, bufferIndex = 0; char buff[64]; char current; int count = 1; current = pathname[pathnameIndex]; strcpy(dirname, "\0"); if (current == '/') { strcat(dirname, "/"); pathnameIndex++; current = pathname[pathnameIndex]; } else if (current == '\0') { // case no pathname return 0; } while (isValidChar(current)) { buff[bufferIndex] = current; bufferIndex++; pathnameIndex++; // This is the end of this directory in the path if (pathname[pathnameIndex] == '/') { count++; // No more input after the final '/' if (pathname[pathnameIndex + 1] == '\0') { buff[bufferIndex] = '\0'; strcpy(basename, buff); return count; } // There is more input after '/' buff[bufferIndex] = '/'; buff[bufferIndex + 1] = '\0'; strcat(dirname, buff); bufferIndex = 0; pathnameIndex++; // This is the end of Input } else if (pathname[pathnameIndex] == '\0') { buff[bufferIndex] = '\0'; strcpy(basename, buff); return count; } current = pathname[pathnameIndex]; } // Current char wasn't valid return -1; }
bool Calculator::checkValidChars(const char * infix) { // TODO: checks an input string in infix notation for illegal characters // (anything other than operators, blanks, parentheses, or digits); no stack is needed here. unsigned int i = 0; while (i < strlen(infix)) { if (isValidChar(infix[i])) i++; else return false; } return true; }
void* mainThread(void* threadArgs){ pthread_mutex_lock(&lock); while(1){ if(q_get(rxq,¤tByte) != NULL){ if(isValidChar(currentByte)){ printf("Mengkonsumsi byte ke-%d: ‘%c’\n", countCnsmdBytes++, currentByte); } else if (currentByte == Endfile){ printf("end-of-file\n" ); exit(0); } } usleep(DELAY*3000); } pthread_mutex_unlock(&lock); return NULL; }
bool isPalindrome(string s) { int i=0, j=0; for (; j<s.length(); j++) { if (isValidChar(s[j])) { s[i++] = s[j]; } } s = s.substr(0, i); int len = (int)s.length(); i = (len-1)/2; j = len % 2 == 1 ? i : i+1; while (i >= 0) { if (s[i] != s[j]) return false; i--; j++; } return true; }
static Byte *q_get(QTYPE *queue, Byte *data){ // Nothing in the queue if (queue->count == 0) return (NULL); else { do{ getFromQueue(&*queue, &*data); }while(queue->count > 0 && !isValidChar(*data)); if(queue->count < MAX_LOWERLIMIT){ controlChar = XON; bufferSend[0] = controlChar; printf("Buffer < maksimum lowerlimit. Send XON\n"); ssize_t lengthSentBytes = sendto(sockfd, bufferSend, sizeof(bufferSend), 4, (struct sockaddr *) &remAddr, remAddrLen); if(lengthSentBytes < 0){ perror("sendto() failed\n"); } } } return data; }
void onlyValidChars(char *str) //maks { char *s = str, *d = str; int inString = 0; while(*s) { if(*s == '\"') { inString = 1 - inString; } if(inString || isValidChar(*s)) { *d = *s; d++; } s++; } *d = 0; }
struct linked_list* create_token_list(char* buffer) { struct linked_list *tok_list = get_new_list(); // int iter = 0; int line_num = 1; char current; char next; while ((buffer[iter] != '\0') && (buffer[iter] != EOF)) { command_t temp = (command_t) checked_malloc(sizeof(command_t)); current = buffer[iter]; next = buffer[iter + 1]; switch (current) { //Skip blank spaces and new lines case '\t': case ' ': iter++; continue; case ';': temp->tok_type = SEMICOLON; temp->type = SEQUENCE_COMMAND; break; case '\n': if (current == next) { temp->tok_type = -1; temp->type = -1; } else { temp->tok_type = NEWLINE; temp->type = SEQUENCE_COMMAND; line_num++; } break; case '|': if (current == next) { temp->tok_type = OR; temp->type = OR_COMMAND; iter++; //fprintf(stderr, "WE are in OReo CITY %d\n", iter); } else { temp->tok_type = PIPE; temp->type = PIPE_COMMAND; //fprintf(stderr, "Doese this PIPE ever get hit at %d\n", iter); } break; case '&': if (current == next) { temp->tok_type = AND; temp->type = AND_COMMAND; iter++; //fprintf(stderr, "Does this statement ever get hit at %d\n", iter); break; } else { temp->tok_type = OTHER; //fprintf(stderr, "Does this OTHER statement get hit at %d\n", iter); } break; case '(': temp->tok_type = RIGHT_PAREN; temp->type = SUBSHELL_COMMAND; break; case ')': temp->tok_type = LEFT_PAREN; temp->type = SUBSHELL_COMMAND; break; case '<': temp->tok_type = LEFT_ARROW; temp->type = SIMPLE_COMMAND; break; case '>': temp->tok_type = RIGHT_ARROW; temp->type = SIMPLE_COMMAND; break; default: temp->tok_type = OTHER; break; } int len = 1; //Case that the token is a word if (isValidChar(current)) { temp->tok_type = WORD; temp->type = SIMPLE_COMMAND; //Gets the length of the word within the buffer. while (isValidChar(buffer[iter + len])) len++; //Allocates memory for the string stored in the token, accounting for NULL byte at end. temp->u.word = (char **)checked_malloc(sizeof(char*)); *(temp->u.word) = (char *)checked_malloc((sizeof(char *) * len) + 1); int i; for (i = 0; i < len; i++) { *(*(temp->u.word)+i) = buffer[iter + i]; } iter += len - 1; } else if (temp->tok_type == OTHER) { //fprintf(stderr, "The following chars are a problem: current: %c, next: %c", current, next); error(1, 0, "Unable to tokenize string at %d", iter); exit(1); } temp->line = line_num; //InsertAtTail(temp, tok_list); free(temp); iter++; } return tok_list; }
/** * Changes the text edit according to keyboard input, and * unfocuses the text if Enter is pressed. * @param action Pointer to an action. * @param state State that the action handlers belong to. */ void TextEdit::keyboardPress(Action *action, State *state) { if (Options::keyboardMode == KEYBOARD_OFF) { switch (action->getDetails()->key.keysym.sym) { case SDLK_UP: _ascii++; if (_ascii > L'~') { _ascii = L' '; } break; case SDLK_DOWN: _ascii--; if (_ascii < L' ') { _ascii = L'~'; } break; case SDLK_LEFT: if (_value.length() > 0) { _value.resize(_value.length() - 1); } break; case SDLK_RIGHT: if (!exceedsMaxWidth(_ascii)) { _value += _ascii; } break; default: break; } } else if (Options::keyboardMode == KEYBOARD_ON) { switch (action->getDetails()->key.keysym.sym) { case SDLK_LEFT: if (_caretPos > 0) { _caretPos--; } break; case SDLK_RIGHT: if (_caretPos < _value.length()) { _caretPos++; } break; case SDLK_HOME: _caretPos = 0; break; case SDLK_END: _caretPos = _value.length(); break; case SDLK_BACKSPACE: if (_caretPos > 0) { _value.erase(_caretPos - 1, 1); _caretPos--; } break; case SDLK_DELETE: if (_caretPos < _value.length()) { _value.erase(_caretPos, 1); } break; case SDLK_RETURN: case SDLK_KP_ENTER: if (!_value.empty()) { setFocus(false); } break; default: Uint16 key = action->getDetails()->key.keysym.unicode; if (isValidChar(key) && !exceedsMaxWidth((wchar_t)key)) { _value.insert(_caretPos, 1, (wchar_t)action->getDetails()->key.keysym.unicode); _caretPos++; } } } _redraw = true; if (_change) { (state->*_change)(action); } InteractiveSurface::keyboardPress(action, state); }
bool x86UNIXFont::isValidChar(const UTF8 *str) const { return isValidChar(oneUTF32toUTF16(oneUTF8toUTF32(str,NULL))); }
//------------------------------------------------------------------------------ bool MacCarbFont::isValidChar(const UTF8 *str) const { // since only low order characters are invalid, and since those characters // are single codeunits in UTF8, we can safely cast here. return isValidChar((UTF16)*str); }
int main(int argc, char* argv[]){ char *carts; int index; pthread_t tids[4]; struct queue_info *queue_infos[4]; if(argc != 2){ fprintf(stderr, "wrong argument number"); exit(1); } carts = argv[1]; index = 0; q_init(); pthread_barrier_init(&barrier, NULL, THREAD_NUM); monitor_init(); /* * check input string is valid */ if(strlen(carts) <= 0){ fprintf(stderr, "input is empty"); exit(1); } index = 0; while(index < strlen(carts)){ if(isValidChar(carts[index]) == 0){ fprintf(stderr, "input direction is invalid"); exit(1); } q_putCart(carts[index]); thread_index = getIndexByDir(carts[index]); index++; } index = 0; while(index < THREAD_NUM){ finishes[index] = 0; index++; } /* * 0 -> North * 1 -> West * 2 -> South * 3 -> East */ queue_infos[0] = malloc(sizeof(struct queue_info)); queue_infos[1] = malloc(sizeof(struct queue_info)); queue_infos[2] = malloc(sizeof(struct queue_info)); queue_infos[3] = malloc(sizeof(struct queue_info)); queue_infos[0]->dir = 'n'; queue_infos[0]->thread_index = 0; queue_infos[1]->dir = 'w'; queue_infos[1]->thread_index = 1; queue_infos[2]->dir = 's'; queue_infos[2]->thread_index = 2; queue_infos[3]->dir = 'e'; queue_infos[3]->thread_index = 3; /* * Create four threads */ index = 0; while(index < 4){ if(pthread_create(&tids[index], NULL, threadFunc, queue_infos[index]) != 0) perror("pthread create error"), exit(EXIT_FAILURE); index++; } index = 0; while(index < 4){ pthread_join(tids[index], NULL); index++; } monitor_shutdown(); q_shutdown(); return 1; }
void menuUpdate(int16_t speed, int16_t dir) { uint8_t up = lcdUpFallingEdge(); uint8_t down = lcdDownFallingEdge(); uint8_t right = lcdRightFallingEdge(); uint8_t left = lcdLeftFallingEdge(); uint8_t leftLongPress = lcdLeftLongPress(); static uint8_t cursorPosition = 0; char lcdLine1[LCD_NUM_CHARACTERS+1]; char lcdLine2[LCD_NUM_CHARACTERS+1]; lcdLine1[LCD_NUM_CHARACTERS] = '\0'; lcdLine2[LCD_NUM_CHARACTERS] = '\0'; if (eepromShadowIsPlatformDown) { sprintf(lcdLine1, "Platform down"); lcdLine2[0] = '\0'; lcdText(lcdLine1, lcdLine2, 0); gMotorsDisabled = 1; return; } if (eepromShadowMenuState == MENU_OPTION_PROFILE && leftLongPress) { gNameEditMode = !gNameEditMode; if (gNameEditMode) { sprintf(lcdLine1, "Edit name"); sprintf(lcdLine2, "%s", currentProfileName); lcdText(lcdLine1, lcdLine2, 1); // Display on, LCD cursor on, blink off lcdCommandBlocking(LCD_CMD_DISPLAY_ON_OFF | LCD_CMD_DISPLAY_ON_OFF_D_bm | LCD_CMD_DISPLAY_ON_OFF_C_bm); lcdCommandBlocking(LCD_CMD_SET_DDRAM_ADDR | (LCD_DDRAM_ADDR_bm & LCD_LINE_2_START_ADDR)); cursorPosition = 0; } else { // Display on, LCD cursor off, blink off lcdCommandBlocking(LCD_CMD_DISPLAY_ON_OFF | LCD_CMD_DISPLAY_ON_OFF_D_bm); // Write to eeprom eepromUpdateStringSafe(currentProfileName, eepromProfileName[eepromShadowCurrentProfile]); } } if (gNameEditMode) { gMotorsDisabled = 1; if (!isValidChar(currentProfileName[cursorPosition])) { currentProfileName[cursorPosition] = ' '; currentProfileName[cursorPosition+1] = '\0'; } if (left || right) { if (left && cursorPosition > 0) { cursorPosition--; } if (right && cursorPosition < LCD_NUM_CHARACTERS - 1) { cursorPosition++; if (!isValidChar(currentProfileName[cursorPosition])) { currentProfileName[cursorPosition] = ' '; currentProfileName[cursorPosition+1] = '\0'; } } // Change cursor position lcdCommandBlocking((LCD_CMD_SET_DDRAM_ADDR | (LCD_DDRAM_ADDR_bm & LCD_LINE_2_START_ADDR)) + cursorPosition); } if (up || down) { // Change letter if (up) { if (currentProfileName[cursorPosition] == ' ') { currentProfileName[cursorPosition] = 'A'; } else if (currentProfileName[cursorPosition] == 'Z') { currentProfileName[cursorPosition] = 'a'; } else if (currentProfileName[cursorPosition] == 'z') { currentProfileName[cursorPosition] = '0'; } else if (currentProfileName[cursorPosition] == '9') { currentProfileName[cursorPosition] = ' '; } else { currentProfileName[cursorPosition]++; } } if (down) { if (currentProfileName[cursorPosition] == ' ') { currentProfileName[cursorPosition] = '9'; } else if (currentProfileName[cursorPosition] == '0') { currentProfileName[cursorPosition] = 'z'; } else if (currentProfileName[cursorPosition] == 'a') { currentProfileName[cursorPosition] = 'Z'; } else if (currentProfileName[cursorPosition] == 'A') { currentProfileName[cursorPosition] = ' '; } else { currentProfileName[cursorPosition]--; } } sprintf(lcdLine1, "Edit name"); sprintf(lcdLine2, "%s", currentProfileName); lcdText(lcdLine1, lcdLine2, 1); lcdCommandBlocking((LCD_CMD_SET_DDRAM_ADDR | (LCD_DDRAM_ADDR_bm & LCD_LINE_2_START_ADDR)) + cursorPosition); } return; } gMotorsDisabled = 0; if (right) { if (eepromShadowMenuState < LAST_MENU_OPTION) { eepromUpdateByteSafe(&eepromMenuState, &eepromShadowMenuState, eepromShadowMenuState + 1); } } if (left) { if (eepromShadowMenuState > 0) { eepromUpdateByteSafe(&eepromMenuState, &eepromShadowMenuState, eepromShadowMenuState - 1); } } switch (eepromShadowMenuState) { case MENU_OPTION_PROFILE: if (up && eepromShadowCurrentProfile < PROFILE_COUNT - 1) { eepromUpdateByteSafe(&eepromCurrentProfile, &eepromShadowCurrentProfile, eepromShadowCurrentProfile + 1); menuInit(); } if (down && eepromShadowCurrentProfile > 0) { eepromUpdateByteSafe(&eepromCurrentProfile, &eepromShadowCurrentProfile, eepromShadowCurrentProfile - 1); menuInit(); } sprintf(lcdLine1, "Choose Profile"); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_FWD_THROW: if (up && eepromShadowFwdThrow < 2.45) { eepromUpdateFloatSafe(&eepromFwdThrow[eepromShadowCurrentProfile], &eepromShadowFwdThrow, eepromShadowFwdThrow + 0.05); } if (down && eepromShadowFwdThrow > 0.05) { eepromUpdateFloatSafe(&eepromFwdThrow[eepromShadowCurrentProfile], &eepromShadowFwdThrow, eepromShadowFwdThrow - 0.05); } sprintf(lcdLine1, "Fwd Throw: %.2f", (double)eepromShadowFwdThrow); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_REV_THROW: if (up && eepromShadowRevThrow < 2.45) { eepromUpdateFloatSafe(&eepromRevThrow[eepromShadowCurrentProfile], &eepromShadowRevThrow, eepromShadowRevThrow + 0.05); } if (down && eepromShadowRevThrow > 0.05) { eepromUpdateFloatSafe(&eepromRevThrow[eepromShadowCurrentProfile], &eepromShadowRevThrow, eepromShadowRevThrow - 0.05); } sprintf(lcdLine1, "Rev Throw: %.2f", (double)eepromShadowRevThrow); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_TURN_THROW: if (up && eepromShadowTurnThrow < 2.45) { eepromUpdateFloatSafe(&eepromTurnThrow[eepromShadowCurrentProfile], &eepromShadowTurnThrow, eepromShadowTurnThrow + 0.05); } if (down && eepromShadowTurnThrow > 0.05) { eepromUpdateFloatSafe(&eepromTurnThrow[eepromShadowCurrentProfile], &eepromShadowTurnThrow, eepromShadowTurnThrow - 0.05); } sprintf(lcdLine1, "Turn Throw: %.2f", (double)eepromShadowTurnThrow); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_TOP_FWD_SPEED: if (up && eepromShadowTopFwdSpeed < 125) { eepromUpdateByteSafe(&eepromTopFwdSpeed[eepromShadowCurrentProfile], &eepromShadowTopFwdSpeed, eepromShadowTopFwdSpeed + 5); } if (down && eepromShadowTopFwdSpeed > 5) { eepromUpdateByteSafe(&eepromTopFwdSpeed[eepromShadowCurrentProfile], &eepromShadowTopFwdSpeed, eepromShadowTopFwdSpeed - 5); } sprintf(lcdLine1, "Fwd Speed: %d", eepromShadowTopFwdSpeed); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_TOP_REV_SPEED: if (up && eepromShadowTopRevSpeed < 125) { eepromUpdateByteSafe(&eepromTopRevSpeed[eepromShadowCurrentProfile], &eepromShadowTopRevSpeed, eepromShadowTopRevSpeed + 5); } if (down && eepromShadowTopRevSpeed > 5) { eepromUpdateByteSafe(&eepromTopRevSpeed[eepromShadowCurrentProfile], &eepromShadowTopRevSpeed, eepromShadowTopRevSpeed - 5); } sprintf(lcdLine1, "Rev Speed: %d", eepromShadowTopRevSpeed); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_TOP_TURN_SPEED: if (up && eepromShadowTopTurnSpeed < 125) { eepromUpdateByteSafe(&eepromTopTurnSpeed[eepromShadowCurrentProfile], &eepromShadowTopTurnSpeed, eepromShadowTopTurnSpeed + 5); } if (down && eepromShadowTopTurnSpeed > 5) { eepromUpdateByteSafe(&eepromTopTurnSpeed[eepromShadowCurrentProfile], &eepromShadowTopTurnSpeed, eepromShadowTopTurnSpeed - 5); } sprintf(lcdLine1, "Turn Speed: %d", eepromShadowTopTurnSpeed); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_SENSITIVITY: if (up && eepromShadowSensitivity < 9) { eepromUpdateByteSafe(&eepromSensitivity[eepromShadowCurrentProfile], &eepromShadowSensitivity, eepromShadowSensitivity + 1); } if (down && eepromShadowSensitivity > 0) { eepromUpdateByteSafe(&eepromSensitivity[eepromShadowCurrentProfile], &eepromShadowSensitivity, eepromShadowSensitivity - 1); } sprintf(lcdLine1, "Sensitivity: %d", eepromShadowSensitivity + 1); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_ACCELERATION: if (up && eepromShadowAcceleration > 4) { eepromUpdateByteSafe(&eepromAcceleration[eepromShadowCurrentProfile], &eepromShadowAcceleration, eepromShadowAcceleration - 4); } if (down && eepromShadowAcceleration < 100) { eepromUpdateByteSafe(&eepromAcceleration[eepromShadowCurrentProfile], &eepromShadowAcceleration, eepromShadowAcceleration + 4); } sprintf(lcdLine1, "Acceleration: %d", (104 - eepromShadowAcceleration) / 4); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_DECELERATION: if (up && eepromShadowDeceleration > 4) { eepromUpdateByteSafe(&eepromDeceleration[eepromShadowCurrentProfile], &eepromShadowDeceleration, eepromShadowDeceleration - 4); } if (down && eepromShadowDeceleration < 100) { eepromUpdateByteSafe(&eepromDeceleration[eepromShadowCurrentProfile], &eepromShadowDeceleration, eepromShadowDeceleration + 4); } sprintf(lcdLine1, "Deceleration: %d", (104 - eepromShadowDeceleration) / 4); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_OUTER_DEAD_BAND: if (up && eepromShadowOuterDeadBand < 20) { eepromUpdateByteSafe(&eepromOuterDeadBand[eepromShadowCurrentProfile], &eepromShadowOuterDeadBand, eepromShadowOuterDeadBand + 1); } if (down && eepromShadowOuterDeadBand > 0) { eepromUpdateByteSafe(&eepromOuterDeadBand[eepromShadowCurrentProfile], &eepromShadowOuterDeadBand, eepromShadowOuterDeadBand - 1); } if (eepromShadowOuterDeadBand == 0) { // 0: off sprintf(lcdLine1, "Outer DB: Off"); } else if (eepromShadowOuterDeadBand == 1) { // 1: immediate sprintf(lcdLine1, "Outer DB: Immed."); } else { // 2: 0.5s, 3: 1.0s, 4: 1.5s, etc // Conversion: y=(x-1)/2 sprintf(lcdLine1, "Outer DB: %d.%ds", (eepromShadowOuterDeadBand-1)/2, (eepromShadowOuterDeadBand-1) % 2 ? 5 : 0); } sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_CTR_DEAD_BAND: if (up && eepromShadowCenterDeadBand < 56) { eepromUpdateByteSafe(&eepromCenterDeadBand[eepromShadowCurrentProfile], &eepromShadowCenterDeadBand, eepromShadowCenterDeadBand + 6); } if (down && eepromShadowCenterDeadBand > 2) { eepromUpdateByteSafe(&eepromCenterDeadBand[eepromShadowCurrentProfile], &eepromShadowCenterDeadBand, eepromShadowCenterDeadBand - 6); } sprintf(lcdLine1, "Center DB: %d", (eepromShadowCenterDeadBand - 2) / 6 + 1); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_PROP_AS_SWITCH: if (up || down) { if (eepromShadowPropAsSwitch) { eepromUpdateByteSafe(&eepromPropAsSwitch[eepromShadowCurrentProfile], &eepromShadowPropAsSwitch, 0); } else { eepromUpdateByteSafe(&eepromPropAsSwitch[eepromShadowCurrentProfile], &eepromShadowPropAsSwitch, 1); } } sprintf(lcdLine1, "PropAsSwitch:%s", eepromShadowPropAsSwitch ? " On" : "Off"); sprintf(lcdLine2, "%s", currentProfileName); break; case MENU_OPTION_INVERT: if (up || down) { if (eepromShadowInvert) { eepromUpdateByteSafe(&eepromInvert[eepromShadowCurrentProfile], &eepromShadowInvert, 0); } else { eepromUpdateByteSafe(&eepromInvert[eepromShadowCurrentProfile], &eepromShadowInvert, 1); } } sprintf(lcdLine1, "Invert: %s", eepromShadowInvert ? "On" : "Off"); sprintf(lcdLine2, "%s", currentProfileName); break; default: lcdLine1[0] = '\0'; lcdLine2[0] = '\0'; break; } if ((up || down) && eepromShadowCurrentProfile == PROFILE_COUNT - 1) { menuInit(); } //if (eepromShadowMenuState != MENU_OPTION_PROFILE) { //sprintf(lcdLine2, "S=%4d T=%4d%3d", speed, dir, gWirelessTimeoutCount); //} lcdText(lcdLine1, lcdLine2, 0); }
/* Format a received message in the Eventinfo structure */ int OS_CleanMSG(char *msg, Eventinfo *lf) { size_t loglen; char *pieces; struct tm *p; /* The message is formatted in the following way: * id:location:message. */ /* Ignore the id of the message in here */ msg += 2; /* Set pieces as the message */ pieces = strchr(msg, ':'); if (!pieces) { merror(FORMAT_ERROR, ARGV0); return (-1); } /* Is this from an agent? */ if ( *msg == '(' ) { /* look past '->' for the first ':' */ pieces = strchr(strstr(msg, "->"), ':'); if(!pieces) { merror(FORMAT_ERROR, ARGV0); return(-1); } } *pieces = '\0'; pieces++; os_strdup(msg, lf->location); /* Get the log length */ loglen = strlen(pieces) + 1; /* Assign the values in the structure (lf->full_log) */ os_malloc((2 * loglen) + 1, lf->full_log); /* Set the whole message at full_log */ strncpy(lf->full_log, pieces, loglen); /* Log is the one used for parsing in the decoders and rules */ lf->log = lf->full_log + loglen; strncpy(lf->log, pieces, loglen); /* check if month contains an umlaut and repair * umlauts are non-ASCII and use 2 slots in the char array * repair to only one slot so we can detect the correct date format in the next step * ex: Mär 02 17:30:52 */ if (pieces[1] == (char) 195) { if (pieces[2] == (char) 164) { pieces[0] = '\0'; pieces[1] = 'M'; pieces[2] = 'a'; pieces++; } } /* Check for the syslog date format * ( ex: Dec 29 10:00:01 * or 2015 Dec 29 10:00:01 * or 2007-06-14T15:48:55-04:00 for syslog-ng isodate * or 2009-05-22T09:36:46.214994-07:00 for rsyslog ) * or 2015-04-16 21:51:02,805 (proftpd 1.3.5) */ if ( ( (loglen > 17) && (pieces[3] == ' ') && (pieces[6] == ' ') && (pieces[9] == ':') && (pieces[12] == ':') && (pieces[15] == ' ') && (lf->log += 16) ) || ( (loglen > 24) && (pieces[4] == '-') && (pieces[7] == '-') && (pieces[10] == ' ') && (pieces[13] == ':') && (pieces[16] == ':') && (pieces[19] == ',') && (lf->log += 23) ) || ( (loglen > 33) && (pieces[4] == '-') && (pieces[7] == '-') && (pieces[10] == 'T') && (pieces[13] == ':') && (pieces[16] == ':') && ( ((pieces[22] == ':') && (pieces[25] == ' ') && (lf->log += 26)) || ((pieces[19] == '.') && (pieces[29] == ':') && (lf->log += 32)) ) ) || ( (loglen > 21) && (isdigit(pieces[0])) && (pieces[4] == ' ') && (pieces[8] == ' ') && (pieces[11] == ' ') && (pieces[14] == ':') && (pieces[17] == ':') && (pieces[20] == ' ') && (lf->log += 21) ) ) { /* Check for an extra space in here */ if (*lf->log == ' ') { lf->log++; } /* Hostname */ pieces = lf->hostname = lf->log; /* Check for a valid hostname */ while (isValidChar(*pieces) == 1) { pieces++; } /* Check if it is a syslog without hostname (common on Solaris) */ if (*pieces == ':' && pieces[1] == ' ') { /* Getting solaris 8/9 messages without hostname. * In these cases, the process_name should be there. * http://www.ossec.net/wiki/index.php/Log_Samples_Solaris */ lf->program_name = lf->hostname; lf->hostname = NULL; /* End the program name string */ *pieces = '\0'; pieces += 2; lf->log = pieces; } /* Extract the hostname */ else if (*pieces != ' ') { /* Invalid hostname */ lf->hostname = NULL; pieces = NULL; } else { /* End the hostname string */ *pieces = '\0'; /* Move pieces to the beginning of the log message */ pieces++; lf->log = pieces; /* Get program_name */ lf->program_name = pieces; /* Extract program_name */ /* Valid names: * p_name: * p_name[pid]: * p_name[pid]: [ID xx facility.severity] * auth|security:info p_name: */ while (isValidChar(*pieces) == 1) { pieces++; } /* Check for the first format: p_name: */ if ((*pieces == ':') && (pieces[1] == ' ')) { *pieces = '\0'; pieces += 2; } /* Check for the second format: p_name[pid]: */ else if ((*pieces == '[') && (isdigit((int)pieces[1]))) { *pieces = '\0'; pieces += 2; while (isdigit((int)*pieces)) { pieces++; } if ((*pieces == ']') && (pieces[1] == ':') && (pieces[2] == ' ')) { pieces += 3; } /* Some systems are not terminating the program name with * a ':'. Working around this in here... */ else if ((*pieces == ']') && (pieces[1] == ' ')) { pieces += 2; } else { /* Fix for some weird log formats */ pieces--; while (isdigit((int)*pieces)) { pieces--; } if (*pieces == '\0') { *pieces = '['; } pieces = NULL; lf->program_name = NULL; } } /* AIX syslog */ else if ((*pieces == '|') && islower((int)pieces[1])) { pieces += 2; /* Remove facility */ while (isalnum((int)*pieces)) { pieces++; } if (*pieces == ':') { /* Remove severity */ pieces++; while (isalnum((int)*pieces)) { pieces++; } if (*pieces == ' ') { pieces++; lf->program_name = pieces; /* Get program name again */ while (isValidChar(*pieces) == 1) { pieces++; } /* Check for the first format: p_name: */ if ((*pieces == ':') && (pieces[1] == ' ')) { *pieces = '\0'; pieces += 2; } /* Check for the second format: p_name[pid]: */ else if ((*pieces == '[') && (isdigit((int)pieces[1]))) { *pieces = '\0'; pieces += 2; while (isdigit((int)*pieces)) { pieces++; } if ((*pieces == ']') && (pieces[1] == ':') && (pieces[2] == ' ')) { pieces += 3; } else { pieces = NULL; } } } else { pieces = NULL; lf->program_name = NULL; } } /* Invalid AIX */ else { pieces = NULL; lf->program_name = NULL; } } else { pieces = NULL; lf->program_name = NULL; } } /* Remove [ID xx facility.severity] */ if (pieces) { /* Set log after program name */ lf->log = pieces; if ((pieces[0] == '[') && (pieces[1] == 'I') && (pieces[2] == 'D') && (pieces[3] == ' ')) { pieces += 4; /* Going after the ] */ pieces = strchr(pieces, ']'); if (pieces) { pieces += 2; lf->log = pieces; } } } /* Get program name size */ if (lf->program_name) { lf->p_name_size = strlen(lf->program_name); } } /* xferlog date format * Mon Apr 17 18:27:14 2006 1 64.160.42.130 */ else if ((loglen > 28) && (pieces[3] == ' ') && (pieces[7] == ' ') && (pieces[10] == ' ') && (pieces[13] == ':') && (pieces[16] == ':') && (pieces[19] == ' ') && (pieces[24] == ' ') && (pieces[26] == ' ')) { /* Move log to the beginning of the message */ lf->log += 24; } /* Check for snort date format * ex: 01/28-09:13:16.240702 [**] */ else if ( (loglen > 24) && (pieces[2] == '/') && (pieces[5] == '-') && (pieces[8] == ':') && (pieces[11] == ':') && (pieces[14] == '.') && (pieces[21] == ' ') ) { lf->log += 23; } /* Check for suricata (new) date format * ex: 01/28/1979-09:13:16.240702 [**] */ else if ( (loglen > 26) && (pieces[2] == '/') && (pieces[5] == '/') && (pieces[10] == '-') && (pieces[13] == ':') && (pieces[16] == ':') && (pieces[19] == '.') && (pieces[26] == ' ') ) { lf->log += 28; } /* Check for apache log format */ /* [Fri Feb 11 18:06:35 2004] [warn] */ else if ( (loglen > 27) && (pieces[0] == '[') && (pieces[4] == ' ') && (pieces[8] == ' ') && (pieces[11] == ' ') && (pieces[14] == ':') && (pieces[17] == ':') && (pieces[20] == ' ') && (pieces[25] == ']') ) { lf->log += 27; } /* Check for the osx asl log format. * Examples: * [Time 2006.12.28 15:53:55 UTC] [Facility auth] [Sender sshd] [PID 483] [Message error: PAM: Authentication failure for username from 192.168.0.2] [Level 3] [UID -2] [GID -2] [Host Hostname] * [Time 2006.11.02 14:02:11 UTC] [Facility auth] [Sender sshd] [PID 856] [Message refused connect from 59.124.44.34] [Level 4] [UID -2] [GID -2] [Host robert-wyatts-emac] */ else if ((loglen > 26) && (pieces[0] == '[') && (pieces[1] == 'T') && (pieces[5] == ' ') && (pieces[10] == '.') && (pieces[13] == '.') && (pieces[16] == ' ') && (pieces[19] == ':')) { /* Do not read more than 1 message entry -> log tampering */ short unsigned int done_message = 0; /* Remove the date */ lf->log += 25; /* Get the desired values */ pieces = strchr(lf->log, '['); while (pieces) { pieces++; /* Get the sender (set to program name) */ if ((strncmp(pieces, "Sender ", 7) == 0) && (lf->program_name == NULL)) { pieces += 7; lf->program_name = pieces; /* Get the closing brackets */ pieces = strchr(pieces, ']'); if (pieces) { *pieces = '\0'; /* Set program_name size */ lf->p_name_size = strlen(lf->program_name); pieces++; } /* Invalid program name */ else { lf->program_name = NULL; break; } } /* Get message */ else if ((strncmp(pieces, "Message ", 8) == 0) && (done_message == 0)) { pieces += 8; done_message = 1; lf->log = pieces; /* Get the closing brackets */ pieces = strchr(pieces, ']'); if (pieces) { *pieces = '\0'; pieces++; } /* Invalid log closure */ else { break; } } /* Get hostname */ else if (strncmp(pieces, "Host ", 5) == 0) { pieces += 5; lf->hostname = pieces; /* Get the closing brackets */ pieces = strchr(pieces, ']'); if (pieces) { *pieces = '\0'; pieces++; } /* Invalid hostname */ else { lf->hostname = NULL; } break; } /* Get next entry */ pieces = strchr(pieces, '['); } } /* Check for squid date format * 1140804070.368 11623 * seconds from 00:00:00 1970-01-01 UTC */ else if ((loglen > 32) && (pieces[0] == '1') && (isdigit((int)pieces[1])) && (isdigit((int)pieces[2])) && (isdigit((int)pieces[3])) && (pieces[10] == '.') && (isdigit((int)pieces[13])) && (pieces[14] == ' ') && ((pieces[21] == ' ') || (pieces[22] == ' '))) { lf->log += 14; /* We need to start at the size of the event */ while (*lf->log == ' ') { lf->log++; } } /* Every message must be in the format * hostname->location or * (agent) ip->location. */ /* Set hostname for local messages */ if (lf->location[0] == '(') { /* Messages from an agent */ lf->hostname = lf->location; } else if (lf->hostname == NULL) { lf->hostname = __shost; } /* Set up the event data */ lf->time = c_time; p = localtime(&c_time); /* Assign hour, day, year and month values */ lf->day = p->tm_mday; lf->year = p->tm_year + 1900; strncpy(lf->mon, month[p->tm_mon], 3); snprintf(lf->hour, 9, "%02d:%02d:%02d", p->tm_hour, p->tm_min, p->tm_sec); /* Set the global hour/weekday */ __crt_hour = p->tm_hour; __crt_wday = p->tm_wday; #ifdef TESTRULE if (!alert_only) { print_out("**Phase 1: Completed pre-decoding."); print_out(" full event: '%s'", lf->full_log); print_out(" hostname: '%s'", lf->hostname); print_out(" program_name: '%s'", lf->program_name); print_out(" log: '%s'", lf->log); } #endif return (0); }
command_stream_t make_command_stream(int(*getbyte) (void *), void *arg) { initStackc(&s); initStacko(&o); struct command_stream *command_stream_t = (struct command_stream *)malloc(sizeof(struct command_stream)); initStream(command_stream_t); struct command*com = (struct command*)malloc(sizeof(struct command)); com->type = SIMPLE_COMMAND; com->input = com->output = NULL; struct oper o; char c; bool extra = false; bool done = false; int csize = 10; com->u.word = (char**)malloc(10 * sizeof(char*)); int count = 0; int length = 0; int mlength = 12; char* word = (char*)malloc(12 * sizeof(char)); c = getbyte(arg); int curline = 1; skipws(getbyte, arg, &c, &curline); while (c >= 0) { done = false; if (!isValidChar(c)) printError(curline); extra = false; if (isvalid(c)) { word[length] = c; length++; if (length >= mlength) { mlength += 5; word = (char*)realloc(word, mlength*sizeof(char)); } } else { if (c == '#') { while ((c = (char)getbyte(arg)) >= 0 && c != '\n'); } else if (c == '\\') { while (c == '\\') { c = (char)getbyte(arg); c = (char)getbyte(arg); } continue; } else if (c == ' ') { if (length > 0) { addWord(&com, &count, &length, word); if (count == csize - 1) { csize += 5; com->u.word = (char**)realloc(com->u.word, csize*sizeof(int)); } } else { c = (char)getbyte(arg); continue; } } else if (c == '|' || c == '&' || c == ';' || c == '\n' || c == '(' || c == ')') { /*if (com->type == SUBSHELL_COMMAND) { com = (struct command*)malloc(sizeof(struct command)); com->type = SIMPLE_COMMAND; com->u.word = (char**)malloc(10 * sizeof(char*)); com->input = com->output = NULL; count = 0; csize = 10; } */ addWord(&com, &count, &length, word); if (c == '|') { c = (char)getbyte(arg); if (c == '|') { o.type = OR; } else { o.type = PIPE; } } else if (c == '&') { o.type = AND; c = (char)getbyte(arg); if (c != '&') printError(curline); } else if (c == ';') { o.type = SEQ; c = (char)getbyte(arg); } else if (c == '(') { o.type = LEFT; } else if (c == ')') { o.type = RIGHT; } else if (c == '\n') { curline += 1; while ((c = (char)getbyte(arg)) == ' '); if (c < 0) break; while (c == '\\') { c = (char)getbyte(arg); c = (char)getbyte(arg); } if (c == '#') { while ((c = getbyte(arg)) >= 0 && c != '\n'); } if (c != '\n' && (com->u.word != NULL || com->u.subshell_command != NULL)) { o.type = SEQ; extra = true; } else { extra = false; if (count > 0) { com->u.word[count] = '\0'; pushc(&s, com); } o.type = SEQ; add(o, 't', curline); insert(command_stream_t, topc(&s)); popc(&s); done = true; reset(&com, &word, &length, &count, &csize, &mlength, 'f'); c = (char)getbyte(arg); skipws(getbyte, arg, &c, &curline); continue; } } if (o.type != PIPE && o.type != SEQ) { c = (char)getbyte(arg); } if (count > 0) { com->u.word[count] = '\0'; pushc(&s, com); } add(o, 'f', curline); if (o.type == RIGHT) reset(&com, &word, &length, &count, &csize, &mlength, 't'); else reset(&com, &word, &length, &count, &csize, &mlength, 'f'); if (o.type != RIGHT) { skipws(getbyte, arg, &c,&curline); } extra = true; } else if (c == '>' || c == '<') { char* ptr; int insize = 12; ptr = (char*)malloc(insize * sizeof(char)); int i = 0; char t = c; while ((c = (char)getbyte(arg)) == ' '); if (!isvalid(c)) printError(curline); while ((c >= 0)) { if (!isvalid(c)) { extra = true; break; } ptr[i] = c; i++; if (i == insize - 1) { insize += 5; ptr = (char*)realloc(ptr, insize*sizeof(char)); } c = (char)getbyte(arg); } ptr[i] = '\0'; if (t == '<') com->input = ptr; else com->output = ptr; } } if (!extra) c = (char)getbyte(arg); } if (!done) { addWord(&com, &count, &length, word); if (count > 0) { com->u.word[count] = '\0'; pushc(&s, com); } o.type = SEQ; add(o, 't', curline); if (sizec(&s) != 1) printError(curline); insert(command_stream_t, topc(&s)); popc(&s); } return command_stream_t; }