Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.  * Copyright (c) 2017 rxi
  3.  *
  4.  * This library is free software; you can redistribute it and/or modify it
  5.  * under the terms of the MIT license. See `microtar.c` for details.
  6.  */
  7.  
  8. #ifndef MICROTAR_H
  9. #define MICROTAR_H
  10.  
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19. #define MTAR_VERSION "0.1.0"
  20.  
  21. enum {
  22.   MTAR_ESUCCESS     =  0,
  23.   MTAR_EFAILURE     = -1,
  24.   MTAR_EOPENFAIL    = -2,
  25.   MTAR_EREADFAIL    = -3,
  26.   MTAR_EWRITEFAIL   = -4,
  27.   MTAR_ESEEKFAIL    = -5,
  28.   MTAR_EBADCHKSUM   = -6,
  29.   MTAR_ENULLRECORD  = -7,
  30.   MTAR_ENOTFOUND    = -8
  31. };
  32.  
  33. enum {
  34.   MTAR_TREG   = '0',
  35.   MTAR_TLNK   = '1',
  36.   MTAR_TSYM   = '2',
  37.   MTAR_TCHR   = '3',
  38.   MTAR_TBLK   = '4',
  39.   MTAR_TDIR   = '5',
  40.   MTAR_TFIFO  = '6'
  41. };
  42.  
  43. typedef struct {
  44.   unsigned mode;
  45.   unsigned owner;
  46.   unsigned size;
  47.   unsigned mtime;
  48.   unsigned type;
  49.   char name[100];
  50.   char linkname[100];
  51. } mtar_header_t;
  52.  
  53.  
  54. typedef struct mtar_t mtar_t;
  55.  
  56. #pragma pack(push,1)
  57. struct mtar_t {
  58.   int (*read)(mtar_t *tar, void *data, unsigned size);
  59.   int (*write)(mtar_t *tar, const void *data, unsigned size);
  60.   int (*seek)(mtar_t *tar, unsigned pos);
  61.   int (*close)(mtar_t *tar);
  62.   void *stream;
  63.   unsigned pos;
  64.   unsigned remaining_data;
  65.   unsigned last_header;
  66. };
  67. #pragma pack(pop)
  68.  
  69. const char* mtar_strerror(int err);
  70.  
  71. int mtar_open(mtar_t *tar, const char *filename, const char *mode);
  72. int mtar_close(mtar_t *tar);
  73.  
  74. int mtar_seek(mtar_t *tar, unsigned pos);
  75. int mtar_rewind(mtar_t *tar);
  76. int mtar_next(mtar_t *tar);
  77. int mtar_find(mtar_t *tar, const char *name, mtar_header_t *h);
  78. int mtar_read_header(mtar_t *tar, mtar_header_t *h);
  79. int mtar_read_data(mtar_t *tar, void *ptr, unsigned size);
  80.  
  81. int mtar_write_header(mtar_t *tar, const mtar_header_t *h);
  82. int mtar_write_file_header(mtar_t *tar, const char *name, unsigned size);
  83. int mtar_write_dir_header(mtar_t *tar, const char *name);
  84. int mtar_write_data(mtar_t *tar, const void *data, unsigned size);
  85. int mtar_finalize(mtar_t *tar);
  86.  
  87. #ifdef __cplusplus
  88. }
  89. #endif
  90.  
  91. #endif
  92.