blob: 9ca07d9ce2756df5a5a8403e0aca75f8bda6b8dd [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
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#ifndef _BUILD_TOOLS_APPLYPATCH_UTILS_H
18#define _BUILD_TOOLS_APPLYPATCH_UTILS_H
19
20#include <sys/types.h>
21
22#include <stdio.h>
23#include <sys/stat.h>
24#include <errno.h>
25#include <string.h>
26#include <stdlib.h>
27#include <assert.h>
28#include <err.h>
29#include <fcntl.h>
30
31#ifndef _WIN32
32#include <unistd.h>
33#endif
34
35#define free_ptr(ptr) if(ptr!=NULL)free(ptr);ptr=NULL;
36#define close_fp(fp) if(fp!=NULL)fclose(fp);fp=NULL;
37#define safe_free(x) do { if(x) {free(x); x=NULL;} } while(0)
38
39#define GOERR_IF_FAIL(x) do{if((x) < 0) goto out_error;}while(0)
40
41#define GOERR_IF_NOK(x) do{if((x) != 0) goto out_error;}while(0)
42
43typedef unsigned int uint32_t;
44typedef int int32_t;
45typedef unsigned char u_char;
46typedef unsigned char uint8_t;
47
48#define MIN(x,y) (((x)<(y)) ? (x) : (y))
49#define MAX(x,y) (((x)>(y)) ? (x) : (y))
50
51#define safe_free(x) do { if(x) {free(x); x=NULL;} } while(0)
52
53#define log(fmt, args...) do {printf("[%s:%d]"fmt"\n", __FUNCTION__, __LINE__, ##args);}while(0)
54#define LOGD(fmt, args...) do {printf("[%s:%d]"fmt"\n", __FUNCTION__, __LINE__, ##args);}while(0)
55#define LOGE(fmt, args...) do {printf("[%s:%d]"fmt"\n", __FUNCTION__, __LINE__, ##args);}while(0)
56
57#define GOERR_IF_FAIL(x) do{if((x) < 0) goto out_error;}while(0)
58
59unsigned char * readfile(const char *dir, const char *name, size_t *len);
60int write_file(FILE* fp, const void *buf, int count);
61
62void dump_data(const u_char* data, int n);
63uint32_t little_endian_offin(u_char *buf);
64void little_endian_offout(uint32_t x, u_char *buf);
65FILE* create_new_file(const char* name);
66char *dir_name(char * path);
67int write_file_by_path(const char*path, const char*value, int size);
68int read_file_by_path(const char*path, char*buf, int sz);
69
70// Read and write little-endian values of various sizes.
71
72void Write4(int value, FILE* f);
73void Write8(long long value, FILE* f);
74int Read2(void* p);
75int Read4(void* p);
76long long Read8(void* p);
77
78#endif // _BUILD_TOOLS_APPLYPATCH_UTILS_H