static int parse_patch_binary( git_patch_parsed *patch, git_patch_parse_ctx *ctx) { int error; if (parse_advance_expected_s(ctx, "GIT binary patch") < 0 || parse_advance_nl(ctx) < 0) return parse_err("corrupt git binary header at line %d", ctx->line_num); /* parse old->new binary diff */ if ((error = parse_patch_binary_side( &patch->base.binary.new_file, ctx)) < 0) return error; if (parse_advance_nl(ctx) < 0) return parse_err("corrupt git binary separator at line %d", ctx->line_num); /* parse new->old binary diff */ if ((error = parse_patch_binary_side( &patch->base.binary.old_file, ctx)) < 0) return error; if (parse_advance_nl(ctx) < 0) return parse_err("corrupt git binary patch separator at line %d", ctx->line_num); patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY; return 0; }
static int parse_patch_binary_nodata( git_patch_parsed *patch, git_patch_parse_ctx *ctx) { if (parse_advance_expected_str(ctx, "Binary files ") < 0 || parse_advance_expected_str(ctx, patch->header_old_path) < 0 || parse_advance_expected_str(ctx, " and ") < 0 || parse_advance_expected_str(ctx, patch->header_new_path) < 0 || parse_advance_expected_str(ctx, " differ") < 0 || parse_advance_nl(ctx) < 0) return parse_err("corrupt git binary header at line %"PRIuZ, ctx->line_num); patch->base.binary.contains_data = 0; patch->base.delta->flags |= GIT_DIFF_FLAG_BINARY; return 0; }
static int parse_patch_binary_side( git_diff_binary_file *binary, git_patch_parse_ctx *ctx) { git_diff_binary_t type = GIT_DIFF_BINARY_NONE; git_buf base85 = GIT_BUF_INIT, decoded = GIT_BUF_INIT; git_off_t len; int error = 0; if (parse_ctx_contains_s(ctx, "literal ")) { type = GIT_DIFF_BINARY_LITERAL; parse_advance_chars(ctx, 8); } else if (parse_ctx_contains_s(ctx, "delta ")) { type = GIT_DIFF_BINARY_DELTA; parse_advance_chars(ctx, 6); } else { error = parse_err( "unknown binary delta type at line %d", ctx->line_num); goto done; } if (parse_number(&len, ctx) < 0 || parse_advance_nl(ctx) < 0 || len < 0) { error = parse_err("invalid binary size at line %d", ctx->line_num); goto done; } while (ctx->line_len) { char c = ctx->line[0]; size_t encoded_len, decoded_len = 0, decoded_orig = decoded.size; if (c == '\n') break; else if (c >= 'A' && c <= 'Z') decoded_len = c - 'A' + 1; else if (c >= 'a' && c <= 'z') decoded_len = c - 'a' + (('z' - 'a') + 1) + 1; if (!decoded_len) { error = parse_err("invalid binary length at line %d", ctx->line_num); goto done; } parse_advance_chars(ctx, 1); encoded_len = ((decoded_len / 4) + !!(decoded_len % 4)) * 5; if (encoded_len > ctx->line_len - 1) { error = parse_err("truncated binary data at line %d", ctx->line_num); goto done; } if ((error = git_buf_decode_base85( &decoded, ctx->line, encoded_len, decoded_len)) < 0) goto done; if (decoded.size - decoded_orig != decoded_len) { error = parse_err("truncated binary data at line %d", ctx->line_num); goto done; } parse_advance_chars(ctx, encoded_len); if (parse_advance_nl(ctx) < 0) { error = parse_err("trailing data at line %d", ctx->line_num); goto done; } } binary->type = type; binary->inflatedlen = (size_t)len; binary->datalen = decoded.size; binary->data = git_buf_detach(&decoded); done: git_buf_free(&base85); git_buf_free(&decoded); return error; }