##// END OF EJS Templates
util.h: replace ntohl/htonl with get/putbe32
Matt Mackall -
r16437:d126a0d1 default
parent child Browse files
Show More
@@ -338,7 +338,6 static PyObject *bdiff(PyObject *self, P
338 338 PyObject *result = NULL;
339 339 struct line *al, *bl;
340 340 struct hunk l, *h;
341 uint32_t encode[3];
342 341 int an, bn, len = 0, la, lb, count;
343 342
344 343 if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb))
@@ -375,10 +374,9 static PyObject *bdiff(PyObject *self, P
375 374 for (h = l.next; h; h = h->next) {
376 375 if (h->a1 != la || h->b1 != lb) {
377 376 len = bl[h->b1].l - bl[lb].l;
378 encode[0] = htonl(al[la].l - al->l);
379 encode[1] = htonl(al[h->a1].l - al->l);
380 encode[2] = htonl(len);
381 memcpy(rb, encode, 12);
377 putbe32(al[la].l - al->l, rb);
378 putbe32(al[h->a1].l - al->l, rb + 4);
379 putbe32(len, rb + 8);
382 380 memcpy(rb + 12, bl[lb].l, len);
383 381 rb += 12 + len;
384 382 }
@@ -202,7 +202,6 static struct flist *decode(const char *
202 202 struct flist *l;
203 203 struct frag *lt;
204 204 const char *data = bin + 12, *end = bin + len;
205 uint32_t decode[3]; /* for dealing with alignment issues */
206 205
207 206 /* assume worst case size, we won't have many of these lists */
208 207 l = lalloc(len / 12);
@@ -212,10 +211,9 static struct flist *decode(const char *
212 211 lt = l->tail;
213 212
214 213 while (data <= end) {
215 memcpy(decode, bin, 12);
216 lt->start = ntohl(decode[0]);
217 lt->end = ntohl(decode[1]);
218 lt->len = ntohl(decode[2]);
214 lt->start = getbe32(bin);
215 lt->end = getbe32(bin + 4);
216 lt->len = getbe32(bin + 8);
219 217 if (lt->start > lt->end)
220 218 break; /* sanity check */
221 219 bin = data + lt->len;
@@ -361,7 +359,6 patchedsize(PyObject *self, PyObject *ar
361 359 long orig, start, end, len, outlen = 0, last = 0;
362 360 int patchlen;
363 361 char *bin, *binend, *data;
364 uint32_t decode[3]; /* for dealing with alignment issues */
365 362
366 363 if (!PyArg_ParseTuple(args, "ls#", &orig, &bin, &patchlen))
367 364 return NULL;
@@ -370,10 +367,9 patchedsize(PyObject *self, PyObject *ar
370 367 data = bin + 12;
371 368
372 369 while (data <= binend) {
373 memcpy(decode, bin, 12);
374 start = ntohl(decode[0]);
375 end = ntohl(decode[1]);
376 len = ntohl(decode[2]);
370 start = getbe32(bin);
371 end = getbe32(bin + 4);
372 len = getbe32(bin + 8);
377 373 if (start > end)
378 374 break; /* sanity check */
379 375 bin = data + len;
@@ -143,7 +143,6 static PyObject *parse_dirstate(PyObject
143 143 int state, mode, size, mtime;
144 144 unsigned int flen;
145 145 int len;
146 uint32_t decode[4]; /* for alignment */
147 146
148 147 if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate",
149 148 &PyDict_Type, &dmap,
@@ -166,11 +165,10 static PyObject *parse_dirstate(PyObject
166 165 while (cur < end - 17) {
167 166 /* unpack header */
168 167 state = *cur;
169 memcpy(decode, cur + 1, 16);
170 mode = ntohl(decode[0]);
171 size = ntohl(decode[1]);
172 mtime = ntohl(decode[2]);
173 flen = ntohl(decode[3]);
168 mode = getbe32(cur + 1);
169 size = getbe32(cur + 5);
170 mtime = getbe32(cur + 9);
171 flen = getbe32(cur + 13);
174 172 cur += 17;
175 173 if (cur + flen > end || cur + flen < cur) {
176 174 PyErr_SetString(PyExc_ValueError, "overflow in dirstate");
@@ -308,7 +306,6 static const char *index_deref(indexObje
308 306 */
309 307 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
310 308 {
311 uint32_t decode[8]; /* to enforce alignment with inline data */
312 309 uint64_t offset_flags;
313 310 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
314 311 const char *c_node_id;
@@ -351,22 +348,20 static PyObject *index_get(indexObject *
351 348 if (data == NULL)
352 349 return NULL;
353 350
354 memcpy(decode, data, 8 * sizeof(uint32_t));
355
356 offset_flags = ntohl(decode[1]);
351 offset_flags = getbe32(data + 4);
357 352 if (pos == 0) /* mask out version number for the first entry */
358 353 offset_flags &= 0xFFFF;
359 354 else {
360 uint32_t offset_high = ntohl(decode[0]);
355 uint32_t offset_high = getbe32(data);
361 356 offset_flags |= ((uint64_t)offset_high) << 32;
362 357 }
363 358
364 comp_len = ntohl(decode[2]);
365 uncomp_len = ntohl(decode[3]);
366 base_rev = ntohl(decode[4]);
367 link_rev = ntohl(decode[5]);
368 parent_1 = ntohl(decode[6]);
369 parent_2 = ntohl(decode[7]);
359 comp_len = getbe32(data + 8);
360 uncomp_len = getbe32(data + 12);
361 base_rev = getbe32(data + 16);
362 link_rev = getbe32(data + 20);
363 parent_1 = getbe32(data + 24);
364 parent_2 = getbe32(data + 28);
370 365 c_node_id = data + 32;
371 366
372 367 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
@@ -940,8 +935,8 static long inline_scan(indexObject *sel
940 935 uint32_t comp_len;
941 936 const char *old_data;
942 937 /* 3rd element of header is length of compressed inline data */
943 memcpy(&comp_len, data + 8, sizeof(uint32_t));
944 incr = hdrsize + ntohl(comp_len);
938 comp_len = getbe32(data + 8);
939 incr = hdrsize + comp_len;
945 940 if (incr < hdrsize)
946 941 break;
947 942 if (offsets)
@@ -125,13 +125,6 typedef unsigned __int64 uint64_t;
125 125 #else
126 126 #include <stdint.h>
127 127 #endif
128 static uint32_t ntohl(uint32_t x)
129 {
130 return ((x & 0x000000ffUL) << 24) |
131 ((x & 0x0000ff00UL) << 8) |
132 ((x & 0x00ff0000UL) >> 8) |
133 ((x & 0xff000000UL) >> 24);
134 }
135 128 #else
136 129 /* not windows */
137 130 #include <sys/types.h>
@@ -151,4 +144,22 static uint32_t ntohl(uint32_t x)
151 144 #define inline __inline
152 145 #endif
153 146
147 static inline uint32_t getbe32(const char *c)
148 {
149 const unsigned char *d = (const unsigned char *)c;
150
151 return ((d[0] << 24) |
152 (d[1] << 16) |
153 (d[2] << 8) |
154 (d[3]));
155 }
156
157 static inline void putbe32(uint32_t x, char *c)
158 {
159 c[0] = (x >> 24) & 0xff;
160 c[1] = (x >> 16) & 0xff;
161 c[2] = (x >> 8) & 0xff;
162 c[3] = (x) & 0xff;
163 }
164
154 165 #endif /* _HG_UTIL_H_ */
General Comments 0
You need to be logged in to leave comments. Login now