/* try to match a hexadecimal number */ token_t lex_nextHexNumber(LexState* ls) { uint16_t number = 0; uint8_t i; int c; for (i=0; i<4; i++) { c = fgetc(ls->src); if (IS_HEX(c)) { number = number << 4; if(IS_NUMBER(c)) number |= (c - '0'); else number |= ((c & ~(0x20)) - 'A'); } else { ungetc(c, ls->src); break; } } if(i == 0) { ls->error = ERR_SYNTAX; return TK_NONE; } ls->kf.number = number; return TK_NUMBER; }
/** * Parse an escaped sequence in a printf-like format string. * * @param pformat pointer to the sequence, the pointer * is changed to point to the next symbol after parsed sequence * @return result character */ static char parse_escaped_char(const char **pformat) { const char* start = *pformat; switch( *((*pformat)++) ) { case 't': return '\t'; case 'r': return '\r'; case 'n': return '\n'; case '\\': return '\\'; case 'x': /* \xNN byte with hexadecimal value NN (1 to 2 digits) */ if( IS_HEX(**pformat) ) { int ch; ch = (**pformat <= '9' ? **pformat & 15 : (**pformat + 9) & 15); (*pformat) ++; if(IS_HEX(**pformat)) { /* read the second digit */ ch = 16 * ch + (**pformat <= '9' ? **pformat & 15 : (**pformat + 9) & 15); (*pformat)++; } if(ch) return ch; } break; default: (*pformat)--; /* \NNN - character with octal value NNN (1 to 3 digits) */ if('0' < **pformat && **pformat <= '7') { int ch = *((*pformat)++) - '0'; if('0' <= **pformat && **pformat <= '7') { ch = ch * 8 + *((*pformat)++) - '0'; if('0' <= **pformat && **pformat <= '7') ch = ch * 8 + *((*pformat)++) - '0'; } return (char)ch; } } *pformat = start; return '\\'; }
/** * Search for a crc32 hash sum in the given file name. * * @param filepath the path to the file. * @param crc32 pointer to integer to receive parsed hash sum. * @return non zero if crc32 was found, zero otherwise. */ static int find_embedded_crc32(const char* filepath, unsigned* crc32_be) { const char* e = filepath + strlen(filepath) - 10; /* search for the sum enclosed in brackets */ for(; e >= filepath && !IS_PATH_SEPARATOR(*e); e--) { if((*e == '[' && e[9] == ']') || (*e == '(' && e[9] == ')')) { const char *p = e + 8; for(; p > e && IS_HEX(*p); p--); if(p == e) { rhash_hex_to_byte(e + 1, (char unsigned*)crc32_be, 8); return 1; } e -= 9; } } return 0; }
int main() { char buf[BUFSIZ]; int state; char digest[40], *dp; char *b_end = 0; int nbytes; state = NEW_UDIG; while ((nbytes = _read(buf, sizeof buf)) > 0) { char *b; b = buf; b_end = buf + nbytes; while (b < b_end) { char c; c = *b++; switch (state) { /* * New sha udig always starts with character 's' */ case NEW_UDIG: if (c != 's') exit(1); state = SCAN_ALGORITHM_h; break; /* * Scan character 'h'. */ case SCAN_ALGORITHM_h: if (c != 'h') exit(1); state = SCAN_ALGORITHM_a; break; /* * Scan character 'a'. */ case SCAN_ALGORITHM_a: if (c != 'a') exit(1); state = SCAN_ALGORITHM_colon; break; /* * Scan a colon character. */ case SCAN_ALGORITHM_colon: if (c != ':') exit(1); state = SCAN_DIGEST; dp = digest; break; /* * Scan up to 40 hex characters. */ case SCAN_DIGEST: if (!IS_HEX(c)) exit(1); *dp++ = c; if (dp - digest == 40) state = SCAN_NEW_LINE; break; case SCAN_NEW_LINE: if (c != '\n') exit(1); add_sha(digest); state = NEW_UDIG; break; } } } if (state != NEW_UDIG) exit(1); exit(b_end ? 0 : 2); }
static int getc (comp_t* com, int noesc) { qse_char_t c; if (com->ptr >= com->end) { com->c.value = QSE_CHAR_EOF; com->c.escaped = 0; return 0; } com->c.value = *com->ptr++; com->c.escaped = 0; if (noesc || com->c.value != QSE_T('\\')) return 0; CHECK_END (com); c = *com->ptr++; if (c == QSE_T('n')) c = QSE_T('\n'); else if (c == QSE_T('r')) c = QSE_T('\r'); else if (c == QSE_T('t')) c = QSE_T('\t'); else if (c == QSE_T('f')) c = QSE_T('\f'); else if (c == QSE_T('b')) c = QSE_T('\b'); else if (c == QSE_T('v')) c = QSE_T('\v'); else if (c == QSE_T('a')) c = QSE_T('\a'); #if 0 /* backrefernce conflicts with octal notation */ else if (c >= QSE_T('0') && c <= QSE_T('7')) { qse_char_t cx; c = c - QSE_T('0'); CHECK_END (com); cx = *com->ptr++; if (cx >= QSE_T('0') && cx <= QSE_T('7')) { c = c * 8 + cx - QSE_T('0'); CHECK_END (com); cx = *com->ptr++; if (cx >= QSE_T('0') && cx <= QSE_T('7')) { c = c * 8 + cx - QSE_T('0'); } } } #endif else if (c == QSE_T('x')) { qse_char_t cx; CHECK_END (com); cx = *com->ptr++; if (IS_HEX(cx)) { c = HEX_TO_NUM(cx); CHECK_END (com); cx = *com->ptr++; if (IS_HEX(cx)) { c = c * 16 + HEX_TO_NUM(cx); } } } #if defined(QSE_CHAR_IS_WCHAR) else if (c == QSE_T('u') && QSE_SIZEOF(qse_char_t) >= 2) { qse_char_t cx; CHECK_END (com); cx = *com->ptr++; if (IS_HEX(cx)) { qse_size_t i; c = HEX_TO_NUM(cx); for (i = 0; i < 3; i++) { CHECK_END (com); cx = *com->ptr++; if (!IS_HEX(cx)) break; c = c * 16 + HEX_TO_NUM(cx); } } } else if (c == QSE_T('U') && QSE_SIZEOF(qse_char_t) >= 4) { qse_char_t cx; CHECK_END (com); cx = *com->ptr++; if (IS_HEX(cx)) { qse_size_t i; c = HEX_TO_NUM(cx); for (i = 0; i < 7; i++) { CHECK_END (com); cx = *com->ptr++; if (!IS_HEX(cx)) break; c = c * 16 + HEX_TO_NUM(cx); } } } #endif com->c.value = c; com->c.escaped = QSE_TRUE; #if 0 com->c = (com->ptr < com->end)? *com->ptr++: QSE_CHAR_EOF; if (com->c == QSE_CHAR_EOF) qse_printf (QSE_T("getc => <EOF>\n")); else qse_printf (QSE_T("getc => %c\n"), com->c); #endif return 0; }
/* Returns the number of bytes in the value. 0 means failure. */ static int scanVal(char **pbp, char *endptr, char *valBuf, int valBufSize) { char *bp, *valBufp; int vallen = 0; PRBool isQuoted; PORT_Assert(valBufSize > 0); /* skip optional leading space */ skipSpace(pbp, endptr); if(*pbp == endptr) { /* nothing left */ return 0; } bp = *pbp; /* quoted? */ if (*bp == C_DOUBLE_QUOTE) { isQuoted = PR_TRUE; /* skip over it */ bp++; } else { isQuoted = PR_FALSE; } valBufp = valBuf; while (bp < endptr) { char c = *bp; if (c == C_BACKSLASH) { /* escape character */ bp++; if (bp >= endptr) { /* escape charater must appear with paired char */ *pbp = bp; return 0; } c = *bp; if (IS_HEX(c) && (endptr - bp) >= 2 && IS_HEX(bp[1])) { bp++; c = (char)((x2b[(PRUint8)c] << 4) | x2b[(PRUint8)*bp]); } } else if (c == '#' && bp == *pbp) { /* ignore leading #, quotation not required for it. */ } else if (!isQuoted && SPECIAL_CHAR(c)) { /* unescaped special and not within quoted value */ break; } else if (c == C_DOUBLE_QUOTE) { /* reached unescaped double quote */ break; } /* append character */ vallen++; if (vallen >= valBufSize) { *pbp = bp; return 0; } *valBufp++ = c; bp++; } /* strip trailing spaces from unquoted values */ if (!isQuoted) { while (valBufp > valBuf) { char c = valBufp[-1]; if (! OPTIONAL_SPACE(c)) break; --valBufp; } vallen = valBufp - valBuf; } if (isQuoted) { /* insist that we stopped on a double quote */ if (*bp != C_DOUBLE_QUOTE) { *pbp = bp; return 0; } /* skip over the quote and skip optional space */ bp++; skipSpace(&bp, endptr); } *pbp = bp; /* null-terminate valBuf -- guaranteed at least one space left */ *valBufp = 0; return vallen; }
int callback(RULE* rule, void* data) { TAG* tag; STRING* string; META* meta; MATCH* match; int rule_match; int string_found; int show = TRUE; int show_tags = TRUE; int show_meta = TRUE; int show_strings = TRUE; rule_match = (rule->flags & RULE_FLAGS_MATCH); if (show) { printf("%s ", rule->identifier); printf(" flags: %x ", rule->flags); if (show_tags) { tag = rule->tag_list_head; printf("tag: ["); while(tag != NULL) { if (tag->next == NULL) { printf("%s", tag->identifier); } else { printf("%s,", tag->identifier); } tag = tag->next; } printf("] "); } if (show_meta) { meta = rule->meta_list_head; printf("meta: ["); while(meta != NULL) { if (meta->type == META_TYPE_INTEGER) { printf("%s=%lu", meta->identifier, meta->integer); } else if (meta->type == META_TYPE_BOOLEAN) { printf("%s=%s", meta->identifier, (meta->boolean)?("true"):("false")); } else { printf("%s=\"%s\"", meta->identifier, meta->string); } if (meta->next != NULL) printf(","); meta = meta->next; } printf("] "); } /* show matched strings */ if (show_strings) { string = rule->string_list_head; while (string != NULL) { string_found = string->flags & STRING_FLAGS_FOUND; if (string_found) { match = string->matches_head; while (match != NULL) { printf("0x%lx:%s: ", match->offset, string->identifier); if (IS_HEX(string)) { print_hex_string(match->data, match->length); } else if (IS_WIDE(string)) { print_string(match->data, match->length, TRUE); } else { print_string(match->data, match->length, FALSE); } match = match->next; } } string = string->next; } } } printf("\n"); return CALLBACK_CONTINUE; }
int callback(RULE* rule, void* data) { TAG* tag; IDENTIFIER* identifier; STRING* string; META* meta; MATCH* match; int rule_match; int string_found; int show = TRUE; if (show_specified_tags) { show = FALSE; tag = specified_tags_list; while (tag != NULL) { if (lookup_tag(rule->tag_list_head, tag->identifier) != NULL) { show = TRUE; break; } tag = tag->next; } } if (show_specified_rules) { show = FALSE; identifier = specified_rules_list; while (identifier != NULL) { if (strcmp(identifier->name, rule->identifier) == 0) { show = TRUE; break; } identifier = identifier->next; } } rule_match = (rule->flags & RULE_FLAGS_MATCH); show = show && ((!negate && rule_match) || (negate && !rule_match)); if (show) { printf("%s ", rule->identifier); if (show_tags) { tag = rule->tag_list_head; printf("["); while(tag != NULL) { if (tag->next == NULL) { printf("%s", tag->identifier); } else { printf("%s,", tag->identifier); } tag = tag->next; } printf("] "); } if (show_meta) { meta = rule->meta_list_head; printf("["); while(meta != NULL) { if (meta->type == META_TYPE_INTEGER) { printf("%s=%lu", meta->identifier, meta->integer); } else if (meta->type == META_TYPE_BOOLEAN) { printf("%s=%s", meta->identifier, (meta->boolean)?("true"):("false")); } else { printf("%s=\"%s\"", meta->identifier, meta->string); } if (meta->next != NULL) printf(","); meta = meta->next; } printf("] "); } printf("%s\n", (char*) data); /* show matched strings */ if (show_strings) { string = rule->string_list_head; while (string != NULL) { string_found = string->flags & STRING_FLAGS_FOUND; if (string_found) { match = string->matches_head; while (match != NULL) { printf("0x%lx:%s: ", match->offset, string->identifier); if (IS_HEX(string)) { print_hex_string(match->data, match->length); } else if (IS_WIDE(string)) { print_string(match->data, match->length, TRUE); } else { print_string(match->data, match->length, FALSE); } match = match->next; } } string = string->next; } } } if (rule_match) count++; if (limit != 0 && count >= limit) return CALLBACK_ABORT; return CALLBACK_CONTINUE; }
static enum http_host_state http_parse_host_char(enum http_host_state s, const char ch) { switch(s) { case s_http_userinfo: case s_http_userinfo_start: if (ch == '@') { return s_http_host_start; } if (IS_USERINFO_CHAR(ch)) { return s_http_userinfo; } break; case s_http_host_start: if (ch == '[') { return s_http_host_v6_start; } if (IS_HOST_CHAR(ch)) { return s_http_host; } break; case s_http_host: if (IS_HOST_CHAR(ch)) { return s_http_host; } /* FALLTHROUGH */ case s_http_host_v6_end: if (ch == ':') { return s_http_host_port_start; } break; case s_http_host_v6: if (ch == ']') { return s_http_host_v6_end; } /* FALLTHROUGH */ case s_http_host_v6_start: if (IS_HEX(ch) || ch == ':' || ch == '.') { return s_http_host_v6; } if (s == s_http_host_v6 && ch == '%') { return s_http_host_v6_zone_start; } break; case s_http_host_v6_zone: if (ch == ']') { return s_http_host_v6_end; } /* FALLTHROUGH */ case s_http_host_v6_zone_start: /* RFC 6874 Zone ID consists of 1*( unreserved / pct-encoded) */ if (IS_ALPHANUM(ch) || ch == '%' || ch == '.' || ch == '-' || ch == '_' || ch == '~') { return s_http_host_v6_zone; } break; case s_http_host_port: case s_http_host_port_start: if (IS_NUM(ch)) { return s_http_host_port; } break; default: break; } return s_http_host_dead; }