Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // #include <strmif.h> |
michael@0 | 2 | #include "EbmlBufferWriter.h" |
michael@0 | 3 | #include "EbmlWriter.h" |
michael@0 | 4 | // #include <cassert> |
michael@0 | 5 | // #include <limits> |
michael@0 | 6 | // #include <malloc.h> //_alloca |
michael@0 | 7 | #include <stdlib.h> |
michael@0 | 8 | #include <wchar.h> |
michael@0 | 9 | #include <string.h> |
michael@0 | 10 | |
michael@0 | 11 | void |
michael@0 | 12 | Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, int buffer_size, unsigned long len) |
michael@0 | 13 | { |
michael@0 | 14 | /* buffer_size: |
michael@0 | 15 | * 1 - int8_t; |
michael@0 | 16 | * 2 - int16_t; |
michael@0 | 17 | * 3 - int32_t; |
michael@0 | 18 | * 4 - int64_t; |
michael@0 | 19 | */ |
michael@0 | 20 | long i; |
michael@0 | 21 | for(i = len-1; i >= 0; i--) { |
michael@0 | 22 | unsigned char x; |
michael@0 | 23 | if (buffer_size == 1) { |
michael@0 | 24 | x = (char)(*(const int8_t *)buffer_in >> (i * 8)); |
michael@0 | 25 | } else if (buffer_size == 2) { |
michael@0 | 26 | x = (char)(*(const int16_t *)buffer_in >> (i * 8)); |
michael@0 | 27 | } else if (buffer_size == 4) { |
michael@0 | 28 | x = (char)(*(const int32_t *)buffer_in >> (i * 8)); |
michael@0 | 29 | } else if (buffer_size == 8) { |
michael@0 | 30 | x = (char)(*(const int64_t *)buffer_in >> (i * 8)); |
michael@0 | 31 | } |
michael@0 | 32 | Ebml_Write(glob, &x, 1); |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | void Ebml_Write(EbmlGlobal *glob, const void *buffer_in, unsigned long len) { |
michael@0 | 37 | unsigned char *src = glob->buf; |
michael@0 | 38 | src += glob->offset; |
michael@0 | 39 | memcpy(src, buffer_in, len); |
michael@0 | 40 | glob->offset += len; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | static void _Serialize(EbmlGlobal *glob, const unsigned char *p, const unsigned char *q) { |
michael@0 | 44 | while (q != p) { |
michael@0 | 45 | --q; |
michael@0 | 46 | |
michael@0 | 47 | memcpy(&(glob->buf[glob->offset]), q, 1); |
michael@0 | 48 | glob->offset++; |
michael@0 | 49 | } |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | /* |
michael@0 | 53 | void Ebml_Serialize(EbmlGlobal *glob, const void *buffer_in, unsigned long len) { |
michael@0 | 54 | // assert(buf); |
michael@0 | 55 | |
michael@0 | 56 | const unsigned char *const p = (const unsigned char *)(buffer_in); |
michael@0 | 57 | const unsigned char *const q = p + len; |
michael@0 | 58 | |
michael@0 | 59 | _Serialize(glob, p, q); |
michael@0 | 60 | } |
michael@0 | 61 | */ |
michael@0 | 62 | |
michael@0 | 63 | void Ebml_StartSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc, unsigned long class_id) { |
michael@0 | 64 | unsigned long long unknownLen = 0x01FFFFFFFFFFFFFFLL; |
michael@0 | 65 | Ebml_WriteID(glob, class_id); |
michael@0 | 66 | ebmlLoc->offset = glob->offset; |
michael@0 | 67 | // todo this is always taking 8 bytes, this may need later optimization |
michael@0 | 68 | Ebml_Serialize(glob, (void *)&unknownLen,sizeof(unknownLen), 8); // this is a key that says lenght unknown |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | void Ebml_EndSubElement(EbmlGlobal *glob, EbmlLoc *ebmlLoc) { |
michael@0 | 72 | unsigned long long size = glob->offset - ebmlLoc->offset - 8; |
michael@0 | 73 | unsigned long long curOffset = glob->offset; |
michael@0 | 74 | glob->offset = ebmlLoc->offset; |
michael@0 | 75 | size |= 0x0100000000000000LL; |
michael@0 | 76 | Ebml_Serialize(glob, &size,sizeof(size), 8); |
michael@0 | 77 | glob->offset = curOffset; |
michael@0 | 78 | } |
michael@0 | 79 |