| 967 | */ |
| 968 | |
| 969 | static int /* O - 0 on success, -1 on error */ |
| 970 | roll_stack(_cups_ps_stack_t *st, /* I - Stack */ |
| 971 | int c, /* I - Number of objects */ |
| 972 | int s) /* I - Amount to shift */ |
| 973 | { |
| 974 | _cups_ps_obj_t *temp; /* Temporary array of objects */ |
| 975 | int n; /* Index into array */ |
| 976 | |
| 977 | |
| 978 | DEBUG_printf(("3roll_stack(st=%p, s=%d, c=%d)", st, s, c)); |
| 979 | |
| 980 | /* |
| 981 | * Range check input... |
| 982 | */ |
| 983 | |
| 984 | if (c < 0) |
| 985 | return (-1); |
| 986 | else if (c == 0) |
| 987 | return (0); |
| 988 | |
| 989 | if ((n = st->num_objs - c) < 0) |
| 990 | return (-1); |
| 991 | |
| 992 | s %= c; |
| 993 | |
| 994 | if (s == 0) |
| 995 | return (0); |
| 996 | |
| 997 | /* |
| 998 | * Copy N objects and move things around... |
| 999 | */ |
| 1000 | |
| 1001 | if (s < 0) |
| 1002 | { |
| 1003 | /* |
| 1004 | * Shift down... |
| 1005 | */ |
| 1006 | |
| 1007 | s = -s; |
| 1008 | |
| 1009 | if ((temp = calloc((size_t)s, sizeof(_cups_ps_obj_t))) == NULL) |
| 1010 | return (-1); |
| 1011 | |
| 1012 | memcpy(temp, st->objs + n, (size_t)s * sizeof(_cups_ps_obj_t)); |
| 1013 | memmove(st->objs + n, st->objs + n + s, (size_t)(c - s) * sizeof(_cups_ps_obj_t)); |
| 1014 | memcpy(st->objs + n + c - s, temp, (size_t)s * sizeof(_cups_ps_obj_t)); |
| 1015 | } |
| 1016 | else |
| 1017 | { |
| 1018 | /* |
| 1019 | * Shift up... |
| 1020 | */ |
| 1021 | |
| 1022 | if ((temp = calloc((size_t)s, sizeof(_cups_ps_obj_t))) == NULL) |
| 1023 | return (-1); |
| 1024 | |
| 1025 | memcpy(temp, st->objs + n + c - s, (size_t)s * sizeof(_cups_ps_obj_t)); |
| 1026 | memmove(st->objs + n + s, st->objs + n, (size_t)(c - s) * sizeof(_cups_ps_obj_t)); |