81a73d5e2b98d539a5a63a24fa4a906f6097bbd7
4 * Copyright (C) 2016 Rafał Miłecki <zajec5@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
21 #if !defined(__BYTE_ORDER)
22 #error "Unknown byte order"
25 #if __BYTE_ORDER == __BIG_ENDIAN
26 #define cpu_to_be32(x) (x)
27 #define be32_to_cpu(x) (x)
28 #define cpu_to_be16(x) (x)
29 #define be16_to_cpu(x) (x)
30 #elif __BYTE_ORDER == __LITTLE_ENDIAN
31 #define cpu_to_be32(x) bswap_32(x)
32 #define be32_to_cpu(x) bswap_32(x)
33 #define cpu_to_be16(x) bswap_16(x)
34 #define be16_to_cpu(x) bswap_16(x)
36 #error "Unsupported endianness"
39 #define SEAMA_MAGIC 0x5ea3a417
41 struct seama_seal_header
{
46 } __attribute__ ((packed
));
48 struct seama_entity_header
{
54 } __attribute__ ((packed
));
59 static inline size_t oseama_min(size_t x
, size_t y
) {
63 /**************************************************
65 **************************************************/
67 static void oseama_info_parse_options(int argc
, char **argv
) {
70 while ((c
= getopt(argc
, argv
, "e:")) != -1) {
73 entity_idx
= atoi(optarg
);
79 static int oseama_info_entities(FILE *seama
) {
80 struct seama_entity_header hdr
;
81 size_t bytes
, metasize
, imagesize
;
87 while ((bytes
= fread(&hdr
, 1, sizeof(hdr
), seama
)) == sizeof(hdr
)) {
88 if (be32_to_cpu(hdr
.magic
) != SEAMA_MAGIC
) {
89 fprintf(stderr
, "Invalid Seama magic: 0x%08x\n", be32_to_cpu(hdr
.magic
));
93 metasize
= be16_to_cpu(hdr
.metasize
);
94 imagesize
= be32_to_cpu(hdr
.imagesize
);
96 if (entity_idx
>= 0 && i
!= entity_idx
) {
97 fseek(seama
, metasize
+ imagesize
, SEEK_CUR
);
102 if (metasize
>= sizeof(buf
)) {
103 fprintf(stderr
, "Too small buffer (%zu B) to read all meta info (%zd B)\n", sizeof(buf
), metasize
);
110 printf("Entity offset:\t%ld\n", ftell(seama
) - sizeof(hdr
));
111 printf("Entity size:\t%zd\n", sizeof(hdr
) + metasize
+ imagesize
);
112 printf("Meta size:\t%zd\n", metasize
);
113 printf("Image size:\t%zd\n", imagesize
);
115 bytes
= fread(buf
, 1, metasize
, seama
);
116 if (bytes
!= metasize
) {
117 fprintf(stderr
, "Couldn't read %zd B of meta\n", metasize
);
122 end
= (char *)&buf
[metasize
- 1];
124 for (tmp
= (char *)buf
; tmp
< end
&& strlen(tmp
); tmp
+= strlen(tmp
) + 1) {
125 printf("Meta entry:\t%s\n", tmp
);
128 fseek(seama
, imagesize
, SEEK_CUR
);
136 static int oseama_info(int argc
, char **argv
) {
138 struct seama_seal_header hdr
;
146 fprintf(stderr
, "No Seama file passed\n");
150 seama_path
= argv
[2];
153 oseama_info_parse_options(argc
, argv
);
155 seama
= fopen(seama_path
, "r");
157 fprintf(stderr
, "Couldn't open %s\n", seama_path
);
162 bytes
= fread(&hdr
, 1, sizeof(hdr
), seama
);
163 if (bytes
!= sizeof(hdr
)) {
164 fprintf(stderr
, "Couldn't read %s header\n", seama_path
);
168 metasize
= be16_to_cpu(hdr
.metasize
);
169 imagesize
= be32_to_cpu(hdr
.imagesize
);
171 if (be32_to_cpu(hdr
.magic
) != SEAMA_MAGIC
) {
172 fprintf(stderr
, "Invalid Seama magic: 0x%08x\n", be32_to_cpu(hdr
.magic
));
177 if (metasize
>= sizeof(buf
)) {
178 fprintf(stderr
, "Too small buffer (%zu B) to read all meta info (%d B)\n", sizeof(buf
), metasize
);
184 fprintf(stderr
, "Invalid Seama image size: 0x%08x (should be 0)\n", imagesize
);
189 bytes
= fread(buf
, 1, metasize
, seama
);
190 if (bytes
!= metasize
) {
191 fprintf(stderr
, "Couldn't read %d B of meta\n", metasize
);
196 if (entity_idx
< 0) {
199 printf("Meta size:\t%d\n", metasize
);
200 printf("Image size:\t%d\n", imagesize
);
202 end
= (char *)&buf
[metasize
- 1];
204 for (tmp
= (char *)buf
; tmp
< end
&& strlen(tmp
); tmp
+= strlen(tmp
) + 1) {
205 printf("Meta entry:\t%s\n", tmp
);
209 oseama_info_entities(seama
);
217 /**************************************************
219 **************************************************/
221 static void usage() {
224 printf("Info about Seama seal (container):\n");
225 printf("\toseama info <file> [options]\n");
226 printf("\t-e\t\t\t\tprint info about specified entity only\n");
229 int main(int argc
, char **argv
) {
231 if (!strcmp(argv
[1], "info"))
232 return oseama_info(argc
, argv
);