rots-utils: add missing files

master
S.J.R. van Schaik 7 years ago
parent 4bced31393
commit 59a532c6f6
  1. 4
      include/info.h
  2. 103
      source/info.c
  3. 4
      source/pack.c

@ -0,0 +1,4 @@
#pragma once
int do_get_offset(int argc, char *argv[]);
int do_get_size(int argc, char *argv[]);

@ -0,0 +1,103 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <image.h>
#include <info.h>
#include <macros.h>
#include <sign.h>
enum {
OPTION_HELP = 'h',
OPTION_IMAGE = 'i',
};
struct args {
const char *image;
};
static int parse_args(struct args *args, int argc, char *argv[])
{
struct option options[] = {
{ "help", no_argument, NULL, OPTION_HELP },
{ "image", required_argument, 0, OPTION_IMAGE },
{ NULL, 0, 0, 0 },
};
int ret;
while ((ret = getopt_long(argc, (char * const *)argv, "hi:d:k:", options,
NULL)) >= 0) {
switch (ret) {
case OPTION_HELP: return -1;
case OPTION_IMAGE: args->image = optarg; break;
default: break;
}
}
return 0;
}
int do_get_offset(int argc, char *argv[])
{
struct args args;
struct rots_hdr hdr;
FILE *fp;
if (parse_args(&args, argc, argv) < 0) {
fprintf(stderr, "invalid\n");
return -1;
}
if (!(fp = fopen(args.image, "rb"))) {
fprintf(stderr, "error: file '%s' not found.\n", args.image);
return -1;
}
if (rots_read_hdr(fp, &hdr) < 0) {
fprintf(stderr, "error: file '%s' is not a ROTS-image.\n", args.image);
goto err_close_fp;
}
printf("%zu\n", ftell(fp));
fclose(fp);
return 0;
err_close_fp:
fclose(fp);
return -1;
}
int do_get_size(int argc, char *argv[])
{
struct args args;
struct rots_hdr hdr;
FILE *fp;
if (parse_args(&args, argc, argv) < 0) {
fprintf(stderr, "invalid\n");
return -1;
}
if (!(fp = fopen(args.image, "rb"))) {
fprintf(stderr, "error: file '%s' not found.\n", args.image);
return -1;
}
if (rots_read_hdr(fp, &hdr) < 0) {
fprintf(stderr, "error: file '%s' is not a ROTS-image.\n", args.image);
goto err_close_fp;
}
printf("%zu\n", hdr.size);
fclose(fp);
return 0;
err_close_fp:
fclose(fp);
return -1;
}

@ -5,6 +5,7 @@
#include <file.h> #include <file.h>
#include <image.h> #include <image.h>
#include <macros.h>
#include <pack.h> #include <pack.h>
enum { enum {
@ -73,12 +74,13 @@ int do_pack(int argc, char *argv[])
get_file_size(&size, args.input); get_file_size(&size, args.input);
hdr.timestamp = (uint64_t)time(NULL); hdr.timestamp = (uint64_t)time(NULL);
hdr.size = size; hdr.size = size;
printf("%zu\n", hdr.size);
if (rots_write_hdr(output, &hdr) < 0) if (rots_write_hdr(output, &hdr) < 0)
goto err_close_output; goto err_close_output;
while (size) { while (size) {
nbytes = fread(data, sizeof *data, sizeof data, input); nbytes = fread(data, sizeof *data, min(sizeof data, size), input);
if (nbytes == 0) { if (nbytes == 0) {
fprintf(stderr, "unable to read the next chunk\n"); fprintf(stderr, "unable to read the next chunk\n");

Loading…
Cancel
Save