private static unsafe int append_mem(minimp4_vector_t v, void *mem, int bytes) { int i; var size = new byte[2]; var p = v.data; for (i = 0; i + 2 < v.bytes;) { int cb = p[i] * 256 + p[i + 1]; var subarray = new byte[cb]; Array.Copy(p, i + 2, subarray, 0, cb); if (cb == bytes && !memcmp(subarray, mem, cb)) { return(1); } i += 2 + cb; } size[0] = (byte)(bytes >> 8); size[1] = (byte)bytes; fixed(byte *buf = size) { return(minimp4_vector_put(v, buf, 2) != -1 && minimp4_vector_put(v, mem, bytes) != -1 ? 1 : 0); } }
static bool minimp4_vector_grow(minimp4_vector_t h, int bytes) { byte[] p; int new_size = h.capacity * 2 + 1024; if (new_size < h.capacity + bytes) { new_size = h.capacity + bytes + 1024; } p = new byte[new_size]; if (p == null) { return(false); } unsafe { fixed(byte *pp = h.data) { memcpy(p, pp, h.capacity); } } h.data = p; h.capacity = new_size; return(true); }
private static bool minimp4_vector_init(minimp4_vector_t h, int capacity) { h.bytes = 0; h.capacity = capacity; h.data = capacity != 0 ? new byte[capacity] : null; return(capacity == 0 || h.data == null); }
private static unsafe int minimp4_vector_put(minimp4_vector_t h, void *buf, int bytes) { int tail = minimp4_vector_alloc_tail(h, bytes); if (tail != -1) { fixed(byte *data = h.data) memcpy(data + tail, buf, bytes); } return(tail); }
private static unsafe int minimp4_vector_alloc_tail(minimp4_vector_t h, int bytes) { if (h.data == null && !minimp4_vector_init(h, 2 * bytes + 1024)) { return(-1); } if (h.capacity - h.bytes < bytes && !minimp4_vector_grow(h, bytes)) { return(-1); } Debug.Assert(h.data != null); Debug.Assert(h.capacity - h.bytes >= bytes); int p = h.bytes; h.bytes += bytes; return(p); }