00001
00016
00017
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021
00022 #include "lbfileio.h"
00023
00027 inline void write_int16_le_file (FILE *fp, unsigned short x)
00028 {
00029 fputc ((int) (x&255), fp);
00030 fputc ((int) ((x>>8)&255), fp);
00031 }
00032
00036 inline void write_int16_le_buf (unsigned char *buff, unsigned short x)
00037 {
00038 buff[0]=(x&255);
00039 buff[1]=((x>>8)&255);
00040 }
00041
00045 inline void write_int32_le_file (FILE *fp, unsigned long x)
00046 {
00047 fputc ((int) (x&255), fp);
00048 fputc ((int) ((x>>8)&255), fp);
00049 fputc ((int) ((x>>16)&255), fp);
00050 fputc ((int) ((x>>24)&255), fp);
00051 }
00052
00056 inline void write_int32_le_buf (unsigned char *buff, unsigned long x)
00057 {
00058 buff[0]=(x&255);
00059 buff[1]=((x>>8)&255);
00060 buff[2]=((x>>16)&255);
00061 buff[3]=((x>>24)&255);
00062 }
00063
00067 inline long read_int32_le_file (FILE *fp)
00068 {
00069 long l;
00070 l = fgetc(fp);
00071 l += fgetc(fp)<<8;
00072 l += fgetc(fp)<<16;
00073 l += fgetc(fp)<<24;
00074 return l;
00075 }
00076
00080 inline long read_int32_le_buf (const unsigned char *buff)
00081 {
00082 long l;
00083 l = buff[0];
00084 l += buff[1]<<8;
00085 l += buff[2]<<16;
00086 l += buff[3]<<24;
00087 return l;
00088 }
00089
00093 inline unsigned short read_int16_le_buf (const unsigned char *buff)
00094 {
00095 long l;
00096 l = buff[0];
00097 l += buff[1]<<8;
00098 return l;
00099 }
00100
00104 inline unsigned short read_int16_le_file (FILE *fp)
00105 {
00106 unsigned short l;
00107 l = fgetc(fp);
00108 l += fgetc(fp)<<8;
00109 return l;
00110 }
00111
00115 inline long read_int32_be_file (FILE *fp)
00116 {
00117 long l;
00118 l = fgetc(fp)<<24;
00119 l += fgetc(fp)<<16;
00120 l += fgetc(fp)<<8;
00121 l += fgetc(fp);
00122 return l;
00123 }
00124
00128 inline long read_int32_be_buf (const unsigned char *buff)
00129 {
00130 long l;
00131 l = buff[3];
00132 l += buff[2]<<8;
00133 l += buff[1]<<16;
00134 l += buff[0]<<24;
00135 return l;
00136 }
00137
00141 inline unsigned short read_int16_be_buf (const unsigned char *buff)
00142 {
00143 long l;
00144 l = buff[1];
00145 l += buff[0]<<8;
00146 return l;
00147 }
00148
00152 inline unsigned short read_int16_be_file (FILE *fp)
00153 {
00154 unsigned short l;
00155 l = fgetc(fp)<<8;
00156 l += fgetc(fp);
00157 return l;
00158 }
00159
00163 inline void write_int16_be_buf (unsigned char *buff, unsigned short x)
00164 {
00165 buff[1]=(x&255);
00166 buff[0]=((x>>8)&255);
00167 }
00168
00172 inline void write_int32_be_buf (unsigned char *buff, unsigned long x)
00173 {
00174 buff[3]=(x&255);
00175 buff[2]=((x>>8)&255);
00176 buff[1]=((x>>16)&255);
00177 buff[0]=((x>>24)&255);
00178 }
00179
00184 inline long file_length (char *path)
00185 {
00186 FILE *fp;
00187 long length;
00188
00189 fp = fopen (path, "rb");
00190 if (fp==NULL)
00191 return -1;
00192 if (fseek(fp, 0, SEEK_END) != 0)
00193 return -1;
00194 length = ftell (fp);
00195 fclose (fp);
00196 return length;
00197 }
00198
00203 inline long file_length_opened (FILE *fp)
00204 {
00205 long length;
00206 long lastpos;
00207
00208 if (fp==NULL)
00209 return -1;
00210 lastpos = ftell (fp);
00211 if (fseek(fp, 0, SEEK_END) != 0)
00212 return -1;
00213 length = ftell(fp);
00214 fseek(fp, lastpos, SEEK_SET);
00215 return length;
00216 }
00217
00222 inline unsigned char read_int8_buf (const unsigned char *buff)
00223 {
00224 return buff[0];
00225 }
00226
00233 inline short nth_bit( unsigned char c, short n )
00234 {
00235 if( (n<0) || (n>7) )
00236 return 0;
00237 c = c>>n;
00238 return (short)(c & 1);
00239
00240 }
00241
00250 inline short nth_bit_fourbytes( unsigned char c[4], short n )
00251 {
00252 if( (n<0) || (n>32) )
00253 return 0;
00254
00255 if( n < 8 )
00256 return nth_bit( c[3], n );
00257 else if( n < 16 )
00258 return nth_bit( c[2], n%8 );
00259 else if( n < 24 )
00260 return nth_bit( c[1], n%8 );
00261 else
00262 return nth_bit( c[0], n%8 );
00263
00264 }