POINTER smalloc_malloc P1(size_t, size) #endif { /* int i; */ u *temp; DEBUG_CHECK(size == 0, "Malloc size 0.\n"); if (size > SMALL_BLOCK_MAX_BYTES) return large_malloc(size, 0); #if SIZEOF_PTR > SIZEOF_INT if (size < SIZEOF_PTR) size = SIZEOF_PTR; #endif size = (size + 7) & ~3; /* block size in bytes */ #define SIZE_INDEX(u_array, size) (*(u*) ((char*)u_array-8+size)) #define SIZE_PNT_INDEX(u_array, size) (*(u**)((char*)u_array-8+size)) /* i = (size - 8) >> 2; */ count_up(small_alloc_stat, size); SIZE_INDEX(small_count, size) += 1; /* update statistics */ SIZE_INDEX(small_total, size) += 1; if (SIZE_INDEX(small_count, size) > SIZE_INDEX(small_max, size)) SIZE_INDEX(small_max, size) = SIZE_INDEX(small_count, size); if (temp = SIZE_PNT_INDEX(sfltable, size)) { /* allocate from the * free list */ count_back(small_free_stat, size); temp++; SIZE_PNT_INDEX(sfltable, size) = *(u **) temp; fake("From free list."); return (char *) temp; } /* else allocate from the chunk */ if (unused_size < size) { /* no room in chunk, get another */ /* * don't waste this smaller block */ if (unused_size) { count_up(small_free_stat, unused_size); *s_size_ptr(next_unused) = unused_size >> 2; *s_next_ptr(next_unused) = SIZE_PNT_INDEX(sfltable, unused_size); SIZE_PNT_INDEX(sfltable, unused_size) = next_unused; } fake("Allocating new small chunk."); next_unused = (u *) large_malloc(SMALL_CHUNK_SIZE + SIZEOF_PTR, 1); if (next_unused == 0) return 0; *next_unused = (u) last_small_chunk; last_small_chunk = next_unused++; count_up(small_chunk_stat, SMALL_CHUNK_SIZE + SIZEOF_PTR); unused_size = SMALL_CHUNK_SIZE; } else
void *malloc(size_t size) { void *ptr; if (size <= TINY_N) ptr = tiny_malloc(size); else if (size <= SMALL_N) ptr = small_malloc(size); else ptr = large_malloc(size); return (ptr); }
static int lua_f_fastlz_compress ( lua_State *L ) { if ( !lua_isstring ( L, 1 ) ) { lua_pushnil ( L ); return 1; } size_t vlen = 0; const char *value = lua_tolstring ( L, 1, &vlen ); if ( vlen < 1 || vlen > 1 * 1024 * 1024 ) { /// Max 1Mb !!! lua_pushnil ( L ); return 1; } char *dst = ( char * ) &temp_buf; if ( vlen + 20 > 4096 ) { dst = large_malloc ( vlen + 20 ); } if ( dst == NULL ) { lua_pushnil ( L ); lua_pushstring ( L, "not enough memory!" ); return 2; } const unsigned int size_header = ( vlen ); // const unsigned int size_header = htonl ( vlen ); memcpy ( dst, &size_header, sizeof ( unsigned int ) ); int dlen = fastlz_compress ( value, vlen, dst + sizeof ( unsigned int ) ); if ( dst ) { lua_pushlstring ( L, dst, dlen + sizeof ( unsigned int ) ); if ( dst != ( char * ) &temp_buf ) { free ( dst ); } return 1; } else { lua_pushnil ( L ); return 1; } }
void *malloc(size_t size) { static char flag = 0; if (size <= 0) return (NULL); if (!flag) { g_pool.tiny_m = NULL; g_pool.small_m = NULL; g_pool.large_m = NULL; flag = 1; } if (size <= TINY_M) return (tiny_malloc(size)); else if (size <= SMALL_M) return (small_malloc(size)); else return (large_malloc(size)); return (NULL); }
//Characters encoded are NUL (ASCII 0), \n, \r, \, ', ", and Control-Z. int cosocket_lua_f_escape ( lua_State *L ) { const char *src = NULL; size_t slen = 0; if ( lua_isnil ( L, 1 ) ) { src = ""; } else { src = luaL_checklstring ( L, 1, &slen ); } if ( src == 0 ) { lua_pushstring ( L, "" ); return 1; } char *dst = _1_temp_buf; if ( slen > 2048 ) { dst = large_malloc ( slen * 2 ); } int i = 0, j = 0, has = 0; for ( i = 0; i < slen; i++ ) { if ( j >= 4 ) { lua_pushlstring ( L, dst, j ); if ( has == 1 ) { lua_concat ( L, 2 ); } has = 1; j = 0; } switch ( src[i] ) { case '\r': dst[j++] = '\\'; dst[j++] = 'r'; continue; break; case '\n': dst[j++] = '\\'; dst[j++] = 'n'; continue; break; case '\\': dst[j++] = '\\'; break; case '\'': dst[j++] = '\\'; break; case '"': dst[j++] = '\\'; break; case '\b': dst[j++] = '\\'; dst[j++] = 'b'; continue; break; case '\t': dst[j++] = '\\'; dst[j++] = 't'; continue; break; case '\0': dst[j++] = '\\'; dst[j++] = '0'; continue; break; case '\032': dst[j++] = '\\'; dst[j++] = 'Z'; continue; break; default: break; } dst[j++] = src[i]; } lua_pushlstring ( L, dst, j ); if ( dst != _1_temp_buf ) { free ( dst ); } if ( has == 1 ) { lua_concat ( L, 2 ); } return 1; }
int worker_process ( epdata_t *epd, int thread_at ) { //printf("worker_process\n"); working_at_fd = epd->fd; long start_time = longtime(); //network_send_error(epd, 503, "Lua Error: main function not found !!!");return 0; //network_send(epd, "aaa", 3);network_be_end(epd);return 0; add_io_counts(); lua_State *L = ( _L ); //lua_newthread if ( main_handle_ref != 0 ) { lua_rawgeti ( L, LUA_REGISTRYINDEX, main_handle_ref ); } else { lua_getglobal ( L, "main" ); } /*if(!lua_isfunction(L,-1)) { lua_pop(L,1); printf("no function\n"); }else*/{ int init_tables = 0; char *pt1 = NULL, *pt2 = NULL, *t1 = NULL, *t2 = NULL, *t3 = NULL, *query = NULL; int is_form_post = 0; char *boundary_post = NULL; char *cookies = NULL; pt1 = epd->headers; int i = 0; while ( t1 = strtok_r ( pt1, "\n", &pt1 ) ) { if ( ++i == 1 ) { /// first line t2 = strtok_r ( t1, " ", &t1 ); t3 = strtok_r ( t1, " ", &t1 ); epd->http_ver = strtok_r ( t1, " ", &t1 ); if ( !epd->http_ver ) { return 1; } else { if ( init_tables == 0 ) { lua_pushlightuserdata ( L, epd ); //lua_pushstring(L, epd->headers); lua_newtable ( L ); } } int len = strlen ( epd->http_ver ); if ( epd->http_ver[len - 1] == 13 ) { // CR == 13 epd->http_ver[len - 1] = '\0'; } if ( t2 && t3 ) { for ( t1 = t2 ; *t1 ; *t1 = toupper ( *t1 ), t1++ ); epd->method = t2; lua_pushstring ( L, t2 ); lua_setfield ( L, -2, "method" ); t1 = strtok_r ( t3, "?", &t3 ); t2 = strtok_r ( t3, "?", &t3 ); epd->uri = t1; lua_pushstring ( L, t1 ); lua_setfield ( L, -2, "uri" ); if ( t2 ) { epd->query = t2; query = t2; lua_pushstring ( L, t2 ); lua_setfield ( L, -2, "query" ); } } continue; } t2 = strtok_r ( t1, ":", &t1 ); if ( t2 ) { for ( t3 = t2; *t3; ++t3 ) { *t3 = *t3 >= 'A' && *t3 <= 'Z' ? *t3 | 0x60 : *t3; } t3 = t2 + strlen ( t2 ) + 1; //strtok_r ( t1, ":", &t1 ) if ( t3 ) { int len = strlen ( t3 ); if ( t3[len - 1] == 13 ) { /// 13 == CR t3[len - 1] = '\0'; } lua_pushstring ( L, t3 + ( t3[0] == ' ' ? 1 : 0 ) ); lua_setfield ( L, -2, t2 ); /// check content-type if ( t2[1] == 'o' && strcmp ( t2, "content-type" ) == 0 ) { if ( stristr ( t3, "x-www-form-urlencoded", len ) ) { is_form_post = 1; } else if ( stristr ( t3, "multipart/form-data", len ) ) { boundary_post = stristr ( t3, "boundary=", len - 2 ); } } else if ( !cookies && t2[1] == 'o' && strcmp ( t2, "cookie" ) == 0 ) { cookies = t3 + ( t3[0] == ' ' ? 1 : 0 ); } else if ( !epd->user_agent && t2[1] == 's' && strcmp ( t2, "user-agent" ) == 0 ) { epd->user_agent = t3 + ( t3[0] == ' ' ? 1 : 0 ); } else if ( !epd->referer && t2[1] == 'e' && strcmp ( t2, "referer" ) == 0 ) { epd->referer = t3 + ( t3[0] == ' ' ? 1 : 0 ); } } } } char *client_ip = inet_ntoa ( epd->client_addr ); lua_pushstring ( L, client_ip ); lua_setfield ( L, -2, "remote-addr" ); lua_newtable ( L ); /// _GET if ( query ) { /// parse query string /?a=1&b=2 while ( t1 = strtok_r ( query, "&", &query ) ) { t2 = strtok_r ( t1, "=", &t1 ); t3 = strtok_r ( t1, "=", &t1 ); if ( t2 && t3 && strlen ( t2 ) > 0 && strlen ( t3 ) > 0 ) { size_t len, dlen; u_char *p; u_char *src, *dst; len = strlen ( t3 ); p = large_malloc ( len ); p[0] = '\0'; dst = p; ngx_http_lua_unescape_uri ( &dst, &t3, len, 0 ); lua_pushlstring ( L, ( char * ) p, dst - p ); len = strlen ( t2 ); if ( len > 4096 ) { free ( p ); p = large_malloc ( len ); } p[0] = '\0'; dst = p; ngx_http_lua_unescape_uri ( &dst, &t2, len, 0 ); p[dst - p] = '\0'; lua_setfield ( L, -2, p ); free ( p ); } } } lua_newtable ( L ); /// _COOKIE if ( cookies ) { while ( t1 = strtok_r ( cookies, ";", &cookies ) ) { t2 = strtok_r ( t1, "=", &t1 ); t3 = strtok_r ( t1, "=", &t1 ); if ( t2 && t3 && strlen ( t2 ) > 0 && strlen ( t3 ) > 0 ) { size_t len, dlen; u_char *p; u_char *src, *dst; len = strlen ( t3 ); p = large_malloc ( len ); p[0] = '\0'; dst = p; ngx_http_lua_unescape_uri ( &dst, &t3, len, 0 ); lua_pushlstring ( L, ( char * ) p, dst - p ); len = strlen ( t2 ); if ( len > 4096 ) { free ( p ); p = large_malloc ( len ); } p[0] = '\0'; dst = p; ngx_http_lua_unescape_uri ( &dst, &t2, len, 0 ); p[dst - p] = '\0'; lua_setfield ( L, -2, p + ( p[0] == ' ' ? 1 : 0 ) ); free ( p ); } } } lua_newtable ( L ); /// _POST if ( is_form_post == 1 && epd->contents ) { /// parse post conents text=aa+bb&text2=%E4%B8%AD%E6%96%87+aa pt1 = epd->contents; while ( t1 = strtok_r ( pt1, "&", &pt1 ) ) { t2 = strtok_r ( t1, "=", &t1 ); t3 = strtok_r ( t1, "=", &t1 ); if ( t2 && t3 && strlen ( t2 ) > 0 && strlen ( t3 ) > 0 ) { size_t len, dlen; u_char *p; u_char *src, *dst; len = strlen ( t3 ); p = large_malloc ( len ); p[0] = '\0'; dst = p; ngx_http_lua_unescape_uri ( &dst, &t3, len, 0 ); lua_pushlstring ( L, ( char * ) p, dst - p ); free ( p ); //lua_pushstring(L, t3); lua_setfield ( L, -2, t2 ); } } } else if ( boundary_post ) { /// parse boundary body boundary_post += 9; int blen = strlen ( boundary_post ); int len = 0; char *start = epd->contents, *p2 = NULL, *p1 = NULL, *pp = NULL, *value = NULL; int i = 0; do { p2 = strstr ( start, boundary_post ); if ( p2 ) { start = p2 + blen; } if ( p1 ) { p1 += blen; if ( p2 ) { * ( p2 - 4 ) = '\0'; } else { break; } len = p2 - p1; value = stristr ( p1, "\r\n\r\n", len ); if ( value && value[4] != '\0' ) { value[0] = '\0'; value += 4; char *keyname = strstr ( p1, "name=\"" ); char *filename = NULL; char *content_type = NULL; if ( keyname ) { keyname += 6; for ( pp = keyname; *pp != '\0'; pp++ ) { if ( *pp == '"' ) { *pp = '\0'; p1 = pp + 2; break; } } filename = strstr ( p1, "filename=\"" ); if ( filename ) { /// is file filed filename += 10; for ( pp = filename; *pp != '\0'; pp++ ) { if ( *pp == '"' ) { *pp = '\0'; p1 = pp + 2; break; } } content_type = strstr ( p1, "Content-Type:" ); if ( content_type ) { content_type += 13; if ( content_type[0] == ' ' ) { content_type += 1; } } lua_newtable ( L ); lua_pushstring ( L, filename ); lua_setfield ( L, -2, "filename" ); lua_pushstring ( L, content_type ); lua_setfield ( L, -2, "type" ); lua_pushnumber ( L, p2 - value - 4 ); lua_setfield ( L, -2, "size" ); lua_pushlstring ( L, value, p2 - value - 4 ); lua_setfield ( L, -2, "data" ); lua_setfield ( L, -2, keyname ); } else { lua_pushstring ( L, value ); lua_setfield ( L, -2, keyname ); } } } } p1 = p2 + 2; } while ( p2 ); } if ( epd->headers != &epd->iov ) { free ( epd->headers ); } epd->headers = NULL; epd->header_len = 0; epd->contents = NULL; epd->content_length = 0; epd->iov[0].iov_base = NULL; epd->iov[0].iov_len = 0; epd->iov[1].iov_base = NULL; epd->iov[1].iov_len = 0; if ( lua_pcall ( L, 5, 0, 0 ) ) { if ( lua_isstring ( L, -1 ) ) { printf ( "Lua:error %s\n", lua_tostring ( L, -1 ) ); const char *data = lua_tostring ( L, -1 ); network_send_error ( epd, 503, data ); lua_pop ( L, 1 ); //network_be_end(epd); } } return 0; } network_send_error ( epd, 503, "Lua Error: main function not found !!!" ); return 0; }
int cosocket_be_read(se_ptr_t *ptr) { cosocket_t *c*k = ptr->data; int n = 0, ret = 0; init_read_buf: if(!c*k->read_buf || (c*k->last_buf->buf_len >= c*k->last_buf->buf_size)) { /// init read buf cosocket_link_buf_t *nbuf = NULL; nbuf = malloc(sizeof(cosocket_link_buf_t)); if(nbuf == NULL) { LOGF(ERR, "malloc error @%s:%d\n", __FILE__, __LINE__); exit(1); } nbuf->buf = large_malloc(4096); if(!nbuf->buf) { LOGF(ERR, "malloc error @%s:%d\n", __FILE__, __LINE__); exit(1); } nbuf->buf_size = 4096; nbuf->buf_len = 0; nbuf->next = NULL; if(c*k->read_buf) { c*k->last_buf->next = nbuf; } else { c*k->read_buf = nbuf; } c*k->last_buf = nbuf; } if(!c*k->ssl) { while((n = recv(c*k->fd, c*k->last_buf->buf + c*k->last_buf->buf_len, c*k->last_buf->buf_size - c*k->last_buf->buf_len, 0)) > 0) { c*k->last_buf->buf_len += n; c*k->total_buf_len += n; if(c*k->last_buf->buf_len >= c*k->last_buf->buf_size) { goto init_read_buf; } } } else { while((n = SSL_read(c*k->ssl, c*k->last_buf->buf + c*k->last_buf->buf_len, c*k->last_buf->buf_size - c*k->last_buf->buf_len)) > 0) { c*k->last_buf->buf_len += n; c*k->total_buf_len += n; if(c*k->last_buf->buf_len >= c*k->last_buf->buf_size) { goto init_read_buf; } } } if(n == 0 || (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) { int rfd = c*k->fd; /// socket closed delete_timeout(c*k->timeout_ptr); c*k->timeout_ptr = NULL; { c*k->status = 0; se_delete(c*k->ptr); c*k->ptr = NULL; connection_pool_counter_operate(c*k->pool_key, -1); close(c*k->fd); c*k->fd = -1; c*k->status = 0; } if(c*k->in_read_action == 1) { c*k->in_read_action = 0; int rt = lua_co_read_(c*k); c*k->inuse = 0; if(rt > 0) { ret = lua_f_lua_uthread_resume_in_c(c*k->L, rt); } else if(n == 0) { lua_pushnil(c*k->L); ret = lua_f_lua_uthread_resume_in_c(c*k->L, 1); } if(ret == LUA_ERRRUN) { se_delete(c*k->ptr); c*k->ptr = NULL; connection_pool_counter_operate(c*k->pool_key, -1); close(c*k->fd); c*k->fd = -1; c*k->status = 0; } } } else { if(c*k->in_read_action == 1) { int rt = lua_co_read_(c*k); if(rt > 0) { c*k->in_read_action = 0; delete_timeout(c*k->timeout_ptr); c*k->timeout_ptr = NULL; c*k->inuse = 0; ret = lua_f_lua_uthread_resume_in_c(c*k->L, rt); if(ret == LUA_ERRRUN) { se_delete(c*k->ptr); c*k->ptr = NULL; connection_pool_counter_operate(c*k->pool_key, -1); close(c*k->fd); c*k->fd = -1; c*k->status = 0; } } } } return 0; }
int lua_co_read_(cosocket_t *c*k) { if(c*k->total_buf_len < 1) { if(c*k->status == 0) { lua_pushnil(c*k->L); lua_pushstring(c*k->L, "Not connected!"); return 2; } return 0; } size_t be_copy = c*k->buf_read_len; if(c*k->buf_read_len == -1) { // read line int i = 0; int oi = 0; int has = 0; cosocket_link_buf_t *nbuf = c*k->read_buf; while(nbuf) { for(i = 0; i < nbuf->buf_len; i++) { if(nbuf->buf[i] == '\n') { has = 1; break; } } if(has == 1) { break; } oi += i; nbuf = nbuf->next; } i += oi; if(has == 1) { i += 1; be_copy = i; } else { return 0; } } else if(c*k->buf_read_len == -2) { be_copy = c*k->total_buf_len; } if(c*k->status == 0) { if(be_copy > c*k->total_buf_len) { be_copy = c*k->total_buf_len; } } int kk = 0; if(be_copy > 0 && c*k->total_buf_len >= be_copy) { char *buf2lua = large_malloc(be_copy); if(!buf2lua) { LOGF(ERR, "malloc error @%s:%d\n", __FILE__, __LINE__); exit(1); } size_t copy_len = be_copy; size_t copy_ed = 0; int this_copy_len = 0; cosocket_link_buf_t *bf = NULL; while(c*k->read_buf) { this_copy_len = (c*k->read_buf->buf_len + copy_ed > copy_len ? copy_len - copy_ed : c*k->read_buf->buf_len); if(this_copy_len > 0) { memcpy(buf2lua + copy_ed, c*k->read_buf->buf, this_copy_len); copy_ed += this_copy_len; memmove(c*k->read_buf->buf, c*k->read_buf->buf + this_copy_len, c*k->read_buf->buf_len - this_copy_len); c*k->read_buf->buf_len -= this_copy_len; } if(copy_ed >= be_copy) { /// not empty c*k->total_buf_len -= copy_ed; if(c*k->buf_read_len == -1) { /// read line , cut the \r \n if(buf2lua[be_copy - 1] == '\n') { be_copy -= 1; } if(buf2lua[be_copy - 1] == '\r') { be_copy -= 1; } } lua_pushlstring(c*k->L, buf2lua, be_copy); free(buf2lua); return 1; } else { bf = c*k->read_buf; c*k->read_buf = c*k->read_buf->next; if(c*k->last_buf == bf) { c*k->last_buf = NULL; c*k->read_buf = NULL; } free(bf->buf); free(bf); } } free(buf2lua); } return 0; }
static int lua_co_send(lua_State *L) { cosocket_t *c*k = NULL; { if(lua_gettop(L) < 2) { return 0; } int t = lua_type(L, 2); if(!lua_isuserdata(L, 1) || (t != LUA_TSTRING && t != LUA_TTABLE)) { lua_pushboolean(L, 0); lua_pushstring(L, "Error params!"); return 2; } c*k = (cosocket_t *) lua_touserdata(L, 1); if(c*k->status != 2 || c*k->fd == -1 || !c*k->ptr) { lua_pushboolean(L, 0); lua_pushstring(L, "Not connected!"); return 2; } if(c*k->inuse == 1) { lua_pushnil(L); lua_pushstring(L, "socket busy!"); return 2; } c*k->L = L; c*k->send_buf_ed = 0; if(t == LUA_TTABLE) { c*k->send_buf_len = lua_calc_strlen_in_table(L, 2, 2, 1 /* strict */); if(c*k->send_buf_len > 0) { if(c*k->send_buf_len < _SENDBUF_SIZE) { c*k->send_buf_need_free = NULL; lua_copy_str_in_table(L, 2, c*k->_send_buf); c*k->send_buf = c*k->_send_buf; } else { c*k->send_buf_need_free = large_malloc(c*k->send_buf_len); if(!c*k->send_buf_need_free) { LOGF(ERR, "malloc error @%s:%d\n", __FILE__, __LINE__); exit(1); } lua_copy_str_in_table(L, 2, c*k->send_buf_need_free); c*k->send_buf = c*k->send_buf_need_free; } } } else { const char *p = lua_tolstring(L, 2, &c*k->send_buf_len); c*k->send_buf_need_free = NULL; if(c*k->send_buf_len < _SENDBUF_SIZE) { memcpy(c*k->_send_buf, p, c*k->send_buf_len); c*k->send_buf = c*k->_send_buf; } else { c*k->send_buf_need_free = large_malloc(c*k->send_buf_len); if(!c*k->send_buf_need_free) { LOGF(ERR, "malloc error @%s:%d\n", __FILE__, __LINE__); exit(1); } memcpy(c*k->send_buf_need_free, p, c*k->send_buf_len); c*k->send_buf = c*k->send_buf_need_free; } } if(c*k->send_buf_len < 1) { lua_pushboolean(L, 0); lua_pushstring(L, "content empty!"); return 2; } c*k->inuse = 0; int ret = cosocket_be_write(c*k->ptr); if(ret > 0) { se_be_write(c*k->ptr, cosocket_be_write); c*k->timeout_ptr = add_timeout(c*k, c*k->timeout, timeout_handle); c*k->inuse = 1; return lua_yield(L, 0); } else { return 0 - ret; } } }
STATIC inline int INIT unlz4(u8 *input, int in_len, int (*fill) (void *, unsigned int), int (*flush) (void *, unsigned int), u8 *output, int *posp, void (*error) (char *x)) { int ret = -1; size_t chunksize = 0; size_t uncomp_chunksize = LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE; u8 *inp; u8 *inp_start; u8 *outp; int size = in_len; #ifdef PREBOOT size_t out_len = get_unaligned_le32(input + in_len); #endif size_t dest_len; if (output) { outp = output; } else if (!flush) { error("NULL output pointer and no flush function provided"); goto exit_0; } else { outp = large_malloc(uncomp_chunksize); if (!outp) { error("Could not allocate output buffer"); goto exit_0; } } if (input && fill) { error("Both input pointer and fill function provided,"); goto exit_1; } else if (input) { inp = input; } else if (!fill) { error("NULL input pointer and missing fill function"); goto exit_1; } else { inp = large_malloc(lz4_compressbound(uncomp_chunksize)); if (!inp) { error("Could not allocate input buffer"); goto exit_1; } } inp_start = inp; if (posp) *posp = 0; if (fill) { size = fill(inp, 4); if (size < 4) { error("data corrupted"); goto exit_2; } } chunksize = get_unaligned_le32(inp); if (chunksize == ARCHIVE_MAGICNUMBER) { if (!fill) { inp += 4; size -= 4; } } else { error("invalid header"); goto exit_2; } if (posp) *posp += 4; for (;;) { if (fill) { size = fill(inp, 4); if (size == 0) break; if (size < 4) { error("data corrupted"); goto exit_2; } } chunksize = get_unaligned_le32(inp); if (chunksize == ARCHIVE_MAGICNUMBER) { if (!fill) { inp += 4; size -= 4; } if (posp) *posp += 4; continue; } if (posp) *posp += 4; if (!fill) { inp += 4; size -= 4; } else { if (chunksize > lz4_compressbound(uncomp_chunksize)) { error("chunk length is longer than allocated"); goto exit_2; } size = fill(inp, chunksize); if (size < chunksize) { error("data corrupted"); goto exit_2; } } #ifdef PREBOOT if (out_len >= uncomp_chunksize) { dest_len = uncomp_chunksize; out_len -= dest_len; } else dest_len = out_len; ret = lz4_decompress(inp, &chunksize, outp, dest_len); #else dest_len = uncomp_chunksize; ret = lz4_decompress_unknownoutputsize(inp, chunksize, outp, &dest_len); #endif if (ret < 0) { error("Decoding failed"); goto exit_2; } if (flush && flush(outp, dest_len) != dest_len) goto exit_2; if (output) outp += dest_len; if (posp) *posp += chunksize; if (!fill) { size -= chunksize; if (size == 0) break; else if (size < 0) { error("data corrupted"); goto exit_2; } inp += chunksize; } } ret = 0; exit_2: if (!input) large_free(inp_start); exit_1: if (!output) large_free(outp); exit_0: return ret; }
static int lua_f_fastlz_decompress ( lua_State *L ) { if ( !lua_isstring ( L, 1 ) ) { lua_pushnil ( L ); return 1; } size_t vlen = 0; const char *value = lua_tolstring ( L, 1, &vlen ); printf("=========================\n"); printf("vlen %d\n", vlen); if ( vlen < 1 ) { lua_pushnil ( L ); return 1; } unsigned int value_len = 0; // value_len = *value; memcpy ( &value_len, value, sizeof ( unsigned int ) ); printf("value_len %d\n", value_len); // if (ntohl ( value_len ) > 0 && vlen < 1000) { // value_len = ntohl ( value_len ); // } printf("value_len %d\n", value_len); if ( value_len > 1024 * 1024 + 20 ) { lua_pushnil ( L ); lua_pushstring ( L, "not enough memory! too big value" ); return 2; } char *dst = ( char * ) &temp_buf; if ( value_len + 20 > 4096 ) { dst = ( unsigned char * ) large_malloc ( value_len + 20 ); } if ( dst == NULL ) { lua_pushnil ( L ); lua_pushstring ( L, "not enough memory! cant malloc" ); return 2; } int dlen = fastlz_decompress ( value + sizeof ( unsigned int ), vlen - sizeof ( unsigned int ), dst, value_len + 20 ); if ( dst ) { lua_pushlstring ( L, dst, value_len ); if ( dst != ( char * ) &temp_buf ) { free ( dst ); } return 1; } else { lua_pushnil ( L ); return 1; } }
int lua_echo ( lua_State *L ) { if ( !lua_isuserdata ( L, 1 ) ) { luaL_error ( L, "miss epd!" ); return 0; } int nargs = lua_gettop ( L ); if ( nargs < 2 ) { luaL_error ( L, "miss content!" ); return 0; } size_t len = 0; epdata_t *epd = lua_touserdata ( L, 1 ); if ( lua_istable ( L, 2 ) ) { len = lua_calc_strlen_in_table ( L, 2, 2, 0 /* strict */ ); if ( len < 1 ) { return 0; } char *buf = tbuf_4096; if ( len > 4096 ) { buf = large_malloc ( len ); if ( !buf ) { return 0; } lua_copy_str_in_table ( L, 2, buf ); network_send ( epd, buf, len ); free ( buf ); } else { lua_copy_str_in_table ( L, 2, buf ); network_send ( epd, buf, len ); } } else { const char *data = NULL; int i = 0; for ( i = 2; i <= nargs; i++ ) { if ( lua_isboolean ( L, i ) ) { if ( lua_toboolean ( L, i ) ) { network_send ( epd, "true", 4 ); } else { network_send ( epd, "false", 5 ); } } else { data = lua_tolstring ( L, i, &len ); network_send ( epd, data, len ); } } } return 0; }