blob: 450dc8d76825dfcf78ed52199208df5c4483023a [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
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. */
22void 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. */
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070030void Write8(int64_t value, FILE* f) {
Doug Zongker512536a2010-02-17 16:11:44 -080031 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
Tianjie Xuaced5d92016-10-12 10:55:04 -070041int Read2(const void* pv) {
42 const unsigned char* p = reinterpret_cast<const unsigned char*>(pv);
Doug Zongkerc4351c72010-02-22 14:46:32 -080043 return (int)(((unsigned int)p[1] << 8) |
44 (unsigned int)p[0]);
Doug Zongker512536a2010-02-17 16:11:44 -080045}
46
Tianjie Xuaced5d92016-10-12 10:55:04 -070047int Read4(const void* pv) {
48 const unsigned char* p = reinterpret_cast<const unsigned char*>(pv);
Doug Zongkerc4351c72010-02-22 14:46:32 -080049 return (int)(((unsigned int)p[3] << 24) |
50 ((unsigned int)p[2] << 16) |
51 ((unsigned int)p[1] << 8) |
52 (unsigned int)p[0]);
Doug Zongker512536a2010-02-17 16:11:44 -080053}
54
Tianjie Xuaced5d92016-10-12 10:55:04 -070055int64_t Read8(const void* pv) {
56 const unsigned char* p = reinterpret_cast<const unsigned char*>(pv);
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070057 return (int64_t)(((uint64_t)p[7] << 56) |
58 ((uint64_t)p[6] << 48) |
59 ((uint64_t)p[5] << 40) |
60 ((uint64_t)p[4] << 32) |
61 ((uint64_t)p[3] << 24) |
62 ((uint64_t)p[2] << 16) |
63 ((uint64_t)p[1] << 8) |
64 (uint64_t)p[0]);
Doug Zongker512536a2010-02-17 16:11:44 -080065}