Commits

carl douglas  committed 3c0dc3e

refactor bitstream-c

  • Participants
  • Parent commits 8f349c6
  • Branches egg

Comments (0)

Files changed (2)

 LFLAGS  = 
 
 INC     = bits.h
-SRC     = bits.c bitstream.c
+SRC     = bits.c bitstream-c.c
 
 OBJ     = $(SRC:.c=.o)
 BIN     = example

File bitstream-c.c

 #include <string.h>
 
 struct bit_stream {
-  FILE *fp;
-  unsigned octet;
-  unsigned offset;
+  void           *user;
+  unsigned        octet;
+  unsigned        offset;
+
+  unsigned int    (*read) (void *);
+  void            (*write) (unsigned int, void *);
 };
 
-void bit_stream_init(struct bit_stream *st, FILE *fp) {
-  st->fp = fp;
+void 
+bit_stream_init(struct bit_stream * st, void *user)
+{
+  st->user = user;
   st->octet = 0;
   st->offset = 0;
 }
 
-unsigned long long 
-bit_stream_read
-(struct bit_stream *st, unsigned int length) {
-  unsigned i = 0;
-  unsigned long long value   = 0;
-  unsigned int bit;
+unsigned long long
+                bit_stream_read
+                (struct bit_stream * st, unsigned int length) {
+  unsigned        i = 0;
+  unsigned long long value = 0;
+  unsigned int    bit;
   for (i = 0; i < length; i++, st->offset++) {
     if ((st->offset % 8) == 0) {
-      st->octet = fgetc(st->fp);
+      st->octet = st->read(st->user);
     }
     bit = (st->octet & (1 << (7 - (st->offset % 8)))) >> (7 - (st->offset % 8));
     value |= (unsigned long long) (bit << (length - 1 - i));
   return value;
 }
 
-void 
-bit_stream_write
-(struct bit_stream *st, unsigned int length, unsigned long long value) {
-  unsigned i = 0;
-  unsigned int bit;
-  for (i = 0; i < length; i++, st->offset++) {
-    bit = value & (1 << (length - 1 - i));
-    if ((st->offset % 8) == 0) {
+void bit_stream_write
+(struct bit_stream * st, unsigned int length, unsigned long long value) {
+  unsigned int    index = 0;
+  unsigned int    bit_offset = 0;
+  unsigned int    bit = length - 1;
+  for (index = 0; index < length; index++) {
+    unsigned long long b = 0;
+    if ((st->offset > 7)) {
+      st->write(st->octet, st->user);
+      st->offset = 0;
+      st->octet = 0;
     }
+    bit_offset = 7 - st->offset;
+    b = (value & (1 << (bit))) >> (bit);
+    if (b == 0)
+      st->octet &= ~(1 << bit_offset);
+    else
+      st->octet |= (1 << bit_offset);
+    bit--;
+    st->offset++;
   }
 }
 
 #if MAIN
-int main(int argc, char *argv[]) {
+int 
+main(int argc, char *argv[])
+{
   struct bit_stream st1;
   unsigned long long v[16];
-  FILE *fp = fopen(argv[1], "rb");
+  FILE           *fp = fopen(argv[1], "rb");
   bit_stream_init(&st1, fp);
 m0:
-  v[0] =  bit_stream_read(&st1, 3);
-  v[1] =  bit_stream_read(&st1, 3);
-  v[2] =  bit_stream_read(&st1, 10);
-  v[3] =  bit_stream_read(&st1, 32);
-  v[4] =  bit_stream_read(&st1, 32);
-  printf ("--------------" __DATE__ "  " __TIME__ "\n");
-  printf ("reserved: %lld\n", v[0]);
-  printf ("version:  %lld\n", v[1]);
-  printf ("type:     %lld\n", v[2]);
-  printf ("length:   %lld\n", v[3]);
-  printf ("id:       %lld\n", v[4]);
+  v[0] = bit_stream_read(&st1, 3);
+  v[1] = bit_stream_read(&st1, 3);
+  v[2] = bit_stream_read(&st1, 10);
+  v[3] = bit_stream_read(&st1, 32);
+  v[4] = bit_stream_read(&st1, 32);
+  printf("--------------" __DATE__ "  " __TIME__ "\n");
+  printf("reserved: %lld\n", v[0]);
+  printf("version:  %lld\n", v[1]);
+  printf("type:     %lld\n", v[2]);
+  printf("length:   %lld\n", v[3]);
+  printf("id:       %lld\n", v[4]);
 p0:
   v[5] = bit_stream_read(&st1, 1);
-  if (feof(st1.fp)) goto f0;
-  if (v[5] == 1) goto tv0;
-  else goto tlv0;
+  if (feof(st1.fp))
+    goto f0;
+  if (v[5] == 1)
+    goto tv0;
+  else
+    goto tlv0;
 tv0:
   v[6] = bit_stream_read(&st1, 7);
-  printf ("  type:     %lld\n", v[6]);
+  printf("  type:     %lld\n", v[6]);
   switch (v[6]) {
-    case 13: 
-      { int i;
-        printf ("  epc 96: ");
-        for (i = 0; i < 12; i++) {
-          unsigned long long x = bit_stream_read(&st1, 8);
-          printf ("%2X", (unsigned int) x);
-        }
-        printf ("\n");
+  case 13:
+    {
+      int             i;
+      printf("  epc 96: ");
+      for (i = 0; i < 12; i++) {
+	unsigned long long x = bit_stream_read(&st1, 8);
+	printf("%2X", (unsigned int) x);
       }
+      printf("\n");
+    }
   }
   goto p0;
 tlv0:
   v[7] = bit_stream_read(&st1, 5);
-  printf ("  reserved: %lld\n", v[7]);
+  printf("  reserved: %lld\n", v[7]);
   v[8] = bit_stream_read(&st1, 10);
-  printf ("  type:     %lld\n", v[8]);
+  printf("  type:     %lld\n", v[8]);
   v[9] = bit_stream_read(&st1, 16);
-  printf ("  length:   %lld\n", v[9]);
+  printf("  length:   %lld\n", v[9]);
   goto p0;
 f0:
-  printf ("--------------" __DATE__ "  " __TIME__ "\n");
+  printf("--------------" __DATE__ "  " __TIME__ "\n");
   fclose(fp);
   return 0;
 }
 #endif
-