No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pack.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2018 Farhan Khan. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include <sys/stat.h>
  28. #include <sys/mman.h>
  29. #include <dirent.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <limits.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include "zlib_handler.h"
  37. #include "pack.h"
  38. #include "common.h"
  39. #include "ini.h"
  40. #include <netinet/in.h>
  41. unsigned long
  42. readvint(unsigned char **datap, unsigned char *top)
  43. {
  44. unsigned char *data = *datap;
  45. unsigned long opcode, size = 0;
  46. int i = 0;
  47. do {
  48. opcode = *data++;
  49. size |= (opcode & 0x7f) << i;
  50. i += 7;
  51. } while (opcode & BIT(7) && data < top);
  52. *datap = data;
  53. return size;
  54. }
  55. void
  56. applypatch(struct decompressed_object *base, struct decompressed_object *delta, struct objectinfo *objectinfo)
  57. {
  58. unsigned long size;
  59. unsigned char *data, *top;
  60. unsigned char *out, opcode;
  61. unsigned long cp_off, cp_size;
  62. data = delta->data;
  63. top = delta->data + delta->size;
  64. size = readvint(&data, top);
  65. if (size != base->size) {
  66. printf("Error, bad original set\n");
  67. exit(0);
  68. }
  69. size = readvint(&data, top);
  70. objectinfo->data = malloc(size);
  71. objectinfo->isize = size;
  72. out = objectinfo->data;
  73. while(data < top) {
  74. opcode = *data++;
  75. if (opcode & BIT(7)) {
  76. cp_off = 0;
  77. cp_size = 0;
  78. /* Offset in base */
  79. if (opcode & BIT(0))
  80. cp_off |= (*data++ << 0);
  81. if (opcode & BIT(1))
  82. cp_off |= (*data++ << 8);
  83. if (opcode & BIT(2))
  84. cp_off |= (*data++ << 16);
  85. if (opcode & BIT(3))
  86. cp_off |= (*data++ << 24);
  87. /* Length to copy */
  88. if (opcode & BIT(4))
  89. cp_size |= (*data++ << 0);
  90. if (opcode & BIT(5))
  91. cp_size |= (*data++ << 8);
  92. if (opcode & BIT(6))
  93. cp_size |= (*data++ << 16);
  94. if (cp_size == 0)
  95. cp_size = 0x10000;
  96. if (cp_off + cp_size > base->size) { // || cp_size > size) {
  97. printf("Bad length: First\n");
  98. exit(0);
  99. }
  100. memcpy(out, (char *) base->data + cp_off, cp_size);
  101. out += cp_size;
  102. } else if (opcode) {
  103. memcpy(out, data, opcode);
  104. out += opcode;
  105. data += opcode;
  106. } else {
  107. printf("Unexpected opcode 0x00, exiting.\n");
  108. exit(0);
  109. }
  110. }
  111. }
  112. /*
  113. * This function reassembles an OBJ_OFS_DELTA object. It requires that the
  114. * objectinfo variable has already passed through pack_object_header.
  115. * It starts by inflating the base object. Then, it loops through each delta
  116. * by offset, inflates the contents and applies the patch.
  117. *
  118. * Note: This is a memory-extensive function, as it requires a copy of the base
  119. * object and fully patched object in memory at once and I cannot think of any
  120. * way to only have one object in memory at once.
  121. */
  122. void
  123. pack_delta_content(int packfd, struct objectinfo *objectinfo)
  124. {
  125. struct decompressed_object base_object, delta_object;
  126. int q;
  127. base_object.data = NULL;
  128. base_object.size = 0;
  129. lseek(packfd, objectinfo->base, SEEK_SET);
  130. deflate_caller(packfd, buffer_cb, &base_object);
  131. for(q=objectinfo->ndeltas;q>0;q--) {
  132. lseek(packfd, objectinfo->deltas[q-1], SEEK_SET);
  133. delta_object.data = NULL;
  134. delta_object.size = 0;
  135. deflate_caller(packfd, buffer_cb, &delta_object);
  136. applypatch(&base_object, &delta_object, objectinfo);
  137. free(base_object.data);
  138. free(delta_object.data);
  139. base_object.data = objectinfo->data;
  140. base_object.size = objectinfo->isize;
  141. }
  142. }
  143. /* Used by index-pack to compute SHA and get offset bytes */
  144. unsigned char *
  145. pack_get_index_bytes_cb(unsigned char *buf, int size, int deflated_bytes, void *arg)
  146. {
  147. struct index_generate_arg *index_generate_arg = arg;
  148. SHA1_Update(&index_generate_arg->shactx, buf, size);
  149. index_generate_arg->bytes += deflated_bytes;
  150. return buf;
  151. }
  152. int
  153. pack_get_packfile_offset(char *sha_str, char *filename)
  154. {
  155. DIR *d;
  156. struct dirent *dir;
  157. char packdir[PATH_MAX];
  158. int packfd;
  159. char *file_ext;
  160. unsigned char *idxmap;
  161. struct stat sb;
  162. int offset = 0;
  163. uint8_t sha_bin[20];
  164. int i;
  165. for (i=0;i<20;i++)
  166. sscanf(sha_str+i*2, "%2hhx", &sha_bin[i]);
  167. sprintf(packdir, "%s/objects/pack", dotgitpath);
  168. d = opendir(packdir);
  169. /* Find hash in idx file or die */
  170. if (d) {
  171. while((dir = readdir(d)) != NULL) {
  172. file_ext = strrchr(dir->d_name, '.');
  173. if (!file_ext || strncmp(file_ext, ".idx", 4))
  174. continue;
  175. sprintf(filename, "%s/objects/pack/%s", dotgitpath, dir->d_name);
  176. packfd = open(filename, O_RDONLY);
  177. fstat(packfd, &sb);
  178. idxmap = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, packfd, 0);
  179. if (idxmap == NULL) {
  180. fprintf(stderr, "mmap(2) error, exiting.\n");
  181. exit(0);
  182. }
  183. close(packfd);
  184. offset = pack_find_sha_offset(sha_bin, idxmap);
  185. munmap(idxmap, sb.st_size);
  186. if (offset != -1)
  187. break;
  188. }
  189. }
  190. closedir(d);
  191. return offset;
  192. }
  193. void
  194. pack_parse_header(int packfd, struct packfilehdr *packfilehdr)
  195. {
  196. int version;
  197. int nobjects;
  198. unsigned char buf[4];
  199. read(packfd, buf, 4);
  200. if (memcmp(buf, "PACK", 4)) {
  201. fprintf(stderr, "error: bad object header. Git repository may be corrupt.\n");
  202. exit(128);
  203. }
  204. read(packfd, &version, 4);
  205. packfilehdr->version = ntohl(version);
  206. if (packfilehdr->version != 2) {
  207. fprintf(stderr, "error: unsupported version: %d\n", version);
  208. exit(128);
  209. }
  210. read(packfd, &nobjects, 4);
  211. packfilehdr->nobjects = ntohl(nobjects);
  212. }
  213. /*
  214. * The function pack_object_header checks the object type of the current object.
  215. * If the type is not deltified, it will immediately return and the variable
  216. * objectinfo will contain the type as objectinfo->ftype. Additionally, we will
  217. * have the objectinfo->isize.
  218. *
  219. * If the object is deltified, it will iteratively call object_header_ofs to
  220. * locate the following values:
  221. * A. The base object type, stored in objectinto->ftype
  222. * B. The base offset, stored in objectinfo->base
  223. * C. All delta offsets, stored in objectinfo->deltas
  224. * D. Number of deltas, stored in objectinfo->ndelta
  225. * Note: The objectinfo->isize value is NOT captured
  226. *
  227. * If the application needs the objectinfo->isize or objectinfo->data, it must
  228. * run pack_delta_content. This function will consumed values B-D to produce
  229. * the final non-deltified data.
  230. */
  231. void
  232. object_header_ofs(int packfd, int offset, int layer, struct objectinfo *childinfo, struct objectinfo *objectinfo)
  233. {
  234. unsigned long c;
  235. lseek(packfd, offset, SEEK_SET);
  236. read(packfd, &c, sizeof(unsigned long));
  237. childinfo->used = 1;
  238. childinfo->psize = c & 0xf;
  239. childinfo->ptype = (c >> 4) & 0x7;
  240. while(c & 0x80) {
  241. lseek(packfd, offset + childinfo->used, SEEK_SET);
  242. read(packfd, &c, sizeof(unsigned long));
  243. childinfo->psize += (c & 0x7F) << (4 + (7*(childinfo->used-1)));
  244. childinfo->used++;
  245. }
  246. if (childinfo->ptype != OBJ_OFS_DELTA) {
  247. childinfo->ftype = childinfo->ptype;
  248. objectinfo->deltas = malloc(sizeof(unsigned long) * (layer + 1));
  249. objectinfo->base = offset + childinfo->used;
  250. return;
  251. }
  252. int q;
  253. unsigned long delta;
  254. int r = 1;
  255. lseek(packfd, offset + childinfo->used, SEEK_SET);
  256. read(packfd, &q, 1);
  257. delta = q & 0x7f;
  258. while(q & BIT(7)) {
  259. r++;
  260. delta++;
  261. read(packfd, &q, 1);
  262. delta = (delta << 7) + (q & 0x7f);
  263. }
  264. object_header_ofs(packfd, offset - delta, layer+1, childinfo, objectinfo);
  265. objectinfo->deltas[layer] = offset + childinfo->used + r;
  266. objectinfo->ndeltas++;
  267. }
  268. void
  269. pack_object_header(int packfd, int offset, struct objectinfo *objectinfo)
  270. {
  271. unsigned long c;
  272. objectinfo->offset = offset;
  273. objectinfo->used = 1;
  274. read(packfd, &c, sizeof(unsigned long));
  275. objectinfo->psize = c & 0x0F;
  276. objectinfo->ptype = (c >> 4) & 0x7;
  277. while(c & 0x80) {
  278. lseek(packfd, offset + objectinfo->used, SEEK_SET);
  279. read(packfd, &c, sizeof(unsigned long));
  280. objectinfo->psize += (c & 0x7F) << (4 + (7*(objectinfo->used-1)));
  281. objectinfo->used++;
  282. }
  283. if (objectinfo->ptype != OBJ_OFS_DELTA && objectinfo->ptype != OBJ_REF_DELTA) {
  284. objectinfo->ftype = objectinfo->ptype;
  285. return;
  286. }
  287. else {
  288. objectinfo->isize = objectinfo->psize;
  289. }
  290. /* We have to dig deeper */
  291. struct objectinfo childinfo;
  292. objectinfo->ndeltas = 0;
  293. objectinfo->base = 0;
  294. childinfo.ptype = OBJ_OFS_DELTA;
  295. object_header_ofs(packfd, offset, 0, &childinfo, objectinfo);
  296. objectinfo->ftype = childinfo.ftype;
  297. return;
  298. }
  299. int
  300. pack_find_sha_offset(unsigned char *sha, unsigned char *idxmap)
  301. {
  302. struct fan *fans;
  303. struct entry *entries;
  304. struct checksum *checksums;
  305. struct offset *offsets;
  306. int idx_offset;
  307. char idx_version;
  308. int nelements;
  309. int n;
  310. if (memcmp(idxmap, "\xff\x74\x4f\x63", 4)) {
  311. fprintf(stderr, "Header signature does not match index version 2.\n");
  312. exit(0);
  313. }
  314. idx_offset = 4;
  315. idx_version = *(idxmap + idx_offset+3);
  316. if (idx_version != 2) {
  317. fprintf(stderr, "opengit currently only supports version 2. Exiting.\n");
  318. exit(0);
  319. }
  320. idx_offset += 4;
  321. // Get the fan table and capture last element
  322. fans = (struct fan *)(idxmap + idx_offset);
  323. nelements = ntohl(fans->count[255]);
  324. // Move to SHA entries
  325. idx_offset += sizeof(struct fan);
  326. // Point to SHA entries
  327. entries = (struct entry *)(idxmap + idx_offset);
  328. for(n=0;n<nelements;n++)
  329. if (!memcmp(entries[n].sha, sha, 20))
  330. break;
  331. if (n==nelements)
  332. return -1;
  333. // Move to Checksums
  334. idx_offset += (sizeof(struct entry) * nelements);
  335. // Point to checksums
  336. checksums = (struct checksum *)(idxmap + idx_offset);
  337. // Move to Offsets
  338. idx_offset += (nelements * 4);
  339. // Capture Offsets
  340. offsets = (struct offset *)(idxmap + idx_offset);
  341. return ntohl(offsets[n].addr);
  342. }