From 952e7efdd7b07a83e99ec4a680684020a7adabc8 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 12 Jun 2021 16:13:02 +0900 Subject: pack.c: refactor pack/unpack 'X'. --- mrbgems/mruby-pack/src/pack.c | 40 ++++++++++++---------------------------- 1 file changed, 12 insertions(+), 28 deletions(-) (limited to 'mrbgems/mruby-pack') diff --git a/mrbgems/mruby-pack/src/pack.c b/mrbgems/mruby-pack/src/pack.c index 70852438a..094844eb7 100644 --- a/mrbgems/mruby-pack/src/pack.c +++ b/mrbgems/mruby-pack/src/pack.c @@ -1016,31 +1016,12 @@ pack_x(mrb_state *mrb, mrb_value dst, mrb_int didx, int count) return count; } -static int -unpack_x(mrb_state *mrb, int slen, int count) -{ - if (slen < count) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "x outside of string"); - } - return count; -} - -static int -pack_X(mrb_state *mrb, mrb_value dst, mrb_int didx, int count) -{ - if (count > didx) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "X outside of string"); - } - return count; -} - -static int -unpack_X(mrb_state *mrb, int sidx, int count) +static void +check_x(mrb_state *mrb, int a, int count, char c) { - if (sidx < count) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "X outside of string"); + if (a < count) { + mrb_raisef(mrb, E_ARGUMENT_ERROR, "%c outside of string", c); } - return count; } static void @@ -1318,13 +1299,14 @@ mrb_pack_pack(mrb_state *mrb, mrb_value ary) if (dir == PACK_DIR_INVALID) continue; else if (dir == PACK_DIR_NUL) { - if (count > 0 && ridx > INT_MAX - count) goto overflow; + grow: + if (ridx > INT_MAX - count) goto overflow; ridx += pack_x(mrb, result, ridx, count); continue; } else if (dir == PACK_DIR_BACK) { - if (count > 0 && ridx > INT_MAX - count) goto overflow; - ridx -= pack_X(mrb, result, ridx, count); + check_x(mrb, ridx, count, 'X'); + ridx -= count; continue; } @@ -1436,11 +1418,13 @@ pack_unpack(mrb_state *mrb, mrb_value str, int single) if (dir == PACK_DIR_INVALID) continue; else if (dir == PACK_DIR_NUL) { - srcidx += unpack_x(mrb, srclen-srcidx, count); + check_x(mrb, srclen-srcidx, count, 'x'); + srcidx += count; continue; } else if (dir == PACK_DIR_BACK) { - srcidx -= unpack_X(mrb, srcidx, count); + check_x(mrb, srcidx, count, 'X'); + srcidx -= count; continue; } -- cgit v1.2.3