コード例 #1
0
ファイル: File.cpp プロジェクト: delahee/hxlibc
/**
	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);
}
コード例 #2
0
    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;
    }
コード例 #3
0
    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);
コード例 #4
0
ファイル: Bytes.cpp プロジェクト: blue112/lime
	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;
		
	}
コード例 #5
0
ファイル: ExternalInterface.cpp プロジェクト: Blank101/nmemic
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();
	}
}
コード例 #6
0
ファイル: Bytes.cpp プロジェクト: blue112/lime
	inline void initialize () {
		
		if (!init) {
			
			id_b = val_id ("b");
			id_length = val_id ("length");
			
			buffer b = alloc_buffer_len (1);
			
			if (buffer_data (b)) {
				
				useBuffer = true;
				
			}
			
			init = true;
			
		}
		
	}
コード例 #7
0
ファイル: hxfcgi.cpp プロジェクト: RudolfVonKrugstein/hxfcgi
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;
}
コード例 #8
0
    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);
    }