1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef EBMLUTIL_H_
18 #define EBMLUTIL_H_
19 
20 #include <stdint.h>
21 
22 namespace webm {
23 
24 // Encode the id and/or size of an EBML element bytes by setting a leading length descriptor bit:
25 //
26 //   1xxxxxxx                                                                - 1-byte values
27 //   01xxxxxx xxxxxxxx                                                       -
28 //   001xxxxx xxxxxxxx xxxxxxxx                                              -
29 //   0001xxxx xxxxxxxx xxxxxxxx xxxxxxxx                                     - ...
30 //   00001xxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx                            -
31 //   000001xx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx                   -
32 //   0000001x xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx          -
33 //   00000001 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx - 8-byte values
34 //
35 // This function uses the least the number of bytes possible.
36 uint64_t encodeUnsigned(uint64_t u);
37 
38 // Like above but pads the input value with leading zeros up to the specified width. The length
39 // descriptor is calculated based on width.
40 uint64_t encodeUnsigned(uint64_t u, int width);
41 
42 // Serialize an EBML coded id or size in big-endian order.
43 int serializeCodedUnsigned(uint64_t u, uint8_t* bary);
44 
45 // Calculate the length of an EBML coded id or size from its length descriptor.
46 int sizeOf(uint64_t u);
47 
48 }
49 
50 #endif /* EBMLUTIL_H_ */
51