Beispiel #1
0
bool MCSessionGenerateID(MCStringRef &r_id)
{
	// php calculates session ids by hashing a string composed of REMOTE_ADDR, time in seconds & milliseconds, and a random value

	MCAutoStringRef t_remote_addr_string;
	char *t_remote_addr;
	t_remote_addr = NULL;

	if (MCS_getenv(MCSTR("REMOTE_ADDR"), &t_remote_addr_string))
		MCCStringClone(MCStringGetCString(*t_remote_addr_string), t_remote_addr);
		
	time_t t_time;
	time(&t_time);
	
	MCAutoDataRef t_randombytes;
    
    // MW-2013-05-21; [[ RandomBytes ]] Use system primitive rather than SSL
	//   directly.
	/* UNCHECKED */ MCU_random_bytes(64, &t_randombytes);
	
	md5_state_t t_state;
	md5_byte_t t_digest[16];
	md5_init(&t_state);
	if (t_remote_addr != NULL)
		md5_append(&t_state, (md5_byte_t *)t_remote_addr, MCCStringLength(t_remote_addr));
	md5_append(&t_state, (md5_byte_t *)&t_time, sizeof(t_time));
	md5_append(&t_state, (md5_byte_t *)MCDataGetBytePtr(*t_randombytes), 64);
	md5_finish(&t_state, t_digest);
	
	return byte_to_hex((uint8_t*)t_digest, 16, r_id);
}
Beispiel #2
0
bool MCReferencedImageRep::GetDataStream(IO_handle &r_stream)
{
	IO_handle t_stream = nil;
	if (MCSecureModeCanAccessDisk())
		t_stream = MCS_open(m_file_name, kMCOpenFileModeRead, false, false, 0);
	
	if (t_stream == nil)
    {
        // MW-2013-09-25: [[ Bug 10983 ]] Only ever try to load the rep as a url once.
        if (!m_url_load_attempted)
        {
            // MW-2013-09-25: [[ Bug 10983 ]] Mark the rep has having attempted url load.
            m_url_load_attempted = true;

            MCExecContext ctxt(MCdefaultstackptr, nil, nil);
            MCAutoValueRef t_data;
            MCU_geturl(ctxt, m_file_name, &t_data);
            if (ctxt.HasError() || MCValueIsEmpty(*t_data))
                return false;
            MCAutoDataRef t_dataref;
            /* UNCHECKED */ ctxt . ConvertToData(*t_data, &t_dataref);

            /* UNCHECKED */ MCMemoryAllocateCopy(MCDataGetBytePtr(*t_dataref), MCDataGetLength(*t_dataref), m_url_data);
            m_url_data_size = MCDataGetLength(*t_dataref);
        }

        // IM-2014-09-30: [[ Bug 13501 ]] If we already have the url data then make sure we use it.
        if (m_url_data != nil)
            t_stream =  MCS_fakeopen((const char *)m_url_data, m_url_data_size);
	}

	if (t_stream != nil)
		r_stream = t_stream;

	return t_stream != nil;
}
Beispiel #3
0
void MCArraysEvalArrayDecode(MCExecContext& ctxt, MCDataRef p_encoding, MCArrayRef& r_array)
{
	bool t_success;
	t_success = true;

	IO_handle t_stream_handle;
	t_stream_handle = nil;
    if (t_success)
    {
        t_stream_handle = MCS_fakeopen(MCDataGetBytePtr(p_encoding), MCDataGetLength(p_encoding));
		if (t_stream_handle == nil)
			t_success = false;
	}

    uint8_t t_type;
	if (t_success)
		if (IO_read_uint1(&t_type, t_stream_handle) != IO_NORMAL)
			t_success = false;
        
    // AL-2014-05-01: [[ Bug 11989 ]] If the type is 'empty' then just return the empty array.
	if (t_success && t_type == kMCEncodedValueTypeEmpty)
    {
        r_array = MCValueRetain(kMCEmptyArray);
        return;
    }
    
    // AL-2014-05-15: [[ Bug 12203 ]] Check initial byte for version 7.0 encoded array.
    bool t_legacy;
    t_legacy = t_type < kMCEncodedValueTypeArray;
    
    MCArrayRef t_array;
	t_array = nil;
	if (t_success)
		t_success = MCArrayCreateMutable(t_array);

	if (t_legacy)
    {
        if (t_success)
            if (MCS_putback(t_type, t_stream_handle) != IO_NORMAL)
                t_success = false;
        
        MCObjectInputStream *t_stream;
        t_stream = nil;
        if (t_success)
        {
            t_stream = new MCObjectInputStream(t_stream_handle, MCDataGetLength(p_encoding), false);
            if (t_stream == nil)
                t_success = false;
        }
        
        if (t_success)
            if (t_stream -> ReadU8(t_type) != IO_NORMAL)
                t_success = false;
        
        if (t_success)
            if (MCArrayLoadFromStreamLegacy(t_array, *t_stream) != IO_NORMAL)
                t_success = false;
        
        delete t_stream;
    }
    else
    {
        if (t_success)
            if (IO_read_valueref_new((MCValueRef &)t_array, t_stream_handle) != IO_NORMAL)
                t_success = false;
    }
    
	MCS_close(t_stream_handle);

	if (t_success)
	{
		r_array = t_array;
		return;
	}

	MCValueRelease(t_array);

	ctxt . Throw();
}
Beispiel #4
0
void MCKeywordsExecRepeatFor(MCExecContext& ctxt, MCStatement *statements, MCExpression *endcond, MCVarref *loopvar, File_unit each, uint2 line, uint2 pos)
{
    MCAutoArrayRef t_array;
    MCAutoStringRef t_string;
    MCAutoDataRef t_data;
    MCRange t_chunk_range;
    t_chunk_range = MCRangeMake(0,0);
    uindex_t t_length = 0;
    MCAutoValueRef t_condition;
	MCNameRef t_key;
	MCValueRef t_value;
	uintptr_t t_iterator;
    // SN2015-06-15: [[ Bug 15457 ]] The index can be a negative index.
    index_t t_sequenced_iterator;
    const byte_t *t_data_ptr;
    
    MCAutoPointer<MCTextChunkIterator> tci;
    
    if (!ctxt . TryToEvaluateExpression(endcond, line, pos, EE_REPEAT_BADFORCOND, &t_condition))
        return;
    
    bool t_sequence_array;
    t_sequence_array = false;

    if (each == FU_ELEMENT || each == FU_KEY)
    {
        if (!ctxt . ConvertToArray(*t_condition, &t_array))
            return;
        
        // SN-2015-06-15: [[ Bug 15457 ]] If this is a numerical array, do
        //  it in order - even if it does not start at 1
        if (each == FU_ELEMENT && MCArrayIsNumericSequence(*t_array, t_sequenced_iterator))
        {
            t_sequence_array = true;
            if (!MCArrayFetchValueAtIndex(*t_array, t_sequenced_iterator, t_value))
                return;
        }
        else
        {
            t_iterator = 0;
            if (!MCArrayIterate(*t_array, t_iterator, t_key, t_value))
                return;
        }
    }
    else if (each == FU_BYTE)
    {
        if (!ctxt . ConvertToData(*t_condition, &t_data))
            return;
        
        t_length = MCDataGetLength(*t_data);
        t_data_ptr = MCDataGetBytePtr(*t_data);
    }
    else
    {
        if (!ctxt . ConvertToString(*t_condition, &t_string))
            return;
        
        switch (each)
        {
            case FU_LINE:
                tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_LINE);
                break;
            case FU_PARAGRAPH:
                tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_PARAGRAPH);
                break;
            case FU_SENTENCE:
                tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_SENTENCE);
                break;
            case FU_ITEM:
                tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_ITEM);
                break;
            case FU_WORD:
                tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_WORD);
                break;
            case FU_TRUEWORD:
                tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_TRUEWORD);
                break;
            case FU_TOKEN:
                tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_TOKEN);
                break;
            case FU_CODEPOINT:
                tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_CODEPOINT);
                break;
            case FU_CODEUNIT:
                tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_CODEUNIT);
                break;
            case FU_CHARACTER:
            default:
                tci = MCStringsTextChunkIteratorCreate(ctxt, *t_string, CT_CHARACTER);
                break;
        } 
    }
    
    bool done;
    done = false;
    
    bool endnext;
    endnext = false;
    
    bool t_found;
    t_found = false;
    
    while (!done)
    {
        MCAutoStringRef t_unit;
        MCAutoDataRef t_byte;
        switch (each)
        {
            case FU_KEY:
            {
                loopvar -> set(ctxt, t_key);
                if (!MCArrayIterate(*t_array, t_iterator, t_key, t_value))
                    endnext = true;
            }
            break;
                
            case FU_ELEMENT:
            {
                loopvar -> set(ctxt, t_value);
                // SN-2015-06-15: [[ Bug 15457 ]] Sequenced, numeric arrays
                //  have their own iterator
                if (t_sequence_array)
                {
                    if (!MCArrayFetchValueAtIndex(*t_array, ++t_sequenced_iterator, t_value))
                        endnext = true;
                }
                else
                {
                    if (!MCArrayIterate(*t_array, t_iterator, t_key, t_value))
                        endnext = true;
                }
            }
            break;
            
            case FU_BYTE:
            {
                // SN-2014-04-14 [[ Bug 12184 ]] If we have no data at all, we don't want to start the loop
                if (t_length)
                {
                    MCDataCreateWithBytes(t_data_ptr++, 1, &t_byte);
                    
                    endnext = (--t_length) == 0;
                }
                else
                    done = true;
            }
            break;
                
            default:
            {
                t_found = MCStringsTextChunkIteratorNext(ctxt, *tci);
                endnext = tci -> IsExhausted();
                
                if (!t_found)
                {
                    t_unit = kMCEmptyString;
                    done = true;
                }
                else
                    tci -> CopyString(&t_unit);
            }
            break;
        }
        // MW-2010-12-15: [[ Bug 9218 ]] Added KEY to the type of repeat that already
        //   copies the value.
        // MW-2011-02-08: [[ Bug ]] Make sure we don't use 't_unit' if the repeat type is 'key' or
        //   'element'.
        // Set the loop variable to whatever the value was in the last iteration.
        if (each == FU_BYTE)
        {
            // SN-2014-04-14 [[ Bug 12184 ]] We don't need to set anything since we are not going in the loop
            if (!done)
                loopvar -> set(ctxt, *t_byte);
        }
        else if (each != FU_ELEMENT && each != FU_KEY)
            loopvar -> set(ctxt, *t_unit);
        
        if (!done)
            MCKeywordsExecuteRepeatStatements(ctxt, statements, line, pos, done);
        
        if (endnext)
        {
            // Reset the loop variable to whatever the value was in the last iteration.
            if (loopvar != nil)
            {
                if (each == FU_BYTE)
                    loopvar -> set(ctxt, *t_byte);
                else if (each != FU_ELEMENT && each != FU_KEY)
                    loopvar -> set(ctxt, *t_unit);
            }
        }
        
        done = done || endnext;
    }
}