int main(void) { struct taia taia; char stamp[TAIA_TAI64N + 1]; char ch; stamp[0] = '@'; for (;;) { if (buffer_get(buffer0, &ch, 1) != 1) { done(); goto FINISH; } taia_now(&taia); taia_tai64n(stamp + 1, &taia); stamp[sizeof(stamp) - 1] = ' '; buffer_put(buffer1, stamp, sizeof(stamp)); for (;;) { buffer_put(buffer1, &ch, 1); if (ch == '\n') break; if (buffer_get(buffer0, &ch, 1) != 1) { done(); goto FINISH; } } } FINISH: return 0; }
/** * Parses a Content-Length header value. */ int parse_header_content_length(struct parser *p) { char c, *buffer; int i, m, r; if (ready(p) < 3) return PARSING_WAIT; c = buffer_get(&(p->buffer), p->mark); for (m = 1; isdigit(c) || c == ' ' || c == '\t'; m++) { if (ready(p) < 3 + m) return PARSING_WAIT; c = buffer_get(&(p->buffer), p->mark + m); } buffer = buffer_copy(&(p->buffer), p->mark, m - 1); if (buffer == NULL) { p->error = E_MEMORY; return PARSING_ERROR; } advance_mark(p, m - 1); r = parse_constant(p, CRLF, 2); if (r != PARSING_DONE) { free(buffer); return r; } errno = 0; p->request.content_length = strtol(buffer, NULL, 10); free(buffer); return (errno == 0) ? PARSING_DONE : PARSING_ERROR; }
main() { int pid; int wstat; char ch; sig_ignore(sig_pipe); pid = fork(); if (pid == -1) strerr_die2sys(111,"mconnect-io: fatal: ","unable to fork: "); if (!pid) { buffer_init(&bin,myread,0,inbuf,sizeof inbuf); buffer_init(&bout,write,7,outbuf,sizeof outbuf); while (buffer_get(&bin,&ch,1) == 1) { if (ch == '\n') buffer_put(&bout,"\r",1); buffer_put(&bout,&ch,1); } _exit(0); } buffer_init(&bin,myread,6,inbuf,sizeof inbuf); buffer_init(&bout,write,1,outbuf,sizeof outbuf); while (buffer_get(&bin,&ch,1) == 1) buffer_put(&bout,&ch,1); kill(pid,sig_term); wait_pid(&wstat,pid); _exit(0); }
static int trace_free (void *buf) { uintptr_t ptr = buffer_get(uintptr_t); uintptr_t caller = buffer_get(uintptr_t); unsigned long long time = buffer_get(unsigned long long); int res = memory_free(ptr, caller, time); assert_inner(!res, "memory_free"); return 0; }
static int trace_enter (void *buf) { uintptr_t func = buffer_get(uintptr_t); uintptr_t caller = buffer_get(uintptr_t); unsigned long long time = buffer_get(unsigned long long); int res = function_enter(func, caller, time); assert_inner(!res, "function_enter"); return 0; }
static int trace_malloc (void *buf) { size_t size = buffer_get(size_t); uintptr_t caller = buffer_get(uintptr_t); uintptr_t result = buffer_get(uintptr_t); unsigned long long time = buffer_get(unsigned long long); int res = memory_malloc(size, caller, result, time); assert_inner(!res, "memory_malloc"); return 0; }
void CAN_start_transmission_internal(volatile can_stream_data_t* data) { int i=0; while ((buffer_bytes_available((buffer_t*)&data->byte_buffer)>0) && i<8) { data->can_msg.data.u8[i]=buffer_get((buffer_t*)&data->byte_buffer); i++; } if (i==0) { // nothing left to transmit data->transmission_active=false; return; } /* if (data->can_channel==0) { can_bus_init(data->can_channel, ((uint32_t)&mob_ram_ch0[0]), CANIF_CHANNEL_MODE_NORMAL, can_callback0); } else { can_bus_init(data->can_channel, ((uint32_t)&mob_ram_ch1[0]), CANIF_CHANNEL_MODE_NORMAL, can_callback1); } */ data->can_mob.handle=CAN_TX_MOB; data->can_mob.dlc=i; data->can_msg.id=data->id; data->can_msg.id_mask = data->id_mask; data->can_mob.req_type=CAN_DATA_FRAME; data->transmission_active=true; can_bus_tx(data->can_channel, data->can_mob.handle, data->can_mob.dlc, data->can_mob.req_type, data->can_mob.can_msg); }
void mkinvalid_secret(PCPCTX *ptx, pcp_key_t *k, int type) { pcp_key_t *key = ucmalloc(sizeof(pcp_key_t)); memcpy(key, k, sizeof(pcp_key_t)); FILE *fd = NULL; fprintf(stderr, "fd test %d\n", type); switch(type) { case 0: key->version = 0; fd = F("testkey-wrong-version"); break; case 1: key->serial = 0; fd = F("testkey-wrong-serial"); break; case 2: key->ctime = 0; fd = F("testkey-invalid-ctime"); break; } if(fd != NULL) { pcp_dumpkey(key); Buffer *b = pcp_export_secret(ptx, key, "xxx"); fwrite(buffer_get(b), 1, buffer_size(b), fd); fclose(fd); } else { fprintf(stderr, "fd not opened for test %d\n", type); } free(key); }
void mkinvalid_public(PCPCTX *ptx, pcp_key_t *k, int type) { pcp_key_t *key = ucmalloc(sizeof(pcp_key_t)); memcpy(key, k, sizeof(pcp_key_t)); FILE *fd = NULL; switch(type) { case 0: key->type = 0; fd = F("testpubkey-wrong-type"); break; case 1: key->version = 0; fd = F("testpubkey-wrong-version"); break; case 2: key->serial = 0; fd = F("testpubkey-wrong-serial"); break; case 3: key->ctime = 0; fd = F("testpubkey-invalid-ctime"); break; } if(fd != NULL) { Buffer *b = pcp_export_rfc_pub(ptx, key); fwrite(buffer_get(b), 1, buffer_size(b), fd); fclose(fd); } free(key); }
/* Find section header based on ignored section name */ static Elf64_Shdr *find_ignored_section_header(struct parsed_elf *pelf, const char *ignore_section) { int i; const char *shstrtab; /* No section needs to be ignored */ if (ignore_section == NULL) return NULL; DEBUG("Section to be ignored: %s\n", ignore_section); /* Get pointer to string table */ shstrtab = buffer_get(pelf->strtabs[pelf->ehdr.e_shstrndx]); for (i = 0; i < pelf->ehdr.e_shnum; i++) { Elf64_Shdr *shdr; const char *section_name; shdr = &pelf->shdr[i]; section_name = &shstrtab[shdr->sh_name]; /* If section name matches ignored string, return shdr */ if (strcmp(section_name, ignore_section) == 0) return shdr; } /* No section matches ignore string */ return NULL; }
static void* consumer_operation() { set_signal_mask(); while(status_on) { if(pthread_mutex_lock(&buffer_lock_mtx)!=0) print_log(WARNING,"\nthere is error in lock cons"); while(is_buffer_empty()) { if(pthread_cond_wait(&buffer_not_empty_cond,&buffer_lock_mtx)!=0) print_log(WARNING,"\nthere is error in wait cons"); } int peer_sfd = buffer_get(); if(peer_sfd < 0) { print_log(WARNING,"\nRECIEVED FAILED ACCEPT SOCKET"); pthread_mutex_unlock(&buffer_lock_mtx)!=0; continue; } if(pthread_cond_broadcast(&buffer_not_full_cond)!=0) print_log(WARNING,"\nthere is error in broadcast cons"); if(pthread_mutex_unlock(&buffer_lock_mtx)!=0) print_log(WARNING,"\nthere is error in unlock cons"); manage_single_request(peer_sfd); close(peer_sfd); } pthread_exit(pthread_self); }
void doit() { char ch; int match = 1; long linenum = 0; stralloc fifoline = { 0 }; linenum = 0; /* try duming data in 23 seconds */ alarm(dumpfreq); buffer_putsflush(buffer_2, ARGV0 "entering main loop\n"); /* forever read from pipe line by line and handle it */ while(1) { while(match) { if(flagdumpasap == 1) dumpcheck(flagdumpasap, flagchanged, flagchildrunning, flagsighup); ++linenum; if(buffer_get(&wr, &ch, 1) == 1) { if(getln(&wr, &fifoline, &match, '\n') == -1) continue; buffer_put(buffer_2, &ch, 1); buffer_putflush(buffer_2, fifoline.s, fifoline.len); handle_line(&fifoline, ch); } } } }
/** * Parses a header value. Deprecated header line folding is not supported. */ int parse_header_value(struct parser *p) { char c; if (ready(p) < 3) return PARSING_WAIT; c = buffer_get(&(p->buffer), p->mark); while (isgraph(c) || c == ' ' || c == '\t') { advance_mark(p, 1); if (ready(p) < 3) return PARSING_WAIT; c = buffer_get(&(p->buffer), p->mark); } return parse_constant(p, CRLF, 2); }
/** * @internal * * @brief Verify the hmac of a packet * * @param session The session to use. * @param buffer The buffer to verify the hmac from. * @param mac The mac to compare with the hmac. * * @return 0 if hmac and mac are equal, < 0 if not or an error * occured. */ int packet_hmac_verify(SSH_SESSION *session, BUFFER *buffer, unsigned char *mac) { unsigned char hmacbuf[EVP_MAX_MD_SIZE] = {0}; HMACCTX ctx; unsigned int len; u32 seq; ctx = hmac_init(session->current_crypto->decryptMAC, 20, HMAC_SHA1); if (ctx == NULL) { return -1; } seq = htonl(session->recv_seq); hmac_update(ctx, (unsigned char *) &seq, sizeof(u32)); hmac_update(ctx, buffer_get(buffer), buffer_get_len(buffer)); hmac_final(ctx, hmacbuf, &len); #ifdef DEBUG_CRYPTO ssh_print_hexa("received mac",mac,len); ssh_print_hexa("Computed mac",hmacbuf,len); ssh_print_hexa("seq",(unsigned char *)&seq,sizeof(u32)); #endif if (memcmp(mac, hmacbuf, len) == 0) { return 0; } return -1; }
static int next (unsigned char *c, void *p) { register int r = buffer_get((buffer *)p, (char *)c, 1) ; if (r < 0) return 0 ; if (!r) *c = 0 ; return 1 ; }
/* this is a public key in openssh's format */ static STRING *make_rsa1_string(STRING *e, STRING *n){ BUFFER *buffer = NULL; STRING *rsa = NULL; STRING *ret = NULL; buffer = buffer_new(); rsa = string_from_char("ssh-rsa1"); if (buffer_add_ssh_string(buffer, rsa) < 0) { goto error; } if (buffer_add_ssh_string(buffer, e) < 0) { goto error; } if (buffer_add_ssh_string(buffer, n) < 0) { goto error; } ret = string_new(buffer_get_len(buffer)); if (ret == NULL) { goto error; } string_fill(ret, buffer_get(buffer), buffer_get_len(buffer)); error: buffer_free(buffer); string_free(rsa); return ret; }
static void *ihy_playing(void *data) { /* contains uncompressed data, (ihy_buffer_content *) */ t_buffer buffer = ((struct ihy_streaming_data *)data)->buffer; ihy_data *ihy = ((struct ihy_streaming_data *)data)->ihy; ao_device *audio_device; struct ihy_buffer_content *to_play; audio_device = ao_init_device(16, ihy->Channels, ihy->Frequency); /* we need to ensure that the other thread has added, * at least, 1 element */ while (buffer_isempty(buffer)) usleep(50000); while (!buffer_isempty(buffer)) { to_play = buffer_get(buffer); ao_play_samples(audio_device, to_play->samples, to_play->samplesSize); free(to_play->samples); free(to_play); /* hum quite dangerous */ } ao_close_device(audio_device); return NULL; }
int main(void) { clock_setup(); gpio_setup(); usart_setup(); spi_setup(); buffer_init(send_buffer,BUFFER_SIZE); buffer_init(receive_buffer,BUFFER_SIZE); usart_enable_tx_interrupt(USART1); /* Send a greeting message on USART1. */ usart_print_string("SD Card SPI Mode Test\r\n"); while (1) { /* Command interface */ if (buffer_input_available(receive_buffer)) { char character = buffer_get(receive_buffer); if (character == 0x0D) { line[characterPosition] = 0; characterPosition = 0; parseCommand(line); } else line[characterPosition++] = character; } } return 0; }
static int doit(stralloc *out,int s,char ipremote[16],uint16 portremote,char iplocal[16],uint16 portlocal,unsigned int timeout,uint32 netif) { buffer b; char bspace[128]; char strnum[FMT_ULONG]; int numcolons; char ch; if (socket_bind6(s,iplocal,0,netif) == -1) return -1; if (timeoutconn6(s,ipremote,113,timeout,netif) == -1) return -1; buffer_init(&b,mywrite,s,bspace,sizeof bspace); buffer_put(&b,strnum,fmt_ulong(strnum,portremote)); buffer_put(&b," , ",3); buffer_put(&b,strnum,fmt_ulong(strnum,portlocal)); buffer_put(&b,"\r\n",2); if (buffer_flush(&b) == -1) return -1; buffer_init(&b,myread,s,bspace,sizeof bspace); numcolons = 0; for (;;) { if (buffer_get(&b,&ch,1) != 1) return -1; if ((ch == ' ') || (ch == '\t') || (ch == '\r')) continue; if (ch == '\n') return 0; if (numcolons < 3) { if (ch == ':') ++numcolons; } else { if (!stralloc_append(out,&ch)) return -1; if (out->len > 256) return 0; } } }
s08 buffer_getLine (buffer_struct *rb, char *s ) { // Devuelve lineas enteras del buffer. // El s debe tener capacidad suficiente. s08 res = FALSE; char cChar; if ( buffer_empty ( &xUart0RxedCharsBuffer )) { // No hay datos. res = FALSE; } else { // Copio vaciando el ringBuffer hasta encontrar un CR. while(1) { cChar = buffer_get(&xUart0RxedCharsBuffer); *(s++) = cChar; if ( (cChar == '\n') || (cChar == NULL)) break; } *(s++) = '\0'; res = TRUE; } return (res); }
int main(void) { initStartup(); buffer_put(&SerialData0.rx, 0); // sleep this! while (1) { if (!buffer_isempty(&SerialData0.rx)) { //if you've received some input then save it to the input if (uart0_peek_int() != input) { prevInput = input; input = uart0_fgetchar_int(&uart_str); } else buffer_get(&SerialData0.rx); // remove from buffer! } if (prevInput != input) { if (input != 0) { // if the hosts requests a readout if ((input & 0b00001000) == 0) //if you've not requested the rear pad then disable mux and it's decoder input PORTL = 0x00; if (firstOffRx == 0) {// if you were in sleep then get out ()might need to change this sleepSystemWake(); firstOffRx = 1; } uart0_fputchar_int(MAIN_PACKET_END_BYTE,&uart_str); //send the start packet id for (int i = 0; i < 5; i++) { // rx all requested sensors uint8_t tempInput = (input & (0b00000001 << i)); if (tempInput != 0) helperRetrieveSensor(tempInput); } } else if (firstOffRx == 1) // sleep here once and only once sleepSystem(); } } return 0; }
static int asn1_check_sequence(ssh_buffer buffer) { unsigned char *j = NULL; unsigned char tmp; int i; uint32_t size; uint32_t padding; if (buffer_get_data(buffer, &tmp, 1) == 0 || tmp != ASN1_SEQUENCE) { return 0; } size = asn1_get_len(buffer); if ((padding = buffer_get_len(buffer) - buffer->pos - size) > 0) { for (i = buffer_get_len(buffer) - buffer->pos - size, j = (unsigned char*)buffer_get(buffer) + size + buffer->pos; i; i--, j++) { if (*j != padding) { /* padding is allowed */ return 0; /* but nothing else */ } } } return 1; }
static void handle_username(client *c) { const char *buf; buf = buffer_get(c->buffer); character_set_username(c->ch, buf); }
void usart1_isr(void) { u8 ch; //if Receive interrupt if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART1) & USART_SR_RXNE) != 0)) { ch=usart_recv(USART1); buffer_put(&u1rx, ch); } if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) && ((USART_SR(USART1) & USART_SR_TXE) != 0)) { if (buffer_get(&u1tx, &ch) == SUCCESS) { //if char read from buffer usart_send(USART1, ch); } else //if buffer empty { //disable Transmit Data Register empty interrupt usart_disable_tx_interrupt(USART1); } } }
/* Export key state after authentication */ Newkeys * mm_newkeys_from_blob(u_char *blob, int blen) { Buffer b; u_int len; Newkeys *newkey = NULL; Enc *enc; Mac *mac; Comp *comp; debug3("%s: %p(%d)", __func__, blob, blen); #ifdef DEBUG_PK dump_base64(stderr, blob, blen); #endif buffer_init(&b); buffer_append(&b, blob, blen); newkey = xmalloc(sizeof(*newkey)); enc = &newkey->enc; mac = &newkey->mac; comp = &newkey->comp; /* Enc structure */ enc->name = buffer_get_string(&b, NULL); buffer_get(&b, &enc->cipher, sizeof(enc->cipher)); enc->enabled = buffer_get_int(&b); enc->block_size = buffer_get_int(&b); enc->key = buffer_get_string(&b, &enc->key_len); enc->iv = buffer_get_string(&b, &len); if (len != enc->block_size) fatal("%s: bad ivlen: expected %u != %u", __func__, enc->block_size, len); if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher) fatal("%s: bad cipher name %s or pointer %p", __func__, enc->name, enc->cipher); /* Mac structure */ mac->name = buffer_get_string(&b, NULL); if (mac->name == NULL || mac_setup(mac, mac->name) == -1) fatal("%s: can not setup mac %s", __func__, mac->name); mac->enabled = buffer_get_int(&b); mac->key = buffer_get_string(&b, &len); if (len > mac->key_len) fatal("%s: bad mac key length: %u > %d", __func__, len, mac->key_len); mac->key_len = len; /* Comp structure */ comp->type = buffer_get_int(&b); comp->enabled = buffer_get_int(&b); comp->name = buffer_get_string(&b, NULL); len = buffer_len(&b); if (len != 0) error("newkeys_from_blob: remaining bytes in blob %u", len); buffer_free(&b); return (newkey); }
/****************************************************************************************** * Function to send a command to a GSM modem and to check response * * Passed Parameters: * Pointer to command string * Pointer to response buffer * Size of response buffer * Modem timeout time in seconds * * Return Value: * 0: OK response received from modem * -1: ERROR: response received from MODEM * -2: Modem timed out *******************************************************************************************/ int send_command(char * command_ptr, char * response_ptr, int size, int wait_time) { volatile int time; char CR_count=0; int x, termination_char_received = 0, command_received=0; char c; char buff[20]; char rx_count = 0; char *temp_ptr = response_ptr; PRINTF("HERE"); for(x=0;x<size;x++) //clear response buffer *temp_ptr++ = 0; for(x=0;x<20;x++) buff[x] = 0; //send command to serial port while(*command_ptr != '\0') { PUTCHAR(*command_ptr++); } //wait for a command response or a timeout time = tick_count; x=0; while(!command_received &&(tick_count - time < wait_time)) { c = buffer_get(); if(c) //buffer not empty { *response_ptr++ = c; rx_count++; if(!termination_char_received) { if(strstr(response_ptr - rx_count, "OK") || strstr(response_ptr - rx_count, "ERROR:")) { PRINTF("\nOK or ERROR received\n"); termination_char_received = 1; } } else if(c == '\r') { command_received = 1; //printf("\nResponse Received: \n"); } } } if(command_received) { if(strstr(response_ptr - rx_count, "OK")) return SUCCESS; else if(strstr(response_ptr - rx_count, "ERROR:")) return ERROR; } else { PRINTF("\nTimeout on serial response\n"); return FAIL; } }
static int privatekey_decrypt(int algo, int mode, unsigned int key_len, unsigned char *iv, unsigned int iv_len, ssh_buffer data, ssh_auth_callback cb, void *userdata, const char *desc) { char passphrase[MAX_PASSPHRASE_SIZE] = {0}; unsigned char key[MAX_KEY_SIZE] = {0}; unsigned char *tmp = NULL; gcry_cipher_hd_t cipher; int rc = -1; if (!algo) { return -1; } if (cb) { rc = (*cb)(desc, passphrase, MAX_PASSPHRASE_SIZE, 0, 0, userdata); if (rc < 0) { return -1; } } else if (cb == NULL && userdata != NULL) { snprintf(passphrase, MAX_PASSPHRASE_SIZE, "%s", (char *) userdata); } if (passphrase_to_key(passphrase, strlen(passphrase), iv, key, key_len) < 0) { return -1; } if (gcry_cipher_open(&cipher, algo, mode, 0) || gcry_cipher_setkey(cipher, key, key_len) || gcry_cipher_setiv(cipher, iv, iv_len) || (tmp = malloc(buffer_get_len(data) * sizeof (char))) == NULL || gcry_cipher_decrypt(cipher, tmp, buffer_get_len(data), buffer_get(data), buffer_get_len(data))) { gcry_cipher_close(cipher); return -1; } memcpy(buffer_get(data), tmp, buffer_get_len(data)); SAFE_FREE(tmp); gcry_cipher_close(cipher); return 0; }
int send_sms(char * number, char * message) { char sms_send_command[40] = "AT+CMGS="; char * ptr; volatile int time = 0; char response_received = 0; char c; char response[20]; int result = 0; char char_recived = 0; strcat(sms_send_command, number); strcat(sms_send_command, "\r"); ptr = sms_send_command; while(*ptr != '\0') { PUTCHAR(*ptr); *ptr++; } time = tick_count; while( (char_recived == 0) && ( tick_count - time ) < 5 ) { c = buffer_get(); if(c) { char_recived = 1; } } if((tick_count - time) >=5) //buffer not empty { //printf("\nTime out\n"); return FAIL; } else { if(c == '>') { char_recived = 1; memset(sms_send_command,'\0',sizeof(sms_send_command)); strcat(sms_send_command, message); strcat(sms_send_command, "\x1A"); ptr = sms_send_command; result = send_command(sms_send_command, response, sizeof(response), 20); if(result == SUCCESS || result == ERROR) { return result; } } else { printf("\n Major Error rebooting . . .\n"); return ERROR; } } }
void test(buffer_t *buf, mark_t *cur) { char *data; bint_t data_len; mark_move_beginning(cur); mark_insert_before(cur, "s", 1); buffer_get(buf, &data, &data_len); ASSERT("insb", 0, strncmp("shello\nworld", data, data_len)); ASSERT("col", 1, cur->col); }
s08 xUartGetChar(u08 nUart, u08 *pcRxedChar,u08 xBlockTime ) { /* Es la funcion que usamos para leer los caracteres recibidos. * Los programas la usan bloqueandose hasta recibir un dato. */ /* Get the next character from the buffer. Return false if no characters are available, or arrive before xBlockTime expires. */ s08 res = FALSE; switch (nUart) { case 0: if (bufferType == RINGBUFFER ) { if ( xSemaphoreTake ( sem_GprsUart, xBlockTime ) ) { if ( buffer_empty ( &xUart0RxedCharsBuffer )) { res = FALSE; } else { *pcRxedChar = buffer_get(&xUart0RxedCharsBuffer); res = TRUE; } xSemaphoreGive( sem_GprsUart); } } else { if ( buffer_empty ( &xUart0RxedCharsBuffer )) { res = FALSE; } else { *pcRxedChar = buffer_get(&xUart0RxedCharsBuffer); res = TRUE; } } return (res); break; case 1: if( xQueueReceive( xUart1RxedCharsQueue, pcRxedChar, xBlockTime ) ) { return pdTRUE; } else { return pdFALSE; } break; } }