blob: d7b9cbf80abbf62706193868bb03466557108e11 [file] [log] [blame]
Bill Richardson794d4d42011-02-10 19:13:10 -08001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <errno.h>
6#include <fcntl.h>
7#include <stdio.h>
8#include <string.h>
9#include <sys/mman.h>
10#include <sys/stat.h>
11#include <sys/types.h>
12#include <unistd.h>
13
14#include "bmpblk_util.h"
15
16
17// Returns pointer to buffer containing entire file, sets length.
18static void *read_entire_file(const char *filename, size_t *length) {
19 int fd;
20 struct stat sbuf;
21 void *ptr;
22
23 *length = 0; // just in case
24
25 if (0 != stat(filename, &sbuf)) {
26 fprintf(stderr, "Unable to stat %s: %s\n", filename, strerror(errno));
27 return 0;
28 }
29
30 if (!sbuf.st_size) {
31 fprintf(stderr, "File %s is empty\n", filename);
32 return 0;
33 }
34
35 fd = open(filename, O_RDONLY);
36 if (fd < 0) {
37 fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno));
38 return 0;
39 }
40
41 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
42 if (MAP_FAILED == ptr) {
43 fprintf(stderr, "Unable to mmap %s: %s\n", filename, strerror(errno));
44 close(fd);
45 return 0;
46 }
47
48 *length = sbuf.st_size;
49
50 close(fd);
51
52 return ptr;
53}
54
55
56// Reclaims buffer from read_entire_file().
57static void discard_file(void *ptr, size_t length) {
58 munmap(ptr, length);
59}
60
61
62// Show what's inside
63int display_bmpblock(const char *infile) {
64 char *ptr;
65 size_t length = 0;
66 BmpBlockHeader *hdr;
67
68 ptr = (char *)read_entire_file(infile, &length);
69 if (!ptr)
70 return 1;
71
72 if (length < sizeof(BmpBlockHeader)) {
73 fprintf(stderr, "File %s is too small to be a BMPBLOCK\n", infile);
74 discard_file(ptr, length);
75 return 1;
76 }
77
78 if (0 != memcmp(ptr, BMPBLOCK_SIGNATURE, BMPBLOCK_SIGNATURE_SIZE)) {
79 fprintf(stderr, "File %s is not a BMPBLOCK\n", infile);
80 discard_file(ptr, length);
81 return 1;
82 }
83
84 hdr = (BmpBlockHeader *)ptr;
85 printf("%s:\n", infile);
86 printf(" version %d.%d\n", hdr->major_version, hdr->minor_version);
87 printf(" %d screens\n", hdr->number_of_screenlayouts);
88 printf(" %d localizations\n", hdr->number_of_localizations);
89 printf(" %d discrete images\n", hdr->number_of_imageinfos);
90
91 discard_file(ptr, length);
92
93 return 0;
94}
95
96int extract_bmpblock(const char *infile, const char *dirname, int force) {
97 printf("extract parts from %s into %s %s overwriting\n",
98 infile, dirname, force ? "with" : "without");
99 printf("NOT YET IMPLEMENTED\n");
100 return 0;
101}