| 157 | }; |
| 158 | |
| 159 | static int gmext_write_sequence (std::vector<unsigned char> &midi, |
| 160 | const struct gmstream *stream, unsigned int channel, |
| 161 | const struct seq *seq, struct output_status *status) |
| 162 | { |
| 163 | const unsigned char *data = seq->data; |
| 164 | unsigned int left = seq->size; |
| 165 | |
| 166 | unsigned char cmd = -1; |
| 167 | |
| 168 | while (left) { |
| 169 | |
| 170 | // read delta |
| 171 | unsigned int ndelta = 0; |
| 172 | |
| 173 | for (int i=0; ; ) { |
| 174 | unsigned char c = *data++; |
| 175 | left--; |
| 176 | ndelta += c & 0x7F; |
| 177 | if (!(c & 0x80)) |
| 178 | break; |
| 179 | if ((++i == 4) || !left) |
| 180 | return -1; |
| 181 | ndelta <<= 7; |
| 182 | } |
| 183 | |
| 184 | status->delta += ndelta; |
| 185 | |
| 186 | // read cmd byte |
| 187 | if (!left) |
| 188 | return -1; |
| 189 | |
| 190 | if (*data & 0x80) { |
| 191 | // actual cmd byte |
| 192 | cmd = *data++; |
| 193 | left--; |
| 194 | switch (cmd) { |
| 195 | case 0xFF: // end track |
| 196 | case 0xFD: // end subsequence |
| 197 | return 0; |
| 198 | case 0xFE: // insert subsequence |
| 199 | if (!left--) |
| 200 | return -1; |
| 201 | if (*data >= stream->nsubs) |
| 202 | // invalid subsequence |
| 203 | return -1; |
| 204 | if (gmext_write_sequence(midi, |
| 205 | stream, channel, |
| 206 | &stream->subs[*data++], status) |
| 207 | == -1) |
| 208 | return -1; |
| 209 | cmd = 0; |
| 210 | continue; |
| 211 | default: |
| 212 | cmd &= 0xF0; |
| 213 | } |
| 214 | } else if (cmd == 0) |
| 215 | return -1; // invalid running mode |
| 216 |
no test coverage detected