/**@ingroup tsk_buffer_group * Removes a chunck of data from the buffer. * @param self The buffer from which to remove the chunck. * @param position The chunck start position. * @param size The size of the chunck. * @retval Zero if succeed and non-zero error code otherwise. */ int tsk_buffer_remove(tsk_buffer_t* self, tsk_size_t position, tsk_size_t size) { if(self && self->data && size){ if((position == 0) && ((position + size) >= self->size)){ /* Very common case. */ return tsk_buffer_cleanup(self); } else if((position + size) < self->size){ memcpy(((uint8_t*)self->data) + position, ((uint8_t*)self->data) + position + size, self->size-(position+size)); return tsk_buffer_realloc(self, (self->size-size)); } } return -1; }
int tmsrp_data_in_put(tmsrp_data_in_t* self, const void* pdata, tsk_size_t size) { int ret = -1; if(!self || !self->buffer || !pdata || !size){ TSK_DEBUG_ERROR("Invalid parameter"); return ret; } if((ret = tsk_buffer_append(self->buffer, pdata, size))){ TSK_DEBUG_ERROR("Failed to append data"); tsk_buffer_cleanup(self->buffer); return ret; } else{ if(TSK_BUFFER_SIZE(self->buffer) > TMSRP_DATA_IN_MAX_BUFFER){ tsk_buffer_cleanup(self->buffer); TSK_DEBUG_ERROR("Too many bytes are waiting."); return -3; } } return ret; }
/**@ingroup tsk_buffer_group * Reallocates the buffer. * @param self The buffer to realloc. * @param size The new size. * @retval Zero if succeed and non-zero error code otherwise. */ int tsk_buffer_realloc(tsk_buffer_t* self, tsk_size_t size) { if(self) { if(size == 0){ return tsk_buffer_cleanup(self); } if(self->size == 0){ // first time? self->data = tsk_calloc(size, sizeof(uint8_t)); } else if(self->size != size){ // only realloc if different sizes self->data = tsk_realloc(self->data, size); } self->size = size; return 0; } return -1; }
int tsk_hmac_xxxcompute(const uint8_t* input, tsk_size_t input_size, const char* key, tsk_size_t key_size, tsk_hash_type_t type, uint8_t* digest) { #define TSK_MAX_BLOCK_SIZE TSK_SHA1_BLOCK_SIZE tsk_size_t i, newkey_size; tsk_size_t block_size = type == md5 ? TSK_MD5_BLOCK_SIZE : TSK_SHA1_BLOCK_SIZE; // Only SHA-1 and MD5 are supported for now tsk_size_t digest_size = type == md5 ? TSK_MD5_DIGEST_SIZE : TSK_SHA1_DIGEST_SIZE; char hkey [TSK_MAX_BLOCK_SIZE]; uint8_t ipad [TSK_MAX_BLOCK_SIZE]; uint8_t opad [TSK_MAX_BLOCK_SIZE]; memset(ipad, 0, sizeof(ipad)); memset(opad, 0, sizeof(ipad)); /* * H(K XOR opad, H(K XOR ipad, input)) */ // Check key len if (key_size > block_size){ if(type == md5){ TSK_MD5_DIGEST_CALC(key, key_size, (uint8_t*)hkey); } else if(type == sha1){ TSK_SHA1_DIGEST_CALC((uint8_t*)key, (unsigned int)key_size, (uint8_t*)hkey); } else return -3; newkey_size = digest_size; } else{ memcpy(hkey, key, key_size); newkey_size = key_size; } memcpy(ipad, hkey, newkey_size); memcpy(opad, hkey, newkey_size); /* [K XOR ipad] and [K XOR opad]*/ for (i=0; i<block_size; i++){ ipad[i] ^= 0x36; opad[i] ^= 0x5c; } { tsk_buffer_t *passx; // pass1 or pass2 int pass1_done = 0; passx = tsk_buffer_create(ipad, block_size); // pass1 tsk_buffer_append(passx, input, input_size); digest_compute: if(type == md5){ TSK_MD5_DIGEST_CALC(TSK_BUFFER_TO_U8(passx), (unsigned int)TSK_BUFFER_SIZE(passx), digest); } else{ TSK_SHA1_DIGEST_CALC(TSK_BUFFER_TO_U8(passx), (unsigned int)TSK_BUFFER_SIZE(passx), digest); } if(pass1_done){ TSK_OBJECT_SAFE_FREE(passx); goto pass1_and_pass2_done; } else{ pass1_done = 1; } tsk_buffer_cleanup(passx); tsk_buffer_append(passx, opad, block_size); // pass2 tsk_buffer_append(passx, digest, digest_size); goto digest_compute; } pass1_and_pass2_done: return 0; }
int tmsrp_receiver_recv(tmsrp_receiver_t* self, const void* data, tsk_size_t size) { tmsrp_message_t* message; if(!self || !data || !size){ TSK_DEBUG_ERROR("Invalid parameter"); return -1; } // put the data tmsrp_data_in_put(self->data_in, data, size); // get msrp messages while((message = tmsrp_data_in_get(self->data_in))){ /* alert that we have received a message (Request or Response) */ _tmsrp_receiver_alert_user(self, tsk_false, message); // // REQUEST // if(TMSRP_MESSAGE_IS_REQUEST(message)){ /* ============= SEND =============== */ if(TMSRP_REQUEST_IS_SEND(message)){ tmsrp_response_t* r2xx; tmsrp_request_t* REPORT; // send 200 OK if((r2xx = tmsrp_create_response(message, 200, "OK"))){ if(tmsrp_message_serialize(r2xx, self->buffer) == 0 && self->buffer->data){ tnet_sockfd_send(self->fd, self->buffer->data, self->buffer->size, 0); } tsk_buffer_cleanup(self->buffer); TSK_OBJECT_SAFE_FREE(r2xx); } // send REPORT if(tmsrp_isReportRequired(message, tsk_false)){ if((REPORT = tmsrp_create_report(message, 200, "OK"))){ if(tmsrp_message_serialize(REPORT, self->buffer) == 0 && self->buffer->data){ tnet_sockfd_send(self->fd, self->buffer->data, self->buffer->size, 0); } tsk_buffer_cleanup(self->buffer); TSK_OBJECT_SAFE_FREE(REPORT); } } } /* ============= REPORT =============== */ if(TMSRP_REQUEST_IS_REPORT(message)){ tmsrp_response_t* r2xx; // send 200 OK if((r2xx = tmsrp_create_response(message, 200, "Report received"))){ if(tmsrp_message_serialize(r2xx, self->buffer) == 0 && self->buffer->data){ tnet_sockfd_send(self->fd, self->buffer->data, self->buffer->size, 0); } tsk_buffer_cleanup(self->buffer); TSK_OBJECT_SAFE_FREE(r2xx); } } /* ============= AUTH =============== */ /* ============= METHOD =============== */ } // // RESPONSE // else{ //short code = TMSRP_RESPONSE_CODE(message); //TSK_DEBUG_INFO("code=%u, tid=%s, phrase=%s", code, message->tid, TMSRP_RESPONSE_PHRASE(message)); } // alert user layer TSK_OBJECT_SAFE_FREE(message); } return 0; }
/* === entry point === */ int main(int argc, char** argv) { char cmdbuf[4096]; tsk_buffer_t* buffer = tsk_null; cmd_t* cmd = tsk_null; tsk_bool_t comment = tsk_false; int ret; int i, index; const char* start = tsk_null, *end = tsk_null; int a = 32 | 1 | 2; /* Copyright */ printf("Doubango Project (tinyDEMO)\nCopyright (C) 2009 - 2013 Mamadou Diop \n\n"); /* Initialize Network Layer ==> Mandatory */ tnet_startup(); /* Initialize Doubango Audio/Video Framework ==> will register all plugins(codecs and sessions) * Not mandatory if you have your own plugins*/ tdav_init(); /* Print Usage */ //cmd_print_help(); /* create user's ctx */ if(!(ctx = ctx_create()) || !ctx->stack){ TSK_DEBUG_ERROR("Failed to create user's ctx."); goto bail; } /* create new buffer */ if(!(buffer = tsk_buffer_create_null())){ TSK_DEBUG_ERROR("Failed to create new buffer."); goto bail; } /* initial args */ for(i=1 /* index zero contains the exe path */, index=0; i<argc && argv[i]; i++){ if(index){ tsk_buffer_append(buffer, " ", 1); } tsk_buffer_append(buffer, argv[i], tsk_strlen(argv[i])); } /* If initial args ==> parse it now */ if(buffer->size){ TSK_DEBUG_INFO("Initial command-line: %s", buffer->data); goto init_buffer; } /* always use fgets() instead of gets. gets() is considered to be unsafe.(Android and Mac OS X will warn) */ while(fgets(cmdbuf, sizeof(cmdbuf), stdin)){ TSK_DEBUG_INFO("Command-Line: %s", cmdbuf); tsk_buffer_cleanup(buffer); /* cannot read from console while executing scenario */ tsk_buffer_append(buffer, cmdbuf, tsk_strlen(cmdbuf)); init_buffer: start = buffer->data; //start = trim(start); end = start + buffer->size; if(start >= end){ TSK_DEBUG_INFO("Empty buffer"); continue; } parse_buffer: TSK_OBJECT_SAFE_FREE(cmd); /* Free old value */ cmd = cmd_parse(start, (end-start), &comment, ctx->params); if(cmd){ if(comment || cmd->type == cmd_none){ goto nex_line; } } else{ continue; } /* Load from scenario file? */ if(cmd->type == cmd_scenario){ FILE* file; const opt_t* opt; tsk_size_t read = 0; tsk_bool_t rm_lf = tsk_false; if((opt = opt_get_by_type(cmd->opts, opt_path)) && !tsk_strnullORempty(opt->value)){ /* --path option */ if((file = fopen(opt->value, "r"))){ memset(cmdbuf, '\0', sizeof(cmdbuf)), cmdbuf[0] = '\n'; read = fread(cmdbuf+1, sizeof(uint8_t), sizeof(cmdbuf)-1, file); fclose(file), file = tsk_null; if(read == 0){ TSK_DEBUG_ERROR("[%s] is empty.", opt->value); goto nex_line; } else if(read == sizeof(cmdbuf)-1){ TSK_DEBUG_ERROR("Buffer too short."); goto nex_line; } read++; /* \n */ /* repplace all '\' with spaces (easier than handling that in the ragel file) */ for(i=0; ((tsk_size_t)i)<read; i++){ if(cmdbuf[i] == '\\'){ cmdbuf[i] = ' '; rm_lf = tsk_true; } else if(rm_lf && cmdbuf[i] == '\n'){ cmdbuf[i] = ' '; rm_lf = tsk_false; } } cmdbuf[read] = '\n'; /* insert embedded scenario */ if((index = tsk_strindexOf(start, (end-start), "\n")) == -1){ /* ++sn line */ index = buffer->size; } else{ index += (start - ((const char*)buffer->data)); } if(tsk_buffer_insert(buffer, index, cmdbuf, read)){ continue; } else{ start = ((const char*)buffer->data) + index; // because insert use realloc() end = (((const char*)buffer->data) + buffer->size); goto nex_line; } } else{ TSK_DEBUG_ERROR("Failed to open scenario-file [%s].", opt->value); goto nex_line; } continue; } else{ TSK_DEBUG_ERROR("++scenario command must have --path option."); continue; } } /* execute current command */ switch(cmd->type){ case cmd_exit: TSK_DEBUG_INFO("Exit/Quit"); goto bail; default: ret = execute(cmd); break; } /* next line */ nex_line: if((index = tsk_strindexOf(start, (end - start), "\n")) !=-1){ start += index; while((start < end) && isspace(*start)){ start ++; } if((start + 2/*++*/) < end){ goto parse_buffer; /* next line */ } else{ continue; /* wait for new commands */ } } } /* while(buffer) */ bail: /* Free current command */ TSK_OBJECT_SAFE_FREE(cmd); /* Free buffer */ TSK_OBJECT_SAFE_FREE(buffer); /* Destroy the user's ctx */ TSK_OBJECT_SAFE_FREE(ctx); /* Deinitialize Doubango Audio/Video Framework ==> will unregister all plugins(codecs and sessions) * Not mandatory */ tdav_init(); /* Uninitilize Network Layer */ tnet_cleanup(); #if ANDROID exit(0); #endif return 0; }