Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 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. */ |
| 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. */ |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 30 | void Write8(int64_t value, FILE* f) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 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 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 41 | int Read2(void* pv) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 42 | unsigned char* p = reinterpret_cast<unsigned char*>(pv); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 43 | return (int)(((unsigned int)p[1] << 8) | |
| 44 | (unsigned int)p[0]); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 47 | int Read4(void* pv) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 48 | unsigned char* p = reinterpret_cast<unsigned char*>(pv); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 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]); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 55 | int64_t Read8(void* pv) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 56 | unsigned char* p = reinterpret_cast<unsigned char*>(pv); |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 57 | 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 Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 65 | } |