| 1 | /** |
| 2 | * Copyright 2013, GitHub, Inc |
| 3 | * Copyright 2009-2013, Daniel Lemire, Cliff Moon, |
| 4 | * David McIntosh, Robert Becho, Google Inc. and Veronika Zenz |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 21 | |
| 22 | #include "git-compat-util.h" |
| 23 | #include "ewok.h" |
| 24 | #include "strbuf.h" |
| 25 | |
| 26 | int ewah_serialize_to(struct ewah_bitmap *self, |
| 27 | int (*write_fun)(void *, const void *, size_t), |
| 28 | void *data) |
| 29 | { |
| 30 | size_t i; |
| 31 | eword_t dump[2048]; |
| 32 | const size_t words_per_dump = sizeof(dump) / sizeof(eword_t); |
| 33 | uint32_t bitsize, word_count, rlw_pos; |
| 34 | |
| 35 | const eword_t *buffer; |
| 36 | size_t words_left; |
| 37 | |
| 38 | /* 32 bit -- bit size for the map */ |
| 39 | bitsize = htonl((uint32_t)self->bit_size); |
| 40 | if (write_fun(data, &bitsize, 4) != 4) |
| 41 | return -1; |
| 42 | |
| 43 | /** 32 bit -- number of compressed 64-bit words */ |
| 44 | word_count = htonl((uint32_t)self->buffer_size); |
| 45 | if (write_fun(data, &word_count, 4) != 4) |
| 46 | return -1; |
| 47 | |
| 48 | /** 64 bit x N -- compressed words */ |
| 49 | buffer = self->buffer; |
| 50 | words_left = self->buffer_size; |
| 51 | |
| 52 | while (words_left >= words_per_dump) { |
| 53 | for (i = 0; i < words_per_dump; ++i, ++buffer) |
| 54 | dump[i] = htonll(*buffer); |
| 55 | |
| 56 | if (write_fun(data, dump, sizeof(dump)) != sizeof(dump)) |
| 57 | return -1; |
| 58 | |
| 59 | words_left -= words_per_dump; |
| 60 | } |
| 61 | |
| 62 | if (words_left) { |
| 63 | for (i = 0; i < words_left; ++i, ++buffer) |
| 64 | dump[i] = htonll(*buffer); |
| 65 | |
| 66 | if (write_fun(data, dump, words_left * 8) != words_left * 8) |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | /** 32 bit -- position for the RLW */ |
| 71 | rlw_pos = (uint8_t*)self->rlw - (uint8_t *)self->buffer; |
| 72 | rlw_pos = htonl(rlw_pos / sizeof(eword_t)); |
| 73 | |
| 74 | if (write_fun(data, &rlw_pos, 4) != 4) |
| 75 | return -1; |
| 76 | |
| 77 | return (3 * 4) + (self->buffer_size * 8); |
| 78 | } |
| 79 | |
| 80 | static int write_strbuf(void *user_data, const void *data, size_t len) |
| 81 | { |
| 82 | struct strbuf *sb = user_data; |
| 83 | strbuf_add(sb, data, len); |
| 84 | return len; |
| 85 | } |
| 86 | |
| 87 | int ewah_serialize_strbuf(struct ewah_bitmap *self, struct strbuf *sb) |
| 88 | { |
| 89 | return ewah_serialize_to(self, write_strbuf, sb); |
| 90 | } |
| 91 | |
| 92 | ssize_t ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len) |
| 93 | { |
| 94 | const uint8_t *ptr = map; |
| 95 | size_t data_len; |
| 96 | size_t i; |
| 97 | |
| 98 | if (len < sizeof(uint32_t)) |
| 99 | return error("corrupt ewah bitmap: eof before bit size"); |
| 100 | self->bit_size = get_be32(ptr); |
| 101 | ptr += sizeof(uint32_t); |
| 102 | len -= sizeof(uint32_t); |
| 103 | |
| 104 | if (len < sizeof(uint32_t)) |
| 105 | return error("corrupt ewah bitmap: eof before length"); |
| 106 | self->buffer_size = self->alloc_size = get_be32(ptr); |
| 107 | ptr += sizeof(uint32_t); |
| 108 | len -= sizeof(uint32_t); |
| 109 | |
| 110 | REALLOC_ARRAY(self->buffer, self->alloc_size); |
| 111 | |
| 112 | /* |
| 113 | * Copy the raw data for the bitmap as a whole chunk; |
| 114 | * if we're in a little-endian platform, we'll perform |
| 115 | * the endianness conversion in a separate pass to ensure |
| 116 | * we're loading 8-byte aligned words. |
| 117 | */ |
| 118 | data_len = st_mult(self->buffer_size, sizeof(eword_t)); |
| 119 | if (len < data_len) |
| 120 | return error("corrupt ewah bitmap: eof in data " |
| 121 | "(%"PRIuMAX" bytes short)", |
| 122 | (uintmax_t)(data_len - len)); |
| 123 | memcpy(self->buffer, ptr, data_len); |
| 124 | ptr += data_len; |
| 125 | len -= data_len; |
| 126 | |
| 127 | for (i = 0; i < self->buffer_size; ++i) |
| 128 | self->buffer[i] = ntohll(self->buffer[i]); |
| 129 | |
| 130 | if (len < sizeof(uint32_t)) |
| 131 | return error("corrupt ewah bitmap: eof before rlw"); |
| 132 | self->rlw = self->buffer + get_be32(ptr); |
| 133 | ptr += sizeof(uint32_t); |
| 134 | len -= sizeof(uint32_t); |
| 135 | |
| 136 | return ptr - (const uint8_t *)map; |
| 137 | } |