1 /*
2  * Copyright (C) 2009 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 #include <stdio.h>
18 
19 #include "utils.h"
20 
21 /** Write a 4-byte value to f in little-endian order. */
Write4(int value,FILE * f)22 void Write4(int value, FILE* f) {
23   fputc(value & 0xff, f);
24   fputc((value >> 8) & 0xff, f);
25   fputc((value >> 16) & 0xff, f);
26   fputc((value >> 24) & 0xff, f);
27 }
28 
29 /** Write an 8-byte value to f in little-endian order. */
Write8(long long value,FILE * f)30 void Write8(long long value, FILE* f) {
31   fputc(value & 0xff, f);
32   fputc((value >> 8) & 0xff, f);
33   fputc((value >> 16) & 0xff, f);
34   fputc((value >> 24) & 0xff, f);
35   fputc((value >> 32) & 0xff, f);
36   fputc((value >> 40) & 0xff, f);
37   fputc((value >> 48) & 0xff, f);
38   fputc((value >> 56) & 0xff, f);
39 }
40 
Read2(void * pv)41 int Read2(void* pv) {
42     unsigned char* p = reinterpret_cast<unsigned char*>(pv);
43     return (int)(((unsigned int)p[1] << 8) |
44                  (unsigned int)p[0]);
45 }
46 
Read4(void * pv)47 int Read4(void* pv) {
48     unsigned char* p = reinterpret_cast<unsigned char*>(pv);
49     return (int)(((unsigned int)p[3] << 24) |
50                  ((unsigned int)p[2] << 16) |
51                  ((unsigned int)p[1] << 8) |
52                  (unsigned int)p[0]);
53 }
54 
Read8(void * pv)55 long long Read8(void* pv) {
56     unsigned char* p = reinterpret_cast<unsigned char*>(pv);
57     return (long long)(((unsigned long long)p[7] << 56) |
58                        ((unsigned long long)p[6] << 48) |
59                        ((unsigned long long)p[5] << 40) |
60                        ((unsigned long long)p[4] << 32) |
61                        ((unsigned long long)p[3] << 24) |
62                        ((unsigned long long)p[2] << 16) |
63                        ((unsigned long long)p[1] << 8) |
64                        (unsigned long long)p[0]);
65 }
66