Android-cuttlefish cvd tool
image.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 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 __FEC_H__
18#define __FEC_H__
19
20#include <utils/Compat.h>
21#include <string>
22#include <vector>
23#include <fec/io.h>
24#include <fec/ecc.h>
25
26#define IMAGE_MIN_THREADS 1
27#define IMAGE_MAX_THREADS 128
28
29#define INFO(x...) \
30 fprintf(stderr, x);
31#define FATAL(x...) { \
32 fprintf(stderr, x); \
33 exit(1); \
34}
35
36#define unlikely(x) __builtin_expect(!!(x), 0)
37
38struct image {
39 /* if true, decode file in place instead of creating a new output file */
40 bool inplace;
41 /* if true, assume input is a sparse file */
42 bool sparse;
43 /* if true, print more verbose information to stderr */
44 bool verbose;
45 const char *fec_filename;
46 int fec_fd;
47 int inp_fd;
48 /* the number of Reed-Solomon generator polynomial roots, also the number
49 of parity bytes generated for each N bytes in RS(M, N) */
50 int roots;
51 /* for RS(M, N), N = M - roots */
52 int rs_n;
54 uint32_t fec_size;
55 uint32_t padding;
56 uint64_t blocks;
57 uint64_t inp_size;
58 uint64_t pos;
59 uint64_t rounds;
60 uint64_t rv;
61 uint8_t *fec;
62 uint8_t *input;
63 uint8_t *output;
64};
65
66struct image_proc_ctx;
68
71 int id;
73 uint64_t rv;
74 uint64_t fec_pos;
75 uint64_t start;
76 uint64_t end;
77 void *rs;
78};
79
80extern bool image_load(const std::vector<std::string>& filename, image *ctx);
81extern bool image_save(const std::string& filename, image *ctx);
82
83extern bool image_ecc_new(const std::string& filename, image *ctx);
84extern bool image_ecc_load(const std::string& filename, image *ctx);
85extern bool image_ecc_save(image *ctx);
86
87extern bool image_process(image_proc_func f, image *ctx);
88
89extern void image_init(image *ctx);
90extern void image_free(image *ctx);
91
92inline uint8_t image_get_interleaved_byte(uint64_t i, image *ctx)
93{
94 uint64_t offset = fec_ecc_interleave(i, ctx->rs_n, ctx->rounds);
95
96 if (unlikely(offset >= ctx->inp_size)) {
97 return 0;
98 }
99
100 return ctx->input[offset];
101}
102
103inline void image_set_interleaved_byte(uint64_t i, image *ctx,
104 uint8_t value)
105{
106 uint64_t offset = fec_ecc_interleave(i, ctx->rs_n, ctx->rounds);
107
108 if (unlikely(offset >= ctx->inp_size)) {
109 assert(value == 0);
110 } else if (ctx->output && ctx->output[offset] != value) {
111 ctx->output[offset] = value;
112 }
113}
114
115#endif // __FEC_H__
uint64_t fec_ecc_interleave(uint64_t offset, int rsn, uint64_t rounds)
Definition: ecc.h:51
bool image_load(const std::vector< std::string > &filename, image *ctx)
Definition: image.cpp:145
void(* image_proc_func)(image_proc_ctx *)
Definition: image.h:67
void image_init(image *ctx)
Definition: image.cpp:46
void image_free(image *ctx)
Definition: image.cpp:51
bool image_save(const std::string &filename, image *ctx)
Definition: image.cpp:173
bool image_ecc_save(image *ctx)
Definition: image.cpp:292
bool image_ecc_new(const std::string &filename, image *ctx)
Definition: image.cpp:192
#define unlikely(x)
Definition: image.h:36
void image_set_interleaved_byte(uint64_t i, image *ctx, uint8_t value)
Definition: image.h:103
uint8_t image_get_interleaved_byte(uint64_t i, image *ctx)
Definition: image.h:92
bool image_process(image_proc_func f, image *ctx)
Definition: image.cpp:353
bool image_ecc_load(const std::string &filename, image *ctx)
Definition: image.cpp:212
Definition: image.h:69
uint64_t fec_pos
Definition: image.h:74
uint64_t rv
Definition: image.h:73
image * ctx
Definition: image.h:72
image_proc_func func
Definition: image.h:70
uint64_t end
Definition: image.h:76
uint64_t start
Definition: image.h:75
void * rs
Definition: image.h:77
int id
Definition: image.h:71
Definition: image.h:38
const char * fec_filename
Definition: image.h:45
int fec_fd
Definition: image.h:46
uint8_t * input
Definition: image.h:62
uint8_t * output
Definition: image.h:63
bool sparse
Definition: image.h:42
uint32_t fec_size
Definition: image.h:54
bool verbose
Definition: image.h:44
uint64_t pos
Definition: image.h:58
int threads
Definition: image.h:53
int rs_n
Definition: image.h:52
uint32_t padding
Definition: image.h:55
bool inplace
Definition: image.h:40
int inp_fd
Definition: image.h:47
uint64_t rv
Definition: image.h:60
uint8_t * fec
Definition: image.h:61
uint64_t blocks
Definition: image.h:56
uint64_t inp_size
Definition: image.h:57
int roots
Definition: image.h:50
uint64_t rounds
Definition: image.h:59