Exemplo n.º 1
0
char* loadLzfStringObject()
{
    unsigned int slen, clen;
    char* c, *s;

    if ((clen = loadLength(NULL)) == REDIS_RDB_LENERR) {
        return NULL;
    }
    if ((slen = loadLength(NULL)) == REDIS_RDB_LENERR) {
        return NULL;
    }

    c = malloc(clen);
    if (!readBytes(c, clen)) {
        free(c);
        return NULL;
    }

    s = malloc(slen + 1);
    if (lzf_decompress(c, clen, s, slen) == 0) {
        free(c);
        free(s);
        return NULL;
    }

    free(c);
    return s;
}
Exemplo n.º 2
0
/* returns NULL when not processable, char* when valid */
char* loadStringObject() {
    uint32_t offset = CURR_OFFSET;
    int isencoded;
    uint32_t len;
    char *buf;

    len = loadLength(&isencoded);
    if (isencoded) {
        switch(len) {
        case REDIS_RDB_ENC_INT8:
        case REDIS_RDB_ENC_INT16:
        case REDIS_RDB_ENC_INT32:
            return loadIntegerObject(len);
        case REDIS_RDB_ENC_LZF:
            return loadLzfStringObject();
        default:
            /* unknown encoding */
            SHIFT_ERROR(offset, "Unknown string encoding (0x%02x)", len);
            return NULL;
        }
    }

    if (len == REDIS_RDB_LENERR) return NULL;

    buf = malloc(sizeof(char) * (len+1));
    buf[len] = '\0';
    if (!readBytes(buf, len)) {
        free(buf);
        return NULL;
    }
    return buf;
}
Exemplo n.º 3
0
entry loadEntry() {
    entry e = { NULL, -1, 0 };
    uint32_t length, offset[4];

    /* reset error container */
    errors.level = 0;

    offset[0] = CURR_OFFSET;
    if (!loadType(&e)) {
        return e;
    }

    offset[1] = CURR_OFFSET;
    if (e.type == REDIS_SELECTDB) {
        if ((length = loadLength(NULL)) == REDIS_RDB_LENERR) {
            SHIFT_ERROR(offset[1], 
                "Error reading database number");
            return e;
        }
        if (length > 63) {
            SHIFT_ERROR(offset[1], 
                "Database number out of range (%d)", length);
            return e;
        }
    } else if (e.type == REDIS_EOF) {
        if (positions[level].offset < positions[level].size) {
            SHIFT_ERROR(offset[0], 
                "Unexpected EOF");
        } else {
            e.success = 1;
        }
        return e;
    } else {
        /* optionally consume expire */
        if (e.type == REDIS_EXPIRETIME) {
            if (!processTime()) return e;
            if (!loadType(&e)) return e;
            db_stats.total_expires++;
        }

        offset[1] = CURR_OFFSET;
        if (!loadPair(&e)) {
            SHIFT_ERROR(offset[1], "Error for type %s", types[e.type]);
            return e;
        }
    }

    /* all entries are followed by a valid type:
     * e.g. a new entry, SELECTDB, EXPIRE, EOF */
    offset[2] = CURR_OFFSET;
    if (peekType() == -1) {
        SHIFT_ERROR(offset[2], "Followed by invalid type");
        SHIFT_ERROR(offset[0], "Error for type %s", types[e.type]);
        e.success = 0;
    } else {
        e.success = 1;
    }

    return e;
}
Exemplo n.º 4
0
static char* loadLzfStringObject() {
    unsigned int slen, clen;
    char *c, *s;

    if ((clen = loadLength(NULL)) == RDB_LENERR) return NULL;
    if ((slen = loadLength(NULL)) == RDB_LENERR) return NULL;

    c = zmalloc(clen);
    if (!readBytes(c, clen)) {
        zfree(c);
        return NULL;
    }

    s = zmalloc(slen+1);
    if (lzf_decompress(c,clen,s,slen) == 0) {
        zfree(c); zfree(s);
        return NULL;
    }

    zfree(c);
    return s;
}
Exemplo n.º 5
0
int loadPair(entry *e) {
    uint32_t offset = CURR_OFFSET;
    uint32_t i;
    uint32_t length = 0;

    /* read key first */
    char *key;
    if (processStringObject(&key)) {
        e->key = key;
    } else {
        SHIFT_ERROR(offset, "Error reading entry key");
        return 0;
    }

    if (e->type == REDIS_LIST ||
        e->type == REDIS_SET  ||
        e->type == REDIS_ZSET ||
        e->type == REDIS_HASH) {
        if ((length = loadLength(NULL)) == REDIS_RDB_LENERR) {
            SHIFT_ERROR(offset, "Error reading %s length", types[e->type]);
            return 0;
        }
    }

    switch(e->type) {
    case REDIS_STRING:
    case REDIS_HASH_ZIPMAP:
    case REDIS_LIST_ZIPLIST:
    case REDIS_SET_INTSET:
    case REDIS_ZSET_ZIPLIST:
    case REDIS_HASH_ZIPLIST:
        if (!processStringObject(NULL)) {
            SHIFT_ERROR(offset, "Error reading entry value");
            return 0;
        }
    break;
    case REDIS_LIST:
    case REDIS_SET:
        for (i = 0; i < length; i++) {
            offset = CURR_OFFSET;
            if (!processStringObject(NULL)) {
                SHIFT_ERROR(offset, "Error reading element at index %d (length: %d)", i, length);
                return 0;
            }
        }
    break;
    case REDIS_ZSET:
        for (i = 0; i < length; i++) {
            offset = CURR_OFFSET;
            if (!processStringObject(NULL)) {
                SHIFT_ERROR(offset, "Error reading element key at index %d (length: %d)", i, length);
                return 0;
            }
            offset = CURR_OFFSET;
            if (!processDoubleValue(NULL)) {
                SHIFT_ERROR(offset, "Error reading element value at index %d (length: %d)", i, length);
                return 0;
            }
        }
    break;
    case REDIS_HASH:
        for (i = 0; i < length; i++) {
            offset = CURR_OFFSET;
            if (!processStringObject(NULL)) {
                SHIFT_ERROR(offset, "Error reading element key at index %d (length: %d)", i, length);
                return 0;
            }
            offset = CURR_OFFSET;
            if (!processStringObject(NULL)) {
                SHIFT_ERROR(offset, "Error reading element value at index %d (length: %d)", i, length);
                return 0;
            }
        }
    break;
    default:
        SHIFT_ERROR(offset, "Type not implemented");
        return 0;
    }
    /* because we're done, we assume success */
    e->success = 1;
    return 1;
}
Exemplo n.º 6
0
TrackFrames buildTrackFramesFromMidi(const std::string& filename)
{
    TrackFrames trackFrames;

    FILE* pFic;
    fopen_s(&pFic, filename.c_str(), "rb");

    // Load header
    struct HeaderChunk
    {
        char HThd[4];
        uint32_t size;
        uint16_t format;
        uint16_t nbTrack;
        uint16_t division;
    } headerChunk;
    loadStr(headerChunk.HThd, 4, pFic);
    headerChunk.size = load32(pFic);
    headerChunk.format = load16(pFic);
    headerChunk.nbTrack = load16(pFic);
    headerChunk.division = load16(pFic);

    uint32_t maxTime = 0;

    std::set<int> channelSet;
    //int midiToChannels[] = { // mario
    //    5, 0, 1, 2,
    //    5, 5, 5, 5,
    //    5, 5, 5, 5,
    //    5, 5, 5, 5,
    //};
    //int midiToChannels[] = { // sm2
    //    0, 2, 1, 5,
    //    5, 5, 5, 5,
    //    5, 3, 5, 5,
    //    5, 5, 5, 5,
    //};
    int midiToChannels[] = {
        0, 1, 2, 5,
        5, 5, 5, 5,
        5, 5, 5, 5,
        5, 5, 5, 5,
    };
    //int midiToChannels[] = { // zelda
    //    0, 1, 2, 5,
    //    5, 5, 5, 5,
    //    5, 3, 5, 5,
    //    5, 5, 5, 5,
    //};

    // Load tracks
    for (auto i = 0u; i < headerChunk.nbTrack; ++i)
    {
        char MTrk[4];
        loadStr(MTrk, 4, pFic);
        auto len = load32(pFic);
        auto start = ftell(pFic);
        auto cur = start;
        uint32_t curTime = 0;

        while (cur - start < static_cast<decltype(cur)>(len))
        {
            auto v_time = loadLength(pFic);
            auto event = load8(pFic);

            curTime += v_time;// / (headerChunk.division / 24);
            maxTime = std::max(maxTime, curTime);
            trackFrames.resize(maxTime * 4 + 16);

            if (event == 0xFF)
            {
                // Meta event
                event = load8(pFic);
                if (event == 0x58)
                {
                    // Time Signature
                    event = load8(pFic);
                    assert(event == 0x04);
                    load32(pFic); // Ignore who f*****g cares
                }
                else if (event == 0x7F)
                {
                    // Sequencer Specific Meta-Event
                    auto dataLen = load8(pFic); // Len, ignore
                    while (dataLen)
                    {
                        load8(pFic);
                        --dataLen;
                    }
                }
                else if (event == 0x51)
                {
                    // Set Tempo
                    event = load8(pFic);
                    assert(event == 0x03);
                    load8(pFic);
                    load8(pFic);
                    load8(pFic);
                }
                else if (event == 0x2F)
                {
                    // End of Track
                    event = load8(pFic);
                    assert(event == 0x00);
                }
                else if (event == 0x03)
                {
                    // Sequence/Track Name
                    auto textLen = load8(pFic);
                    char text[257] = {0};
                    loadStr(text, textLen, pFic);
                    printf("Track found: %i, %s\n", i, text);
                }
                else if (event == 0x59)
                {
                    // Key Signature
                    event = load8(pFic);
                    assert(event == 0x02);
                    auto sf = load8(pFic);
                    auto mi = load8(pFic);
                }
                else if (event == 0x21)
                {
                    // End of Track
                    event = load8(pFic);
                    assert(event == 0x01);
                    load8(pFic);
                }
                else if (event == 0x20)
                {
                    // MIDI Channel Prefix
                    event = load8(pFic);
                    assert(event == 0x01);
                    auto channel = load8(pFic);
                }
                else
                {
                    assert(false);
                }
            }
            else if (event & 0x80)
            {
                auto channelNo = event & 0x0F;
                channelSet.insert(channelNo);
                if (channelNo < sizeof(midiToChannels) / sizeof(int)) channelNo = midiToChannels[channelNo];
                event = event & 0xF0;

                if (event == 0xB0)
                {
                    // Control Change
                    auto controllerNumber = load8(pFic) & 0x7F;
                    auto controllerValue = load8(pFic) & 0x7F;
                }
                else if (event == 0xC0)
                {
                    // Program Change
                    auto programNumber = load8(pFic) & 0x7F;
                }
                else if (event == 0x90)
                {
                    // Note On event
                    auto note = load8(pFic) & 0x7F;
                    auto vol = load8(pFic) & 0x7F;
                    if (channelNo < 3)
                    {
                        auto& trackFrame = trackFrames[curTime * 4 + channelNo];
                        trackFrame = NOTE(noteTable[note], channelNo, (vol * 15) / 127);
                    }
                    else if (channelNo == 3)
                    {
                        auto& trackFrame = trackFrames[curTime * 4 + channelNo];
                        trackFrame = NOTE(noteTable[note + 12], channelNo, (vol * 15) / 127);
                    }
                }
                else if (event == 0x80)
                {
                    // Note Off event
                    auto note = load8(pFic) & 0x7F;
                    auto vol = load8(pFic) & 0x7F;
                    if (channelNo == 2)
                    {
                        //auto& trackFrame = trackFrames[curTime * 4 + channelNo];
                        //trackFrame = NOTE(noteTable[note + 12], channelNo, 0);
                    }
                    if (channelNo == 3)
                    {
                        auto& trackFrame = trackFrames[curTime * 4 + channelNo];
                        trackFrame = NOTE(noteTable[note], channelNo, 0);
                    }
                }
                else if (event == 0xE0)
                {
                    // Pitch Wheel Change
                    auto l = load8(pFic) & 0x7F;
                    auto m = load8(pFic) & 0x7F;
                }
                else
                {
                    assert(false);
                }
            }
            else if (event <= 0x7F)
            {
                load8(pFic);
            }
            else
            {
                assert(false);
            }

            cur = ftell(pFic);
        }
    }

    fclose(pFic);

    for (auto& channelNo : channelSet)
    {
        printf("%i\n", channelNo);
    }

    return trackFrames;
}
Exemplo n.º 7
0
/**
 * @brief cwRegionLoadTask::loadNoteTranformation
 * @param protoNoteTransformation
 * @param noteTransformation
 */
void cwRegionLoadTask::loadNoteTranformation(const CavewhereProto::NoteTranformation& protoNoteTransformation, cwNoteTranformation *noteTransformation)
{
    noteTransformation->setNorthUp(protoNoteTransformation.northup());
    loadLength(protoNoteTransformation.scalenumerator(), noteTransformation->scaleNumerator());
    loadLength(protoNoteTransformation.scaledenominator(), noteTransformation->scaleDenominator());
}