blob: 912229bcfc436f0359d686c785588759903f9ab0 [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
41int Read2(unsigned char* p) {
42 return (int)(((unsigned int)p[1] << 8) |
43 (unsigned int)p[0]);
44}
45
46int Read4(unsigned char* p) {
47 return (int)(((unsigned int)p[3] << 24) |
48 ((unsigned int)p[2] << 16) |
49 ((unsigned int)p[1] << 8) |
50 (unsigned int)p[0]);
51}
52
53long long Read8(unsigned char* p) {
54 return (long long)(((unsigned long long)p[7] << 56) |
55 ((unsigned long long)p[6] << 48) |
56 ((unsigned long long)p[5] << 40) |
57 ((unsigned long long)p[4] << 32) |
58 ((unsigned long long)p[3] << 24) |
59 ((unsigned long long)p[2] << 16) |
60 ((unsigned long long)p[1] << 8) |
61 (unsigned long long)p[0]);
62}