Kannel: Open Source WAP and SMS gateway  svn-r5335
seewbmp.c File Reference
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  parm
 

Functions

static long get_mbi (FILE *infile)
 
static int skip_mbi (FILE *infile)
 
static int show_image_from_file (char *bmpname, FILE *bmpfile, long width, long height)
 
static void clear_extparms (void)
 
static int new_extparm (char *name, char *value)
 
static void print_extparms (FILE *outfile)
 
static int parse_headers (FILE *bmpfile)
 
static int show_wbmp_from_file (char *bmpname, FILE *bmpfile)
 
int main (int argc, char *argv[])
 

Variables

struct parmextparms = NULL
 

Function Documentation

◆ clear_extparms()

static void clear_extparms ( void  )
static

Definition at line 162 of file seewbmp.c.

References extparms, free(), parm::name, parm::next, and parm::value.

Referenced by parse_headers().

162  {
163  while (extparms) {
164  struct parm *tmp = extparms;
166  free(tmp->name);
167  free(tmp->value);
168  free(tmp);
169  }
170 }
char * name
Definition: seewbmp.c:156
struct parm * extparms
Definition: seewbmp.c:160
void free(void *)
struct parm * next
Definition: seewbmp.c:155
char * value
Definition: seewbmp.c:157
Definition: seewbmp.c:154

◆ get_mbi()

static long get_mbi ( FILE *  infile)
static

Definition at line 98 of file seewbmp.c.

References infile.

Referenced by show_wbmp_from_file().

98  {
99  int c;
100  long result = 0;
101 
102  do {
103  c = getc(infile);
104  if (c < 0) return -1;
105  result = (result << 7) | (c & 0x7f);
106  } while (c & 0x80);
107 
108  return result;
109 }
char * infile
Definition: fakewap.c:247

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 304 of file seewbmp.c.

References show_wbmp_from_file().

304  {
305  int i;
306  /* 1 means an I/O error. No other error values are used yet. */
307  int exitvalue = 0;
308 
309  if (argc > 1) {
310  for (i = 1; i < argc; i++) {
311  FILE *bmpfile;
312 
313  bmpfile = fopen(argv[i], "r");
314  if (bmpfile) {
315  if (show_wbmp_from_file(argv[i], bmpfile) < 0) {
316  /* We've already reported the error */
317  exitvalue = 1;
318  }
319  if (fclose(bmpfile) < 0) {
320  perror(argv[i]);
321  exitvalue = 1;
322  }
323  } else {
324  perror(argv[i]);
325  exitvalue = 1;
326  }
327  if (i < argc - 1) {
328  /* more files follow -- separate them */
329  printf("\n");
330  }
331  }
332  } else {
333  /* No files specified -- read from standard input */
334  if (show_wbmp_from_file("stdin", stdin)) {
335  exitvalue = 1;
336  }
337  }
338 
339  return exitvalue;
340 }
static int show_wbmp_from_file(char *bmpname, FILE *bmpfile)
Definition: seewbmp.c:262

◆ new_extparm()

static int new_extparm ( char *  name,
char *  value 
)
static

Definition at line 174 of file seewbmp.c.

References extparms, malloc(), name, parm::next, and parm::value.

Referenced by parse_headers().

174  {
175  struct parm *new;
176  struct parm *p;
177 
178  /* Construct a new node */
179  new = (struct parm *)malloc(sizeof(struct parm));
180  if (!new)
181  return -1;
182 
183  new->name = name;
184  new->value = value;
185  new->next = NULL;
186 
187  /* Add it to the end of the list. */
188  if (!extparms) {
189  extparms = new;
190  } else {
191  p = extparms;
192  while (p->next) {
193  p = p->next;
194  }
195  p->next = new;
196  }
197 
198  return 0;
199 }
void * malloc(YYSIZE_T)
struct parm * extparms
Definition: seewbmp.c:160
struct parm * next
Definition: seewbmp.c:155
char * value
Definition: seewbmp.c:157
char * name
Definition: smsc_cimd2.c:212
Definition: seewbmp.c:154

◆ parse_headers()

static int parse_headers ( FILE *  bmpfile)
static

Definition at line 209 of file seewbmp.c.

References clear_extparms(), free(), malloc(), name, new_extparm(), skip_mbi(), and parm::value.

Referenced by show_wbmp_from_file().

209  {
210  int c;
211  int exttype;
212 
213  clear_extparms();
214 
215  c = getc(bmpfile);
216  if (c < 0) return -1;
217  if (!(c & 0x80)) {
218  /* No extension headers follow */
219  return 0;
220  }
221  exttype = (c >> 5) & 0x03;
222  /* None of these headers do much at this time, but they
223  * might be meaningful with later specifications */
224  switch (exttype) {
225  case 0:
226  /* All we know of type 0 headers is that
227  * the high bit is a continuation bit.
228  * That makes them exactly like an MBI. */
229  if (skip_mbi(bmpfile) < 0) return -1;
230  break;
231  case 1: case 2:
232  /* We don't know what to do with these */
233  return -1;
234  case 3:
235  /* A sequence of parameter/value combinations */
236  do {
237  int namelen, valuelen;
238  char *name, *value;
239  c = getc(bmpfile);
240  if (c < 0) return -1;
241 
242  namelen = (c >> 4) & 0x07;
243  name = malloc(namelen + 1);
244  if (!name) return -1;
245  if (fread(name, namelen, 1, bmpfile) < (size_t) namelen)
246  return -1;
247 
248  valuelen = c & 0x0f;
249  value = malloc(valuelen + 1);
250  if (!value) { free(name); return -1; }
251  if (fread(value, valuelen, 1, bmpfile) < (size_t) valuelen)
252  return -1;
253 
255  } while (c & 0x80);
256  break;
257  }
258  return 0;
259 }
static void clear_extparms(void)
Definition: seewbmp.c:162
void * malloc(YYSIZE_T)
static int new_extparm(char *name, char *value)
Definition: seewbmp.c:174
void free(void *)
char * value
Definition: seewbmp.c:157
char * name
Definition: smsc_cimd2.c:212
static int skip_mbi(FILE *infile)
Definition: seewbmp.c:115

◆ print_extparms()

static void print_extparms ( FILE *  outfile)
static

Definition at line 201 of file seewbmp.c.

References extparms, parm::name, parm::next, outfile, and parm::value.

Referenced by show_wbmp_from_file().

201  {
202  struct parm *p;
203 
204  for (p = extparms; p; p = p->next) {
205  fprintf(outfile, "%s=%s\n", p->name, p->value);
206  }
207 }
char * outfile
Definition: fakewap.c:248
char * name
Definition: seewbmp.c:156
struct parm * extparms
Definition: seewbmp.c:160
struct parm * next
Definition: seewbmp.c:155
char * value
Definition: seewbmp.c:157
Definition: seewbmp.c:154

◆ show_image_from_file()

static int show_image_from_file ( char *  bmpname,
FILE *  bmpfile,
long  width,
long  height 
)
static

Definition at line 126 of file seewbmp.c.

Referenced by show_wbmp_from_file().

127  {
128  long w, h;
129 
130  for (h = 0; h < height; h++) {
131  /* w is incremented in its inner loop */
132  for (w = 0; w < width; ) {
133  int c;
134  unsigned int bit;
135 
136  c = getc(bmpfile);
137  if (c < 0) {
138  perror(bmpname);
139  return -1;
140  }
141 
142  for (bit = 0x80; bit > 0 && w < width; bit >>= 1, w++) {
143  putc((c & bit) ? '*' : ' ', stdout);
144  }
145  }
146  putc('\n', stdout);
147  }
148 
149  return 0;
150 }

◆ show_wbmp_from_file()

static int show_wbmp_from_file ( char *  bmpname,
FILE *  bmpfile 
)
static

Definition at line 262 of file seewbmp.c.

References get_mbi(), parse_headers(), print_extparms(), and show_image_from_file().

Referenced by main().

262  {
263  long typefield;
264  long width, height;
265 
266  typefield = get_mbi(bmpfile);
267  if (typefield < 0) {
268  perror(bmpname);
269  return -1;
270  }
271 
272  if (parse_headers(bmpfile) < 0) {
273  fprintf(stderr, "%s: format error in headers\n", bmpname);
274  return -1;
275  }
276 
277  width = get_mbi(bmpfile);
278  height = get_mbi(bmpfile);
279  if (width < 0 || height < 0) {
280  fprintf(stderr, "%s: error reading height and width\n",
281  bmpname);
282  return -1;
283  }
284 
285  switch (typefield) {
286  case 0:
287  printf("%s, %ldx%ld B/W bitmap, no compression\n",
288  bmpname, width, height);
289  print_extparms(stdout);
290  if (show_image_from_file(bmpname, bmpfile,
291  width, height) < 0) {
292  return -1;
293  }
294  break;
295  default:
296  fprintf(stderr, "%s: cannot handle level %ld wbmp\n",
297  bmpname, typefield);
298  return -1;
299  }
300 
301  return 0;
302 }
static int show_image_from_file(char *bmpname, FILE *bmpfile, long width, long height)
Definition: seewbmp.c:126
static void print_extparms(FILE *outfile)
Definition: seewbmp.c:201
static long get_mbi(FILE *infile)
Definition: seewbmp.c:98
static int parse_headers(FILE *bmpfile)
Definition: seewbmp.c:209

◆ skip_mbi()

static int skip_mbi ( FILE *  infile)
static

Definition at line 115 of file seewbmp.c.

References infile.

Referenced by parse_headers().

115  {
116  int c;
117 
118  do {
119  c = getc(infile);
120  if (c < 0) return -1;
121  } while (c & 0x80);
122 
123  return 0;
124 }
char * infile
Definition: fakewap.c:247

Variable Documentation

◆ extparms

struct parm* extparms = NULL

Definition at line 160 of file seewbmp.c.

Referenced by clear_extparms(), new_extparm(), and print_extparms().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.