/* * Parses the given little endian bit string into a bit string padded or truncated to * binary_size bits. */ char *convert_binary_string_of_size_to_bit_string(short is_dont_care_number, char *orig_string, int binary_size) { if (!is_binary_string(orig_string) && !is_dont_care_number) error_message(PARSE_ERROR, -1, -1, "Invalid binary number: %s.\n", orig_string); int count = strlen(orig_string); char *bit_string = (char *)calloc(count + 1, sizeof(char)); // Copy the original string into the buffer. strcat(bit_string, orig_string); // Change to big endian. reverse_string(bit_string, count); // Pad with zeros to binary_size. while (count < binary_size) { bit_string = (char *)realloc(bit_string, sizeof(char) * (count + 2)); bit_string[count++] = '0'; bit_string[count] = '\0'; } // Truncate to binary_size bit_string[binary_size] = '\0'; // Change to little endian reverse_string(bit_string, binary_size); // Copy out only the bits before the truncation. char *return_string = strdup(bit_string); free(bit_string); return return_string; }
static void rubyobj2variant(VALUE value, otama_variant_t *var) { VALUE s; switch (TYPE(value)) { case T_NIL: otama_variant_set_null(var); break; case T_FLOAT: otama_variant_set_float(var, NUM2DBL(value)); break; case T_FIXNUM: case T_BIGNUM: otama_variant_set_int(var, NUM2LL(value)); break; case T_TRUE: otama_variant_set_int(var, 1); break; case T_FALSE: otama_variant_set_int(var, 0); break; case T_STRING: StringValue(value); if (is_binary_string(value)) { otama_variant_set_binary(var, RSTRING_PTR(value), RSTRING_LEN(value)); } else { otama_variant_set_string(var, RSTRING_PTR(value)); } break; case T_SYMBOL: s = rb_funcall(value, rb_intern("to_s"), 0); otama_variant_set_string(var, StringValuePtr(s)); break; case T_ARRAY: { int len = (int)RARRAY_LEN(value), i; otama_variant_set_array(var); for (i = 0; i < len; ++i) { VALUE elm = rb_ary_entry(value, i); rubyobj2variant(elm, otama_variant_array_at(var, i)); } } break; case T_HASH: otama_variant_set_hash(var); rb_iterate(rb_each, value, rubyobj2variant_pair, (VALUE)var); break; default: if (rb_obj_is_instance_of(value, cOtamaFeatureRaw) == Qtrue) { otama_feature_raw_t *raw = NULL; Data_Get_Struct(value, otama_feature_raw_t, raw); otama_variant_set_pointer(var, raw); } else { otama_variant_set_null(var); } break; } }
int is_string_of_radix(char *string, int radix) { if (radix == 16) return is_hex_string(string); else if (radix == 10) return is_decimal_string(string); else if (radix == 8) return is_octal_string(string); else if (radix == 2) return is_binary_string(string); else return FALSE; }
/** * Check hash sums in a hash file. * Lines beginning with ';' and '#' are ignored. * * @param hash_file_path - the path of the file with hash sums to verify. * @param chdir - true if function should emulate chdir to directory of filepath before checking it. * @return zero on success, -1 on fail */ int check_hash_file(file_t* file, int chdir) { FILE *fd; char buf[2048]; size_t pos; const char *ralign; timedelta_t timer; struct file_info info; const char* hash_file_path = file->path; int res = 0, line_num = 0; double time; /* process --check-embedded option */ if(opt.mode & MODE_CHECK_EMBEDDED) { unsigned crc32_be; if(find_embedded_crc32(hash_file_path, &crc32_be)) { /* initialize file_info structure */ memset(&info, 0, sizeof(info)); info.full_path = rsh_strdup(hash_file_path); info.file = file; file_info_set_print_path(&info, info.full_path); info.sums_flags = info.hc.hash_mask = RHASH_CRC32; info.hc.flags = HC_HAS_EMBCRC32; info.hc.embedded_crc32_be = crc32_be; res = verify_sums(&info); fflush(rhash_data.out); if(!rhash_data.interrupted) { if(res == 0) rhash_data.ok++; else if(res == -1 && errno == ENOENT) rhash_data.miss++; rhash_data.processed++; } free(info.full_path); file_info_destroy(&info); } else { log_warning(_("file name doesn't contain a CRC32: %s\n"), hash_file_path); return -1; } return 0; } /* initialize statistics */ rhash_data.processed = rhash_data.ok = rhash_data.miss = 0; rhash_data.total_size = 0; if(file->mode & FILE_IFSTDIN) { fd = stdin; hash_file_path = "<stdin>"; } else if( !(fd = rsh_fopen_bin(hash_file_path, "rb") )) { log_file_error(hash_file_path); return -1; } pos = strlen(hash_file_path)+16; ralign = str_set(buf, '-', (pos < 80 ? 80 - (int)pos : 2)); fprintf(rhash_data.out, _("\n--( Verifying %s )%s\n"), hash_file_path, ralign); fflush(rhash_data.out); rhash_timer_start(&timer); /* mark the directory part of the path, by setting the pos index */ if(chdir) { pos = strlen(hash_file_path); for(; pos > 0 && !IS_PATH_SEPARATOR(hash_file_path[pos]); pos--); if(IS_PATH_SEPARATOR(hash_file_path[pos])) pos++; } else pos = 0; /* read crc file line by line */ for(line_num = 0; fgets(buf, 2048, fd); line_num++) { char* line = buf; char* path_without_ext = NULL; /* skip unicode BOM */ if(line_num == 0 && buf[0] == (char)0xEF && buf[1] == (char)0xBB && buf[2] == (char)0xBF) line += 3; if(*line == 0) continue; /* skip empty lines */ if(is_binary_string(line)) { log_error(_("file is binary: %s\n"), hash_file_path); if(fd != stdin) fclose(fd); return -1; } /* skip comments and empty lines */ if(IS_COMMENT(*line) || *line == '\r' || *line == '\n') continue; memset(&info, 0, sizeof(info)); if(!hash_check_parse_line(line, &info.hc, !feof(fd))) continue; if(info.hc.hash_mask == 0) continue; info.print_path = info.hc.file_path; info.sums_flags = info.hc.hash_mask; /* see if crc file contains a hash sum without a filename */ if(info.print_path == NULL) { char* point; path_without_ext = rsh_strdup(hash_file_path); point = strrchr(path_without_ext, '.'); if(point) { *point = '\0'; file_info_set_print_path(&info, path_without_ext); } } if(info.print_path != NULL) { file_t file_to_check; int is_absolute = IS_PATH_SEPARATOR(info.print_path[0]); IF_WINDOWS(is_absolute = is_absolute || (info.print_path[0] && info.print_path[1] == ':')); /* if filename shall be prepended by a directory path */ if(pos && !is_absolute) { size_t len = strlen(info.print_path); info.full_path = (char*)rsh_malloc(pos + len + 1); memcpy(info.full_path, hash_file_path, pos); strcpy(info.full_path + pos, info.print_path); } else { info.full_path = rsh_strdup(info.print_path); } memset(&file_to_check, 0, sizeof(file_t)); file_to_check.path = info.full_path; rsh_file_stat(&file_to_check); info.file = &file_to_check; /* verify hash sums of the file */ res = verify_sums(&info); fflush(rhash_data.out); rsh_file_cleanup(&file_to_check); file_info_destroy(&info); if(rhash_data.interrupted) { free(path_without_ext); break; } /* update statistics */ if(res == 0) rhash_data.ok++; else if(res == -1 && errno == ENOENT) rhash_data.miss++; rhash_data.processed++; } free(path_without_ext); } time = rhash_timer_stop(&timer); fprintf(rhash_data.out, "%s\n", str_set(buf, '-', 80)); print_check_stats(); if(rhash_data.processed != rhash_data.ok) rhash_data.error_flag = 1; if(opt.flags & OPT_SPEED && rhash_data.processed > 1) { print_time_stats(time, rhash_data.total_size, 1); } rhash_data.processed = 0; res = ferror(fd); /* check that crc file has been read without errors */ if(fd != stdin) fclose(fd); return (res == 0 ? 0 : -1); }