unsigned int CVideoInfoTag::GetDurationFromMinuteString(const std::string &runtime) { unsigned int duration = (unsigned int)str2uint64(runtime); if (!duration) { // failed for some reason, or zero duration = strtoul(runtime.c_str(), NULL, 10); CLog::Log(LOGWARNING, "%s <runtime> should be in minutes. Interpreting '%s' as %u minutes", __FUNCTION__, runtime.c_str(), duration); } return duration*60; }
uint64_t CVariant::asUnsignedInteger(uint64_t fallback) const { switch (m_type) { case VariantTypeUnsignedInteger: return m_data.unsignedinteger; case VariantTypeInteger: return (uint64_t)m_data.integer; case VariantTypeDouble: return (uint64_t)m_data.dvalue; case VariantTypeString: return str2uint64(*m_data.string, fallback); case VariantTypeWideString: return str2uint64(*m_data.wstring, fallback); default: return fallback; } return fallback; }
int main(int argc,const char ** argv) { uint64_t n; uint64_t inv; if (argc < 2){ printf("Enter the number for which the inverse has to be computed\n"); exit(EXIT_FAILURE); } n = str2uint64(argv[1],10); if (! (n & 1ull)){ fprintf(stderr,"even number is not invertible\n"); exit(EXIT_FAILURE); } inv = inverse_mod2to64(n); printf("%llu\n",inv); }
bool CHttpRanges::Parse(const std::string& header, uint64_t totalLength) { m_ranges.clear(); if (header.empty() || totalLength == 0 || !StringUtils::StartsWithNoCase(header, "bytes=")) return false; uint64_t lastPossiblePosition = totalLength - 1; // remove "bytes=" from the beginning std::string rangesValue = header.substr(6); // split the value of the "Range" header by "," std::vector<std::string> rangeValues = StringUtils::Split(rangesValue, ","); for (std::vector<std::string>::const_iterator range = rangeValues.begin(); range != rangeValues.end(); range++) { // there must be a "-" in the range definition if (range->find("-") == std::string::npos) return false; std::vector<std::string> positions = StringUtils::Split(*range, "-"); if (positions.size() != 2) return false; bool hasStart = false; uint64_t start = 0; bool hasEnd = false; uint64_t end = 0; // parse the start and end positions if (!positions.front().empty()) { if (!StringUtils::IsNaturalNumber(positions.front())) return false; start = str2uint64(positions.front(), 0); hasStart = true; } if (!positions.back().empty()) { if (!StringUtils::IsNaturalNumber(positions.back())) return false; end = str2uint64(positions.back(), 0); hasEnd = true; } // nothing defined at all if (!hasStart && !hasEnd) return false; // make sure that the end position makes sense if (hasEnd) end = std::min(end, lastPossiblePosition); if (!hasStart && hasEnd) { // the range is defined as the number of bytes from the end start = totalLength - end; end = lastPossiblePosition; } else if (hasStart && !hasEnd) end = lastPossiblePosition; // make sure the start position makes sense if (start > lastPossiblePosition) return false; // make sure that the start position is smaller or equal to the end position if (end < start) return false; m_ranges.push_back(CHttpRange(start, end)); } if (m_ranges.empty()) return false; SortAndCleanup(); return !m_ranges.empty(); }
static int handle_args(int argc, char *argv[], struct app_params *p) { int c, idx; bool ok; bladerf_log_level log_level; /* Print help */ if (argc == 1) { return 1; } while ((c = getopt_long(argc, argv, OPTSTR, long_options, &idx)) >= 0) { switch (c) { case 'v': log_level = str2loglevel(optarg, &ok); if (!ok) { fprintf(stderr, "Invalid log level: %s\n", optarg); return -1; } else { bladerf_log_set_verbosity(log_level); } break; case 'h': return 1; case 'd': if (p->device_str != NULL) { fprintf(stderr, "Device already specified: %s\n", p->device_str); return -1; } else { p->device_str = strdup(optarg); if (p->device_str == NULL) { perror("strdup"); return -1; } } break; case 's': p->samplerate = str2uint_suffix(optarg, BLADERF_SAMPLERATE_MIN, BLADERF_SAMPLERATE_REC_MAX, freq_suffixes, ARRAY_SIZE(freq_suffixes), &ok); if (!ok) { fprintf(stderr, "Invalid sample rate: %s\n", optarg); return -1; } break; case 'B': p->buf_size = str2uint_suffix(optarg, 1024, UINT_MAX, len_suffixes, ARRAY_SIZE(len_suffixes), &ok); if (!ok || (p->buf_size % 1024) != 0) { fprintf(stderr, "Invalid buffer length: %s\n", optarg); return -1; } break; case 't': p->test_name = strdup(optarg); if (p->test_name == NULL) { perror("strdup"); return -1; } break; case 'S': p->prng_seed = str2uint64(optarg, 1, UINT64_MAX, &ok); if (!ok) { fprintf(stderr, "Invalid seed value: %s\n", optarg); return -1; } break; default: return -1; } } return 0; }
static spt str_to_spt(const struct formatter_field *field, const char *str, bool *success) { spt value; bool conv_ok = false; const unsigned int field_width = get_width(field); const uint64_t mask = (field_width < 64) ? (1llu << field_width) - 1 : 0xffffffffffffffffllu; *success = false; switch (field->format) { case FORMATTER_FMT_HEX: case FORMATTER_FMT_UNSIGNED_DEC: { uint64_t tmp = str2uint64(str, 0, UINT64_MAX, &conv_ok); if (!conv_ok) { goto inval; } tmp = (uint64_t) (((float) tmp - field->offset) / field->scaling); value = spt_from_uint64(tmp); break; } case FORMATTER_FMT_TWOS_COMPLEMENT: { int64_t tmp = str2int64(str, INT64_MIN, INT64_MAX, &conv_ok); if (!conv_ok) { goto inval; } tmp = (int64_t) (((float) tmp - field->offset) / field->scaling); value = spt_from_int64(tmp); /* Mask off sign-extended bits to field with */ value &= mask; break; } case FORMATTER_FMT_SIGN_MAGNITUDE: { int64_t tmp = str2int64(str, INT64_MIN, INT64_MAX, &conv_ok); bool negative = (tmp < 0); if (!conv_ok) { goto inval; } tmp = (int64_t) (((float) tmp - field->offset) / field->scaling); /* Field width includes sign bit (assumed to be MSB) */ tmp &= (1 << (field_width - 1)) - 1; if (negative) { tmp |= (1 << (field_width - 1)); } value = spt_from_uint64((uint64_t) tmp); break; } case FORMATTER_FMT_FLOAT: { float tmp = (float) str2double(str, -DBL_MAX, DBL_MAX, &conv_ok); if (!conv_ok) { goto inval; } value = spt_from_float((float) tmp, field->scaling, field->offset); /* Mask off sign-extended bits to field with */ value &= mask; break; } case FORMATTER_FMT_ENUM: { size_t i; bool have_enum = false; for (i = 0; i < field->enum_count && !have_enum; i++) { if (!strcasecmp(str, field->enums[i].str)) { value = field->enums[i].value; have_enum = true; } } if (!have_enum) { uint64_t tmp = str2uint64(str, 0, UINT64_MAX, &conv_ok); if (!conv_ok) { goto inval; } value = spt_from_uint64(tmp); } break; } default: log_critical("Bug: Invalid field format: %d\n", field->format); return spt_from_uint64(0); } if ((spt_to_uint64(value) & mask) != spt_to_uint64(value)) { log_error("Value is too large for field \"%s\": %s\n", field->name, str); return spt_from_uint64(0); } *success = true; return value; inval: log_error("Invalid value for field \"%s\": %s\n", field->name, str); return spt_from_uint64(0); }
int get_params(int argc, char *argv[], struct app_params *p) { int c, idx; bool ok; bladerf_log_level level; memset(p, 0, sizeof(p[0])); p->randval_seed = 1; while((c = getopt_long(argc, argv, OPTARG, long_options, &idx)) != -1) { switch (c) { case 'd': if (p->device_str) { fprintf(stderr, "Device already specified!\n"); return -1; } else { p->device_str = strdup(optarg); if (p->device_str == NULL) { perror("strdup"); return -1; } } break; case 't': if (p->test_name) { fprintf(stderr, "Test already specified!\n"); return -1; } else { p->test_name = strdup(optarg); if (p->test_name == NULL) { perror("strdup"); return -1; } } break; case 's': p->randval_seed = str2uint64(optarg, 1, UINT64_MAX, &ok); if (!ok) { fprintf(stderr, "Invalid seed value: %s\n", optarg); return -1; } break; case 'h': usage(argv[0]); return 1; case 1: p->use_xb200 = true; break; case 'L': list_tests(); return 1; case 'v': level = str2loglevel(optarg, &ok); if (ok) { bladerf_log_set_verbosity(level); } else { fprintf(stderr, "Invalid log level: %s\n", optarg); return -1; } break; default: return -1; } } return 0; }
/****************************************************************************** * * * Function: parse_cfg_file * * * * Purpose: parse configuration file * * * * Parameters: cfg_file - full name of config file * * cfg - pointer to configuration parameter structure * * level - a level of included file * * optional - do not treat missing configuration file as error * * strict - treat unknown parameters as error * * * * Return value: SUCCEED - parsed successfully * * FAIL - error processing config file * * * * Author: Alexei Vladishev, Eugene Grigorjev * * * * Comments: * * * ******************************************************************************/ static int __parse_cfg_file(const char *cfg_file, struct cfg_line *cfg, int level, int optional, int strict) { #define ZBX_MAX_INCLUDE_LEVEL 10 #define ZBX_CFG_LTRIM_CHARS "\t " #define ZBX_CFG_RTRIM_CHARS ZBX_CFG_LTRIM_CHARS "\r\n" FILE *file; int i, lineno, result = SUCCEED, param_valid; char line[MAX_STRING_LEN], *parameter, *value; zbx_uint64_t var; assert(cfg); if (++level > ZBX_MAX_INCLUDE_LEVEL) { zbx_error("Recursion detected! Skipped processing of '%s'.", cfg_file); return FAIL; } if (NULL != cfg_file) { if (NULL == (file = fopen(cfg_file, "r"))) goto cannot_open; for (lineno = 1; NULL != fgets(line, sizeof(line), file); lineno++) { zbx_ltrim(line, ZBX_CFG_LTRIM_CHARS); zbx_rtrim(line, ZBX_CFG_RTRIM_CHARS); if ('#' == *line || '\0' == *line) continue; parameter = line; if (NULL == (value = strchr(line, '='))) goto garbage; *value++ = '\0'; zbx_rtrim(parameter, ZBX_CFG_RTRIM_CHARS); zbx_ltrim(value, ZBX_CFG_LTRIM_CHARS); zabbix_log(LOG_LEVEL_DEBUG, "cfg: para: [%s] val [%s]", parameter, value); if (0 == strcmp(parameter, "Include")) { if (FAIL == (result = parse_cfg_object(value, cfg, level, strict))) break; continue; } for (i = 0; '\0' != value[i]; i++) { if ('\n' == value[i]) { value[i] = '\0'; break; } } param_valid = 0; for (i = 0; NULL != cfg[i].parameter; i++) { if (0 != strcmp(cfg[i].parameter, parameter)) continue; param_valid = 1; zabbix_log(LOG_LEVEL_DEBUG, "accepted configuration parameter: '%s' = '%s'",parameter, value); if (TYPE_INT == cfg[i].type) { if (FAIL == str2uint64(value, &var)) goto incorrect_config; if ((cfg[i].min && var < cfg[i].min) || (cfg[i].max && var > cfg[i].max)) goto incorrect_config; *((int *)cfg[i].variable) = (int)var; } else if (TYPE_STRING == cfg[i].type) { /* free previous value memory */ char *p = *((char **)cfg[i].variable); if (NULL != p) zbx_free(p); *((char **)cfg[i].variable) = strdup(value); } else if (TYPE_MULTISTRING == cfg[i].type) { zbx_strarr_add(cfg[i].variable, value); } else assert(0); } if (0 == param_valid && ZBX_CFG_STRICT == strict) goto unknown_parameter; } fclose(file); } if (1 != level) /* skip mandatory parameters check for included files */ return result; for (i = 0; NULL != cfg[i].parameter; i++) /* check for mandatory parameters */ { if (PARM_MAND != cfg[i].mandatory) continue; if (TYPE_INT == cfg[i].type) { if (0 == *((int *)cfg[i].variable)) goto missing_mandatory; } else if (TYPE_STRING == cfg[i].type) { if (NULL == (*(char **)cfg[i].variable)) goto missing_mandatory; } else assert(0); } return result; cannot_open: if (optional) return result; zbx_error("cannot open config file [%s]: %s", cfg_file, zbx_strerror(errno)); exit(1); missing_mandatory: zbx_error("missing mandatory parameter [%s] in config file [%s]", cfg[i].parameter, cfg_file); exit(1); incorrect_config: fclose(file); zbx_error("wrong value of [%s] in config file [%s], line %d", cfg[i].parameter, cfg_file, lineno); exit(1); unknown_parameter: fclose(file); zbx_error("unknown parameter [%s] in config file [%s], line %d", parameter, cfg_file, lineno); exit(1); garbage: fclose(file); zbx_error("invalid entry [%s] (not following \"parameter=value\" notation) in config file [%s], line %d", line, cfg_file, lineno); exit(1); }
/****************************************************************************** * * * Function: parse_cfg_file * * * * Purpose: parse configuration file * * * * Parameters: cfg_file - full name of config file * * cfg - pointer to configuration parameter structure * * * * Return value: SUCCEED - parsed successfully * * FAIL - error processing config file * * * * Author: Alexei Vladishev, Eugene Grigorjev * * * * Comments: * * * ******************************************************************************/ static int __parse_cfg_file(const char *cfg_file, struct cfg_line *cfg, int level, int optional) { #define ZBX_MAX_INCLUDE_LEVEL 10 #define ZBX_CFG_LTRIM_CHARS "\t " #define ZBX_CFG_RTRIM_CHARS ZBX_CFG_LTRIM_CHARS "\r\n" FILE *file; int i, lineno, result = SUCCEED; char line[MAX_STRING_LEN], *parameter, *value; zbx_uint64_t var; assert(cfg); if (++level > ZBX_MAX_INCLUDE_LEVEL) { zbx_error("Recursion detected! Skipped processing of '%s'.", cfg_file); return FAIL; } if (NULL != cfg_file) { if (NULL == (file = fopen(cfg_file, "r"))) goto cannot_open; for (lineno = 1; NULL != fgets(line, sizeof(line), file); lineno++) { zbx_ltrim(line, ZBX_CFG_LTRIM_CHARS); if ('#' == *line) continue; if (strlen(line) < 3) continue; parameter = line; value = strstr(line, "="); if (NULL == value) { zbx_error("error in line [%d] \"%s\"", lineno, line); result = FAIL; break; } *value++ = '\0'; zbx_rtrim(parameter, ZBX_CFG_RTRIM_CHARS); zbx_ltrim(value, ZBX_CFG_LTRIM_CHARS); zbx_rtrim(value, ZBX_CFG_RTRIM_CHARS); zabbix_log(LOG_LEVEL_DEBUG, "cfg: para: [%s] val [%s]", parameter, value); if (0 == strcmp(parameter, "Include")) { if (FAIL == (result = parse_cfg_object(value, cfg, level))) break; } for (i = 0; '\0' != value[i]; i++) { if ('\n' == value[i]) { value[i] = '\0'; break; } } for (i = 0; NULL != cfg[i].parameter; i++) { if (0 != strcmp(cfg[i].parameter, parameter)) continue; zabbix_log(LOG_LEVEL_DEBUG, "accepted configuration parameter: '%s' = '%s'",parameter, value); if (NULL != cfg[i].function) { if (SUCCEED != cfg[i].function(value)) goto incorrect_config; } else if (TYPE_INT == cfg[i].type) { if (FAIL == str2uint64(value, &var)) goto incorrect_config; if ((cfg[i].min && var < cfg[i].min) || (cfg[i].max && var > cfg[i].max)) goto incorrect_config; *((int *)cfg[i].variable) = var; } else if (TYPE_STRING == cfg[i].type) { *((char **)cfg[i].variable) = strdup(value); } else assert(0); } } fclose(file); } if (1 != level) /* skip mandatory parameters check for included files */ return result; for (i = 0; NULL != cfg[i].parameter; i++) /* check for mandatory parameters */ { if (PARM_MAND != cfg[i].mandatory) continue; if (TYPE_INT == cfg[i].type) { if (0 == *((int *)cfg[i].variable)) goto missing_mandatory; } else if (TYPE_STRING == cfg[i].type) { if (NULL == (*(char **)cfg[i].variable)) goto missing_mandatory; } else assert(0); } return result; cannot_open: if (optional) return result; zbx_error("cannot open config file [%s] [%s]", cfg_file, strerror(errno)); exit(1); missing_mandatory: zbx_error("missing mandatory parameter [%s]", cfg[i].parameter); exit(1); incorrect_config: zbx_error("wrong value for [%s] in line %d", cfg[i].parameter, lineno); exit(1); }