void died() { lives--; if ( lives < 0) return; int digits = nDigits(lives); tft.drawNumber(lives,(24-digits)*12, 4, 2, RED, SCOREBOARD_COLOR); }
int printToLog (int t, int nlug, int id, char* obs) { int nSpaces = 10 - nDigits(t); for(; nSpaces > 0; --nSpaces) { fprintf(pLog, " "); } fprintf(pLog, "%d ;", t); nSpaces = 10 - nDigits(nlug); for(; nSpaces > 0; --nSpaces) { fprintf(pLog, " "); } fprintf(pLog, "%d ; %d ;", nlug, id); fprintf(pLog, "%s\n", obs); return 0; }
static int greyhole_aio_write(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb) { int result; FILE *spoolf; char filename[255]; struct timeval tp; result = SMB_VFS_NEXT_AIO_WRITE(handle, fsp, aiocb); if (result >= 0) { gettimeofday(&tp, (struct timezone *) NULL); char *share = lp_servicename(handle->conn->params->service); snprintf(filename, 39 + strlen(share) + nDigits(fsp->fh->fd), "/var/spool/greyhole/%.0f-%s-%d", ((double) (tp.tv_sec)*1000000.0), share, fsp->fh->fd); spoolf = fopen(filename, "wt"); fprintf(spoolf, "fwrite\n%s\n%d\n\n", share, fsp->fh->fd); fclose(spoolf); } return result; }
static ssize_t greyhole_recvfile(vfs_handle_struct *handle, int fromfd, files_struct *tofsp, SMB_OFF_T offset, size_t n) { ssize_t result; FILE *spoolf; char filename[255]; struct timeval tp; result = SMB_VFS_NEXT_RECVFILE(handle, fromfd, tofsp, offset, n); if (result >= 0) { gettimeofday(&tp, (struct timezone *) NULL); char *share = lp_servicename(handle->conn->params->service); snprintf(filename, 39 + strlen(share) + nDigits(tofsp->fh->fd), "/var/spool/greyhole/%.0f-%s-%d", ((double) (tp.tv_sec)*1000000.0), share, tofsp->fh->fd); spoolf = fopen(filename, "wt"); fprintf(spoolf, "fwrite\n%s\n%d\n\n", share, tofsp->fh->fd); fclose(spoolf); } return result; }
static ssize_t greyhole_pwrite(vfs_handle_struct *handle, files_struct *fsp, const void *data, size_t count, off_t offset) { ssize_t result; FILE *spoolf; char filename[255]; struct timeval tp; result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, count, offset); if (result >= 0) { gettimeofday(&tp, (struct timezone *) NULL); char *share = lp_servicename(talloc_tos(), handle->conn->params->service); snprintf(filename, 43 + strlen(share) + nDigits(fsp->fh->fd), "/var/spool/greyhole/mem/%.0f-%s-%d", ((double) (tp.tv_sec)*1000000.0), share, fsp->fh->fd); spoolf = fopen(filename, "wt"); fprintf(spoolf, "fwrite\n%s\n%d\n\n", share, fsp->fh->fd); fclose(spoolf); } return result; }
static int rehash_verity(const int meta_version, const int dm_verity_version, const char * data_device, const size_t block_size) { #if 0 int r = -1; char *table, *salt = NULL; salt = malloc(32); /* biggest length */ if (NULL == salt) { fprintf(stderr, "Failed to malloc mem for salt\n"); return -1; } #endif int ret = 0; uint64_t part_size; if(ext4_part_size(TARGET_DEV, &part_size)) { printf("failed to get part size.\n"); return -1; } const char * tmp_hash_file = TMP_HASH_FILE; const char * tmp_hash_table = TMP_HASH_TABLE; //unlink(tmp_hash_file); //unlink(tmp_hash_table); const char * hash_name = HASH_NAME; int i; char * table = NULL, *p; const long data_blocks = part_size / DMVERITY_BLOCK_SIZE; #ifdef USE_SHA256 const int digest_size = 32;//sha256 #elif USE_SHA1 const int digest_size = 20;//sha1 #else const int digest_size = 16;//md5 #endif char salt[digest_size]; char root_hash[digest_size]; const loff_t hash_start = (part_size + DMVERITY_META_SIZE)/DMVERITY_BLOCK_SIZE; const loff_t hash_position = DMVERITY_META_SIZE/DMVERITY_BLOCK_SIZE; struct verity_meta_header meta_header; FILE * fp; //0.1 generate random salt /* generate_salt(salt); */ if (generate_salt(salt, digest_size) != digest_size) { printf("Error generating random salt.\n"); // how to handle it better? now it will use whatever compiler gives: all 0's } // 1. generate hash, table and write to a tmp hash file if(VERITY_create_hash(dm_verity_version, \ hash_name, \ TMP_HASH_FILE, \ data_device, \ block_size, \ block_size, \ data_blocks, \ hash_position, \ (unsigned char *)root_hash, \ digest_size, \ (const unsigned char *)salt, \ digest_size)) { printf("failed to create hash tree\n"); ret = -1; goto rehash_out; } int table_size = nDigits(dm_verity_version) + 1 + strlen(TARGET_DEV) + 1 + strlen(TARGET_DEV) + 1 + nDigits(DMVERITY_BLOCK_SIZE) + 1 + nDigits(DMVERITY_BLOCK_SIZE) + 1 + nDigits(data_blocks) + 1 + nDigits(hash_start) + 1 + strlen(hash_name) + 1 + digest_size * 2 + 1 + digest_size * 2 + 1; table = malloc(table_size); if(NULL == table){ printf("malloc failed\n"); ret = -1; goto rehash_out; } table[table_size-1] = 0; i = sprintf(table, "%d %s %s %lld %lld %lld %lld %s ", dm_verity_version, TARGET_DEV, TARGET_DEV, (long long int) DMVERITY_BLOCK_SIZE, (long long int) DMVERITY_BLOCK_SIZE, (long long int) data_blocks, (long long int) hash_start, hash_name); if(i <= 0){ printf("sprintf error"); free(table); ret = -1; goto rehash_out; } //printf("Table: %s\n", table); p = table + i; bytes_to_hex(root_hash, p, digest_size); p += digest_size * 2; p += sprintf(p, " "); bytes_to_hex(salt, p, digest_size); printf("table: %s", table); // 2.1 generate meta_header meta_header.magic_number = VERITY_METADATA_MAGIC_NUMBER; meta_header.protocol_version = 0; meta_header.table_length = strlen(table);//not including trailing NULL memset(&meta_header.signature, 0, sizeof(meta_header.signature)); //tmp_hash_file it should have been created by generate_dm_verity_hash already. // 2.2 write table and meta_header to tmp hash file fp = fopen(tmp_hash_file, "r+"); if (NULL == fp) { printf("failed to open temp file\n"); ret = -1; goto rehash_out; } if(1 != fwrite(&meta_header, sizeof(struct verity_meta_header), 1, fp)){ printf("failed to write temp file\n"); fclose(fp); ret = -1; goto rehash_out; } printf("write meta_header %d\n", sizeof(struct verity_meta_header)); if(1 != fwrite(table, meta_header.table_length+1, 1, fp)){ printf("failed to write temp file\n"); fclose(fp); ret = -1; goto rehash_out; } printf("write table %d\n", meta_header.table_length+1); fflush(fp); fsync(fileno(fp)); fclose(fp); // 2.3 write table to tmp hash meta table file fp = fopen(tmp_hash_table, "w"); if (NULL == fp) { printf("failed to open temp meta table file\n"); ret = -1; goto rehash_out; } if(1 != fwrite(table, meta_header.table_length+1, 1, fp)){ printf("failed to write temp meta table file\n"); fclose(fp); ret = -1; goto rehash_out; } printf("write table %d\n", meta_header.table_length+1); fflush(fp); fsync(fileno(fp)); fclose(fp); // 3. write tmp hash file to the /system (not signed) /* if(file_to_device(tmp_hash_file, TARGET_DEV, 1024*1024, part_size)){ printf("failed to write hash 001\n"); goto rehash_out; } */ rehash_out: //unlink(tmp_hash_file); if(table) free(table); return ret; }
void nextLevel() { level++; int digits = nDigits(level); tft.drawNumber(level,(20-digits)*12, 4, 2, RED, SCOREBOARD_COLOR); }
void update( int scored ){ score+= scored; int digits = nDigits(score); tft.drawNumber(score, (15-digits)*12, 4, 2, RED, SCOREBOARD_COLOR); }