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