-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsegcc.cp
1491 lines (1149 loc) · 36.9 KB
/
parsegcc.cp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CCF = Cabernet Configuration File
Version 4.8
- with support for hidden panels)
- with support for CCF version 2 (Beep and Timer actions)
WANRING: only works on BIG-ENDIAN machines!!!
(because the ccf file format is also big endian)
done 1999 Markus Fritze, <http://www.markus-fritze.de/>
Modified June 2001 by Michael Durket
Now works properly on all machines regardless of their
byte-order (uses netinet/in.h to provide endian conversion
macros present in Unix systems and most other operating
systems that have TCP/IP stacks)
Corrected data structures (added fields for home bitmap,
different field layouts in header depending on version,
timer list pointer field in header).
Changed leaked memory feature to be a map feature which
details the layout order of a CCF file.
Eliminated non-standard typedefs SInt32, UInt16, SInt16
and SInt8 (most of which weren't really signed values
anyway).
Eliminated #pragmas, which are compiler-dependent.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <netinet/in.h>
typedef unsigned long offset;
typedef struct header
{
offset version; /* CCF version string */
unsigned long filler1;
char id[8]; /* 0x40A55A405F434346 or "@\xA5Z@_CCF" */
offset CRC; /* offset to the checksum (same as end) */
/* The date and time this CCF file was last modified */
unsigned short year;
unsigned char month;
unsigned char day;
unsigned char unused;
unsigned char hour;
unsigned char minute;
unsigned char seconds;
unsigned long filler3;
char id2[4]; /* "CCF\0" */
/* The following two fields indicate the Pronto App version major and
minor numbers. (Minor version 2 is the one with Timer support). */
unsigned short App_minor;
unsigned short App_major;
offset end; /* Address of 1 byte beyond the end of the CCF (points to CRC) */
offset header_end;
offset Home;
offset Devices;
offset Macros;
union
{
struct
{
unsigned long confProp;
offset macroXPos;
unsigned short unknown;
} v1;
struct
{
offset Timer_list;
unsigned long confProp;
offset macroXPos;
unsigned short unknown;
} v2;
} u;
} HEADER;
/* A device or macro is described by the following structure. The only
actual difference in the Pronto between devices and macros is
a macro cannot define values for the 2 soft keys (the left and
right key) at the bottom of the Pronto. */
typedef struct device
{
offset next;
offset name;
offset home_bitmap_offset;
unsigned long unknown1;
offset actions;
offset LeftSoftKey;
offset RightSoftKey;
offset VolumeDown;
offset VolumeUp;
offset ChannelDown;
offset ChannelUp;
offset Mute;
unsigned long unknown2;
offset LeftSoftKeyName;
offset RightSoftKeyName;
offset panels;
unsigned char attr;
} DEVICE;
/* An element is either a frame or button. An element descriptor
describes the location of the element within its container
(panel or frame), the element's type and a pointer to the
element definition. */
typedef enum element_type
{
ELEMENT_TYPE_FRAME = 0,
ELEMENT_TYPE_BUTTON = 1
} ELEMENT_TYPE;
typedef struct element_descriptor
{
unsigned short x __attribute__ ((__packed__));
unsigned short y __attribute__ ((__packed__));
offset subPanelPos __attribute__ ((__packed__));
unsigned char type;
} ELEMENT_DESCRIPTOR __attribute__ ((__packed__));
typedef struct panel
{
offset next;
offset name; /* name of the panel (Bit 31 is set when panel is hidden) */
unsigned char count; /* number of entries, always (?) == count2 */
unsigned char count2;
ELEMENT_DESCRIPTOR data[0];
} PANEL __attribute__ ((__packed__));
typedef struct button
{
unsigned short w;
unsigned short h;
offset actionPos;
offset name;
unsigned long unknown;
unsigned short attr;
offset bitmap1 __attribute__ ((__packed__));
offset bitmap2 __attribute__ ((__packed__));
offset bitmap3 __attribute__ ((__packed__));
offset bitmap4 __attribute__ ((__packed__));
unsigned char inactiveUnselected;
unsigned char inactiveSelected;
unsigned char activeUnselected;
unsigned char activeSelected;
} BUTTON;
typedef struct frame
{
unsigned short w;
unsigned short h;
offset name;
offset bitmap;
unsigned long unknown;
unsigned short attr;
/* Remember that frames can contain other frames and buttons */
unsigned char count; /* number of entries, always (?) == count2 */
unsigned char count2;
ELEMENT_DESCRIPTOR data[0];
} FRAME;
typedef struct element
{
union
{
FRAME def __attribute__ ((__packed__));
BUTTON button __attribute__ ((__packed__));
};
} ELEMENT __attribute__ ((__packed__));
typedef struct bitmap
{
unsigned short size; /* size of the structure in bytes */
unsigned short w;
unsigned short h;
/* The mode word is encoded as follows (I think):
(Bits are numbered 0-7 with bit 0 the MSB, the low order
bits of the mode word don't appear to be used, or may
not even be part of the mode)
0 If on, means the image is run length encoded (RLE)
(appears to only apply to 4 color images)
1-2 Don't know
3-4 The color index of the '1' pixel color in a 2 color
bitmap
5-6 The color index of the '0' pixel color in a 2 color
bitmap
7 The number of bits/pixel - 1 (ie 0 for a 2 color bitmap
and 1 for a 4 color bitmap).
These are all guesses at this point based on experimentation on
various CCF files.
In ths special case where there is no data present for the bitmap,
then the bitmap is completely filled in with the color specified in
bits 3-4 (even for a 4 color bit map since with no data, there's
no other way to figure out the colors).
RLE bitmaps are described in the RLE routine later in this code.
*/
unsigned short mode;
unsigned char data[0];
} BITMAP __attribute__ ((__packed__));
typedef enum action_type
{
ACTION_IR_CODE = 1,
ACTION_BUTTON_ALIAS = 2,
ACTION_PANEL_JUMP = 3,
ACTION_DELAY = 4,
ACTION_KEY_ALIAS = 5,
ACTION_DEVICE_ALIAS = 6,
ACTION_TIMER = 7,
ACTION_BEEP = 8
} ACTION_TYPE;
typedef struct ircode_action
{
unsigned long unused;
offset ir_entry;
} IRCODE_ACTION;
typedef struct button_alias_action
{
offset device;
offset button;
} BUTTON_ALIAS_ACTION;
typedef enum jump_code
{
SCROLL_DOWN_JUMP_CODE = 0xDDDDDDDD,
SCROLL_UP_JUMP_CODE = 0xEEEEEEEE,
MOUSE_MODE_JUMP_CODE = 0xFFFFFFFF
} JUMP_CODE;
typedef struct jump_action
{
offset device;
unsigned long jump_code_or_panel;
} JUMP_ACTION;
typedef struct delay_action
{
unsigned long unused;
unsigned long delay_in_ms;
} DELAY_ACTION;
typedef enum key_alias
{
LEFT_KEY_ALIAS = 0,
RIGHT_KEY_ALIAS = 1,
VOLUME_DOWN_ALIAS = 2,
VOLUME_UP_ALIAS = 3,
CHANNEL_DOWN_ALIAS = 4,
CHANNEL_UP_ALIAS = 5,
MUTE_ALIAS = 6
} KEY_ALIAS;
typedef struct key_alias_action
{
offset device;
KEY_ALIAS key;
} KEY_ALIAS_ACTION;
typedef struct device_alias_action
{
offset device;
unsigned long unused;
} DEVICE_ALIAS_ACTION;
typedef struct timer_action
{
unsigned long unused;
offset timer_entry;
} TIMER_ACTION;
typedef struct beep_action
{
unsigned long unused;
unsigned long value;
} BEEP_ACTION;
typedef struct unknown_action
{
unsigned long p1;
unsigned long p2;
} UNKNOWN_ACTION;
typedef struct action
{
unsigned char type; /* See ACTION_TYPE above */
union
{
IRCODE_ACTION ircode;
BUTTON_ALIAS_ACTION button;
JUMP_ACTION jump;
DELAY_ACTION delay;
KEY_ALIAS_ACTION key;
DEVICE_ALIAS_ACTION device;
TIMER_ACTION timer;
BEEP_ACTION beep;
UNKNOWN_ACTION unknown;
} u __attribute__ ((__packed__));
} ACTION __attribute__ ((__packed__));
typedef struct action_list
{
unsigned char count; /* number of entries, always (?) == count2 */
unsigned char count2;
ACTION data[0] __attribute__ ((__packed__));
} ACTION_LIST __attribute__ ((__packed__));
typedef struct ir_code
{
unsigned short size; /* size of the structure in bytes */
offset name __attribute__ ((__packed__));
unsigned short details; // 0x0000 = pulse-width coding
// 0x0100 = pulse-position coding
// 0x5000 = RC5 (phase modulation)
// 0x5001 = RC5x (phase modulation)
// 0x6000 = RC6 (phase modulation)
unsigned short carrier;
unsigned short onceCounter;
unsigned short repeatCounter;
union
{
struct
{
unsigned short device;
unsigned short command;
} RC5;
struct
{
unsigned short device;
unsigned short command;
unsigned short data;
} RC5x;
struct
{
unsigned short device;
unsigned short command;
} RC6;
struct
{
unsigned short data[0];
} LRN;
};
} IR_CODE __attribute__ ((__packed__));
typedef struct day_time
{
unsigned short day_mask;
unsigned short hours_mins;
} DAY_TIME;
typedef struct timer
{
offset previous;
DAY_TIME start;
DAY_TIME stop;
ACTION start_action __attribute__ ((__packed__));
ACTION stop_action __attribute__ ((__packed__));
} TIMER __attribute__ ((__packed__));
typedef struct
{
unsigned char length;
unsigned char s[1];
} String;
/***
*
***/
unsigned char *gData;
unsigned char *gUse;
long gFileSize;
/* A set of characters used to simulate colors for bitmaps when
making ASCII drawings. */
static unsigned char color_map[4] = {'#', '+', '.', ' '};
/* A required forward reference since we recurse on frames */
static void PrintElement(ELEMENT_DESCRIPTOR *ed);
static inline unsigned char *GetPosPtr(offset pos)
{
return (ntohl(pos) + gData);
}
static HEADER *GetHeader(offset pos)
{
HEADER *p = (HEADER *)(GetPosPtr(pos));
assert(p);
memset(gUse + ntohl(pos), 'H', sizeof(HEADER));
return p;
}
static unsigned short GetCRC(offset pos)
{
unsigned short *crcptr;
memset(gUse + ntohl(pos), '?', sizeof(unsigned short));
crcptr = (unsigned short *) GetPosPtr(pos);
return (ntohs(*crcptr));
}
static inline PANEL *GetPanel(offset pos)
{
PANEL *p = (PANEL *) GetPosPtr(pos);
assert(p);
memset(gUse + ntohl(pos), 'P', sizeof(PANEL) + (9 * p->count));
return p;
}
static inline DEVICE *GetEntry(offset pos)
{
DEVICE *p = (DEVICE *) GetPosPtr(pos);
assert(p);
memset(gUse + ntohl(pos), 'D', 65 /* gcc won't pack the structure */);
return p;
}
static inline ACTION_LIST *GetActionList(offset pos)
{
ACTION_LIST *p = (ACTION_LIST*)(GetPosPtr(pos));
assert(p);
memset(gUse + ntohl(pos),
'L',
sizeof(ACTION_LIST) + (p->count * sizeof(ACTION)));
return p;
}
static inline IR_CODE *GetIRCode(offset pos)
{
IR_CODE *p = (IR_CODE*)(GetPosPtr(pos));
assert(p);
memset(gUse + ntohl(pos), 'I', ntohs(p->size));
return p;
}
static inline ELEMENT *GetElement(offset pos, ELEMENT_TYPE type)
{
ELEMENT *p = (ELEMENT *) GetPosPtr(pos);
assert(p);
if (type == ELEMENT_TYPE_FRAME)
{
FRAME *f = (FRAME *) p;
memset(gUse + ntohl(pos),
'F',
sizeof(FRAME) + (f->count * 9));
}
else
memset(gUse + ntohl(pos), 'B', sizeof(BUTTON));
return p;
}
static inline BITMAP *GetBitmap(offset pos)
{
BITMAP *p = (BITMAP *) GetPosPtr(pos);
assert(p);
memset(gUse + ntohl(pos), 'X', ntohs(p->size));
return p;
}
static inline TIMER *GetTimerEntry(offset pos)
{
TIMER *p = (TIMER*)(GetPosPtr(pos));
assert(p);
memset(gUse + ntohl(pos), 'T', sizeof(TIMER));
return p;
}
static String *GetCCFString(offset pos)
{
static String null_string = {0, {0}};
String *p = (String *) (GetPosPtr(pos));
if (pos == 0)
return &null_string;
assert(p);
memset(gUse + ntohl(pos), 'S', p->length + 1);
return p;
}
static void printfStringAtOffset(char *format, offset at)
{
String *s = GetCCFString(at);
printf(format, s->length, s->length, s->s);
}
static void printStringAtOffset(offset at)
{
printfStringAtOffset("%*.*s", at);
}
static void printDayTime(DAY_TIME *p)
{
static char *days[] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Weekly" };
int i;
for (i = 0; i < 8; i++)
if (ntohs(p->day_mask) & (0x0100 << i))
printf("%s ", days[i]);
printf("%d:%02d\n", ntohs(p->hours_mins) / 60, ntohs(p->hours_mins) % 60);
}
/***
* 2 bit color index => string
***/
static char *GetColor(char color)
{
switch(color & 3)
{
default:
case 0: return "Black";
case 1: return "Dark Gray";
case 2: return "Light Gray";
case 3: return "White";
}
}
/***
* 8 bit font index => string
***/
static char *GetFont(char font)
{
switch(font)
{
case 0: return "<none>";
case 1: return "Pronto 8";
case 2: return "Pronto 10";
case 3: return "Pronto 12";
case 4: return "Pronto 14";
case 5: return "Pronto 16";
case 6: return "Pronto 18";
default:return "Pronto xx";
}
}
static void printColor(char *s, unsigned color)
{
printf("%s Color %s", s, GetColor(color));
}
static void printFont(unsigned font)
{
printf("Font %s\n",GetFont(font));
}
static void printTextAttributes(unsigned font,
unsigned color)
{
printColor("Text", color);
printf(", ");
printFont(font);
}
static void printTextAndBackgroundColors(char *s,
unsigned char attr)
{
printf("%s Text Color %s, Background Color %s\n",
s,
attr >> 4,
attr & 0x0F);
}
/***
*
***/
static void PrintAction(ACTION *si)
{
IR_CODE *irs;
DEVICE *es;
PANEL *ps;
ELEMENT *pss;
switch(si->type)
{
case ACTION_IR_CODE:
{
IR_CODE *irs;
irs = GetIRCode(si->u.ircode.ir_entry);
printfStringAtOffset("[C] %*.*s ", irs->name);
switch(ntohs(irs->details))
{
case 0x0100: // pulse-position coding
printf("(pulse-position coding)\n");
goto cont;
case 0x0000: // learned
printf("(pulse-width coding)\n");
cont:
#if 0
int index;
printf("Carrier: %x\n", irs->carrier);
printf("Once: ");
for(index=0; index < irs->onceCounter*2; index += 2)
printf("%.4x/%.4x ", irs->LRN.data[index], irs->LRN.data[index+1]);
printf("\n");
printf("Repeat: ");
for(int i=0; i < irs->repeatCounter*2; i += 2)
printf("%.4x/%.4x ", irs->LRN.data[i + index], irs->LRN.data[i + index + 1]);
printf("\n");
#endif
break;
case 0x5000:
printf("%d %d\n", ntohs(irs->RC5.device), ntohs(irs->RC5.command));
break;
case 0x5001:
printf("%d %d %d\n",
ntohs(irs->RC5x.device),
ntohs(irs->RC5x.command),
ntohs(irs->RC5x.data));
break;
case 0x6000:
printf("%d %d\n", ntohs(irs->RC6.device), ntohs(irs->RC6.command));
break;
default:
printf("unknown: %x\n", ntohs(irs->details));
printf("Carrier: %x\n", ntohs(irs->carrier));
printf("Once : %x\n", ntohs(irs->onceCounter));
printf("Repeat : %x\n", ntohs(irs->repeatCounter));
break;
}
}
break;
case ACTION_BUTTON_ALIAS:
{
DEVICE *es;
ELEMENT *pss;
es = GetEntry(si->u.button.device);
if (es->name)
printfStringAtOffset("[B] %*.*s - ", es->name);
pss = GetElement(si->u.button.button, ELEMENT_TYPE_BUTTON);
if (pss->button.name)
printStringAtOffset(pss->button.name);
printf("\n");
}
break;
case ACTION_PANEL_JUMP: // Jump (last possible action in a macro)
{
printf("Jump: ");
switch (si->u.jump.jump_code_or_panel)
{
case SCROLL_DOWN_JUMP_CODE:
printf("Scroll down");
break;
case SCROLL_UP_JUMP_CODE:
printf("Scroll up");
break;
case MOUSE_MODE_JUMP_CODE:
printf("Mouse mode");
break;
default:
{
DEVICE *es;
PANEL *ps;
es = GetEntry(si->u.jump.device);
if (es->name)
printfStringAtOffset("%*.*s - ", es->name);
ps = GetPanel(si->u.jump.jump_code_or_panel);
if (ps->name)
printStringAtOffset(ps->name);
printf("\n");
}
break;
}
printf("\n");
}
break;
case ACTION_DELAY:
printf("[D] %.1f sec\n", (float) (ntohl(si->u.delay.delay_in_ms)) / 1000);
break;
case ACTION_KEY_ALIAS:
{
DEVICE *es;
static char *key_names[] =
{
"Left", "Right", "Vol-", "Vol+", "Chan-", "Chan+", "Mute"
};
printf("[K] ");
es = GetEntry(si->u.key.device);
if (es->name)
printfStringAtOffset("%*.*s - ", es->name);
if ( (ntohl(si->u.key.key) >= LEFT_KEY_ALIAS)
&& (ntohl(si->u.key.key) <= MUTE_ALIAS)
)
printf(key_names[ntohl(si->u.key.key)]);
else
printf("Unknown code value %d", ntohl(si->u.key.key));
printf("\n");
}
break;
case ACTION_DEVICE_ALIAS:
{
DEVICE *es;
es = GetEntry(si->u.device.device);
if (es->name)
printfStringAtOffset("[A] %*.*s - ", es->name);
}
break;
case ACTION_TIMER:
{
TIMER *ts;
printf("[T] TimerAction\n");
ts = GetTimerEntry(si->u.timer.timer_entry);
printf("Start: ");
PrintAction(&ts->start_action);
printf(" ");
printDayTime(&ts->start);
printf("Stop : ");
PrintAction(&ts->stop_action);
printf(" ");
printDayTime(&ts->stop);
}
break;
case ACTION_BEEP:
{
unsigned long val = ntohl(si->u.beep.value);
printf("[S] %ld Hz %ld %% %ld0 ms\n", (val >> 8) & 0xFFFF, val & 0xFF, (val >> 24) & 0xFF);
}
break;
default:
printf("unknown Action Type : %d : %ld/%lx\n",
si->type,
si->u.unknown.p1,
si->u.unknown.p2);
break;
}
}
static void PrintActionList(char *name, unsigned char name_length, offset pos)
{
ACTION_LIST *sa = GetActionList(pos);
int i;
printf("%*.*s: ", name_length, name_length, name);
for (i = 0; i < sa->count; i++)
PrintAction(&sa->data[i]);
}
/* Bitmaps in the Pronto can be encoded or unencoded. The only encoded
bitmaps appear to be the ones representing the buttons in the default
macro group. All others are unencoded.
The encoded bitmaps encode pixel runs of more than 4 consecutive pixels
of the same value. Each byte represents an encoded run or just 3
pixel values as follows:
If encoded, the byte looks like this (with bit number 0 as the
most-significant bit of the byte):
Bit 0 = on (means this byte is RLE encoded)
Bits 1-3 run count - 1
Bits 4-5 remainder after dividing run by 4
Bits 6-7 the pixel value (0-3)
If not encoded, bits 0-1 are both 0, and bits 2-3, 4-5 and 6-7
represent the pixel values.
*/
static void PrintRLEBitmap(BITMAP *si)
{
unsigned char *dataPtr = si->data;
unsigned char *s;
unsigned char *mapstart;
unsigned size;
unsigned char *bitMap = "#+. ";
int h;
int i;
mapstart = s = (unsigned char *) malloc(ntohs(si->h) * ntohs(si->w));
assert(s);
size = ntohs(si->size) - 8; /* Number of encoded pixels */
for (i = 0; i < size; i++)
{
if (*dataPtr & 0x80)
{
int count;
count = ((((*dataPtr & 0x70) >> 4) + 1) * 4)
+ ((*dataPtr & 0x0C) >> 2);
memset(s, (*dataPtr & 0x03), count);
s += count;
}
else /* Not encoded */
{
s[0] = (*dataPtr & 0x30) >> 4;
s[1] = (*dataPtr & 0xC0) >> 2;
s[2] = (*dataPtr & 0x03);
s += 3;
}
dataPtr += 1;
}
s = mapstart;
for (h = 0; h < ntohs(si->h); h++)
{
int w;
for (w = 0; w < ntohs(si->w); w++)
putchar(bitMap[*s++]);
printf("\n");
}
free(mapstart);
}
static void PrintNormalBitmap(BITMAP *si)
{
unsigned char bitMap[4];
unsigned char *dataPtr = si->data;
unsigned char mode_byte = (ntohs(si->mode) >> 8);
int bits_per_pixel = (mode_byte & 0x01) + 1;
int bitMask = (1 << bits_per_pixel) - 1;
int h;
if (bits_per_pixel == 1)
{
bitMap[0] = color_map[(mode_byte >> 1) & 0x03];
bitMap[1] = color_map[(mode_byte >> 3) & 0x03];
}
else
{
memcpy(bitMap, color_map, sizeof bitMap);
}
for (h = 0; h < ntohs(si->h); h++)
{
int bitPos = 8;
int w;
for (w = 0; w < ntohs(si->w); w++)
{
if (bitPos == 0)
{
dataPtr += 1;
bitPos = 8 - bits_per_pixel;
}
else
bitPos -= bits_per_pixel;
putchar(bitMap[((*dataPtr) >> bitPos) & bitMask]);
}
dataPtr += 1;
/* Have to figure this out better, but apparently
4 color data scan lines start on even byte boundaries.
(Will 16 color or 256 color ones start on higher
boundaries?) */
if ((bits_per_pixel == 2) && (((unsigned long) dataPtr) & 1))
dataPtr += 1;
printf("\n");
}
}
static void PrintFilledBitmap(BITMAP *si)
{
unsigned char fill_color = color_map[(ntohs(si->mode) >> 11) & 0x03];
int h;
for (h = 0; h < ntohs(si->h); h++)
{
int w;
for (w = 0; w < ntohs(si->w); w++)
putchar(fill_color);
printf("\n");
}
}
static void PrintBitmap(offset pos)
{
BITMAP *si = GetBitmap(pos);
printf("BITMAP(P:0x%X,S:%d,W:%d,H:%d,M:0x%04X)\n",
ntohl(pos),
ntohs(si->size),