/** file_contents : f:string -> string <doc>Read the content of the file [f] and return it.</doc> **/ static value file_contents( value name ) { buffer s; int len; int p; val_check(name,string); fio f(val_filename(name)); const char *fname = val_string(name); gc_enter_blocking(); f.io = fopen(fname,"rb"); if( f.io == NULL ) file_error("file_contents",&f); fseek(f.io,0,SEEK_END); len = ftell(f.io); fseek(f.io,0,SEEK_SET); gc_exit_blocking(); s = alloc_buffer_len(len); p = 0; gc_enter_blocking(); while( len > 0 ) { int d; POSIX_LABEL(file_contents); d = (int)fread((char*)buffer_data(s)+p,1,len,f.io); if( d <= 0 ) { HANDLE_FINTR(f.io,file_contents); fclose(f.io); file_error("file_contents",&f); } p += d; len -= d; } fclose(f.io); gc_exit_blocking(); return buffer_val(s); }
DEFINE_FUNC_1(webp_decode_argb, data_buffer_value) { if (!val_is_buffer(data_buffer_value)) { val_throw(alloc_string("webp_decode_argb: Expected to be a buffer")); return alloc_null(); } buffer data_buffer = val_to_buffer(data_buffer_value); int data_len = buffer_size(data_buffer); char *data_ptr = buffer_data(data_buffer); int webp_width = -1, webp_height = -1; char *webp_data_ptr = (char *)WebPDecodeARGB((const unsigned char *)data_ptr, data_len, &webp_width, &webp_height); int webp_data_len = webp_width * webp_height * 4; if (webp_data_ptr == NULL) { val_throw(alloc_string("webp_decode_argb: Invalid webp data")); return alloc_null(); } buffer webp_buffer = alloc_buffer_len(0); buffer_append_sub(webp_buffer, webp_data_ptr, webp_data_len); buffer_set_size(webp_buffer, webp_data_len); value array = alloc_array(3); val_array_set_i(array, 0, alloc_int(webp_width)); val_array_set_i(array, 1, alloc_int(webp_height)); val_array_set_i(array, 2, buffer_val(webp_buffer)); if (webp_data_ptr != NULL) free(webp_data_ptr); return array; }
value hx_zmq_getbytessockopt(value socket_handle_,value option_) { val_check_kind(socket_handle_, k_zmq_socket_handle); if (!val_is_int(option_)) { val_throw(alloc_int(EINVAL)); return alloc_null(); } int rc = 0; int err = 0; char optval [255]; size_t optvallen = 255; rc = zmq_getsockopt(val_data(socket_handle_),val_int(option_),&optval, &optvallen); err = zmq_errno(); if (rc != 0) { val_throw(alloc_int(err)); return alloc_int(0); } // Return data to Haxe // Create a return buffer byte array, by memcopying the message data, then discard the ZMQ message buffer b = alloc_buffer(NULL); buffer_append_sub(b,optval,optvallen); err = zmq_errno(); if (rc != 0) { val_throw(alloc_int(err)); return alloc_null(); } return buffer_val(b); }
value snow_lzma_decode(value input_value) { buffer input_buffer = val_to_buffer(input_value); buffer output_buffer = alloc_buffer_len(0); native_toolkit_lzma::Lzma::Decode(input_buffer, output_buffer); return buffer_val(output_buffer); } DEFINE_PRIM(snow_lzma_decode,1);
void Bytes::Resize (int size) { if (size != _length) { if (!_value) { _value = alloc_empty_object (); } if (val_is_null (val_field (_value, id_b))) { value dataValue; if (useBuffer) { buffer b = alloc_buffer_len (size); dataValue = buffer_val (b); _data = (unsigned char*)buffer_data (b); } else { dataValue = alloc_raw_string (size); _data = (unsigned char*)val_string (dataValue); } alloc_field (_value, id_b, dataValue); } else { if (useBuffer) { buffer b = val_to_buffer (val_field (_value, id_b)); buffer_set_size (b, size); _data = (unsigned char*)buffer_data (b); } else { value s = alloc_raw_string (size); memcpy ((char *)val_string (s), val_string (val_field (_value, id_b)), size); alloc_field (_value, id_b, s); _data = (unsigned char*)val_string (s); } } alloc_field (_value, id_length, alloc_int (size)); } _length = size; }
static value nme_microphone_poll (value outHaxeObj) { mutex->Lock(); if (outgoing_pos > 0) { buffer buf = alloc_buffer_len(0); buffer_append_sub(buf, outgoing, outgoing_len); outgoing_pos = 0; outgoing_len = 0; alloc_field(outHaxeObj, val_id("state"), alloc_int(state)); state = 0; mutex->Unlock(); return buffer_val(buf); } else { mutex->Unlock(); return alloc_null(); } }
/** socket_read : 'socket -> string <doc>Read the whole content of a the data available from a socket until the connection close. If the socket hasn't been close by the other side, the function might block. </doc> **/ static value socket_read( value o ) { buffer b; char buf[256]; int len; SOCKET sock = val_sock(o); b = alloc_buffer(NULL); gc_enter_blocking(); while( true ) { POSIX_LABEL(read_again); len = recv(sock,buf,256,MSG_NOSIGNAL); if( len == SOCKET_ERROR ) { HANDLE_EINTR(read_again); return block_error(); } if( len == 0 ) break; gc_exit_blocking(); buffer_append_sub(b,buf,len); gc_enter_blocking(); } gc_exit_blocking(); return buffer_val(b); }
value hxfcgi_parse_multipart(value hreq, value onpart, value ondata ) { val_check_kind(hreq,hxRequest); val_check_function(onpart,2); val_check_function(ondata,3); hxfcgi::Request *req = get_request(hreq); buffer buf; int len = 0; buffer boundstr; buf = alloc_buffer_len(BUFSIZE); hxfcgi::BasicData b; string ctype = b.getHeader("CONTENT_TYPE"); if(ctype.find("multipart/form-data") != 0) return val_null; // extract boundary value { const char *boundary, *bend; if( (boundary = strstr(ctype.c_str(),"boundary=")) == NULL ) neko_error(); boundary += 9; PARSE_HEADER(boundary,bend); len = (int)(bend - boundary); boundstr = alloc_buffer_len(len+3); if( strlen(buffer_data(boundstr)) > BUFSIZE / 2 ) neko_error(); buffer_data(boundstr)[0] = '-'; buffer_data(boundstr)[1] = '-'; memcpy(buffer_data(boundstr)+2,boundary,len); buffer_data(boundstr)[len+2] = 0; } len = 0; while( true ) { char *name, *end_name, *filename, *end_file_name, *data; int pos; // refill buffer // we assume here that the the whole multipart header can fit in the buffer req->bufferFill(buf,&len); // is boundary at the beginning of buffer ? if( len < (int) strlen(buffer_data(boundstr)) || memcmp(buffer_data(buf),buffer_data(boundstr),strlen(buffer_data(boundstr))) != 0 ) return val_null; name = memfind(buffer_data(buf),len,"Content-Disposition:"); if( name == NULL ) break; name = memfind(name,len - (int)(name - buffer_data(buf)),"name="); if( name == NULL ) return val_null; name += 5; PARSE_HEADER(name,end_name); data = memfind(end_name,len - (int)(end_name - buffer_data(buf)),"\r\n\r\n"); if( data == NULL ) return val_null; filename = memfind(name,(int)(data - name),"filename="); if( filename != NULL ) { filename += 9; PARSE_HEADER(filename,end_file_name); // send part name val_call2(onpart,copy_string(name,(int)(end_name - name)),copy_string(filename,(int)(end_file_name - filename))); } else { // send part name val_call2(onpart,copy_string(name,(int)(end_name - name)),val_null); } data += 4; pos = (int)(data - buffer_data(buf)); // read data while( true ) { const char *boundary; // recall buffer memcpy(buffer_data(buf),buffer_data(buf)+pos,len - pos); len -= pos; pos = 0; req->bufferFill(buf,&len); // lookup bounds boundary = memfind(buffer_data(buf),len,buffer_data(boundstr)); if( boundary == NULL ) { if( len == 0 ) return val_null; // send as much buffer as possible to client if( len < BUFSIZE ) pos = len; else pos = len - strlen(buffer_data(boundstr)) + 1; val_call3(ondata,buffer_val(buf),alloc_int(0),alloc_int(pos)); } else { // send remaining data pos = (int)(boundary - buffer_data(buf)); val_call3(ondata,buffer_val(buf),alloc_int(0),alloc_int(pos-2)); // recall memcpy(buffer_data(buf),buffer_data(buf)+pos,len - pos); len -= pos; break; } } } return val_null; }
DEFINE_FUNC_5(webp_encode_argb, data_buffer_value, width_value, height_value, lossless_value, quality_factor_value) { int width = 0; int height = 0; float quality_factor = 100; int lossless = 1; int stride = 0; uint8_t* abgr = NULL; uint8_t* rgba = NULL; uint8_t* output = NULL; int output_size = 0; int pixels_size; int bytes_size; buffer data_buffer; if (val_is_int(width_value)) width = val_int(width_value); if (val_is_int(height_value)) height = val_int(height_value); if (val_is_bool(lossless_value)) lossless = val_bool(lossless_value); if (val_is_float(quality_factor_value)) quality_factor = val_float(quality_factor_value); stride = width * 4; pixels_size = width * height; bytes_size = pixels_size * 4; if (!val_is_buffer(data_buffer_value)) { val_throw(alloc_string("webp_encode_argb: Expected to be a buffer")); return alloc_null(); } data_buffer = val_to_buffer(data_buffer_value); if (bytes_size != buffer_size(data_buffer)) { val_throw(alloc_string("webp_encode_argb: Invalid buffer size")); return alloc_null(); } //if () abgr = (uint8_t *)buffer_data(data_buffer); rgba = (uint8_t *)malloc(bytes_size); uint8_t* _abgr = abgr; uint8_t* _rgba = rgba; int _pixels_size = pixels_size; while (_pixels_size-- > 0) { //_rgba[0] = _abgr[3]; _rgba[0] = _abgr[1]; _rgba[1] = _abgr[2]; _rgba[2] = _abgr[3]; _rgba[3] = _abgr[0]; _abgr += 4; _rgba += 4; } if (lossless) { output_size = WebPEncodeLosslessRGBA( rgba, //abgr, width, height, stride, &output ); } else { output_size = WebPEncodeRGBA( rgba, //abgr, width, height, stride, quality_factor, &output ); } printf("output_size: (%d, %d, %d) : %d\n", width, height, stride, output_size); buffer output_buffer = alloc_buffer_len(0); buffer_append_sub(output_buffer, (char *)output, output_size); buffer_set_size(output_buffer, output_size); if (output != NULL) free(output); if (rgba != NULL) free(rgba); return buffer_val(output_buffer); }
/** * Receive data from socket * Based on code in https://github.com/zeromq/jzmq/blob/master/src/Socket.cpp */ value hx_zmq_rcv(value socket_handle_, value flags) { val_check_kind(socket_handle_, k_zmq_socket_handle); if (!val_is_null(flags) && !val_is_int(flags)) { val_throw(alloc_int(EINVAL)); return alloc_null(); } zmq_msg_t message; int rc = zmq_msg_init (&message); int err = zmq_errno(); if (rc != 0) { val_throw(alloc_int(err)); return alloc_null(); } gc_enter_blocking(); #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0) rc = zmq_recvmsg (val_data(socket_handle_), &message, val_int(flags)); #else rc = zmq_recv (val_data(socket_handle_), &message, val_int(flags)); #endif gc_exit_blocking(); err = zmq_errno(); if (rc == -1 && err == EAGAIN) { rc = zmq_msg_close (&message); err = zmq_errno(); if (rc != 0) { val_throw(alloc_int(err)); return alloc_null(); } return alloc_null(); } if (rc == -1) { rc = zmq_msg_close (&message); int err1 = zmq_errno(); if (rc != 0) { val_throw(alloc_int(err1)); return alloc_null(); } val_throw(alloc_int(err)); return alloc_null(); } // Return data to Haxe int sz = zmq_msg_size (&message); const char* pd = (char *)zmq_msg_data (&message); // Create a return buffer byte array, by memcopying the message data, then discard the ZMQ message buffer b = alloc_buffer(NULL); buffer_append_sub(b,pd,sz); rc = zmq_msg_close (&message); err = zmq_errno(); if (rc != 0) { val_throw(alloc_int(err)); return alloc_null(); } return buffer_val(b); }