Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports
Path: blob/main/misc/bdelta/files/patch-file.h
18160 views
1
--- file.h.orig 2013-01-30 20:16:01 UTC
2
+++ file.h
3
@@ -3,6 +3,7 @@
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include <stdio.h>
7
+#include <cstdlib>
8
9
#define MAX_IO_BLOCK_SIZE (1024 * 1024)
10
11
@@ -58,6 +59,44 @@ unsigned read_dword(FILE *f) {
12
return (read_word(f) << 16) + low;
13
}
14
15
+static size_t scan_varint(const char* in,size_t len, unsigned long long* n) {
16
+ size_t i;
17
+ unsigned long long l;
18
+ if (len==0) return 0;
19
+ for (l=0, i=0; i<len; ++i) {
20
+ l+=(unsigned long long)(in[i]&0x7f) << (i*7);
21
+ if (!(in[i]&0x80)) {
22
+ *n=l;
23
+ return i+1;
24
+ }
25
+ }
26
+ return 0;
27
+}
28
+
29
+size_t scan_pb_type0_sint(const char* in,size_t len,signed long long* l) {
30
+ unsigned long long m;
31
+ size_t n=scan_varint(in,len,&m);
32
+ if (!n) return 0;
33
+ *l=(-(m&1)) ^ (m>>1);
34
+ return n;
35
+}
36
+
37
+long long read_varint(FILE* f) {
38
+ char buf[20];
39
+ size_t i;
40
+ long long l;
41
+ for (i=0; i<sizeof(buf); ++i) {
42
+ fread_fixed(f,buf+i,1);
43
+ if (!(buf[i]&0x80)) {
44
+ if (scan_pb_type0_sint(buf,i+1,&l)!=i+1) break;
45
+ return l;
46
+ }
47
+ }
48
+ static char read_error_message[128];
49
+ strcpy(read_error_message, "parse error: read_varint() failed");
50
+ throw read_error_message;
51
+}
52
+
53
void write_word(FILE *f, unsigned number) {
54
unsigned char b = number & 255,
55
b2 = number >> 8;
56
@@ -70,6 +109,31 @@ void write_dword(FILE *f, unsigned numbe
57
write_word(f, number >> 16);
58
}
59
60
+
61
+/* write int in least amount of bytes, return number of bytes */
62
+/* as used in varints from Google protocol buffers */
63
+static size_t fmt_varint(char* dest,unsigned long long l) {
64
+ /* high bit says if more bytes are coming, lower 7 bits are payload; little endian */
65
+ size_t i;
66
+ for (i=0; l; ++i, l>>=7) {
67
+ if (dest) dest[i]=(l&0x7f) | ((!!(l&~0x7f))<<7);
68
+ }
69
+ if (!i) { /* l was 0 */
70
+ if (dest) dest[0]=0;
71
+ ++i;
72
+ }
73
+ return i;
74
+}
75
+
76
+static size_t fmt_pb_type0_sint(char* dest,signed long long l) {
77
+ return fmt_varint(dest,(l << 1) ^ (l >> (sizeof(l)*8-1)));
78
+}
79
+
80
+void write_varint(FILE* f, long long number) {
81
+ char tmp[20];
82
+ fwrite_fixed(f,tmp,fmt_pb_type0_sint(tmp,number));
83
+}
84
+
85
bool fileExists(char *fname) {
86
FILE *f = fopen(fname, "rb");
87
bool exists = (f != NULL);
88
89