void reqc_read(int sock, struct read_packet *p) { struct object_descriptor obj; void *buffer = malloc(p->size); int rsize; memset(buffer, 0, p->size); if (read_metadata(p->obj, &obj)) { rsize = read_data(&obj, p->offset, p->size, buffer); if (rsize >= 0) { send_reply(sock, buffer, rsize); free(buffer); return; } } for (unsigned int i = 0; i < peers.size(); i++) { pthread_mutex_lock(&peer_locks[i]); forward_packet(peer_srv_socks[i], OP_READ, OS_MASTER, p, sizeof(*p)); rsize = receive_reply(peer_srv_socks[i], buffer, p->size); pthread_mutex_unlock(&peer_locks[i]); if (rsize >= 0) { send_reply(sock, buffer, rsize); free(buffer); return; } } free(buffer); send_error(sock, ENOENT); }
int create_object_mst(oid_t dir, oid_t obj, const char *name, int type) { struct object_descriptor o, d; char entry[256]; unsigned int i, append = 1; if (read_metadata(obj, &o)) return EEXIST; if (!read_metadata_sch(dir, &d) || d.meta.type != ODT_DIR) return ENOTDIR; for (i = 0; i < d.meta.size; i += 256) { read_data_sch(&d, i, 256, entry); if (!entry[0]) { write_data_sch(&d, i, 256, name); append = 0; break; } } if (append) write_data_sch(&d, -1, 256, name); return create_object_slv(obj, name, type); }
/** * Parses the file. * */ static stan_csv parse(std::istream& in) { stan_csv data; if (!read_metadata(in, data.metadata)) { std::cout << "Warning: non-fatal error reading metadata" << std::endl; } if (!read_header(in, data.header)) { std::cout << "Error: error reading header" << std::endl; throw std::invalid_argument ("Error with header of input file in parse"); } if (!read_adaptation(in, data.adaptation)) { std::cout << "Warning: non-fatal error reading adapation data" << std::endl; } data.timing.warmup = 0; data.timing.sampling = 0; if (!read_samples(in, data.samples, data.timing)) { std::cout << "Warning: non-fatal error reading samples" << std::endl; } return data; }
void create_replica(int node, oid_t id) { struct create_packet pc; struct write_packet pw; struct object_descriptor obj; void *buffer; read_metadata(id, &obj); buffer = malloc(obj.meta.size); read_data(&obj, 0, obj.meta.size, buffer); pc.obj = id; pc.type = obj.meta.type; strncpy((char*)pc.name, (const char*)obj.meta.name, 256); pw.obj = id; pw.offset = 0; pw.size = obj.meta.size; lock_object(id); pthread_mutex_lock(&peer_locks[node]); forward_packet(peer_srv_socks[node], OP_CREATE, OS_SLAVE, &pc, sizeof(pc)); receive_reply(peer_srv_socks[node], NULL, 0); forward_data(peer_srv_socks[node], OP_WRITE, OS_SLAVE, &pw, sizeof(pw), buffer, pw.size); receive_reply(peer_srv_socks[node], NULL, 0); pthread_mutex_unlock(&peer_locks[node]); unlock_object(id); free(buffer); }
void reqc_write(int sock, struct write_packet *p, const void *buffer) { struct object_descriptor obj; int wsize; lock_object(p->obj); if (read_metadata(p->obj, &obj)) { reqm_write(sock, p, buffer, 1); return; } for (unsigned int i = 0; i < peers.size(); i++) { pthread_mutex_lock(&peer_locks[i]); forward_data(peer_srv_socks[i], OP_WRITE, OS_MASTER, p, sizeof(*p), buffer, p->size); wsize = receive_reply(peer_srv_socks[i], &wsize, sizeof(wsize)); pthread_mutex_unlock(&peer_locks[i]); if (wsize >= 0) { unlock_object(p->obj); send_reply(sock, &wsize, sizeof(wsize)); return; } } unlock_object(p->obj); send_error(sock, ENOENT); }
void input_mpg123::open(std::string path) { int err; int encoding; handle = mpg123_new(NULL, &err); if (handle == NULL) { throw input_error(mpg123_plain_strerror(err)); } if ((err = mpg123_open(handle, path.c_str())) != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } if ((err = mpg123_getformat(handle, &_rate, &_channels, &encoding)) != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } read_metadata(); /* force 16 bit format */ encoding = MPG123_ENC_SIGNED_16; mpg123_format_none(handle); mpg123_format(handle, _rate, _channels, encoding); double seconds_left; if ((err = mpg123_position(handle, 0, 0, NULL, NULL, NULL, &seconds_left)) != MPG123_OK) { throw input_error(mpg123_plain_strerror(err)); } this->_encoding = encoding; this->_length = seconds_left; }
void init_server() { struct object_descriptor d, *obj; struct stat f_stat; char *path = (char*)malloc(strlen(filesystem_path) + 256); obj = read_metadata(path_to_id("/"), NULL); if (obj) { free(path); return; } /* meta / */ strcpy(path, filesystem_path); strcat(path, "/"); stat(path, &f_stat); memset(&d, 0, sizeof(d)); d.id = path_to_id("/"); if (f_stat.st_mode & S_IFDIR) d.meta.type = ODT_DIR; else d.meta.type = ODT_FILE; d.meta.size = 0; strcpy((char*)d.meta.name, "/"); write_metadata(&d); /* data / */ const char data[] = ""; create_data(&d, 0, data); free(path); }
int remove_object_mst(oid_t dir, oid_t obj, int type) { struct object_descriptor o, d; char entry[256]; char eoe = 0; unsigned int i; if (!read_metadata(obj, &o)) return ENOENT; if (o.meta.type != type) return type == ODT_DIR ? ENOTDIR : EISDIR; if (type == ODT_DIR) { for (i = 0; i < o.meta.size; i += 256) { read_data(&o, i, 256, entry); if (entry[0]) { return ENOTEMPTY; } } } if (!read_metadata_sch(dir, &d) || d.meta.type != ODT_DIR) return ENOTDIR; for (i = 0; i < d.meta.size; i += 256) { read_data_sch(&d, i, 256, entry); if (!strcmp(entry, (const char*)o.meta.name)) { write_data_sch(&d, i, 1, &eoe); break; } } return remove_object_slv(obj); }
void reqm_getmeta(int sock, struct getmeta_packet *p) { struct object_descriptor obj; if (read_metadata(p->obj, &obj)) send_reply(sock, &obj.meta, sizeof(struct object_metadata)); else send_error(sock, ENOENT); }
UINT32 chd_file::guess_unitbytes() { // look for hard disk metadata; if found, then the unit size == sector size astring metadata; int i0, i1, i2, i3; if (read_metadata(HARD_DISK_METADATA_TAG, 0, metadata) == CHDERR_NONE && sscanf(metadata, HARD_DISK_METADATA_FORMAT, &i0, &i1, &i2, &i3) == 4) return i3; // look for CD-ROM metadata; if found, then the unit size == CD frame size if (read_metadata(CDROM_OLD_METADATA_TAG, 0, metadata) == CHDERR_NONE || read_metadata(CDROM_TRACK_METADATA_TAG, 0, metadata) == CHDERR_NONE || read_metadata(CDROM_TRACK_METADATA2_TAG, 0, metadata) == CHDERR_NONE || read_metadata(GDROM_TRACK_METADATA_TAG, 0, metadata) == CHDERR_NONE) return CD_FRAME_SIZE; // otherwise, just map 1:1 with the hunk size return m_hunkbytes; }
int print_path(S_metadata* md) { char *path[1000]; int path_count = 1; path[0] = (char*)malloc(NAME_SIZE); memcpy(path[0], md->name, NAME_SIZE); for(int i = 1; i < 1000; i++) { path[i] = NULL; } S_metadata* aux_md = read_metadata(md->part_id, md->address, READ_FULL_MD); printf("%s - aux_md->name=%s\n", __func__, md->name); printf("%s - aux_md->parent_address=%" PRIu64 ", root_metadata->address=%" PRIu64 "\n", __func__, aux_md->parent_address, fs_defs[aux_md->part_id].root_metadata->address); while(aux_md->parent_address != fs_defs[aux_md->part_id].root_metadata->address) { S_metadata* parent_md = read_metadata(aux_md->part_id, aux_md->parent_address, READ_FULL_MD); // printf("%s - parent_md->name=%s\n", __func__, parent_md->name); path[path_count]= (char*)malloc(NAME_SIZE); memcpy(path[path_count], parent_md->name, NAME_SIZE); path_count += 1; free(aux_md); aux_md = parent_md; if (path_count >= 1000) { printf("%s - error, path_count=%d, either this is very deeply nested or this is an error\n", __func__, path_count); } } free(aux_md); char* print_string = (char*)malloc(path_count*NAME_SIZE); char* pos = print_string; for(int i = path_count-1; i >= 0; i--) { pos += sprintf(pos, "/%s", path[i]); } printf("%s - full path:%s\n", __func__, print_string); free(print_string); return pos - print_string; }
int remove_object_slv(oid_t obj) { struct object_descriptor o; if (!read_metadata(obj, &o)) return ENOENT; delete_data(&o); remove_metadata(&o); return 0; }
static void *open_chd(const char *filename, movie_info &info) { auto chd = new chd_file; // open the file chd_error chderr = chd->open(filename); if (chderr != CHDERR_NONE) { fprintf(stderr, "Error opening CHD file: %s\n", chd_file::error_string(chderr)); delete chd; return nullptr; } // get the metadata std::string metadata; chderr = chd->read_metadata(AV_METADATA_TAG, 0, metadata); if (chderr != CHDERR_NONE) { fprintf(stderr, "Error getting A/V metadata: %s\n", chd_file::error_string(chderr)); delete chd; return nullptr; } // extract the info int fps, fpsfrac, width, height, interlaced, channels, rate; if (sscanf(metadata.c_str(), AV_METADATA_FORMAT, &fps, &fpsfrac, &width, &height, &interlaced, &channels, &rate) != 7) { fprintf(stderr, "Improperly formatted metadata\n"); delete chd; return nullptr; } // extract movie info info.framerate = (fps * 1000000 + fpsfrac) / 1000000.0; info.numframes = chd->hunk_count(); info.width = width; info.height = height; info.samplerate = rate; info.channels = channels; // convert to an interlaced frame chdinterlaced = interlaced; if (interlaced) { info.framerate /= 2; info.numframes = (info.numframes + 1) / 2; info.height *= 2; } return chd; }
/** * read_test_fuse * * @param mask[out] - MDTP efuse value represented by a bitfield. * * @return - negative value for an error, 0 for success. */ static int read_test_fuse(uint8_t *mask) { int status = 0; metadata_t metadata; status = read_metadata(&metadata); if (status) { dprintf(CRITICAL, "mdtp: read_test_fuse: Failure getting metadata\n"); return -1; } *mask = metadata.eFuses.mask; return 0; }
void reqs_write(int node, struct write_packet *p, const void *buffer) { struct object_descriptor obj; if (!read_metadata(p->obj, &obj)) return; write_data(&obj, p->offset, p->size, buffer); if (p->offset != -1) obj.meta.size = obj.meta.size > p->offset + p->size ? obj.meta.size : p->offset + p->size; else obj.meta.size += p->size; write_metadata(&obj); send_reply(peer_cli_socks[node], NULL, 0); }
int create_object_slv(oid_t obj, const char *name, int type) { struct object_descriptor o; if (read_metadata(obj, &o)) return EEXIST; memset(&o, 0, sizeof(o)); o.id = obj; o.meta.type = type; strcpy((char*)o.meta.name, name); write_metadata(&o); create_data(&o, 0, NULL); return 0; }
void reqm_read(int sock, struct read_packet *p) { struct object_descriptor obj; void *buffer; int rsize; if (!read_metadata(p->obj, &obj)) send_error(sock, ENOENT); buffer = malloc(p->size); memset(buffer, 0, p->size); rsize = read_data(&obj, p->offset, p->size, buffer); if (rsize < 0) send_error(sock, ENOENT); else send_reply(sock, buffer, rsize); free(buffer); }
void reqc_remove(int sock, struct remove_packet *p) { struct object_descriptor obj; int err; lock_object(p->dir_obj); lock_object(p->obj); if (read_metadata(p->obj, &obj)) { reqm_remove(sock, p, 1); return; } for (unsigned int i = 0; i < peers.size(); i++) { pthread_mutex_lock(&peer_locks[i]); forward_packet(peer_srv_socks[i], OP_REMOVE, OS_MASTER, p, sizeof(*p)); err = receive_reply(peer_srv_socks[i], NULL, 0); pthread_mutex_unlock(&peer_locks[i]); if (err >= 0) { unlock_object(p->dir_obj); unlock_object(p->obj); send_reply(sock, NULL, 0); return; } else if (err != -ENOENT) { unlock_object(p->dir_obj); unlock_object(p->obj); send_error(sock, -err); return; } } unlock_object(p->dir_obj); unlock_object(p->obj); send_error(sock, ENOENT); }
void reqm_write(int sock, struct write_packet *p, const void *buffer, int unlock) { struct object_descriptor obj; int wsize, node; if (!read_metadata(p->obj, &obj)) { if (unlock) unlock_object(p->obj); send_error(sock, ENOENT); return; } wsize = write_data(&obj, p->offset, p->size, buffer); if (p->offset != -1) obj.meta.size = obj.meta.size > p->offset + p->size ? obj.meta.size : p->offset + p->size; else obj.meta.size += p->size; write_metadata(&obj); pthread_mutex_lock(&replicas_lock); for (unsigned int i = 0; i < replicas.size(); i++) { if (!memcmp(&replicas[i].id, &p->obj, sizeof(oid_t))) { node = replicas[i].node; pthread_mutex_lock(&peer_locks[node]); forward_data(peer_srv_socks[node], OP_WRITE, OS_SLAVE, p, sizeof(*p), buffer, p->size); receive_reply(peer_srv_socks[node], NULL, 0); pthread_mutex_unlock(&peer_locks[node]); } } pthread_mutex_unlock(&replicas_lock); if (unlock) unlock_object(p->obj); send_reply(sock, &wsize, sizeof(wsize)); }
G_MODULE_EXPORT gboolean tracker_extract_get_metadata (TrackerExtractInfo *info) { goffset size; FILE *f; png_structp png_ptr; png_infop info_ptr; png_infop end_ptr; png_bytepp row_pointers; guint row; png_uint_32 width, height; gint bit_depth, color_type; gint interlace_type, compression_type, filter_type; const gchar *dlna_profile, *dlna_mimetype, *graph; TrackerSparqlBuilder *preupdate, *metadata; gchar *filename, *uri; GString *where; GFile *file; file = tracker_extract_info_get_file (info); filename = g_file_get_path (file); size = tracker_file_get_size (filename); preupdate = tracker_extract_info_get_preupdate_builder (info); metadata = tracker_extract_info_get_metadata_builder (info); graph = tracker_extract_info_get_graph (info); if (size < 64) { return FALSE; } f = tracker_file_open (filename); g_free (filename); if (!f) { return FALSE; } png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { tracker_file_close (f, FALSE); return FALSE; } info_ptr = png_create_info_struct (png_ptr); if (!info_ptr) { png_destroy_read_struct (&png_ptr, &info_ptr, NULL); tracker_file_close (f, FALSE); return FALSE; } end_ptr = png_create_info_struct (png_ptr); if (!end_ptr) { png_destroy_read_struct (&png_ptr, &info_ptr, NULL); tracker_file_close (f, FALSE); return FALSE; } if (setjmp (png_jmpbuf (png_ptr))) { png_destroy_read_struct (&png_ptr, &info_ptr, &end_ptr); tracker_file_close (f, FALSE); return FALSE; } png_init_io (png_ptr, f); png_read_info (png_ptr, info_ptr); if (!png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_type)) { png_destroy_read_struct (&png_ptr, &info_ptr, &end_ptr); tracker_file_close (f, FALSE); return FALSE; } /* Read the image. FIXME We should be able to skip this step and * just get the info from the end. This causes some errors atm. */ row_pointers = g_new0 (png_bytep, height); for (row = 0; row < height; row++) { row_pointers[row] = png_malloc (png_ptr, png_get_rowbytes (png_ptr,info_ptr)); } png_read_image (png_ptr, row_pointers); for (row = 0; row < height; row++) { png_free (png_ptr, row_pointers[row]); } g_free (row_pointers); png_read_end (png_ptr, end_ptr); tracker_sparql_builder_predicate (metadata, "a"); tracker_sparql_builder_object (metadata, "nfo:Image"); tracker_sparql_builder_object (metadata, "nmm:Photo"); uri = g_file_get_uri (file); where = g_string_new (""); read_metadata (preupdate, metadata, where, png_ptr, info_ptr, end_ptr, uri, graph); tracker_extract_info_set_where_clause (info, where->str); g_string_free (where, TRUE); g_free (uri); tracker_sparql_builder_predicate (metadata, "nfo:width"); tracker_sparql_builder_object_int64 (metadata, width); tracker_sparql_builder_predicate (metadata, "nfo:height"); tracker_sparql_builder_object_int64 (metadata, height); if (guess_dlna_profile (bit_depth, width, height, &dlna_profile, &dlna_mimetype)) { tracker_sparql_builder_predicate (metadata, "nmm:dlnaProfile"); tracker_sparql_builder_object_string (metadata, dlna_profile); tracker_sparql_builder_predicate (metadata, "nmm:dlnaMime"); tracker_sparql_builder_object_string (metadata, dlna_mimetype); } png_destroy_read_struct (&png_ptr, &info_ptr, &end_ptr); tracker_file_close (f, FALSE); return TRUE; }
int main(int argc, char *argv[]) { /* moved to global double kT; // temperature */ int i,j; int len; int first; int bin_min; int have_energy; char *freefile; FILE *METAFILE, *FREEFILE; struct hist_group *hist_group; struct histogram *hp; double coor; //double num, denom; //double bias, bf; double error; double *free_ene; double *prob,*final_prob; double *ave_p; double *ave_p2; double *ave_pdf; double *ave_pdf2; double *ave_F; double *ave_F2; double sum; double *final_f; int iteration; int max_iteration = 100000; int numpad; int num_mc_runs; int num_used; char *c; long idum; double pdf; if (argc < 2) { printf( COMMAND_LINE ); exit(-1); } // Print the command line out into the output file printf("#"); for (i=0; i<argc; i++) { printf(" %s", argv[i]); } printf("\n"); if (toupper(argv[1][0]) == 'P') { PERIODIC = 1; len = strlen(argv[1]); if (len == 1) { PERIOD = DEGREES; // 360 } else { c= &(argv[1][1]); for (i=0; i<len-1;i++) { c[i] = toupper(c[i]); } if (strncmp(c,"PI",2) == 0) { PERIOD = RADIANS; // 2 pi } else { PERIOD = atof(c); } } printf("#Turning on periodicity with period = %f\n", PERIOD); // now shift down the other command line arguments argc--; argv++; } else { PERIODIC = 0; } // Parse command line arguments if (argc != 9 && argc !=11) { printf( COMMAND_LINE ); exit(-1); } HIST_MIN = atof(argv[1]); HIST_MAX = atof(argv[2]); NUM_BINS = atoi(argv[3]); BIN_WIDTH = (HIST_MAX - HIST_MIN) / (double) NUM_BINS; TOL = atof(argv[4]); kT = atof(argv[5]) * k_B; numpad = atoi(argv[6]); METAFILE = fopen(argv[7], "r"); if (METAFILE == (FILE *)NULL) { printf("couldn't open metadatafile %s: %s\n", argv[7], strerror(errno)); exit(errno); } i = strlen(argv[8]); freefile = (char *) malloc(i * sizeof(char)); freefile = argv[8]; if (!freefile) { printf("couldn't allocate space for freefile name: %s\n", strerror(errno)); exit(errno); } if (argc == 11) { num_mc_runs = atoi(argv[9]); idum = atol(argv[10]); if (idum > 0) { idum = -idum; } // initialize the random number generator ran2(&idum); } else { num_mc_runs = 0; } HISTOGRAM = (double *) malloc(sizeof(double) * NUM_BINS); if (!HISTOGRAM) { printf("couldn't allocate space for HISTOGRAM: %s\n", strerror(errno)); exit(errno); } prob = (double *) malloc(sizeof(double) * NUM_BINS); if (!prob) { printf("couldn't allocate space for prob: %s\n", strerror(errno)); exit(errno); } final_prob = (double *) malloc(sizeof(double) * NUM_BINS); if (!final_prob) { printf("couldn't allocate space for final_prob: %s\n", strerror(errno)); exit(errno); } ave_p = (double *) malloc(sizeof(double) * NUM_BINS); if (!ave_p) { printf("couldn't allocate space for ave_p: %s\n", strerror(errno)); exit(errno); } ave_p2 = (double *) malloc(sizeof(double) * NUM_BINS); if (!ave_p2) { printf("couldn't allocate space for ave_p2: %s\n", strerror(errno)); exit(errno); } ave_pdf = (double *) malloc(sizeof(double) * NUM_BINS); if (!ave_pdf) { printf("couldn't allocate space for ave_pdf: %s\n", strerror(errno)); exit(errno); } ave_pdf2 = (double *) malloc(sizeof(double) * NUM_BINS); if (!ave_pdf2) { printf("couldn't allocate space for ave_pdf2: %s\n", strerror(errno)); exit(errno); } free_ene = (double *) malloc(sizeof(double) * NUM_BINS); if (!free_ene) { printf("couldn't allocate space for free_ene: %s\n", strerror(errno)); exit(errno); } i = get_numwindows(METAFILE); printf("#Number of windows = %d\n", i); ave_F = (double *) malloc(sizeof(double) * i); if (!ave_pdf) { printf("couldn't allocate space for ave_F: %s\n", strerror(errno)); exit(errno); } ave_F2 = (double *) malloc(sizeof(double) * i); if (!ave_F2) { printf("couldn't allocate space for ave_F2: %s\n", strerror(errno)); exit(errno); } hist_group = make_hist_group(i); //printf("From hist_group: %d\n", hist_group->num_windows); i = read_metadata(METAFILE, hist_group); assert(i == hist_group->num_windows); // Figure out if we have trajectories at different temperatures. // Missing temperatures are set to -1 in read_metadata, and // since we require that either all trajectories specify a temperature // or all trajectories are assumed to be at the wham temperature, we only // have to check one of them if (hist_group->kT[0] > 0) { have_energy = 1; } else { have_energy = 0; for (i=0; i< hist_group->num_windows; i++) { hist_group->kT[i] = kT; } } // allocate memory to store the final F values (for when we do MC bootstrap) final_f = (double *) malloc(sizeof(double) * hist_group->num_windows); if (!final_f) { printf("couldn't allocate space for final_f: %s\n", strerror(errno)); exit(errno); } free(HISTOGRAM); // for each window, zero out the estimated perturbation due to the restraints for (i=0; i< hist_group->num_windows; i++) { hist_group->F[i]=0.0; // nonzero ensures not converged first time hist_group->F_old[i]=0.0; } // Do the actual WHAM stuff, iterate to self consistency iteration = 0; first = 1; while (! is_converged(hist_group) || first ) { first = 0; save_free(hist_group); wham_iteration(hist_group, prob,have_energy); // Dump out some info iteration++; if (iteration % 10 == 0) { error = average_diff(hist_group); printf("#Iteration %d: %f\n", iteration, error); } // Dump out the histogram and free energy if (iteration % 100 == 0) { calc_free(free_ene,prob,kT); for (i=0; i< NUM_BINS; i++) { coor = calc_coor(i); printf("%f\t%f\t%f\n", coor, free_ene[i], prob[i]); } printf("\n"); // Write the bias values to stdout printf("# Dumping simulation biases, in the metadata file order \n"); printf("# Window F (free energy units)\n"); for (j=0; j<hist_group->num_windows;j++) { printf("# %d\t%f\n", j, hist_group->F[j]); final_f[j] = hist_group->F[j]; } } // Cheesy bailout if we're going on too long if (iteration >= max_iteration) { printf("Too many iterations: %d\n", iteration); break; } } // We're done, write out the free energy, histogram, and bias values // Write the bias values to stdout printf("# Dumping simulation biases, in the metadata file order \n"); printf("# Window F (free energy units)\n"); for (j=0; j<hist_group->num_windows;j++) { printf("# %d\t%f\n", j, hist_group->F[j]); final_f[j] = hist_group->F[j]; } // Normalize the probability, store it sum = 0.0; for (i=0; i < NUM_BINS; i++) { sum += prob[i]; } for (i=0; i < NUM_BINS; i++) { prob[i] /= sum;; final_prob[i] = prob[i]; } // Compute the free energy from the normalized probability bin_min = calc_free(free_ene, prob,kT); // Do the requested number of bootstrap monte carlo error analysis runs. if (num_mc_runs <= 0) { printf("# No MC error analysis requested\n"); } // initialize averaging arrays for (i=0; i< NUM_BINS; i++) { ave_p[i] = 0.0; ave_p2[i] = 0.0; ave_pdf[i] = 0.0; ave_pdf2[i] = 0.0; } for (i=0; i< hist_group->num_windows; i++) { ave_F[i] = 0.0; ave_F2[i] = 0.0; } for (i=0; i< num_mc_runs; i++) { // pick a set of fake data sets for (j=0; j<hist_group->num_windows;j++) { hp = &hist_group->hists[j]; //printf("Faking %d: %d %d\n", i,j,hp->num_points); num_used = hp->last - hp->first + 1; mk_new_hist(hp->cum, hp->data, num_used, hp->num_mc_samples, &idum); hist_group->F_old[j] = 0.0; hist_group->F[j] = 0.0; } // perform WHAM iterations on the fake data sets iteration = 0; first = 1; while (! is_converged(hist_group) || first ) { first = 0; save_free(hist_group); wham_iteration(hist_group, prob,have_energy); iteration++; // Cheesy bailout if we're going on too long if (iteration >= max_iteration) { printf("Too many iterations: %d\n", iteration); break; } } printf("#MC trial %d: %d iterations\n", i, iteration); printf("#PMF values\n"); // accumulate the average and stdev of the resulting probabilities sum = 0.0; for (j=0; j < NUM_BINS; j++) { sum += prob[j]; } for (j=0; j < NUM_BINS; j++) { prob[j] /= sum; } for (j=0; j < NUM_BINS; j++) { pdf = -kT*log(prob[j]); ave_p[j] += prob[j]; ave_pdf[j] += pdf; ave_p2[j] += prob[j] * prob[j]; ave_pdf2[j] += pdf*pdf; } for (j=0; j<hist_group->num_windows;j++) { ave_F[j] += hist_group->F[j] - hist_group->F[0]; ave_F2[j] += hist_group->F[j]*hist_group->F[j] ; } } for (i=0; i < NUM_BINS; i++) { ave_p[i] /= (double)num_mc_runs; ave_p2[i] /= (double)num_mc_runs; ave_p2[i] = sqrt(ave_p2[i] - ave_p[i]*ave_p[i]); ave_pdf[i] /= (double)num_mc_runs; ave_pdf2[i] /= (double)num_mc_runs; ave_pdf2[i] = sqrt(ave_pdf2[i] - ave_pdf[i]*ave_pdf[i]); } for (i=0; i < hist_group->num_windows; i++) { ave_F[i] /= (double)num_mc_runs; ave_F2[i] /= (double)num_mc_runs; ave_F2[i] = sqrt(ave_F2[i] - ave_F[i]*ave_F[i]); } FREEFILE = fopen(freefile, "w"); if (!FREEFILE) { printf("couldn't open %s: %s\n", freefile, strerror(errno)); printf("dumping free energy,probability, and window free energies to stdout\n"); for (i=0; i< NUM_BINS; i++) { coor = calc_coor(i); printf("%f\t%f\t%f\t%f\t%f\n", coor, free_ene[i], ave_pdf2[i], prob[i], ave_p2[i]); } for (i=0; i<hist_group->num_windows; i++) { fprintf(FREEFILE,"%d\t%f\t%f\n", i, final_f[i],ave_F2[i]); } exit(errno); } else { // write out header fprintf(FREEFILE, "#Coor\t\tFree\t+/-\t\tProb\t\t+/-\n"); // write out the leading padded values for (i=-numpad; i<0; i++) { coor = calc_coor(i); fprintf(FREEFILE,"%f\t%f\t%f\t%f\t%f\n", coor, free_ene[NUM_BINS+i], ave_pdf2[NUM_BINS+i], final_prob[NUM_BINS+i], ave_p2[NUM_BINS+i]); } // write out the center values for (i=0; i<NUM_BINS; i++) { coor = calc_coor(i); fprintf(FREEFILE,"%f\t%f\t%f\t%f\t%f\n", coor, free_ene[i], ave_pdf2[i],final_prob[i], ave_p2[i]); } // write out the trailing padded values for (i=0; i<numpad; i++) { coor = calc_coor(NUM_BINS+i); fprintf(FREEFILE,"%f\t%f\t%f\t%f\t%f\n", coor, free_ene[i], ave_pdf2[i],final_prob[i], ave_p2[i]); } fprintf(FREEFILE, "#Window\t\tFree\t+/-\t\n"); for (i=0; i<hist_group->num_windows; i++) { fprintf(FREEFILE,"#%d\t%f\t%f\n", i, final_f[i],ave_F2[i]); } } exit(0); }
G_MODULE_EXPORT gboolean tracker_extract_get_metadata (TrackerExtractInfo *info) { TrackerSparqlBuilder *preupdate, *metadata; goffset size; GifFileType *gifFile = NULL; GString *where; const gchar *graph; gchar *filename, *uri; GFile *file; int fd; #if GIFLIB_MAJOR >= 5 int err; #endif preupdate = tracker_extract_info_get_preupdate_builder (info); metadata = tracker_extract_info_get_metadata_builder (info); graph = tracker_extract_info_get_graph (info); file = tracker_extract_info_get_file (info); filename = g_file_get_path (file); size = tracker_file_get_size (filename); if (size < 64) { g_free (filename); return FALSE; } fd = tracker_file_open_fd (filename); if (fd == -1) { g_warning ("Could not open GIF file '%s': %s\n", filename, g_strerror (errno)); g_free (filename); return FALSE; } #if GIFLIB_MAJOR < 5 if ((gifFile = DGifOpenFileHandle (fd)) == NULL) { PrintGifError (); #else /* GIFLIB_MAJOR < 5 */ if ((gifFile = DGifOpenFileHandle (fd, &err)) == NULL) { gif_error ("Could not open GIF file with handle", err); #endif /* GIFLIB_MAJOR < 5 */ close (fd); return FALSE; } g_free (filename); tracker_sparql_builder_predicate (metadata, "a"); tracker_sparql_builder_object (metadata, "nfo:Image"); tracker_sparql_builder_object (metadata, "nmm:Photo"); where = g_string_new (""); uri = g_file_get_uri (file); read_metadata (preupdate, metadata, where, gifFile, uri, graph); tracker_extract_info_set_where_clause (info, where->str); g_string_free (where, TRUE); g_free (uri); if (DGifCloseFile (gifFile) != GIF_OK) { #if GIFLIB_MAJOR < 5 PrintGifError (); #else /* GIFLIB_MAJOR < 5 */ gif_error ("Could not close GIF file", gifFile->Error); #endif /* GIFLIB_MAJOR < 5 */ } return TRUE; }
static bool_t flac_play (const char * filename, VFSFile * file) { if (!file) return FALSE; void * play_buffer = NULL; bool_t error = FALSE; info->fd = file; if (read_metadata(decoder, info) == FALSE) { FLACNG_ERROR("Could not prepare file for playing!\n"); error = TRUE; goto ERR_NO_CLOSE; } play_buffer = g_malloc (BUFFER_SIZE_BYTE); if (! aud_input_open_audio (SAMPLE_FMT (info->bits_per_sample), info->sample_rate, info->channels)) { error = TRUE; goto ERR_NO_CLOSE; } aud_input_set_bitrate(info->bitrate); while (FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_END_OF_STREAM) { if (aud_input_check_stop ()) break; int seek_value = aud_input_check_seek (); if (seek_value >= 0) FLAC__stream_decoder_seek_absolute (decoder, (int64_t) seek_value * info->sample_rate / 1000); /* Try to decode a single frame of audio */ if (FLAC__stream_decoder_process_single(decoder) == FALSE) { FLACNG_ERROR("Error while decoding!\n"); error = TRUE; break; } squeeze_audio(info->output_buffer, play_buffer, info->buffer_used, info->bits_per_sample); aud_input_write_audio(play_buffer, info->buffer_used * SAMPLE_SIZE(info->bits_per_sample)); reset_info(info); } ERR_NO_CLOSE: g_free (play_buffer); reset_info(info); if (FLAC__stream_decoder_flush(decoder) == FALSE) FLACNG_ERROR("Could not flush decoder state!\n"); return ! error; }
static void io_read (SoupSocket *sock, SoupMessage *msg) { SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg); SoupMessageIOData *io = priv->io_data; guint status; read_more: switch (io->read_state) { case SOUP_MESSAGE_IO_STATE_NOT_STARTED: return; case SOUP_MESSAGE_IO_STATE_HEADERS: if (!read_metadata (msg, TRUE)) return; /* We need to "rewind" io->read_meta_buf back one line. * That SHOULD be two characters (CR LF), but if the * web server was stupid, it might only be one. */ if (io->read_meta_buf->len < 3 || io->read_meta_buf->data[io->read_meta_buf->len - 2] == '\n') io->read_meta_buf->len--; else io->read_meta_buf->len -= 2; io->read_meta_buf->data[io->read_meta_buf->len] = '\0'; status = io->parse_headers_cb (msg, (char *)io->read_meta_buf->data, io->read_meta_buf->len, &io->read_encoding, io->user_data); g_byte_array_set_size (io->read_meta_buf, 0); if (status != SOUP_STATUS_OK) { /* Either we couldn't parse the headers, or they * indicated something that would mean we wouldn't * be able to parse the body. (Eg, unknown * Transfer-Encoding.). Skip the rest of the * reading, and make sure the connection gets * closed when we're done. */ soup_message_set_status (msg, status); soup_message_headers_append (msg->request_headers, "Connection", "close"); io->read_state = SOUP_MESSAGE_IO_STATE_FINISHING; break; } if (io->read_encoding == SOUP_ENCODING_CONTENT_LENGTH) { SoupMessageHeaders *hdrs = (io->mode == SOUP_MESSAGE_IO_CLIENT) ? msg->response_headers : msg->request_headers; io->read_length = soup_message_headers_get_content_length (hdrs); } if (io->mode == SOUP_MESSAGE_IO_CLIENT && SOUP_STATUS_IS_INFORMATIONAL (msg->status_code)) { if (msg->status_code == SOUP_STATUS_CONTINUE && io->write_state == SOUP_MESSAGE_IO_STATE_BLOCKING) { /* Pause the reader, unpause the writer */ io->read_state = SOUP_MESSAGE_IO_STATE_BLOCKING; io->write_state = io_body_state (io->write_encoding); } else { /* Just stay in HEADERS */ io->read_state = SOUP_MESSAGE_IO_STATE_HEADERS; } } else if (io->mode == SOUP_MESSAGE_IO_SERVER && soup_message_headers_get_expectations (msg->request_headers) & SOUP_EXPECTATION_CONTINUE) { /* The client requested a Continue response. The * got_headers handler may change this to something * else though. */ soup_message_set_status (msg, SOUP_STATUS_CONTINUE); io->write_state = SOUP_MESSAGE_IO_STATE_HEADERS; io->read_state = SOUP_MESSAGE_IO_STATE_BLOCKING; } else { io->read_state = io_body_state (io->read_encoding); /* If the client was waiting for a Continue * but got something else, then it's done * writing. */ if (io->mode == SOUP_MESSAGE_IO_CLIENT && io->write_state == SOUP_MESSAGE_IO_STATE_BLOCKING) io->write_state = SOUP_MESSAGE_IO_STATE_FINISHING; } if (io->mode == SOUP_MESSAGE_IO_CLIENT && SOUP_STATUS_IS_INFORMATIONAL (msg->status_code)) { SOUP_MESSAGE_IO_PREPARE_FOR_CALLBACK; soup_message_got_informational (msg); soup_message_cleanup_response (msg); SOUP_MESSAGE_IO_RETURN_IF_CANCELLED_OR_PAUSED; } else { SOUP_MESSAGE_IO_PREPARE_FOR_CALLBACK; soup_message_got_headers (msg); SOUP_MESSAGE_IO_RETURN_IF_CANCELLED_OR_PAUSED; } break; case SOUP_MESSAGE_IO_STATE_BLOCKING: io_write (sock, msg); /* As in the io_write case, we *must* return here. */ return; case SOUP_MESSAGE_IO_STATE_BODY: if (!read_body_chunk (msg)) return; got_body: if (!io_handle_sniffing (msg, TRUE)) { /* If the message was paused (as opposed to * cancelled), we need to make sure we wind up * back here when it's unpaused, even if it * was doing a chunked or EOF-terminated read * before. */ if (io == priv->io_data) { io->read_state = SOUP_MESSAGE_IO_STATE_BODY; io->read_encoding = SOUP_ENCODING_CONTENT_LENGTH; io->read_length = 0; } return; } io->read_state = SOUP_MESSAGE_IO_STATE_FINISHING; SOUP_MESSAGE_IO_PREPARE_FOR_CALLBACK; soup_message_got_body (msg); SOUP_MESSAGE_IO_RETURN_IF_CANCELLED_OR_PAUSED; break; case SOUP_MESSAGE_IO_STATE_CHUNK_SIZE: if (!read_metadata (msg, FALSE)) return; io->read_length = strtoul ((char *)io->read_meta_buf->data, NULL, 16); g_byte_array_set_size (io->read_meta_buf, 0); if (io->read_length > 0) io->read_state = SOUP_MESSAGE_IO_STATE_CHUNK; else io->read_state = SOUP_MESSAGE_IO_STATE_TRAILERS; break; case SOUP_MESSAGE_IO_STATE_CHUNK: if (!read_body_chunk (msg)) return; io->read_state = SOUP_MESSAGE_IO_STATE_CHUNK_END; break; case SOUP_MESSAGE_IO_STATE_CHUNK_END: if (!read_metadata (msg, FALSE)) return; g_byte_array_set_size (io->read_meta_buf, 0); io->read_state = SOUP_MESSAGE_IO_STATE_CHUNK_SIZE; break; case SOUP_MESSAGE_IO_STATE_TRAILERS: if (!read_metadata (msg, FALSE)) return; if (io->read_meta_buf->len <= SOUP_MESSAGE_IO_EOL_LEN) goto got_body; /* FIXME: process trailers */ g_byte_array_set_size (io->read_meta_buf, 0); break; case SOUP_MESSAGE_IO_STATE_FINISHING: if (io->read_tag) { g_signal_handler_disconnect (io->sock, io->read_tag); io->read_tag = 0; } io->read_state = SOUP_MESSAGE_IO_STATE_DONE; if (io->mode == SOUP_MESSAGE_IO_SERVER) { io->write_state = SOUP_MESSAGE_IO_STATE_HEADERS; io_write (sock, msg); } else soup_message_io_finished (msg); return; case SOUP_MESSAGE_IO_STATE_DONE: default: g_return_if_reached (); } goto read_more; }
bool Lng::read_files( char *fname1, char *fname2, bool global, pwr_tStatus *sts) { pwr_tFileName filename1, filename2; sprintf( filename2, fname2, get_language_str()); dcli_translate_filename( filename1, fname1); dcli_translate_filename( filename2, filename2); ifstream fp1( filename1); if ( !fp1 && strcmp( fname1, "$pwr_exe/en_us/xtt_lng.dat") == 0) { // Try $pwr_eexe strcpy( fname1, "$pwr_eexe/en_us/xtt_lng.dat"); dcli_translate_filename( filename1, fname1); fp1.open( filename1); if ( !fp1) { *sts = LNG__FILE; return false; } } else if ( !fp1) { *sts = LNG__FILE; return global ? false : true; } ifstream fp2( filename2); if ( !fp2 && strcmp( fname2, "$pwr_exe/%s/xtt_lng.dat") == 0) { // Try $pwr_eexe strcpy( fname2, "$pwr_eexe/%s/xtt_lng.dat"); sprintf( filename2, fname2, get_language_str()); dcli_translate_filename( filename2, filename2); fp2.open( filename2); if ( !fp2) { *sts = LNG__FILE; return false; } } else if ( !fp2) { *sts = LNG__FILE; return global ? false : true; } Row r1( fp1, filename1); Row r2( fp2, filename2); read_metadata( fp1, global, sts); read_metadata( fp2, global, sts); read_include( fp1, fp2, global, sts); bool hit = true; for (;;) { if ( hit) { if ( !read_line( r1)) break; if ( !read_line( r2)) break; } else if ( r1.lt( r2)) { if ( !read_line( r1)) break; } else { if ( !read_line( r2)) break; } hit = false; if ( r1.eq( r2)) hit = true; if ( hit) { lang_sKey key; lang_sRecord *record; strncpy( key.text, r1.text, sizeof(key.text)); key.type = r1.type; record = (lang_sRecord *) tree_Insert( sts, tree, &key); strcpy( record->transl, r2.text); // printf ( "%c %d.%d.%d '%s' '%s'\n", r1.type, r1.n1, r1.n2, r1.n3, r1.text,r2.text); } } *sts = LNG__SUCCESS; return true; }
/* decode hidim png file and write the torrent file */ int decode(const char *pngfilename) { FILE *fp; png_uint_32 height, width; png_bytep *row_pointers = NULL, *transpose = NULL; char *filename, *basefilename, *sha1sum; int start_row, start_column, line_length, c; unsigned int length, metadata_length, count, s; if ((fp = fopen(pngfilename, "rb")) == NULL) { fprintf(stderr, "%s: %s\n", pngfilename, strerror(errno)); return 0; } if (config & CONFIG_VERBOSE) { printf(":: decoding %s\n", pngfilename); } if (!read_png(fp, &row_pointers, &width, &height)) { fclose(fp); fprintf(stderr, "Error: %s is not a png file.\n", pngfilename); return 0; } fclose(fp); /* rotate image */ transpose = rotate_image(row_pointers, width, height); free_array(row_pointers, height); if (transpose == NULL) { // it can't be. already checked before exit(EXIT_FAILURE); } /* search for hidim key */ if (!search_key(transpose, width, height, &start_row, &start_column)) { fprintf(stderr, "Error: %s is not a hidim or is corrupted.\n", pngfilename); free_array(transpose, width); return 0; } else if (config & CONFIG_MORE_VERBOSE) { printf(" hidim key found at (%d, %d)\n", start_row, (int)(height-start_column-1)); } /* extract metadata */ read_metadata(transpose, start_row, start_column, &line_length, &filename, &sha1sum, &metadata_length, &length); /* for security reason, only keep the basename of filename */ basefilename = basename(filename); if (config & CONFIG_MORE_VERBOSE) { printf("==== metadata (%d bytes) ====\n", metadata_length); printf(" line_length: %d sha1sum: %s length: %d\n", line_length, sha1sum, length); printf(" filename : %s\n", filename); printf("====\n"); //printf(":: %d %s %s %d %d\n", line_length, basefilename, sha1sum, metadata_length, length); } /* get the torrent */ unsigned char *torrent = calloc(length, sizeof(char)); if (torrent == NULL) { fprintf(stderr, "Error: Can't allocate %d bytes.\n", length*sizeof(char)); exit(EXIT_FAILURE); } count = c = s = 0; while (count < length+metadata_length) { if (count >= metadata_length) { torrent[count - metadata_length] = (unsigned char)transpose[start_row+s][3*start_column+c]; } if (c == (line_length*3 -1) ) { c = 0; s++; } else { c++; } count++; } free_array(transpose, width); /* check the sha1sum of the torrent we extracted with sha1sum from metadata */ unsigned char md[20]; SHA1(torrent, length, md); char *sha1sum_comp = hexa_sha1sum(md); if (strcmp(sha1sum_comp, sha1sum) != 0) { if (config & CONFIG_MORE_VERBOSE) { printf("sha2sum: expected %s, got %s\n", sha1sum, sha1sum_comp); } fprintf(stderr, "%s: wrong sha1sum for extracted data.\n", filename); free(sha1sum_comp); free(sha1sum); free(torrent); free(filename); return 0; } free(sha1sum); free(sha1sum_comp); /* check if torrent file does not already exist */ if (is_file(basefilename) && (!(config & CONFIG_OVERWRITE))) { fprintf(stderr, "%s already exists. nothing done.\n", basefilename); free(torrent); free(filename); return 0; } /* write torrent to file */ FILE *fo = fopen(basefilename, "w"); fwrite(torrent, sizeof(char), length, fo); fclose(fo); if (config & CONFIG_VERBOSE) { printf("%s has been saved.\n", basefilename); } free(torrent); free(filename); return 1; }
/** * Determine if the file at the specified path needs to be signed. * Signing only occurs if the file is new (i.e. not yet signed), * OR if the file has been appended since the last signing as * determined by reading the hcl ("total") from the metadata stored * at the end of the associated signature file and comparing with the * current size of the specified file. * If a new signature is necessary, the sign function above will be * called with the specified pin and label. */ void sign_file(const char* path, const char* pin, const char* label) { int n, err; struct stat entry_info; char sig_path[PATH_MAX] = ""; FILE* fp; /* Stat the entry */ err = stat(path, &entry_info); if (err) { int e = errno; log_err("error accessing file '%s': %s", path, strerror(e)); return; } /* Only sign files */ if (S_ISDIR(entry_info.st_mode)) return; /* Skip empty files */ if (entry_info.st_size <= 0) { log_inf("'%s' empty", path); return; } /* Build associated sig file path (i.e. <path>/<filename><sig_ext>) */ n = snprintf(sig_path, sizeof(sig_path), "%s%s", path, sig_ext); if (n < 0 || n >= sizeof(sig_path)) { log_err("error building sig file path '%s%s'", path, sig_ext); return; } /* Try to open the sig file to see if one exists yet */ fp = fopen(sig_path, "rb"); err = errno; if (fp) fclose(fp); if (!err) { /* Sig file found => figure out if we need to re-create it */ /* Read the metadata from the sig file */ metadata_t md; err = read_metadata(sig_path, &md); if (err) { log_err("error reading metadata from sig file '%s'; will be re-created", sig_path); } else { /* Figure out if we need to re-create the sig file */ offset_t hcl = sizeof(hcl) == 4 ? md.cll : (offset_t)md.clh << 32 | md.cll; if (entry_info.st_size == hcl) { /* Unmodified so skip */ log_inf("'%s' unmodified", path); return; } else if (entry_info.st_size < hcl) { /* Shrunk so re-sign */ log_wrn("'%s' shrunk", path); err = 1; /* force re-sign from beginning of file */ } else { /* Modified so re-sign the file using the hash state saved in the metatdata */ log_inf("'%s' modified", path); } } /* Create/re-create sig file */ sign(path, pin, label, err ? 0 : &md); } else { /* No sig file found (or err reading it) => create/re-create */ int e = errno; if (e == ENOENT) /* A sig file doesn't yet exist, assume file is new */ log_inf("'%s' not yet signed", path); else /* Error accessing an existing sig file */ log_err("error accessing sig file '%s': %s; will be re-created", sig_path, strerror(e)); /* Create/re-create sig file */ sign(path, pin, label, 0); } }
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { grpc_test_only_set_metadata_hash_seed(0); if (squelch) gpr_set_log_function(dont_log); input_stream inp = {data, data + size}; grpc_resolve_address = my_resolve_address; grpc_tcp_client_connect_impl = my_tcp_client_connect; gpr_now_impl = now_impl; grpc_init(); GPR_ASSERT(g_channel == NULL); GPR_ASSERT(g_server == NULL); bool server_shutdown = false; int pending_server_shutdowns = 0; int pending_channel_watches = 0; int pending_pings = 0; g_active_call = new_call(NULL, ROOT); grpc_completion_queue *cq = grpc_completion_queue_create(NULL); while (!is_eof(&inp) || g_channel != NULL || g_server != NULL || pending_channel_watches > 0 || pending_pings > 0 || g_active_call->type != ROOT || g_active_call->next != g_active_call) { if (is_eof(&inp)) { if (g_channel != NULL) { grpc_channel_destroy(g_channel); g_channel = NULL; } if (g_server != NULL) { if (!server_shutdown) { grpc_server_shutdown_and_notify( g_server, cq, create_validator(assert_success_and_decrement, &pending_server_shutdowns)); server_shutdown = true; pending_server_shutdowns++; } else if (pending_server_shutdowns == 0) { grpc_server_destroy(g_server); g_server = NULL; } } call_state *s = g_active_call; do { if (s->type != PENDING_SERVER && s->call != NULL) { s = destroy_call(s); } else { s = s->next; } } while (s != g_active_call); g_now = gpr_time_add(g_now, gpr_time_from_seconds(1, GPR_TIMESPAN)); } switch (next_byte(&inp)) { // terminate on bad bytes default: end(&inp); break; // tickle completion queue case 0: { grpc_event ev = grpc_completion_queue_next( cq, gpr_inf_past(GPR_CLOCK_REALTIME), NULL); switch (ev.type) { case GRPC_OP_COMPLETE: { validator *v = ev.tag; v->validate(v->arg, ev.success); gpr_free(v); break; } case GRPC_QUEUE_TIMEOUT: break; case GRPC_QUEUE_SHUTDOWN: abort(); break; } break; } // increment global time case 1: { g_now = gpr_time_add( g_now, gpr_time_from_micros(read_uint32(&inp), GPR_TIMESPAN)); break; } // create an insecure channel case 2: { if (g_channel == NULL) { char *target = read_string(&inp); char *target_uri; gpr_asprintf(&target_uri, "dns:%s", target); grpc_channel_args *args = read_args(&inp); g_channel = grpc_insecure_channel_create(target_uri, args, NULL); GPR_ASSERT(g_channel != NULL); grpc_channel_args_destroy(args); gpr_free(target_uri); gpr_free(target); } else { end(&inp); } break; } // destroy a channel case 3: { if (g_channel != NULL) { grpc_channel_destroy(g_channel); g_channel = NULL; } else { end(&inp); } break; } // bring up a server case 4: { if (g_server == NULL) { grpc_channel_args *args = read_args(&inp); g_server = grpc_server_create(args, NULL); GPR_ASSERT(g_server != NULL); grpc_channel_args_destroy(args); grpc_server_register_completion_queue(g_server, cq, NULL); grpc_server_start(g_server); server_shutdown = false; GPR_ASSERT(pending_server_shutdowns == 0); } else { end(&inp); } } // begin server shutdown case 5: { if (g_server != NULL) { grpc_server_shutdown_and_notify( g_server, cq, create_validator(assert_success_and_decrement, &pending_server_shutdowns)); pending_server_shutdowns++; server_shutdown = true; } else { end(&inp); } break; } // cancel all calls if shutdown case 6: { if (g_server != NULL && server_shutdown) { grpc_server_cancel_all_calls(g_server); } else { end(&inp); } break; } // destroy server case 7: { if (g_server != NULL && server_shutdown && pending_server_shutdowns == 0) { grpc_server_destroy(g_server); g_server = NULL; } else { end(&inp); } break; } // check connectivity case 8: { if (g_channel != NULL) { uint8_t try_to_connect = next_byte(&inp); if (try_to_connect == 0 || try_to_connect == 1) { grpc_channel_check_connectivity_state(g_channel, try_to_connect); } else { end(&inp); } } else { end(&inp); } break; } // watch connectivity case 9: { if (g_channel != NULL) { grpc_connectivity_state st = grpc_channel_check_connectivity_state(g_channel, 0); if (st != GRPC_CHANNEL_FATAL_FAILURE) { gpr_timespec deadline = gpr_time_add( gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(read_uint32(&inp), GPR_TIMESPAN)); grpc_channel_watch_connectivity_state( g_channel, st, deadline, cq, create_validator(validate_connectivity_watch, make_connectivity_watch( deadline, &pending_channel_watches))); pending_channel_watches++; } } else { end(&inp); } break; } // create a call case 10: { bool ok = true; if (g_channel == NULL) ok = false; grpc_call *parent_call = NULL; if (g_active_call->type != ROOT) { if (g_active_call->call == NULL || g_active_call->type == CLIENT) { end(&inp); break; } parent_call = g_active_call->call; } uint32_t propagation_mask = read_uint32(&inp); char *method = read_string(&inp); char *host = read_string(&inp); gpr_timespec deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_micros(read_uint32(&inp), GPR_TIMESPAN)); if (ok) { call_state *cs = new_call(g_active_call, CLIENT); cs->call = grpc_channel_create_call(g_channel, parent_call, propagation_mask, cq, method, host, deadline, NULL); } else { end(&inp); } gpr_free(method); gpr_free(host); break; } // switch the 'current' call case 11: { g_active_call = g_active_call->next; break; } // queue some ops on a call case 12: { if (g_active_call->type == PENDING_SERVER || g_active_call->type == ROOT || g_active_call->call == NULL) { end(&inp); break; } size_t num_ops = next_byte(&inp); if (num_ops > 6) { end(&inp); break; } grpc_op *ops = gpr_malloc(sizeof(grpc_op) * num_ops); bool ok = true; size_t i; grpc_op *op; for (i = 0; i < num_ops; i++) { op = &ops[i]; switch (next_byte(&inp)) { default: /* invalid value */ op->op = (grpc_op_type)-1; ok = false; break; case GRPC_OP_SEND_INITIAL_METADATA: op->op = GRPC_OP_SEND_INITIAL_METADATA; read_metadata(&inp, &op->data.send_initial_metadata.count, &op->data.send_initial_metadata.metadata, g_active_call); break; case GRPC_OP_SEND_MESSAGE: op->op = GRPC_OP_SEND_MESSAGE; op->data.send_message = read_message(&inp); break; case GRPC_OP_SEND_CLOSE_FROM_CLIENT: op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; break; case GRPC_OP_SEND_STATUS_FROM_SERVER: op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; read_metadata( &inp, &op->data.send_status_from_server.trailing_metadata_count, &op->data.send_status_from_server.trailing_metadata, g_active_call); op->data.send_status_from_server.status = next_byte(&inp); op->data.send_status_from_server.status_details = read_string(&inp); break; case GRPC_OP_RECV_INITIAL_METADATA: op->op = GRPC_OP_RECV_INITIAL_METADATA; op->data.recv_initial_metadata = &g_active_call->recv_initial_metadata; break; case GRPC_OP_RECV_MESSAGE: op->op = GRPC_OP_RECV_MESSAGE; op->data.recv_message = &g_active_call->recv_message; break; case GRPC_OP_RECV_STATUS_ON_CLIENT: op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; op->data.recv_status_on_client.status = &g_active_call->status; op->data.recv_status_on_client.trailing_metadata = &g_active_call->recv_trailing_metadata; op->data.recv_status_on_client.status_details = &g_active_call->recv_status_details; op->data.recv_status_on_client.status_details_capacity = &g_active_call->recv_status_details_capacity; break; case GRPC_OP_RECV_CLOSE_ON_SERVER: op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; op->data.recv_close_on_server.cancelled = &g_active_call->cancelled; break; } op->reserved = NULL; op->flags = read_uint32(&inp); } if (ok) { validator *v = create_validator(finished_batch, g_active_call); g_active_call->pending_ops++; grpc_call_error error = grpc_call_start_batch(g_active_call->call, ops, num_ops, v, NULL); if (error != GRPC_CALL_OK) { v->validate(v->arg, false); gpr_free(v); } } else { end(&inp); } for (i = 0; i < num_ops; i++) { op = &ops[i]; switch (op->op) { case GRPC_OP_SEND_INITIAL_METADATA: break; case GRPC_OP_SEND_MESSAGE: grpc_byte_buffer_destroy(op->data.send_message); break; case GRPC_OP_SEND_STATUS_FROM_SERVER: gpr_free((void *)op->data.send_status_from_server.status_details); break; case GRPC_OP_SEND_CLOSE_FROM_CLIENT: case GRPC_OP_RECV_INITIAL_METADATA: case GRPC_OP_RECV_MESSAGE: case GRPC_OP_RECV_STATUS_ON_CLIENT: case GRPC_OP_RECV_CLOSE_ON_SERVER: break; } } gpr_free(ops); break; } // cancel current call case 13: { if (g_active_call->type != ROOT && g_active_call->call != NULL) { grpc_call_cancel(g_active_call->call, NULL); } else { end(&inp); } break; } // get a calls peer case 14: { if (g_active_call->type != ROOT && g_active_call->call != NULL) { free_non_null(grpc_call_get_peer(g_active_call->call)); } else { end(&inp); } break; } // get a channels target case 15: { if (g_channel != NULL) { free_non_null(grpc_channel_get_target(g_channel)); } else { end(&inp); } break; } // send a ping on a channel case 16: { if (g_channel != NULL) { pending_pings++; grpc_channel_ping(g_channel, cq, create_validator(decrement, &pending_pings), NULL); } else { end(&inp); } break; } // enable a tracer case 17: { char *tracer = read_string(&inp); grpc_tracer_set_enabled(tracer, 1); gpr_free(tracer); break; } // disable a tracer case 18: { char *tracer = read_string(&inp); grpc_tracer_set_enabled(tracer, 0); gpr_free(tracer); break; } // request a server call case 19: { if (g_server == NULL) { end(&inp); break; } call_state *cs = new_call(g_active_call, PENDING_SERVER); cs->pending_ops++; validator *v = create_validator(finished_request_call, cs); grpc_call_error error = grpc_server_request_call(g_server, &cs->call, &cs->call_details, &cs->recv_initial_metadata, cq, cq, v); if (error != GRPC_CALL_OK) { v->validate(v->arg, false); gpr_free(v); } break; } // destroy a call case 20: { if (g_active_call->type != ROOT && g_active_call->type != PENDING_SERVER && g_active_call->call != NULL) { destroy_call(g_active_call); } else { end(&inp); } break; } } } GPR_ASSERT(g_channel == NULL); GPR_ASSERT(g_server == NULL); GPR_ASSERT(g_active_call->type == ROOT); GPR_ASSERT(g_active_call->next == g_active_call); gpr_free(g_active_call); grpc_completion_queue_shutdown(cq); GPR_ASSERT( grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME), NULL) .type == GRPC_QUEUE_SHUTDOWN); grpc_completion_queue_destroy(cq); grpc_shutdown(); return 0; }