summaryrefslogtreecommitdiff
path: root/ubi-utils/sort-me-out/peb.c
blob: 160a46342d68e9151a8a562d87c18b083b886042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (c) International Business Machines Corp., 2006
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include "peb.h"

int
peb_cmp(peb_t eb_1, peb_t eb_2)
{
	assert(eb_1);
	assert(eb_2);

	return eb_1->num == eb_2->num ? 0
		: eb_1->num > eb_2->num ? 1 : -1;
}

int
peb_new(uint32_t eb_num, uint32_t peb_size, peb_t *peb)
{
	int rc = 0;

	peb_t res = (peb_t) malloc(sizeof(struct peb));
	if (!res) {
		rc = -ENOMEM;
		goto err;
	}

	res->num  = eb_num;
	res->size = peb_size;
	res->data = (uint8_t*) malloc(res->size * sizeof(uint8_t));
	if (!res->data) {
		rc = -ENOMEM;
		goto err;
	}
	memset(res->data, 0xff, res->size);

	*peb = res;
	return 0;
err:
	if (res) {
		if (res->data)
			free(res->data);
		free(res);
	}
	*peb = NULL;
	return rc;
}

int
peb_fill(peb_t peb, uint8_t* buf, size_t buf_size)
{
	if (!peb)
		return -EINVAL;

	if (buf_size > peb->size)
		return -EINVAL;

	memcpy(peb->data, buf, buf_size);
	return 0;
}

int
peb_write(FILE* fp_out, peb_t peb)
{
	size_t written = 0;

	if (peb == NULL)
		return -EINVAL;

	written = fwrite(peb->data, 1, peb->size, fp_out);

	if (written != peb->size)
		return -EIO;

	return 0;
}

int
peb_free(peb_t* peb)
{
	peb_t tmp = *peb;
	if (tmp) {
		if (tmp->data)
			free(tmp->data);
		free(tmp);
	}
	*peb = NULL;

	return 0;
}

void peb_dump(FILE* fp_out, peb_t peb)
{
	fprintf(fp_out, "num: %08d\tsize: 0x%08x\n", peb->num, peb->size);
}