function parseSegments(buffer) {
    if (buffer.length == 0) throw new Error("bad segments stream");

    let pointer = 0;
    let segments = [ ];

    while (pointer < buffer.length) {
        let seg = parseSegment(buffer, pointer);
        segments.push(seg);
        pointer += seg.bufferLength;
        delete seg.bufferLength;
    }

    if (pointer != buffer.length) {
        throw new Error("Bad / incomplete segments");
    }

    return segments;
}