static void fill_buff(SgBTreeNode *n, SgChar *buff, SgSize vl, SgSize hl, SgSize ml, SgSize ls) { SgSize s; if (n == LAST) return; s = 2 << (ml - hl - 1); buff[hl*ls+s/2-1+s*vl] = '.'; fill_buff(n->left, buff, vl << 1, hl + 1, ml, ls); fill_buff(n->right, buff, (vl << 1) + 1, hl + 1, ml, ls); }
static SRP_Result init_random() { if (g_initialized) return SRP_OK; SRP_Result ret = fill_buff(); g_initialized = (ret == SRP_OK); return ret; }
static SRP_Result mpz_fill_random(mpz_t num) { // was call: BN_rand(num, 256, -1, 0); if (RAND_BUFF_MAX - g_rand_idx < 32) if (fill_buff() != SRP_OK) return SRP_ERR; mpz_from_bin((const unsigned char *)(&g_rand_buff[g_rand_idx]), 32, num); g_rand_idx += 32; return SRP_OK; }
// clang-format off SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg, SRP_NGType ng_type, const char *username_for_verifier, const unsigned char *password, size_t len_password, unsigned char **bytes_s, size_t *len_s, unsigned char **bytes_v, size_t *len_v, const char *n_hex, const char *g_hex ) { SRP_Result ret = SRP_OK; mpz_t v; mpz_init(v); mpz_t x; mpz_init(x); // clang-format on NGConstant *ng = new_ng(ng_type, n_hex, g_hex); if (!ng) goto error_and_exit; if (init_random() != SRP_OK) /* Only happens once */ goto error_and_exit; if (*bytes_s == NULL) { size_t size_to_fill = 16; *len_s = size_to_fill; if (RAND_BUFF_MAX - g_rand_idx < size_to_fill) if (fill_buff() != SRP_OK) goto error_and_exit; *bytes_s = (unsigned char *)srp_alloc(size_to_fill); if (!*bytes_s) goto error_and_exit; memcpy(*bytes_s, g_rand_buff + g_rand_idx, size_to_fill); g_rand_idx += size_to_fill; } if (!calculate_x( x, alg, *bytes_s, *len_s, username_for_verifier, password, len_password)) goto error_and_exit; srp_dbg_num(x, "Server calculated x: "); mpz_powm(v, ng->g, x, ng->N); *len_v = mpz_num_bytes(v); *bytes_v = (unsigned char *)srp_alloc(*len_v); if (!*bytes_v) goto error_and_exit; mpz_to_bin(v, *bytes_v); cleanup_and_exit: delete_ng(ng); mpz_clear(v); mpz_clear(x); return ret; error_and_exit: ret = SRP_ERR; goto cleanup_and_exit; }
//initializes audio buffer int initAudio(char* fname) { alt_up_sd_card_dev *device_reference = NULL; device_reference = alt_up_sd_card_open_dev(SD_CARD_NAME); open_sd(); av_config_setup(); init_audio_buff(ab); ab->audio = alt_up_audio_open_dev(AUDIO_0_NAME); alt_up_audio_reset_audio_core(ab->audio); file_handle = alt_up_sd_card_fopen(fname, 0); offset(file_handle, ab); fill_buff(file_handle, ab); return file_handle; }
void sg_display_btree(SgBTreeNode *n) { SgSize ml, ls, i, j; SgChar *buff; if (n == LAST) return; ml = n->mlevel; ls = (2 << ml) / 2; buff = sg_alloc(ls * ml); for (i = 0; i < ml; i++) { for (j = 0; j < (ls - 1); j++) { buff[i*ls+j] = ' '; } buff[i*ls+j] = 0; } fill_buff(n, buff, 0, 0, ml, ls); for (; ml;) { printf("%s\n", &buff[--ml*ls]); } sg_free(buff); }
static int count_time_internal (struct mp3_data *data) { struct xing xing; unsigned long bitrate = 0; int has_xing = 0; int is_vbr = 0; int num_frames = 0; mad_timer_t duration = mad_timer_zero; struct mad_header header; int good_header = 0; /* Have we decoded any header? */ mad_header_init (&header); xing_init (&xing); /* There are three ways of calculating the length of an mp3: 1) Constant bitrate: One frame can provide the information needed: # of frames and duration. Just see how long it is and do the division. 2) Variable bitrate: Xing tag. It provides the number of frames. Each frame has the same number of samples, so just use that. 3) All: Count up the frames and duration of each frames by decoding each one. We do this if we've no other choice, i.e. if it's a VBR file with no Xing tag. */ while (1) { /* Fill the input buffer if needed */ if (data->stream.buffer == NULL || data->stream.error == MAD_ERROR_BUFLEN) { if (!fill_buff(data)) break; } if (mad_header_decode(&header, &data->stream) == -1) { if (MAD_RECOVERABLE(data->stream.error)) continue; else if (data->stream.error == MAD_ERROR_BUFLEN) continue; else { debug ("Can't decode header: %s", mad_stream_errorstr( &data->stream)); break; } } good_header = 1; /* Limit xing testing to the first frame header */ if (!num_frames++) { if (xing_parse(&xing, data->stream.anc_ptr, data->stream.anc_bitlen) != -1) { is_vbr = 1; debug ("Has XING header"); if (xing.flags & XING_FRAMES) { has_xing = 1; num_frames = xing.frames; break; } debug ("XING header doesn't contain number of " "frames."); } } /* Test the first n frames to see if this is a VBR file */ if (!is_vbr && !(num_frames > 20)) { if (bitrate && header.bitrate != bitrate) { debug ("Detected VBR after %d frames", num_frames); is_vbr = 1; } else bitrate = header.bitrate; } /* We have to assume it's not a VBR file if it hasn't already * been marked as one and we've checked n frames for different * bitrates */ else if (!is_vbr) { debug ("Fixed rate MP3"); break; } mad_timer_add (&duration, header.duration); } if (!good_header) return -1; if (!is_vbr) { /* time in seconds */ double time = (data->size * 8.0) / (header.bitrate); double timefrac = (double)time - ((long)(time)); /* samples per frame */ long nsamples = 32 * MAD_NSBSAMPLES(&header); /* samplerate is a constant */ num_frames = (long) (time * header.samplerate / nsamples); /* the average bitrate is the constant bitrate */ data->avg_bitrate = bitrate; mad_timer_set(&duration, (long)time, (long)(timefrac*100), 100); } else if (has_xing) { mad_timer_multiply (&header.duration, num_frames); duration = header.duration; } else { /* the durations have been added up, and the number of frames counted. We do nothing here. */ debug ("Counted duration by counting frames durations in " "VBR file."); } if (data->avg_bitrate == -1 && mad_timer_count(duration, MAD_UNITS_SECONDS) > 0) { data->avg_bitrate = data->size / mad_timer_count(duration, MAD_UNITS_SECONDS) * 8; } mad_header_finish(&header); debug ("MP3 time: %ld", mad_timer_count (duration, MAD_UNITS_SECONDS)); return mad_timer_count (duration, MAD_UNITS_SECONDS); }
int main(int argc, char *argv[]) { int listenfd, connfd, port, clientlen, numworkers; char *sched; struct sockaddr_in clientaddr; struct stat sbuf; pthread_mutex_init(&m, NULL); // lock for producer and consumer loops pthread_cond_init(&empty, NULL); // CV for master/producer to wait on pthread_cond_init(&fill, NULL); // CV for workers/consumers to wait on getargs(&port, &numworkers, &bufsize, &sched, argc, argv); // // CS537: Create some threads... // pthread_t threads[numworkers]; // array of worker threads int i; for (i = 0; i < numworkers; i++) { pthread_create(&threads[i], NULL, consumer, NULL); } listenfd = Open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, (socklen_t *) &clientlen); pthread_mutex_lock(&m); buff_t* newNode = malloc(sizeof(buff_t)); newNode->fd = connfd; Rio_readinitb(&((*newNode).rio), newNode->fd); Rio_readlineb(&((*newNode).rio), newNode->buf, MAXLINE); sscanf(newNode->buf, "%s %s %s", newNode->method, newNode->uri, newNode->version); requestReadhdrs(&((*newNode).rio)); newNode->is_static = requestParseURI(newNode->uri, newNode->filename, newNode->cgiargs); stat(newNode->filename, &sbuf); newNode->filesize = sbuf.st_size; newNode->filenamesize = strlen(newNode->filename); //newNode->cgiargs = cgiargs; while(numrequests == bufsize) { pthread_cond_wait(&empty, &m); } if (strcmp(sched, "FIFO") == 0) { fill_buff(newNode); } else if (strcmp(sched, "SFNF") == 0) { fill_buff2(newNode); } else if (strcmp(sched, "SFF") == 0) { fill_buff3(newNode); } pthread_cond_signal(&fill); pthread_mutex_unlock(&m); //fprintf(stderr, "numrequests: %d\n", numrequests); // // CS537: In general, don't handle the request in the main thread. // Save the relevant info in a buffer and have one of the worker threads // do the work. However, for SFF, you may have to do a little work // here (e.g., a stat() on the filename) ... // //requestHandle(connfd); //Close(connfd); } }