blob: 41ff676dc12931cb3e04e77800eacf11e83f9aa9 [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. */
30void 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
Doug Zongkerc4351c72010-02-22 14:46:32 -080041int Read2(void* pv) {
42 unsigned char* p = pv;
43 return (int)(((unsigned int)p[1] << 8) |
44 (unsigned int)p[0]);
Doug Zongker512536a2010-02-17 16:11:44 -080045}
46
Doug Zongkerc4351c72010-02-22 14:46:32 -080047int Read4(void* pv) {
48 unsigned char* p = 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]);
Doug Zongker512536a2010-02-17 16:11:44 -080053}
54
Doug Zongkerc4351c72010-02-22 14:46:32 -080055long long Read8(void* pv) {
56 unsigned char* p = 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]);
Doug Zongker512536a2010-02-17 16:11:44 -080065}