Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
b7c8bf77
Commit
b7c8bf77
authored
Apr 11, 2020
by
dearblue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support `MRB_DISABLE_STDIO` for mruby-pack; ref #4954
parent
63c7ff34
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
112 additions
and
13 deletions
+112
-13
mrbgems/mruby-pack/src/pack.c
mrbgems/mruby-pack/src/pack.c
+112
-13
No files found.
mrbgems/mruby-pack/src/pack.c
View file @
b7c8bf77
...
...
@@ -3,11 +3,6 @@
*/
#include <mruby.h>
#ifdef MRB_DISABLE_STDIO
# error pack/unpack conflicts 'MRB_DISABLE_STDIO' configuration in your 'build_config.rb'
#endif
#include "mruby/error.h"
#include "mruby/array.h"
#include "mruby/class.h"
...
...
@@ -217,6 +212,59 @@ pack_l(mrb_state *mrb, mrb_value o, mrb_value str, mrb_int sidx, unsigned int fl
return
4
;
}
#ifndef MRB_INT64
static
void
u32tostr
(
char
*
buf
,
size_t
len
,
uint32_t
n
)
{
#ifdef MRB_DISABLE_STDIO
char
*
bufend
=
buf
+
len
;
char
*
p
=
bufend
-
1
;
if
(
len
<
1
)
{
return
;
}
*
p
--
=
'\0'
;
len
--
;
if
(
n
>
0
)
{
for
(;
len
>
0
&&
n
>
0
;
len
--
,
n
/=
10
)
{
*
p
--
=
'0'
+
(
n
%
10
);
}
p
++
;
}
else
if
(
len
>
0
)
{
*
p
=
'0'
;
len
--
;
}
memmove
(
buf
,
p
,
bufend
-
p
);
#else
snprintf
(
buf
,
len
,
"%"
PRIu32
,
n
);
#endif
/* MRB_DISABLE_STDIO */
}
static
void
i32tostr
(
char
*
buf
,
size_t
len
,
int32_t
n
)
{
#ifdef MRB_DISABLE_STDIO
if
(
len
<
1
)
{
return
;
}
if
(
n
<
0
)
{
*
buf
++
=
'-'
;
len
--
;
n
=
-
n
;
}
u32tostr
(
buf
,
len
,
(
uint32_t
)
n
);
#else
snprintf
(
buf
,
len
,
"%"
PRId32
,
n
);
#endif
/* MRB_DISABLE_STDIO */
}
#endif
/* MRB_INT64 */
static
int
unpack_l
(
mrb_state
*
mrb
,
const
unsigned
char
*
src
,
int
srclen
,
mrb_value
ary
,
unsigned
int
flags
)
{
...
...
@@ -241,16 +289,16 @@ unpack_l(mrb_state *mrb, const unsigned char *src, int srclen, mrb_value ary, un
int32_t
sl
=
ul
;
#ifndef MRB_INT64
if
(
!
FIXABLE
(
sl
))
{
snprintf
(
msg
,
sizeof
(
msg
),
"cannot unpack to Fixnum: %"
PRId32
,
sl
);
mrb_raise
(
mrb
,
E_RANGE_ERROR
,
msg
);
i32tostr
(
msg
,
sizeof
(
msg
)
,
sl
);
mrb_raise
f
(
mrb
,
E_RANGE_ERROR
,
"cannot unpack to Fixnum: %s"
,
msg
);
}
#endif
n
=
sl
;
}
else
{
#ifndef MRB_INT64
if
(
!
POSFIXABLE
(
ul
))
{
snprintf
(
msg
,
sizeof
(
msg
),
"cannot unpack to Fixnum: %"
PRIu32
,
ul
);
mrb_raise
(
mrb
,
E_RANGE_ERROR
,
msg
);
u32tostr
(
msg
,
sizeof
(
msg
)
,
ul
);
mrb_raise
f
(
mrb
,
E_RANGE_ERROR
,
"cannot unpack to Fixnum: %s"
,
msg
);
}
#endif
n
=
ul
;
...
...
@@ -288,6 +336,57 @@ pack_q(mrb_state *mrb, mrb_value o, mrb_value str, mrb_int sidx, unsigned int fl
return
8
;
}
static
void
u64tostr
(
char
*
buf
,
size_t
len
,
uint64_t
n
)
{
#ifdef MRB_DISABLE_STDIO
char
*
bufend
=
buf
+
len
;
char
*
p
=
bufend
-
1
;
if
(
len
<
1
)
{
return
;
}
*
p
--
=
'\0'
;
len
--
;
if
(
n
>
0
)
{
for
(;
len
>
0
&&
n
>
0
;
len
--
,
n
/=
10
)
{
*
p
--
=
'0'
+
(
n
%
10
);
}
p
++
;
}
else
if
(
len
>
0
)
{
*
p
=
'0'
;
len
--
;
}
memmove
(
buf
,
p
,
bufend
-
p
);
#else
snprintf
(
buf
,
len
,
"%"
PRIu64
,
n
);
#endif
/* MRB_DISABLE_STDIO */
}
static
void
i64tostr
(
char
*
buf
,
size_t
len
,
int64_t
n
)
{
#ifdef MRB_DISABLE_STDIO
if
(
len
<
1
)
{
return
;
}
if
(
n
<
0
)
{
*
buf
++
=
'-'
;
len
--
;
n
=
-
n
;
}
u64tostr
(
buf
,
len
,
(
uint64_t
)
n
);
#else
snprintf
(
buf
,
len
,
"%"
PRId64
,
n
);
#endif
/* MRB_DISABLE_STDIO */
}
static
int
unpack_q
(
mrb_state
*
mrb
,
const
unsigned
char
*
src
,
int
srclen
,
mrb_value
ary
,
unsigned
int
flags
)
{
...
...
@@ -311,14 +410,14 @@ unpack_q(mrb_state *mrb, const unsigned char *src, int srclen, mrb_value ary, un
if
(
flags
&
PACK_FLAG_SIGNED
)
{
int64_t
sll
=
ull
;
if
(
!
FIXABLE
(
sll
))
{
snprintf
(
msg
,
sizeof
(
msg
),
"cannot unpack to Fixnum: %"
PRId64
,
sll
);
mrb_raise
(
mrb
,
E_RANGE_ERROR
,
msg
);
i64tostr
(
msg
,
sizeof
(
msg
)
,
sll
);
mrb_raise
f
(
mrb
,
E_RANGE_ERROR
,
"cannot unpack to Fixnum: %s"
,
msg
);
}
n
=
sll
;
}
else
{
if
(
!
POSFIXABLE
(
ull
))
{
snprintf
(
msg
,
sizeof
(
msg
),
"cannot unpack to Fixnum: %"
PRIu64
,
ull
);
mrb_raise
(
mrb
,
E_RANGE_ERROR
,
msg
);
u64tostr
(
msg
,
sizeof
(
msg
)
,
ull
);
mrb_raise
f
(
mrb
,
E_RANGE_ERROR
,
"cannot unpack to Fixnum: %s"
,
msg
);
}
n
=
ull
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment