Changeset 18760


Ignore:
Timestamp:
2009-12-12T00:50:51+01:00 (8 years ago)
Author:
mb
Message:

lzma2eva: Endianness fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/firmware-utils/src/lzma2eva.c

    r9401 r18760  
    2323#include <zlib.h> /* crc32 */ 
    2424 
     25 
     26#define ARRAY_SIZE(x)   (sizeof(x) / sizeof((x)[0])) 
     27 
    2528#define checksum_add32(csum, data) \ 
    26   csum += ((uint8_t *)&data)[0]; \ 
    27   csum += ((uint8_t *)&data)[1]; \ 
    28   csum += ((uint8_t *)&data)[2]; \ 
    29   csum += ((uint8_t *)&data)[3]; 
     29  do { \ 
     30    csum += (((data) >> 0)  & 0x000000FF); \ 
     31    csum += (((data) >> 8)  & 0x000000FF); \ 
     32    csum += (((data) >> 16) & 0x000000FF); \ 
     33    csum += (((data) >> 24) & 0x000000FF); \ 
     34  } while (0) 
    3035 
    3136void 
     
    4348} 
    4449 
     50/* Read an 8bit value */ 
     51static int fread_8(uint8_t *buf, FILE *fd) 
     52{ 
     53  return (fread(buf, sizeof(*buf), 1, fd) == 1) ? 0 : -1; 
     54} 
     55 
     56/* Read a 32bit little endian value and convert to host endianness. */ 
     57static int fread_le32(uint32_t *buf, FILE *fd) 
     58{ 
     59  size_t count; 
     60  uint8_t tmp[4]; 
     61  unsigned int i; 
     62 
     63  if (fread(tmp, sizeof(tmp), 1, fd) != 1) 
     64    return -1; 
     65  *buf = 0; 
     66  for (i = 0; i < ARRAY_SIZE(tmp); i++) 
     67    *buf |= (uint32_t)(tmp[i]) << (i * 8); 
     68 
     69  return 0; 
     70} 
     71 
     72/* Read a 64bit little endian value and convert to host endianness. */ 
     73static int fread_le64(uint64_t *buf, FILE *fd) 
     74{ 
     75  size_t count; 
     76  uint8_t tmp[8]; 
     77  unsigned int i; 
     78 
     79  if (fread(tmp, sizeof(tmp), 1, fd) != 1) 
     80    return -1; 
     81  *buf = 0; 
     82  for (i = 0; i < ARRAY_SIZE(tmp); i++) 
     83    *buf |= (uint64_t)(tmp[i]) << (i * 8); 
     84 
     85  return 0; 
     86} 
     87 
     88/* Write an 8bit value */ 
     89static int fwrite_8(uint8_t buf, FILE *fd) 
     90{ 
     91  return (fwrite(&buf, sizeof(buf), 1, fd) == 1) ? 0 : -1; 
     92} 
     93 
     94/* Convert to little endian and write a 32bit value */ 
     95static int fwrite_le32(uint32_t buf, FILE *fd) 
     96{ 
     97  size_t count; 
     98  uint8_t tmp[4]; 
     99  unsigned int i; 
     100 
     101  for (i = 0; i < ARRAY_SIZE(tmp); i++) 
     102    tmp[i] = buf >> (i * 8); 
     103  if (fwrite(tmp, sizeof(tmp), 1, fd) != 1) 
     104    return -1; 
     105 
     106  return 0; 
     107} 
     108 
    45109int 
    46110main(int argc, char *argv[]) 
    47111{ 
    48  
    49112  const char *infile, *outfile; 
    50113  FILE *in, *out; 
     
    68131  uint32_t datacrc32 = crc32(0, 0, 0); 
    69132 
    70   uint32_t zero = 0; 
    71133  uint32_t entry = 0; 
    72134 
     
    88150 
    89151  /* read LZMA header */ 
    90   if (1 != fread(&properties, sizeof properties, 1, in)) 
    91     pexit("fread"); 
    92   if (1 != fread(&dictsize, sizeof dictsize, 1, in)) 
    93     pexit("fread"); 
    94   if (1 != fread(&datasize, sizeof datasize, 1, in)) 
     152  if (fread_8(&properties, in)) 
     153    pexit("fread"); 
     154  if (fread_le32(&dictsize, in)) 
     155    pexit("fread"); 
     156  if (fread_le64(&datasize, in)) 
    95157    pexit("fread"); 
    96158 
    97159  /* write EVA header */ 
    98   if (1 != fwrite(&magic, sizeof magic, 1, out)) 
     160  if (fwrite_le32(magic, out)) 
    99161    pexit("fwrite"); 
    100162  if (fgetpos(out, &reclengthpos)) 
    101163    pexit("fgetpos"); 
    102   if (1 != fwrite(&reclength, sizeof reclength, 1, out)) 
    103     pexit("fwrite"); 
    104   if (1 != fwrite(&loadaddress, sizeof loadaddress, 1, out)) 
    105     pexit("fwrite"); 
    106   if (1 != fwrite(&type, sizeof type, 1, out)) 
     164  if (fwrite_le32(reclength, out)) 
     165    pexit("fwrite"); 
     166  if (fwrite_le32(loadaddress, out)) 
     167    pexit("fwrite"); 
     168  if (fwrite_le32(type, out)) 
    107169    pexit("fwrite"); 
    108170 
     
    110172  if (fgetpos(out, &compsizepos)) 
    111173    pexit("fgetpos"); 
    112   if (1 != fwrite(&compsize, sizeof compsize, 1, out)) 
     174  if (fwrite_le32(compsize, out)) 
    113175    pexit("fwrite"); 
    114176  /* XXX check length */ 
    115177  datasize32 = (uint32_t)datasize; 
    116   if (1 != fwrite(&datasize32, sizeof datasize32, 1, out)) 
    117     pexit("fwrite"); 
    118   if (1 != fwrite(&datacrc32, sizeof datacrc32, 1, out)) 
     178  if (fwrite_le32(datasize32, out)) 
     179    pexit("fwrite"); 
     180  if (fwrite_le32(datacrc32, out)) 
    119181    pexit("fwrite"); 
    120182 
    121183  /* write modified LZMA header */ 
    122   if (1 != fwrite(&properties, sizeof properties, 1, out)) 
    123     pexit("fwrite"); 
    124   if (1 != fwrite(&dictsize, sizeof dictsize, 1, out)) 
    125     pexit("fwrite"); 
    126   if (1 != fwrite(&zero, 3, 1, out)) 
     184  if (fwrite_8(properties, out)) 
     185    pexit("fwrite"); 
     186  if (fwrite_le32(dictsize, out)) 
     187    pexit("fwrite"); 
     188  if (fwrite_le32(0, out)) 
    127189    pexit("fwrite"); 
    128190 
     
    142204  if (fsetpos(out, &reclengthpos)) 
    143205    pexit("fsetpos"); 
    144   if (1 != fwrite(&reclength, sizeof reclength, 1, out)) 
     206  if (fwrite_le32(reclength, out)) 
    145207    pexit("fwrite"); 
    146208 
     
    148210  if (fsetpos(out, &compsizepos)) 
    149211    pexit("fsetpos"); 
    150   if (1 != fwrite(&compsize, sizeof compsize, 1, out)) 
    151     pexit("fwrite"); 
    152   if (1 != fwrite(&datasize32, sizeof datasize32, 1, out)) 
    153     pexit("fwrite"); 
    154   if (1 != fwrite(&datacrc32, sizeof datacrc32, 1, out)) 
     212  if (fwrite_le32(compsize, out)) 
     213    pexit("fwrite"); 
     214  if (fwrite_le32(datasize32, out)) 
     215    pexit("fwrite"); 
     216  if (fwrite_le32(datacrc32, out)) 
    155217    pexit("fwrite"); 
    156218 
     
    175237 
    176238  checksum = ~checksum + 1; 
    177   if (1 != fwrite(&checksum, sizeof checksum, 1, out)) 
     239  if (fwrite_le32(checksum, out)) 
    178240    pexit("fwrite"); 
    179241 
    180242  /* write entry record */ 
    181   if (1 != fwrite(&zero, sizeof zero, 1, out)) 
    182     pexit("fwrite"); 
    183   if (1 != fwrite(&entry, sizeof entry, 1, out)) 
     243  if (fwrite_le32(0, out)) 
     244    pexit("fwrite"); 
     245  if (fwrite_le32(entry, out)) 
    184246    pexit("fwrite"); 
    185247 
Note: See TracChangeset for help on using the changeset viewer.