Back to home page

Redis cross reference

 
 

    


0001 /* endinconv.c -- Endian conversions utilities.
0002  *
0003  * This functions are never called directly, but always using the macros
0004  * defined into endianconv.h, this way we define everything is a non-operation
0005  * if the arch is already little endian.
0006  *
0007  * Redis tries to encode everything as little endian (but a few things that need
0008  * to be backward compatible are still in big endian) because most of the
0009  * production environments are little endian, and we have a lot of conversions
0010  * in a few places because ziplists, intsets, zipmaps, need to be endian-neutral
0011  * even in memory, since they are serialied on RDB files directly with a single
0012  * write(2) without other additional steps.
0013  *
0014  * ----------------------------------------------------------------------------
0015  *
0016  * Copyright (c) 2011-2012, Salvatore Sanfilippo <antirez at gmail dot com>
0017  * All rights reserved.
0018  *
0019  * Redistribution and use in source and binary forms, with or without
0020  * modification, are permitted provided that the following conditions are met:
0021  *
0022  *   * Redistributions of source code must retain the above copyright notice,
0023  *     this list of conditions and the following disclaimer.
0024  *   * Redistributions in binary form must reproduce the above copyright
0025  *     notice, this list of conditions and the following disclaimer in the
0026  *     documentation and/or other materials provided with the distribution.
0027  *   * Neither the name of Redis nor the names of its contributors may be used
0028  *     to endorse or promote products derived from this software without
0029  *     specific prior written permission.
0030  *
0031  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0032  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0033  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0034  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0035  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0036  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0037  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0038  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0039  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0040  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0041  * POSSIBILITY OF SUCH DAMAGE.
0042  */
0043 
0044 
0045 #include <stdint.h>
0046 
0047 /* Toggle the 16 bit unsigned integer pointed by *p from little endian to
0048  * big endian */
0049 void memrev16(void *p) {
0050     unsigned char *x = p, t;
0051 
0052     t = x[0];
0053     x[0] = x[1];
0054     x[1] = t;
0055 }
0056 
0057 /* Toggle the 32 bit unsigned integer pointed by *p from little endian to
0058  * big endian */
0059 void memrev32(void *p) {
0060     unsigned char *x = p, t;
0061 
0062     t = x[0];
0063     x[0] = x[3];
0064     x[3] = t;
0065     t = x[1];
0066     x[1] = x[2];
0067     x[2] = t;
0068 }
0069 
0070 /* Toggle the 64 bit unsigned integer pointed by *p from little endian to
0071  * big endian */
0072 void memrev64(void *p) {
0073     unsigned char *x = p, t;
0074 
0075     t = x[0];
0076     x[0] = x[7];
0077     x[7] = t;
0078     t = x[1];
0079     x[1] = x[6];
0080     x[6] = t;
0081     t = x[2];
0082     x[2] = x[5];
0083     x[5] = t;
0084     t = x[3];
0085     x[3] = x[4];
0086     x[4] = t;
0087 }
0088 
0089 uint16_t intrev16(uint16_t v) {
0090     memrev16(&v);
0091     return v;
0092 }
0093 
0094 uint32_t intrev32(uint32_t v) {
0095     memrev32(&v);
0096     return v;
0097 }
0098 
0099 uint64_t intrev64(uint64_t v) {
0100     memrev64(&v);
0101     return v;
0102 }
0103 
0104 #ifdef TESTMAIN
0105 #include <stdio.h>
0106 
0107 int main(void) {
0108     char buf[32];
0109 
0110     sprintf(buf,"ciaoroma");
0111     memrev16(buf);
0112     printf("%s\n", buf);
0113 
0114     sprintf(buf,"ciaoroma");
0115     memrev32(buf);
0116     printf("%s\n", buf);
0117 
0118     sprintf(buf,"ciaoroma");
0119     memrev64(buf);
0120     printf("%s\n", buf);
0121 
0122     return 0;
0123 }
0124 #endif