Changeset 491

Show
Ignore:
Timestamp:
08/11/07 11:38:14 (1 year ago)
Author:
takkaria
Message:

Make the blockfile and smap functions endian-independent.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/z-blockfile.c

    r482 r491  
    185185{ 
    186186        block_t *tmp; 
     187        u32b nr_blocks = flip_u32b(bf->nr_blocks); 
    187188 
    188189        file_seek(bf->fh, 0); 
    189         file_write(bf->fh, (char *) &(bf->nr_blocks), sizeof(bf->nr_blocks)); 
     190        file_write(bf->fh, (char *) &(nr_blocks), sizeof(nr_blocks)); 
    190191 
    191192        tmp = bf->block_head; 
     
    219220 
    220221        file_read(bf->fh, (char *) &(bf->nr_blocks), sizeof(bf->nr_blocks)); 
     222        bf->nr_blocks = flip_u32b(bf->nr_blocks); 
    221223 
    222224        while (blockno < bf->nr_blocks) 
     
    233235 
    234236        file_read(bf->fh, (char *) &(bl->namelen), sizeof(bl->namelen)); 
     237        bl->namelen = flip_u32b(bl->namelen); 
    235238 
    236239        bl->name = mem_alloc(bl->namelen); 
     
    238241 
    239242        file_read(bf->fh, (char *) &(bl->nr_records), sizeof(bl->nr_records)); 
     243        bl->nr_records = flip_u32b(bl->nr_records); 
    240244 
    241245        while (recno < bl->nr_records) 
     
    261265 
    262266        file_read(bf->fh, (char *) &(rec->len), sizeof(rec->len)); 
     267        rec->len = flip_u32b(rec->len); 
    263268 
    264269        rec->data = mem_alloc(rec->len); 
     
    281286{ 
    282287        record_t *tmp; 
    283  
    284         file_write(bf->fh, (char *) &(bl->namelen), sizeof(bl->namelen)); 
     288        u32b namelen = flip_u32b(bl->namelen); 
     289        u32b nr_records = flip_u32b(bl->nr_records); 
     290 
     291        file_write(bf->fh, (char *) &(namelen), sizeof(namelen)); 
    285292        file_write(bf->fh, bl->name, bl->namelen); 
    286         file_write(bf->fh, (char *) &(bl->nr_records), sizeof(bl->nr_records)); 
     293        file_write(bf->fh, (char *) &(nr_records), sizeof(nr_records)); 
    287294 
    288295        tmp = bl->record_head; 
     
    296303void bf_saverecord(blockfile_t *bf, record_t *rec) 
    297304{ 
    298         file_write(bf->fh, (char *) &(rec->len), sizeof(rec->len)); 
     305        u32b len = flip_u32b(rec->len); 
     306 
     307        file_write(bf->fh, (char *) &(len), sizeof(rec->len)); 
    299308        file_write(bf->fh, rec->data, rec->len); 
    300309} 
  • trunk/src/z-file.c

    r474 r491  
    219219 
    220220 
     221/*** Support for byte-swapping for endian-indepedent files ***/ 
     222 
     223static bool is_bigendian() 
     224{ 
     225        int i = 1; 
     226        char *p = (char *)&i; 
     227 
     228        if (p[0] != 1) 
     229                return TRUE; 
     230 
     231        return FALSE; 
     232} 
     233 
     234u16b flip_u16b(u16b arg) 
     235{ 
     236        u16b ret; 
     237        char *in = (char *)&arg; 
     238        char *out = (char *)&ret; 
     239 
     240        if (is_bigendian()) 
     241                return arg; 
     242 
     243        out[0] = in[1]; 
     244        out[1] = in[0]; 
     245 
     246        return ret; 
     247} 
     248 
     249u32b flip_u32b(u32b arg) 
     250{ 
     251        u32b ret; 
     252        char *in = (char *)&arg; 
     253        char *out = (char *)&ret; 
     254 
     255        if (is_bigendian()) 
     256                return arg; 
     257 
     258        out[0] = in[3]; 
     259        out[1] = in[2]; 
     260        out[2] = in[1]; 
     261        out[3] = in[0]; 
     262 
     263        return ret; 
     264} 
     265 
     266 
    221267 
    222268/*** File-handling API ***/ 
  • trunk/src/z-file.h

    r474 r491  
    3939 */ 
    4040size_t path_build(char *buf, size_t len, const char *base, const char *leaf); 
     41 
     42 
     43 
     44/*** Byte-flipping functions ***/ 
     45 
     46/* 
     47 * "Flip" the bits of the integer specified in `arg` to make them big-endian. 
     48 * Useful when writing to files intended to be portable across systems. 
     49 *  
     50 * Returns the flipped value, or the original if the current system is already 
     51 * big-endian. 
     52 */ 
     53u16b flip_u16b(u16b arg); 
     54u32b flip_u32b(u32b arg); 
    4155 
    4256 
  • trunk/src/z-smap.c

    r482 r491  
    1616 */ 
    1717#include "z-virt.h" 
     18#include "z-file.h" 
    1819#include "z-smap.h" 
    1920 
     
    6566        snew->type = type; 
    6667        snew->keylen = strlen(key) + 1; 
    67         snew->key = strdup(key); 
     68        snew->key = string_make(key); 
    6869        snew->datalen = dlen; 
    6970 
     
    118119} 
    119120 
    120 void smap_put_str(smap_t *smap, const char *key, char *val) 
     121void smap_put_str(smap_t *smap, const char *key, const char *val) 
    121122{ 
    122123        (smap_put(smap, key, ST_STR, strlen(val) + 1))->value.strval = string_make(val); 
     
    213214 
    214215/*** Serialising functions ***/ 
     216 
     217#define memcpy_u32b(where, variable) \ 
     218        do { \ 
     219                u32b temp = flip_u32b(variable); \ 
     220                memcpy(where, &temp, sizeof(temp)); \ 
     221        } while (0); 
    215222 
    216223char *smap_tostring(smap_t *smap, u32b *length) 
     
    234241 
    235242        newbuf = mem_alloc(total_size); 
    236  
    237         memcpy(newbuf + curr_idx, &total_size, sizeof(total_size)); 
     243        memcpy_u32b(newbuf + curr_idx, total_size); 
    238244        curr_idx += sizeof(total_size); 
    239245 
     
    241247        while (se) 
    242248        { 
    243                 memcpy(newbuf + curr_idx, &(se->type), sizeof(se->type)); 
     249                memcpy(newbuf + curr_idx, &se->type, sizeof(se->type)); 
    244250                curr_idx += sizeof(se->type); 
    245251                 
    246                 memcpy(newbuf + curr_idx, &(se->keylen), sizeof(se->keylen)); 
     252                memcpy_u32b(newbuf + curr_idx, se->keylen); 
    247253                curr_idx += sizeof(se->keylen); 
    248254 
    249                 memcpy(newbuf + curr_idx, &(se->datalen), sizeof(se->datalen)); 
     255                memcpy_u32b(newbuf + curr_idx, se->datalen); 
    250256                curr_idx += sizeof(se->datalen); 
    251257 
     
    265271                                break; 
    266272                        case ST_S16B: 
    267                                 memcpy(newbuf + curr_idx, &(se->value.s16bval), sizeof(s16b)); 
    268                                 break; 
     273                        { 
     274                                u16b temp = flip_u16b((u16b) se->value.s16bval); 
     275                                memcpy(newbuf + curr_idx, &temp, sizeof(temp)); 
     276                                break; 
     277                        } 
    269278                        case ST_U16B: 
    270                                 memcpy(newbuf + curr_idx, &(se->value.u16bval), sizeof(u16b)); 
    271                                 break; 
     279                        { 
     280                                u16b temp = flip_u16b(se->value.u16bval); 
     281                                memcpy(newbuf + curr_idx, &temp, sizeof(temp)); 
     282                                break; 
     283                        } 
    272284                        case ST_S32B: 
    273                                 memcpy(newbuf + curr_idx, &(se->value.s32bval), sizeof(s32b)); 
    274                                 break; 
     285                        { 
     286                                u32b temp = flip_u32b((u32b) se->value.s32bval); 
     287                                memcpy(newbuf + curr_idx, &temp, sizeof(temp)); 
     288                                break; 
     289                        } 
    275290                        case ST_U32B: 
    276                                 memcpy(newbuf + curr_idx, &(se->value.u32bval), sizeof(u32b)); 
    277                                 break; 
     291                        { 
     292                                u32b temp = flip_u32b(se->value.u32bval); 
     293                                memcpy(newbuf + curr_idx, &temp, sizeof(temp)); 
     294                                break; 
     295                        } 
    278296                        case ST_STR: 
    279297                                memcpy(newbuf + curr_idx, se->value.strval, se->datalen); 
     
    308326 
    309327        memcpy(&total_len, string + idx, sizeof(total_len)); 
     328        total_len = flip_u32b(total_len); 
    310329        idx += sizeof(total_len); 
     330 
     331        /* We should do some checking of total_len against length here */ 
    311332 
    312333        while (idx < total_len) 
     
    316337 
    317338                memcpy(&tmp_klen, string + idx, sizeof(tmp_klen)); 
     339                tmp_klen = flip_u32b(tmp_klen); 
    318340                idx += sizeof(tmp_klen); 
    319341 
    320342                memcpy(&tmp_dlen, string + idx, sizeof(tmp_dlen)); 
     343                tmp_dlen = flip_u32b(tmp_dlen); 
    321344                idx += sizeof(tmp_dlen); 
    322345 
     
    338361                        case ST_S16B: 
    339362                                memcpy(&(se->value.s16bval), string + idx, sizeof(s16b)); 
     363                                se->value.s16bval = (s16b) flip_u16b((u16b) se->value.s16bval); 
    340364                                break; 
    341365                        case ST_U16B: 
    342366                                memcpy(&(se->value.u16bval), string + idx, sizeof(u16b)); 
     367                                se->value.u16bval = flip_u16b(se->value.u16bval); 
    343368                                break; 
    344369                        case ST_S32B: 
    345370                                memcpy(&(se->value.s32bval), string + idx, sizeof(s32b)); 
     371                                se->value.s32bval = (s32b) flip_u32b((u32b) se->value.s32bval); 
    346372                                break; 
    347373                        case ST_U32B: 
    348374                                memcpy(&(se->value.u32bval), string + idx, sizeof(u32b)); 
     375                                se->value.u32bval = flip_u32b(se->value.u32bval); 
    349376                                break; 
    350377                        case ST_STR: