Rizin
unix-like reverse engineering framework and cli tools
zip_source_crc.c File Reference
#include <limits.h>
#include <stdlib.h>
#include <zlib.h>
#include "zipint.h"

Go to the source code of this file.

Classes

struct  crc_context
 

Functions

static zip_int64_t crc_read (zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t)
 
zip_source_tzip_source_crc_create (zip_source_t *src, int validate, zip_error_t *error)
 

Function Documentation

◆ crc_read()

static zip_int64_t crc_read ( zip_source_t src,
void *  _ctx,
void *  data,
zip_uint64_t  len,
zip_source_cmd_t  cmd 
)
static

Definition at line 80 of file zip_source_crc.c.

80  {
81  struct crc_context *ctx;
82  zip_int64_t n;
83 
84  ctx = (struct crc_context *)_ctx;
85 
86  switch (cmd) {
87  case ZIP_SOURCE_OPEN:
88  ctx->position = 0;
89  return 0;
90 
91  case ZIP_SOURCE_READ:
92  if ((n = zip_source_read(src, data, len)) < 0) {
94  return -1;
95  }
96 
97  if (n == 0) {
98  if (ctx->crc_position == ctx->position) {
99  ctx->crc_complete = 1;
100  ctx->size = ctx->position;
101 
102  if (ctx->validate) {
103  struct zip_stat st;
104 
105  if (zip_source_stat(src, &st) < 0) {
107  return -1;
108  }
109 
110  if ((st.valid & ZIP_STAT_CRC) && st.crc != ctx->crc) {
112  return -1;
113  }
114  if ((st.valid & ZIP_STAT_SIZE) && st.size != ctx->size) {
115  /* We don't have the index here, but the caller should know which file they are reading from. */
117  return -1;
118  }
119  }
120  }
121  }
122  else if (!ctx->crc_complete && ctx->position <= ctx->crc_position) {
123  zip_uint64_t i, nn;
124 
125  for (i = ctx->crc_position - ctx->position; i < (zip_uint64_t)n; i += nn) {
126  nn = ZIP_MIN(UINT_MAX, (zip_uint64_t)n - i);
127 
128  ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data + i, (uInt)nn);
129  ctx->crc_position += nn;
130  }
131  }
132  ctx->position += (zip_uint64_t)n;
133  return n;
134 
135  case ZIP_SOURCE_CLOSE:
136  return 0;
137 
138  case ZIP_SOURCE_STAT: {
139  zip_stat_t *st;
140 
141  st = (zip_stat_t *)data;
142 
143  if (ctx->crc_complete) {
144  /* TODO: Set comp_size, comp_method, encryption_method?
145  After all, this only works for uncompressed data. */
146  st->size = ctx->size;
147  st->crc = ctx->crc;
148  st->comp_size = ctx->size;
152  }
153  return 0;
154  }
155 
156  case ZIP_SOURCE_ERROR:
157  return zip_error_to_data(&ctx->error, data, len);
158 
159  case ZIP_SOURCE_FREE:
160  free(ctx);
161  return 0;
162 
163  case ZIP_SOURCE_SUPPORTS: {
165 
166  if (mask < 0) {
168  return -1;
169  }
170 
172  }
173 
174  case ZIP_SOURCE_SEEK: {
175  zip_int64_t new_position;
177 
178  if (args == NULL) {
179  return -1;
180  }
181  if (zip_source_seek(src, args->offset, args->whence) < 0 || (new_position = zip_source_tell(src)) < 0) {
183  return -1;
184  }
185 
186  ctx->position = (zip_uint64_t)new_position;
187 
188  return 0;
189  }
190 
191  case ZIP_SOURCE_TELL:
192  return (zip_int64_t)ctx->position;
193 
194  default:
196  return -1;
197  }
198 }
size_t len
Definition: 6502dis.c:15
#define mask()
lzma_index ** i
Definition: index.h:629
lzma_index * src
Definition: index.h:567
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags cmd
Definition: sflib.h:79
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_CM_STORE
Definition: zip.h:149
ZIP_EXTERN zip_int64_t zip_source_read(zip_source_t *_Nonnull, void *_Nonnull, zip_uint64_t)
#define ZIP_STAT_SIZE
Definition: zip.h:292
ZIP_EXTERN int zip_source_seek(zip_source_t *_Nonnull, zip_int64_t, int)
#define ZIP_STAT_COMP_METHOD
Definition: zip.h:296
ZIP_EXTERN zip_int64_t zip_source_tell(zip_source_t *_Nonnull)
ZIP_EXTERN int zip_source_stat(zip_source_t *_Nonnull, zip_stat_t *_Nonnull)
#define ZIP_STAT_ENCRYPTION_METHOD
Definition: zip.h:297
#define ZIP_STAT_COMP_SIZE
Definition: zip.h:293
#define ZIP_EM_NONE
Definition: zip.h:177
#define ZIP_ER_CRC
Definition: zip.h:112
@ ZIP_SOURCE_CLOSE
Definition: zip.h:222
@ ZIP_SOURCE_READ
Definition: zip.h:221
@ ZIP_SOURCE_GET_FILE_ATTRIBUTES
Definition: zip.h:239
@ ZIP_SOURCE_FREE
Definition: zip.h:225
@ ZIP_SOURCE_SEEK
Definition: zip.h:226
@ ZIP_SOURCE_SEEK_WRITE
Definition: zip.h:232
@ ZIP_SOURCE_SUPPORTS
Definition: zip.h:234
@ ZIP_SOURCE_STAT
Definition: zip.h:223
@ ZIP_SOURCE_TELL
Definition: zip.h:227
@ ZIP_SOURCE_OPEN
Definition: zip.h:220
@ ZIP_SOURCE_REMOVE
Definition: zip.h:235
@ ZIP_SOURCE_ROLLBACK_WRITE
Definition: zip.h:230
@ ZIP_SOURCE_TELL_WRITE
Definition: zip.h:233
@ ZIP_SOURCE_BEGIN_WRITE
Definition: zip.h:228
@ ZIP_SOURCE_ERROR
Definition: zip.h:224
@ ZIP_SOURCE_COMMIT_WRITE
Definition: zip.h:229
ZIP_EXTERN zip_int64_t zip_source_make_command_bitmap(zip_source_cmd_t,...)
#define ZIP_ER_OPNOTSUPP
Definition: zip.h:133
#define ZIP_STAT_CRC
Definition: zip.h:295
#define ZIP_ER_INCONS
Definition: zip.h:126
ZIP_EXTERN zip_int64_t zip_error_to_data(const zip_error_t *_Nonnull, void *_Nonnull, zip_uint64_t)
Definition: zip_error.c:141
#define ZIP_SOURCE_GET_ARGS(type, data, len, error)
Definition: zip.h:279
int args
Definition: mipsasm.c:18
int n
Definition: mipsasm.c:19
zip_error_t * error
Definition: zip.h:300
zip_uint16_t encryption_method
Definition: zip.h:309
zip_uint64_t valid
Definition: zip.h:301
zip_uint16_t comp_method
Definition: zip.h:308
zip_uint64_t comp_size
Definition: zip.h:305
zip_uint32_t crc
Definition: zip.h:307
zip_uint64_t size
Definition: zip.h:304
#define UINT_MAX
Definition: md5.h:55
unsigned int uInt
Definition: zconf.h:393
Byte FAR Bytef
Definition: zconf.h:400
void _zip_error_set_from_source(zip_error_t *err, zip_source_t *src)
Definition: zip_error.c:135
zip_int64_t zip_source_supports(zip_source_t *src)
uint64_t zip_uint64_t
Definition: zipconf.h:39
uint32_t zip_uint32_t
Definition: zipconf.h:37
int64_t zip_int64_t
Definition: zipconf.h:38
#define MAKE_DETAIL_WITH_INDEX(error, index)
Definition: zipint.h:207
#define ZIP_ER_DETAIL_INVALID_FILE_LENGTH
Definition: zipint.h:232
#define ZIP_MIN(a, b)
Definition: zipint.h:473
#define MAX_DETAIL_INDEX
Definition: zipint.h:206
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:1063

References _zip_error_set_from_source(), args, cmd, zip_stat::comp_method, zip_stat::comp_size, zip_stat::crc, crc32(), zip_stat::encryption_method, ctx::error, free(), i, len, MAKE_DETAIL_WITH_INDEX, mask, MAX_DETAIL_INDEX, n, NULL, zip_stat::size, src, UINT_MAX, zip_stat::valid, ZIP_CM_STORE, ZIP_EM_NONE, ZIP_ER_CRC, ZIP_ER_DETAIL_INVALID_FILE_LENGTH, ZIP_ER_INCONS, ZIP_ER_OPNOTSUPP, zip_error_set(), zip_error_to_data(), ZIP_MIN, ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_CLOSE, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_GET_ARGS, ZIP_SOURCE_GET_FILE_ATTRIBUTES, zip_source_make_command_bitmap(), ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, zip_source_read(), ZIP_SOURCE_REMOVE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK, zip_source_seek(), ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_STAT, zip_source_stat(), ZIP_SOURCE_SUPPORTS, zip_source_supports(), ZIP_SOURCE_TELL, zip_source_tell(), ZIP_SOURCE_TELL_WRITE, ZIP_STAT_COMP_METHOD, ZIP_STAT_COMP_SIZE, ZIP_STAT_CRC, ZIP_STAT_ENCRYPTION_METHOD, and ZIP_STAT_SIZE.

Referenced by zip_source_crc_create().

◆ zip_source_crc_create()

zip_source_t* zip_source_crc_create ( zip_source_t src,
int  validate,
zip_error_t error 
)

Definition at line 55 of file zip_source_crc.c.

55  {
56  struct crc_context *ctx;
57 
58  if (src == NULL) {
60  return NULL;
61  }
62 
63  if ((ctx = (struct crc_context *)malloc(sizeof(*ctx))) == NULL) {
65  return NULL;
66  }
67 
69  ctx->validate = validate;
70  ctx->crc_complete = 0;
71  ctx->crc_position = 0;
72  ctx->crc = (zip_uint32_t)crc32(0, NULL, 0);
73  ctx->size = 0;
74 
76 }
static int validate(const char *const tag)
Definition: checkTag.c:46
#define ZIP_ER_MEMORY
Definition: zip.h:119
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
#define ZIP_ER_INVAL
Definition: zip.h:123
void * malloc(size_t size)
Definition: malloc.c:123
void error(const char *msg)
Definition: untgz.c:593
static zip_int64_t crc_read(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t)
zip_source_t * zip_source_layered_create(zip_source_t *src, zip_source_layered_callback cb, void *ud, zip_error_t *error)

References crc32(), crc_read(), ctx::error, error(), malloc(), NULL, src, validate(), ZIP_ER_INVAL, ZIP_ER_MEMORY, zip_error_init(), zip_error_set(), and zip_source_layered_create().

Referenced by _zip_source_zip_new(), and add_data().