static int write_header(void **data, u_int32_t *size, u_int32_t *pos, u_int16_t type, void* key_data, u_int32_t key_size, void* data_data, u_int32_t data_size) { struct cache_entry_header h; u_int32_t need_memory; univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ALL, "write_header key_size=%d data_size=%d", key_size, data_size); need_memory = sizeof(struct cache_entry_header)+key_size+data_size; if (*size < *pos+need_memory) { while (*size < *pos+need_memory) *size += BUFSIZ; if ((*data = realloc(*data, *size)) == NULL) { univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "failed to allocate memory"); return 1; } } h.type = type; h.key_size = key_size; h.data_size = data_size; append_buffer(data, size, pos, (void*) &h, sizeof(struct cache_entry_header)); append_buffer(data, size, pos, key_data, key_size); append_buffer(data, size, pos, data_data, data_size); return 0; }
void xml_format_verse(struct verse * v, void * ctx) { struct buffer * buf = ctx; append_buffer(buf, " <verse>\n"); append_buffer(buf, get_verse_text(v)); append_buffer(buf, "\n </verse>\n"); }
static void pipe_read_input_callback(INT32 args) { struct input *i; struct pike_string *s; if (args<2 || sp[1-args].type!=T_STRING) Pike_error("Illegal argument to pipe->read_input_callback\n"); i=THIS->firstinput; if (!i) Pike_error("Pipe read callback without any inputs left.\n"); s=sp[1-args].u.string; if(append_buffer(s)) { /* THIS DOES NOT WORK */ push_int(0); push_int(0); push_callback(offset_input_close_callback); apply_low(i->u.obj,i->set_nonblocking_offset,3); pop_stack(); THIS->sleeping=1; } low_start(); pop_n_elems(args-1); }
static inline void emit(struct buffer *buf, unsigned char c) { int err; err = append_buffer(buf, c); assert(!err); }
/* // Name: handle_input // In: fd, The filedescriptor that has been read from. // buf, The data that has been read. // buf_len, The length of the data that has been read. // clients, all connected clients in a hash map. // topic, the current topic in the chat. // Out: >0 if the client wanted to do an action on it's connection, like // if the client wanted to close it's connection. <0 if an error // has occurred. // Purpose: Reads the data received from the client and saves it // in a buffer. Every message received that ends with CRLF will // be parsed. */ int handle_input(int fd, char* buf, int buf_len, hash *clients, char *topic) { clientconn_t *c; if((c = hash_get(fd, clients)) == NULL) { return -1; } fprintf(stderr, "Received %d bytes from" " client %s (%d).\n", buf_len, (strlen(c->name)==0)?"NO NAME":c->name, c->fd); if(buf_len > 3 && buf[buf_len-2]=='\r' && buf[buf_len-1]=='\n') { if(append_buffer(c, buf, buf_len) < 0) { client_write(c, "NO Buffer full.\r\n", 17); } } else { return 0; } char return_msg[MAXBUFSIZE]; int bytes_read = 0; char send_buf[MAXBUFSIZE]; int result = 0, tmpresult; while((bytes_read=read_msg_in_buffer(c, return_msg)) > -1) { if (tmpresult = protocol_parse(return_msg, bytes_read, c, clients, topic) != 0) { result = tmpresult; } memset(return_msg, '\0', MAXBUFSIZE); memset(send_buf, '\0', MAXBUFSIZE); } return result; }
static int post_log_syscall(struct pusha_regs * regs) { struct thread_private_data * tpd = get_tpd(); int nr = tpd->current_syscall_nr; TRACE(LOG_SYSCALL, "post syscall, eax=%u(0x%x), ebx=%u, eflags=0x%x\n", regs->eax, regs->eax, regs->ebx, regs->eflags); /* for 'clone' in child process, don't append buffer */ if (!(((nr == __NR_clone) || (nr == __NR_fork)) && (regs->eax == 0))) { /* put a 'no-signal-mark' */ append_buffer_u32(NO_SIGNAL_MARK); /* put regs */ struct pusha_regs real_regs = *regs; real_regs.esp = (uintptr_t)(tpd->old_stack_top); append_buffer(&real_regs, sizeof(real_regs)); } tpd->current_syscall_nr = -1; if (syscall_table[nr].post_handler) { return syscall_table[nr].post_handler(regs); } else { FATAL(LOG_SYSCALL, "doesn't support syscall %d\n", nr); } }
static OMX_ERRORTYPE fill_buffer_done(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_BUFFERHEADERTYPE *buffer) { OMXCodecContext *s = app_data; append_buffer(&s->output_mutex, &s->output_cond, &s->num_done_out_buffers, s->done_out_buffers, buffer); return OMX_ErrorNone; }
/** * soup_message_body_append: * @body: a #SoupMessageBody * @use: how to use @data * @data: (array length=length) (element-type guint8): data to append * @length: length of @data * * Appends @length bytes from @data to @body according to @use. **/ void soup_message_body_append (SoupMessageBody *body, SoupMemoryUse use, gconstpointer data, gsize length) { if (length > 0) append_buffer (body, soup_buffer_new (use, data, length)); else if (use == SOUP_MEMORY_TAKE) g_free ((gpointer)data); }
char *image_to_string(const xcolor_image_t *image) { fab_buffer_t *buffer; if((buffer = malloc(sizeof(fab_buffer_t))) == NULL) { perror("malloc"); exit(EXIT_FAILURE); } init_buffer(buffer); for(size_t y = 0; y < image->y; y++) { for(size_t x = 0; x < image->x; x++) { const char *color_block = colorize(escape(3, 48, 5, image->pixels[y][x]), escape(1, 49), " "); append_buffer(buffer, color_block); free((void *)color_block); } append_buffer(buffer, "\n"); } truncate_buffer(buffer); char *string = buffer->buffer; free(buffer); return string; }
static OMX_ERRORTYPE empty_buffer_done(OMX_HANDLETYPE component, OMX_PTR app_data, OMX_BUFFERHEADERTYPE *buffer) { OMXCodecContext *s = app_data; if (s->input_zerocopy) { if (buffer->pAppPrivate) { if (buffer->pOutputPortPrivate) av_free(buffer->pAppPrivate); else av_frame_free((AVFrame**)&buffer->pAppPrivate); buffer->pAppPrivate = NULL; } } append_buffer(&s->input_mutex, &s->input_cond, &s->num_free_in_buffers, s->free_in_buffers, buffer); return OMX_ErrorNone; }
/* Let's guess what this function does.... * */ static INLINE void input_finish(void) { struct input *i; while(1) { /* Get the next input from the queue */ i=THIS->firstinput->next; free_input(THIS->firstinput); THIS->firstinput=i; if(!i) break; switch(i->type) { case I_OBJ: THIS->sleeping=0; push_callback(offset_input_read_callback); push_int(0); push_callback(offset_input_close_callback); apply_low(i->u.obj,i->set_nonblocking_offset,3); pop_stack(); return; case I_BLOCKING_OBJ: if (read_some_data()) return; continue; case I_MMAP: if (THIS->fd==-1) return; continue; case I_STRING: append_buffer(i->u.str); case I_NONE: break; } } THIS->sleeping=0; low_start(); finished_p(); }
static void on_read_finished (GObject* input, GAsyncResult* result, gpointer data) { SourceviewIO* sio = SOURCEVIEW_IO(data); GInputStream* input_stream = G_INPUT_STREAM(input); gsize current_bytes = 0; GError* err = NULL; current_bytes = g_input_stream_read_finish (input_stream, result, &err); if (err) { g_signal_emit_by_name (sio, "open-failed", err); g_error_free (err); g_object_unref (input_stream); g_free (sio->read_buffer); sio->read_buffer = NULL; sio->bytes_read = 0; return; } sio->bytes_read += current_bytes; if (current_bytes != 0) { sio->read_buffer = g_realloc (sio->read_buffer, sio->bytes_read + READ_SIZE); g_input_stream_read_async (G_INPUT_STREAM (input_stream), sio->read_buffer + sio->bytes_read, READ_SIZE, G_PRIORITY_LOW, sio->cancel, on_read_finished, sio); return; } else { if (append_buffer (sio, sio->bytes_read)) g_signal_emit_by_name (sio, "open-finished"); sio->bytes_read = 0; g_object_unref (input_stream); setup_monitor (sio); g_free (sio->read_buffer); sio->read_buffer = NULL; } }
/*! @decl void write(string bytes) *! *! Add an input string to this pipe. */ static void pipe_write(INT32 args) { struct input *i; if (args<1 || sp[-args].type!=T_STRING) Pike_error("illegal argument to pipe->write()\n"); if (!THIS->firstinput) { append_buffer(sp[-args].u.string); pop_n_elems(args); push_int(0); return; } i=new_input(); i->type=I_STRING; nstrings++; add_ref(i->u.str=sp[-args].u.string); pop_n_elems(args-1); }
static void append_triangles(struct groupdata *groupdata, buffer *buffer) { triangle *triangles = buffer_data(buffer); size_t trianglecount = buffer_count(buffer, sizeof(struct triangle)); for (size_t i = 0; i < trianglecount; ++i) { vec3 normal; triangle_normal(&normal, &triangles[i]); vec3_normalize(&normal, &normal); vertex vertices[3] = { {normal, triangles[i].a}, {normal, triangles[i].b}, {normal, triangles[i].c} }; append_buffer(groupdata->vertices, vertices, sizeof(vertices)); } }
/* Read some data from the blocking object. * */ static int read_some_data(void) { struct pipe *this = THIS; struct input * i = this->firstinput; if (!i || i->type != I_BLOCKING_OBJ) { Pike_fatal("PIPE: read_some_data(): Bad input type!\n"); return -1; } push_int(8192); push_int(1); /* We don't care if we don't get all 8192 bytes. */ apply(i->u.obj, "read", 2); if ((sp[-1].type == T_STRING) && (sp[-1].u.string->len > 0)) { append_buffer(sp[-1].u.string); pop_stack(); THIS->sleeping = 1; return(1); /* Success */ } /* FIXME: Should we check the return value here? */ pop_stack(); /* EOF */ return(0); /* EOF */ }
void xml_formatter(FILE * f, const struct poem *p, GList * verses) { struct buffer * buf = create_buffer(XML_INITIAL_BUFFER_SIZE); append_buffer(buf, "<?xml version=\"1.0\" standalone=\"yes\"?>\n"); append_buffer(buf, "<poem>\n"); append_buffer(buf, " <name>"); append_buffer(buf, get_poem_name(p)); append_buffer(buf, "</name>\n"); if (verses) g_list_foreach(verses, (GFunc)xml_format_verse, buf); else each_verse(p, xml_format_verse, buf); append_buffer(buf, "</poem>\n"); fprintf(f, "%s\n", get_buffer_string(buf)); destroy_buffer(buf); }
int request_custom_command(buffer* recv_buf, server_stat* status) { unsigned char http_message[1000]; // used to hold http message from robot buffer* http_data = create_buffer(BUFFER_LEN); int n, retries; cst_header request_header; // header from the client buffer* response; // buffer to send to client struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 500000; fprintf(stdout, "\tProcessing request\n"); // acknowledgement of command to client udp_send(recv_buf, status); // create the http request memset(http_message, '\0', 1000); request_header = extract_custom_header(recv_buf); switch (request_header.data[CST_COMMAND]) { case IMAGE: fprintf(stdout, "\t\tContacting image port\n"); snprintf((char*)http_message, 100, "GET /snapshot?topic=/robot_%d/image?width=600?height=500 HTTP/1.1\r\n\r\n", status->r_stat.id); status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, IMAGE_PORT); break; case GPS: fprintf(stdout, "\t\tContacting gps port\n"); snprintf((char*)http_message, 100, "GET /state?id=%s HTTP/1.1\r\n\r\n", status->r_stat.name); status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT); break; case LASERS: fprintf(stdout, "\t\tContacting lasers port\n"); snprintf((char*)http_message, 100, "GET /state?id=%s HTTP/1.1\r\n\r\n", status->r_stat.name); status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, LASERS_PORT); break; case dGPS: fprintf(stdout, "\t\tContacting dgps port\n"); snprintf((char*)http_message, 100, "GET /state?id=%s HTTP/1.1\r\n\r\n", status->r_stat.name); status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, dGPS_PORT); break; case MOVE: fprintf(stdout, "\t\tContacting move port\n"); snprintf((char*)http_message, 100, "GET /twist?id=%s&lx=1 HTTP/1.1\r\n\r\n", status->r_stat.name); status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT); break; case TURN: fprintf(stdout, "\t\tContacting turn port\n"); snprintf((char*)http_message, 100, "GET /twist?id=%s&az=1 HTTP/1.1\r\n\r\n", status->r_stat.name); status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT); break; case STOP: fprintf(stdout, "\t\tContacting stop port\n"); snprintf((char*)http_message, 100, "GET /twist?id=%s&lx=0 HTTP/1.1\r\n\r\n", status->r_stat.name); status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT); break; default: fprintf(stderr, "ERROR: Invalid client request\n"); return -2; break; } // send the http request fprintf(stdout, "\t\tWriting request to server\n"); timeout_setup(status->r_stat.http_sock, timeout); write(status->r_stat.http_sock, (char*)http_message, strlen((char*)http_message)); // read http message into a buffer fprintf(stdout, "\t\tReceiving reply from server\n"); memset(http_message, '\0', 1000); retries = 0; while (1) { n = read(status->r_stat.http_sock, (char*)http_message, 1000); if (n == -1) { if (retries > 2) { break; } retries++; write(status->r_stat.http_sock, (char*)http_message, strlen((char*)http_message)); continue; } else if (n == 0) { break; } fprintf(stdout, "\t\t\tReceived %d bytes\n", n); append_buffer(http_data, (unsigned char*)http_message, n); memset(http_message, '\0', 1000); } fprintf(stdout, "\t\tTotal bytes received: %d\n", http_data->len); // see what we go fprintf(stdout, "\t\tAssembled Message:\n%s\n", http_data->data); // check for '200 OK' char ret_code[4]; memcpy(ret_code, http_data->data + 9, 3); // HTTP/1.1 <ret_code> ~~~~ ret_code[3] = '\0'; if (atoi(ret_code) != 200) { fprintf(stderr, "ERROR: bad http request\n"); response = create_custom_message(GROUP_NUMBER, status->password, HTTP_ERROR, 0, 0, 0); udp_send(response, status); return 0; } // send it to client int http_header_len = http_get_data(http_data->data) - http_data->data; int a = 0; while ((a + http_header_len) < http_data->len) { response = create_custom_message(request_header.data[CST_VERSION], request_header.data[CST_PASSWORD], request_header.data[DATA], request_header.data[CST_SEQUENCE], (http_data->len - http_header_len), ((http_data->len - (a + http_header_len)) > CST_MAX_PAYLOAD ? CST_MAX_PAYLOAD:(http_data->len - (a + http_header_len)))); append_buffer(response, http_data->data + a + http_header_len, CST_MAX_PAYLOAD); fprintf(stdout, "\n\t\tAssembled packet:\n"); print_header(response); fprintf(stdout, "%s\n", response->data + UP_HEADER_LEN); fprintf(stdout, "\t\tSending packet\n"); udp_send(response, status); fprintf(stdout, "\t\tPacket sent\n"); delete_buffer(response); a += 370; } fprintf(stdout, "\t\tRequest complete!\n"); return 1; }
/** * soup_message_body_append_buffer: * @body: a #SoupMessageBody * @buffer: a #SoupBuffer * * Appends the data from @buffer to @body. (#SoupMessageBody uses * #SoupBuffers internally, so this is normally a constant-time * operation that doesn't actually require copying the data in * @buffer.) **/ void soup_message_body_append_buffer (SoupMessageBody *body, SoupBuffer *buffer) { g_return_if_fail (buffer->length > 0); append_buffer (body, soup_buffer_copy (buffer)); }
static int omx_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet) { OMXCodecContext *s = avctx->priv_data; int ret = 0; OMX_BUFFERHEADERTYPE* buffer; OMX_ERRORTYPE err; if (frame) { uint8_t *dst[4]; int linesize[4]; int need_copy; buffer = get_buffer(&s->input_mutex, &s->input_cond, &s->num_free_in_buffers, s->free_in_buffers, 1); buffer->nFilledLen = av_image_fill_arrays(dst, linesize, buffer->pBuffer, avctx->pix_fmt, s->stride, s->plane_size, 1); if (s->input_zerocopy) { uint8_t *src[4] = { NULL }; int src_linesize[4]; av_image_fill_arrays(src, src_linesize, frame->data[0], avctx->pix_fmt, s->stride, s->plane_size, 1); if (frame->linesize[0] == src_linesize[0] && frame->linesize[1] == src_linesize[1] && frame->linesize[2] == src_linesize[2] && frame->data[1] == src[1] && frame->data[2] == src[2]) { // If the input frame happens to have all planes stored contiguously, // with the right strides, just clone the frame and set the OMX // buffer header to point to it AVFrame *local = av_frame_clone(frame); if (!local) { // Return the buffer to the queue so it's not lost append_buffer(&s->input_mutex, &s->input_cond, &s->num_free_in_buffers, s->free_in_buffers, buffer); return AVERROR(ENOMEM); } else { buffer->pAppPrivate = local; buffer->pOutputPortPrivate = NULL; buffer->pBuffer = local->data[0]; need_copy = 0; } } else { // If not, we need to allocate a new buffer with the right // size and copy the input frame into it. uint8_t *buf = NULL; int image_buffer_size = av_image_get_buffer_size(avctx->pix_fmt, s->stride, s->plane_size, 1); if (image_buffer_size >= 0) buf = av_malloc(image_buffer_size); if (!buf) { // Return the buffer to the queue so it's not lost append_buffer(&s->input_mutex, &s->input_cond, &s->num_free_in_buffers, s->free_in_buffers, buffer); return AVERROR(ENOMEM); } else { buffer->pAppPrivate = buf; // Mark that pAppPrivate is an av_malloc'ed buffer, not an AVFrame buffer->pOutputPortPrivate = (void*) 1; buffer->pBuffer = buf; need_copy = 1; buffer->nFilledLen = av_image_fill_arrays(dst, linesize, buffer->pBuffer, avctx->pix_fmt, s->stride, s->plane_size, 1); } } } else { need_copy = 1; } if (need_copy) av_image_copy(dst, linesize, (const uint8_t**) frame->data, frame->linesize, avctx->pix_fmt, avctx->width, avctx->height); buffer->nFlags = OMX_BUFFERFLAG_ENDOFFRAME; buffer->nOffset = 0; // Convert the timestamps to microseconds; some encoders can ignore // the framerate and do VFR bit allocation based on timestamps. buffer->nTimeStamp = to_omx_ticks(av_rescale_q(frame->pts, avctx->time_base, AV_TIME_BASE_Q)); err = OMX_EmptyThisBuffer(s->handle, buffer); if (err != OMX_ErrorNone) { append_buffer(&s->input_mutex, &s->input_cond, &s->num_free_in_buffers, s->free_in_buffers, buffer); av_log(avctx, AV_LOG_ERROR, "OMX_EmptyThisBuffer failed: %x\n", err); return AVERROR_UNKNOWN; } s->num_in_frames++; } while (!*got_packet && ret == 0) { // Only wait for output if flushing and not all frames have been output buffer = get_buffer(&s->output_mutex, &s->output_cond, &s->num_done_out_buffers, s->done_out_buffers, !frame && s->num_out_frames < s->num_in_frames); if (!buffer) break; if (buffer->nFlags & OMX_BUFFERFLAG_CODECCONFIG && avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) { if ((ret = av_reallocp(&avctx->extradata, avctx->extradata_size + buffer->nFilledLen + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) { avctx->extradata_size = 0; goto end; } memcpy(avctx->extradata + avctx->extradata_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen); avctx->extradata_size += buffer->nFilledLen; memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); } else { if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) s->num_out_frames++; if (!(buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) || !pkt->data) { // If the output packet isn't preallocated, just concatenate everything in our // own buffer int newsize = s->output_buf_size + buffer->nFilledLen + AV_INPUT_BUFFER_PADDING_SIZE; if ((ret = av_reallocp(&s->output_buf, newsize)) < 0) { s->output_buf_size = 0; goto end; } memcpy(s->output_buf + s->output_buf_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen); s->output_buf_size += buffer->nFilledLen; if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) { if ((ret = av_packet_from_data(pkt, s->output_buf, s->output_buf_size)) < 0) { av_freep(&s->output_buf); s->output_buf_size = 0; goto end; } s->output_buf = NULL; s->output_buf_size = 0; } } else { // End of frame, and the caller provided a preallocated frame if ((ret = ff_alloc_packet2(avctx, pkt, s->output_buf_size + buffer->nFilledLen, 0)) < 0) { av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", (int)(s->output_buf_size + buffer->nFilledLen)); goto end; } memcpy(pkt->data, s->output_buf, s->output_buf_size); memcpy(pkt->data + s->output_buf_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen); av_freep(&s->output_buf); s->output_buf_size = 0; } if (buffer->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) { pkt->pts = av_rescale_q(from_omx_ticks(buffer->nTimeStamp), AV_TIME_BASE_Q, avctx->time_base); // We don't currently enable B-frames for the encoders, so set // pkt->dts = pkt->pts. (The calling code behaves worse if the encoder // doesn't set the dts). pkt->dts = pkt->pts; if (buffer->nFlags & OMX_BUFFERFLAG_SYNCFRAME) pkt->flags |= AV_PKT_FLAG_KEY; *got_packet = 1; } } end: err = OMX_FillThisBuffer(s->handle, buffer); if (err != OMX_ErrorNone) { append_buffer(&s->output_mutex, &s->output_cond, &s->num_done_out_buffers, s->done_out_buffers, buffer); av_log(avctx, AV_LOG_ERROR, "OMX_FillThisBuffer failed: %x\n", err); ret = AVERROR_UNKNOWN; } } return ret; }
static av_cold int omx_encode_init(AVCodecContext *avctx) { OMXCodecContext *s = avctx->priv_data; int ret = AVERROR_ENCODER_NOT_FOUND; const char *role; OMX_BUFFERHEADERTYPE *buffer; OMX_ERRORTYPE err; #if CONFIG_OMX_RPI s->input_zerocopy = 1; #endif s->omx_context = omx_init(avctx, s->libname, s->libprefix); if (!s->omx_context) return AVERROR_ENCODER_NOT_FOUND; pthread_mutex_init(&s->state_mutex, NULL); pthread_cond_init(&s->state_cond, NULL); pthread_mutex_init(&s->input_mutex, NULL); pthread_cond_init(&s->input_cond, NULL); pthread_mutex_init(&s->output_mutex, NULL); pthread_cond_init(&s->output_cond, NULL); s->mutex_cond_inited = 1; s->avctx = avctx; s->state = OMX_StateLoaded; s->error = OMX_ErrorNone; switch (avctx->codec->id) { case AV_CODEC_ID_MPEG4: role = "video_encoder.mpeg4"; break; case AV_CODEC_ID_H264: role = "video_encoder.avc"; break; default: return AVERROR(ENOSYS); } if ((ret = find_component(s->omx_context, avctx, role, s->component_name, sizeof(s->component_name))) < 0) goto fail; av_log(avctx, AV_LOG_INFO, "Using %s\n", s->component_name); if ((ret = omx_component_init(avctx, role)) < 0) goto fail; if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) { while (1) { buffer = get_buffer(&s->output_mutex, &s->output_cond, &s->num_done_out_buffers, s->done_out_buffers, 1); if (buffer->nFlags & OMX_BUFFERFLAG_CODECCONFIG) { if ((ret = av_reallocp(&avctx->extradata, avctx->extradata_size + buffer->nFilledLen + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) { avctx->extradata_size = 0; goto fail; } memcpy(avctx->extradata + avctx->extradata_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen); avctx->extradata_size += buffer->nFilledLen; memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); } err = OMX_FillThisBuffer(s->handle, buffer); if (err != OMX_ErrorNone) { append_buffer(&s->output_mutex, &s->output_cond, &s->num_done_out_buffers, s->done_out_buffers, buffer); av_log(avctx, AV_LOG_ERROR, "OMX_FillThisBuffer failed: %x\n", err); ret = AVERROR_UNKNOWN; goto fail; } if (avctx->codec->id == AV_CODEC_ID_H264) { // For H.264, the extradata can be returned in two separate buffers // (the videocore encoder on raspberry pi does this); // therefore check that we have got both SPS and PPS before continuing. int nals[32] = { 0 }; int i; for (i = 0; i + 4 < avctx->extradata_size; i++) { if (!avctx->extradata[i + 0] && !avctx->extradata[i + 1] && !avctx->extradata[i + 2] && avctx->extradata[i + 3] == 1) { nals[avctx->extradata[i + 4] & 0x1f]++; } } if (nals[H264_NAL_SPS] && nals[H264_NAL_PPS]) break; } else { if (avctx->extradata_size > 0) break; } } } return 0; fail: return ret; }
/** * soup_message_body_complete: * @body: a #SoupMessageBody * * Tags @body as being complete; Call this when using chunked encoding * after you have appended the last chunk. **/ void soup_message_body_complete (SoupMessageBody *body) { append_buffer (body, soup_buffer_new (SOUP_MEMORY_STATIC, NULL, 0)); }
/** * Build and submit a TLS client hello handshake on the active * connection. It is up to the caller of this function to wait * for the server reply. */ static int send_client_hello( int connection, TLSParameters *parameters ) { ClientHello package; unsigned short supported_suites[ 1 ]; unsigned char supported_compression_methods[ 1 ]; int send_buffer_size; char *send_buffer; void *write_buffer; time_t local_time; int status = 1; package.client_version.major = TLS_VERSION_MAJOR; package.client_version.minor = TLS_VERSION_MINOR; time( &local_time ); package.random.gmt_unix_time = htonl( local_time ); // TODO - actually make this random. // This is 28 bytes, but client random is 32 - the first four bytes of // "client random" are the GMT unix time computed above. memcpy( parameters->client_random, &package.random.gmt_unix_time, 4 ); memcpy( package.random.random_bytes, parameters->client_random + 4, 28 ); package.session_id_length = 0; package.session_id = NULL; // note that this is bytes, not count. package.cipher_suites_length = htons( 2 ); supported_suites[ 0 ] = htons( TLS_RSA_WITH_3DES_EDE_CBC_SHA ); package.cipher_suites = supported_suites; package.compression_methods_length = 1; supported_compression_methods[ 0 ] = 0; package.compression_methods = supported_compression_methods; // Compute the size of the ClientHello message after flattening. send_buffer_size = sizeof( ProtocolVersion ) + sizeof( Random ) + sizeof( unsigned char ) + ( sizeof( unsigned char ) * package.session_id_length ) + sizeof( unsigned short ) + ( sizeof( unsigned short ) * 1 ) + sizeof( unsigned char ) + sizeof( unsigned char ); write_buffer = send_buffer = ( char * ) malloc( send_buffer_size ); write_buffer = append_buffer( write_buffer, ( void * ) &package.client_version.major, 1 ); write_buffer = append_buffer( write_buffer, ( void * ) &package.client_version.minor, 1 ); write_buffer = append_buffer( write_buffer, ( void * ) &package.random.gmt_unix_time, 4 ); write_buffer = append_buffer( write_buffer, ( void * ) &package.random.random_bytes, 28 ); write_buffer = append_buffer( write_buffer, ( void * ) &package.session_id_length, 1 ); if ( package.session_id_length > 0 ) { write_buffer = append_buffer( write_buffer, ( void * )package.session_id, package.session_id_length ); } write_buffer = append_buffer( write_buffer, ( void * ) &package.cipher_suites_length, 2 ); write_buffer = append_buffer( write_buffer, ( void * ) package.cipher_suites, 2 ); write_buffer = append_buffer( write_buffer, ( void * ) &package.compression_methods_length, 1 ); if ( package.compression_methods_length > 0 ) { write_buffer = append_buffer( write_buffer, ( void * ) package.compression_methods, 1 ); } assert( ( ( char * ) write_buffer - send_buffer ) == send_buffer_size ); status = send_handshake_message( connection, client_hello, send_buffer, send_buffer_size, parameters ); free( send_buffer ); return status; }
int main(int argc,char *argv[]) { unsigned int ans,max_buff_size=0,curr_buff_size=0; char *buffer=NULL,*ans_buffer=NULL, *new_filename=(char *)calloc(20,sizeof(char)), *temp=(char *)calloc(2000,sizeof(char)), *temp1=(char *)calloc(3,sizeof(char)); FILE *source_fp=NULL; char temp_char[2]="a"; if(argc<2 || argc>2) { printf("\nUSAGE : ./a.out Filename"); exit(0); } source_fp=fopen(argv[1],"r"); if(source_fp==NULL) { printf("\nERROR Opening File......!!!"); exit(1); } if(fseek(source_fp,0L,SEEK_END)==0) { max_buff_size=ftell(source_fp); if(max_buff_size==-1) { printf("\n ERROR Occured.....!!!"); exit(2); } } buffer=create_buffer(max_buff_size); if (fseek(source_fp, 0L, SEEK_SET) != 0) { printf("\n ERROR Occured......!!!"); exit(3); } while(feof(source_fp)==0) { fscanf(source_fp,"%s",temp); strcat(temp," \0"); curr_buff_size+=append_buffer(buffer,temp,curr_buff_size,max_buff_size); if(strstr(temp,"//")!=NULL || strstr(temp,"\"")!=NULL || strstr(temp,"\'")!=NULL) { do{ fscanf(source_fp,"%c",&temp_char[0]); strcpy(temp1,temp_char+0); curr_buff_size+=append_buffer(buffer,temp1,curr_buff_size,max_buff_size); }while(temp_char[0] != '\n'); } free(temp); temp=(char *)calloc(2000,sizeof(char)); } fclose(source_fp); source_fp=NULL; ans_buffer=cmp_token(buffer); printf("\n\n\t 1.Create a new file \n\n\t 2.Change existing file \n\n\t\t Enter Your choice :"); scanf("%d",&ans); switch(ans) { case 1: printf("\n\n\t\t Enter New File Name : "); scanf("%s",new_filename); break; case 2: new_filename=argv[1]; break; } source_fp=fopen(new_filename,"w"); fwrite(ans_buffer,1,strlen(ans_buffer),source_fp); printf("\n File Created......!!!"); return 0; }
/* * Main function */ int main(int argc, char** argv) { struct timeval t_out; int i, iflag, hflag, nflag, pflag; server_stat status; char *port; char usage[100]; snprintf(usage, 100, "Usage: %s -i <robot_id> -n <robot-name> -h <http_hostname> -p <udp_port>", argv[0]); status.r_stat.hostname[0] = '\0'; if (argc != 9) { fprintf(stderr, "%s\n", usage); return -1; } // read in the required arguments iflag = hflag = pflag = 0; for (i = 1; i < argc; i+=2) { if (strcmp(argv[i], "-n") == 0) { status.r_stat.id = atoi(argv[i + 1]); iflag = 1; } else if (strcmp(argv[i], "-h") == 0) { strncpy(status.r_stat.hostname, argv[i + 1], BUFFER_LEN - 1); hflag = 1; } else if (strcmp(argv[i], "-p") == 0) { port = argv[i + 1]; pflag = 1; } else if (strcmp(argv[i], "-i") == 0) { status.r_stat.name = argv[i + 1]; nflag = 1; }else { fprintf(stderr, "%s\n", usage); return -1; } } if (!(iflag && hflag && nflag && pflag)) { fprintf(stderr, "%s\n", usage); return -1; } // set up the udp socket status.udp_sock = udp_server(port); // timeouts t_out.tv_sec = 60; t_out.tv_usec = 0; timeout_setup(status.udp_sock, t_out); // execution loop srand(time(NULL)); status.size = sizeof(status.cliaddr); status.password = rand() + 1; status.connected = 0; buffer* recv_buf = create_buffer(BUFFER_LEN); for (;;) { timeout_setup(status.udp_sock, t_out); unsigned char temp[BUFFER_LEN]; memset(temp, '\0', BUFFER_LEN); int f = 10000; if ((f = recvfrom(status.udp_sock, temp, BUFFER_LEN, 0, (struct sockaddr *)(&status.cliaddr), &status.size)) < 0) { if (status.connected != 0) { buffer* quit_buf = create_message(0, status.password, QUIT, 0, 0, 0, 0); quit(quit_buf, &status); fprintf(stdout, "Waiting for a connection\n"); } } else { // decipher message clear_buffer(recv_buf); append_buffer(recv_buf, temp, f); header msg_header = extract_header(recv_buf); fprintf(stdout, "\nMessage received\n"); fprintf(stdout, "\tMessage size: %d\n", f); // send to correct protocol void (*protocol_func)(buffer*, server_stat*); protocol_func = check_version(&msg_header) ? &protocol1:&protocol2; protocol_func(recv_buf, &status); } if (status.connected != 0) { fprintf(stdout, "Waiting for a message\n"); } fflush(stdout); fflush(stderr); } return 0; }