static void cnode_range_set(struct cnode *cnode) { char *p; char *cp; int64_t min = 0; int64_t max = 0; char *range; if (cnode->range != NULL) { range = cnode->range; } else { range = cnode->name; } p = strrchr(range, '-'); if (p == NULL) { return; } *p = '\0'; cp = str2int64(range, &min); if (!cp) { return; } *p = '-'; range = p + 1; cp = str2int64(range, &max); if (!cp) { return; } cnode->min = min; cnode->max = max; }
static char * humanize_usage_amount(char *usage) { int64_t num; const char *resource, *amount; char *copy, *humanized, buf[6]; copy = strdup(usage); if (copy == NULL) err(1, "strdup"); resource = strsep(©, "="); amount = copy; assert(resource != NULL); assert(amount != NULL); if (str2int64(amount, &num) != 0 || humanize_number(buf, sizeof(buf), num, "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE) == -1) { free(copy); return (usage); } asprintf(&humanized, "%s=%s", resource, buf); if (humanized == NULL) err(1, "asprintf"); return (humanized); }
int64_t CVariant::asInteger(int64_t fallback) const { switch (m_type) { case VariantTypeInteger: return m_data.integer; case VariantTypeUnsignedInteger: return (int64_t)m_data.unsignedinteger; case VariantTypeDouble: return (int64_t)m_data.dvalue; case VariantTypeString: return str2int64(*m_data.string, fallback); case VariantTypeWideString: return str2int64(*m_data.wstring, fallback); default: return fallback; } return fallback; }
static char * humanize_amount(char *rule) { int64_t num; const char *subject, *subject_id, *resource, *action, *amount, *per; char *copy, *humanized, buf[6]; copy = strdup(rule); if (copy == NULL) err(1, "strdup"); subject = strsep(©, ":"); subject_id = strsep(©, ":"); resource = strsep(©, ":"); action = strsep(©, "=/"); amount = strsep(©, "/"); per = copy; if (amount == NULL || strlen(amount) == 0 || str2int64(amount, &num) != 0) { free(copy); return (rule); } assert(subject != NULL); assert(subject_id != NULL); assert(resource != NULL); assert(action != NULL); if (humanize_number(buf, sizeof(buf), num, "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE) == -1) err(1, "humanize_number"); if (per == NULL) asprintf(&humanized, "%s:%s:%s:%s=%s", subject, subject_id, resource, action, buf); else asprintf(&humanized, "%s:%s:%s:%s=%s/%s", subject, subject_id, resource, action, buf, per); if (humanized == NULL) err(1, "asprintf"); return (humanized); }
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); }
void file_reader::SetFile(HANDLE file) { // init memset(m_remux_sig, 0, 128); LARGE_INTEGER li; li.QuadPart = 0; ::SetFilePointerEx(file, li, &li, SEEK_CUR); m_file = file; m_is_encrypted = false; memset(m_hash, 0, 32); if (m_block_cache) free(m_block_cache); // read parameter DWORD byte_read = 0; __int64 eightcc; if (!::ReadFile(m_file, &eightcc, 8, &byte_read, NULL) || byte_read != 8) goto rewind; if (eightcc != str2int64("my12doom")) goto rewind; __int64 root_size; if (!::ReadFile(m_file, &root_size, 8, &byte_read, NULL) || byte_read != 8) goto rewind; m_start_in_file = root_size + 16; __int64 total_byte_read = 0; do { __int64 leaf_size; if (!::ReadFile(m_file, &eightcc, 8, &byte_read, NULL) || byte_read != 8) goto rewind; if (!::ReadFile(m_file, &leaf_size, 8, &byte_read, NULL) || byte_read != 8) goto rewind; byte_read = 0; if (eightcc == str2int64("filesize")) { if (!::ReadFile(m_file, &m_file_size, 8, &byte_read, NULL) || byte_read != 8) goto rewind; } else if (eightcc == str2int64("blocksize")) { if (!::ReadFile(m_file, &m_block_size, 8, &byte_read, NULL) || byte_read != 8) goto rewind; } else if (eightcc == str2int64("keyhint")) { if (!::ReadFile(m_file, m_keyhint, 32, &byte_read, NULL) || byte_read != 32) goto rewind; } else if (eightcc == str2int64("layout")) { if (!::ReadFile(m_file, &m_layout, 8, &byte_read, NULL) || byte_read != 8) goto rewind; } else if (eightcc == str2int64("srchash")) { if (!::ReadFile(m_file, m_hash, 20, &byte_read, NULL) || byte_read != 20) goto rewind; } else if (eightcc == str2int64("remux")) { if (!::ReadFile(m_file, m_remux_sig, 128, &byte_read, NULL) || byte_read != 128) goto rewind; } SetFilePointer(m_file, leaf_size - byte_read, NULL, SEEK_CUR); total_byte_read += 16 + leaf_size; } while (total_byte_read < root_size); if (m_file_size == 0 || m_block_size == 0) goto rewind; // set member m_file_pos_cache = m_start_in_file; m_is_encrypted = true; m_cache_pos = -1; m_pos = 0; m_block_cache = (unsigned char*)malloc(m_block_size); return; rewind: ::SetFilePointerEx(file, li, NULL, SEEK_SET); }