-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_info.h
94 lines (80 loc) · 2.99 KB
/
file_info.h
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
/* Copyright (c) 2012 Francis Russell <francis@unchartedbackwaters.co.uk>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef FILE_INFO_H
#define FILE_INFO_H
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include "checksum.h"
#include "errors.h"
static const size_t BUFFER_SIZE = 4 * 1048576;
typedef struct
{
FILE *file;
off_t total_length;
off_t block_offset;
long internal_offset;
long buffer_use;
checksum_t checksum;
unsigned char *prev_buffer;
unsigned char *buffer;
} file_info_t;
typedef struct
{
off_t matching_bytes;
off_t total_bytes;
} match_info_t;
status_t open_input_file(file_info_t *info, const char *path, size_t checksum_length);
status_t close_input_file(file_info_t *info);
status_t seek_file(file_info_t *info, off_t offset);
status_t populate_forwards(file_info_t *file);
status_t find_checksum_match(const file_info_t *f1_info, file_info_t *f2_info, int *result);
status_t advance_location(file_info_t *file);
status_t validate_match(file_info_t *f1_info, file_info_t *f2_info, int *result);
status_t write_merged_file(file_info_t *f1_info, file_info_t *f2_info, FILE *out);
status_t compute_match_info(FILE *f1, FILE *f2, match_info_t *info);
status_t get_match_info(file_info_t *f1_info, file_info_t *f2_info, match_info_t *info);
static inline unsigned char get_byte(file_info_t *const info, const long offset)
{
const long local_offset = info->internal_offset + offset;
assert(offset <= 0);
assert(local_offset + BUFFER_SIZE >= 0);
if (local_offset >= 0)
return info->buffer[local_offset];
else
return info->prev_buffer[BUFFER_SIZE + local_offset];
}
static inline off_t file_length(const file_info_t *const info)
{
return info->total_length;
}
static inline int hit_file_end(const file_info_t *const file)
{
return file->total_length == file->internal_offset + file->block_offset;
}
static inline off_t characters_handled(const file_info_t *const info)
{
return info->block_offset + info->internal_offset;
}
#endif