示例#1
0
def test_read_all():
    f = BlobIO(b'test' * 3)

    assert f.read() == b'testtesttest'

    assert f.tell() == 12
    assert f.read() == b''

    f.seek(6)
    assert f.read() == b'sttest'
示例#2
0
def test_out_of_bounds_read():
    f = BlobIO(b'test' * 1000)

    f.seek(3998)
    assert f.read(10) == b'st'
    assert f.tell() == 4000

    assert f.read(10) == b''
    assert f.tell() == 4000

    f.seek(5000)
    assert f.read(10) == b''
示例#3
0
def _parse_format4(blob, offs):
    length, = struct.unpack('>H', bytes(blob[offs + 2:offs + 4]))

    blob = blob[offs:offs + length]
    fin = BlobIO(blob)
    hdr = _cmap_fmt4_header.parse(fin)
    seg_count = hdr.segCountX2 // 2

    glyph_id_count = length - (hdr.size + seg_count * 8 + 2)
    if glyph_id_count < 0 or glyph_id_count % 2 != 0:
        raise RuntimeError('corrupted character map subtable')

    glyph_id_count //= 2

    end_count = _load_array(fin, seg_count, 'H')
    fin.seek(2, 1)
    start_count = _load_array(fin, seg_count, 'H')
    id_delta = _load_array(fin, seg_count, 'H')
    id_range_offset = _load_array(fin, seg_count, 'H')

    glyph_ids = _load_array(fin, glyph_id_count, 'H')

    cmap = [0] * 0x10000

    for sid in six.moves.range(seg_count):
        if id_range_offset[sid] == 0:
            for cid in six.moves.range(start_count[sid], end_count[sid] + 1):
                cmap[cid] = (cid + id_delta[sid]) % 0x10000
        else:
            adj = start_count[sid] + seg_count - sid - id_range_offset[sid] // 2
            for cid in six.moves.range(start_count[sid], end_count[sid] + 1):
                glyph = glyph_ids[cid - adj]
                if glyph != 0:
                    glyph += id_delta[sid]
                cmap[cid] = glyph % 0x10000

    return cmap
示例#4
0
def test_seek():
    f = BlobIO(b'test' * 1000)

    assert f.tell() == 0

    f.seek(0, 2)
    assert f.tell() == 4000

    f.seek(-10, 2)
    assert f.tell() == 3990

    f.seek(-1, 1)
    assert f.tell() == 3989

    f.seek(-10000, 1)
    assert f.tell() == 0

    f.seek(1000, 0)
    assert f.tell() == 1000

    f.seek(2000, 0)
    assert f.tell() == 2000

    f.seek(3000)
    assert f.tell() == 3000

    f.seek(4000, 4)
    assert f.tell() == 4000