1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy. 2141cc406Sopenharmony_ci Copyright (C) 1997 Geoffrey T. Dairiki 3141cc406Sopenharmony_ci This file is part of the SANE package. 4141cc406Sopenharmony_ci 5141cc406Sopenharmony_ci This program is free software; you can redistribute it and/or 6141cc406Sopenharmony_ci modify it under the terms of the GNU General Public License as 7141cc406Sopenharmony_ci published by the Free Software Foundation; either version 2 of the 8141cc406Sopenharmony_ci License, or (at your option) any later version. 9141cc406Sopenharmony_ci 10141cc406Sopenharmony_ci This program is distributed in the hope that it will be useful, but 11141cc406Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 12141cc406Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13141cc406Sopenharmony_ci General Public License for more details. 14141cc406Sopenharmony_ci 15141cc406Sopenharmony_ci You should have received a copy of the GNU General Public License 16141cc406Sopenharmony_ci along with this program. If not, see <https://www.gnu.org/licenses/>. 17141cc406Sopenharmony_ci 18141cc406Sopenharmony_ci As a special exception, the authors of SANE give permission for 19141cc406Sopenharmony_ci additional uses of the libraries contained in this release of SANE. 20141cc406Sopenharmony_ci 21141cc406Sopenharmony_ci The exception is that, if you link a SANE library with other files 22141cc406Sopenharmony_ci to produce an executable, this does not by itself cause the 23141cc406Sopenharmony_ci resulting executable to be covered by the GNU General Public 24141cc406Sopenharmony_ci License. Your use of that executable is in no way restricted on 25141cc406Sopenharmony_ci account of linking the SANE library code into it. 26141cc406Sopenharmony_ci 27141cc406Sopenharmony_ci This exception does not, however, invalidate any other reasons why 28141cc406Sopenharmony_ci the executable file might be covered by the GNU General Public 29141cc406Sopenharmony_ci License. 30141cc406Sopenharmony_ci 31141cc406Sopenharmony_ci If you submit changes to SANE to the maintainers to be included in 32141cc406Sopenharmony_ci a subsequent release, you agree by submitting the changes that 33141cc406Sopenharmony_ci those changes may be distributed with this exception intact. 34141cc406Sopenharmony_ci 35141cc406Sopenharmony_ci If you write modifications of your own for SANE, it is your choice 36141cc406Sopenharmony_ci whether to permit this exception to apply to your modifications. 37141cc406Sopenharmony_ci If you do not wish that, delete this exception notice. 38141cc406Sopenharmony_ci 39141cc406Sopenharmony_ci This file is part of a SANE backend for HP Scanners supporting 40141cc406Sopenharmony_ci HP Scanner Control Language (SCL). 41141cc406Sopenharmony_ci*/ 42141cc406Sopenharmony_ci/* 43141cc406Sopenharmony_ci#define STUBS 44141cc406Sopenharmony_ciextern int sanei_debug_hp;*/ 45141cc406Sopenharmony_ci#define DEBUG_DECLARE_ONLY 46141cc406Sopenharmony_ci#include "../include/sane/config.h" 47141cc406Sopenharmony_ci 48141cc406Sopenharmony_ci#include <stdlib.h> 49141cc406Sopenharmony_ci#include <string.h> 50141cc406Sopenharmony_ci#include "../include/lassert.h" 51141cc406Sopenharmony_ci 52141cc406Sopenharmony_ci#include "hp.h" 53141cc406Sopenharmony_ci 54141cc406Sopenharmony_citypedef struct hp_alloc_s Alloc; 55141cc406Sopenharmony_ci 56141cc406Sopenharmony_cistruct hp_alloc_s 57141cc406Sopenharmony_ci{ 58141cc406Sopenharmony_ci Alloc * prev; 59141cc406Sopenharmony_ci Alloc * next; 60141cc406Sopenharmony_ci hp_byte_t buf[1]; 61141cc406Sopenharmony_ci}; 62141cc406Sopenharmony_ci 63141cc406Sopenharmony_cistatic Alloc head[] = {{ head, head, {0} }}; 64141cc406Sopenharmony_ci 65141cc406Sopenharmony_ci#define DATA_OFFSET (head->buf - (hp_byte_t *)head) 66141cc406Sopenharmony_ci#define VOID_TO_ALLOCP(p) ((Alloc *)((hp_byte_t *)(p) - DATA_OFFSET)) 67141cc406Sopenharmony_ci#define ALLOCSIZE(sz) (sz + DATA_OFFSET) 68141cc406Sopenharmony_ci 69141cc406Sopenharmony_ci 70141cc406Sopenharmony_civoid * 71141cc406Sopenharmony_cisanei_hp_alloc (size_t sz) 72141cc406Sopenharmony_ci{ 73141cc406Sopenharmony_ci Alloc * new = malloc(ALLOCSIZE(sz)); 74141cc406Sopenharmony_ci 75141cc406Sopenharmony_ci if (!new) 76141cc406Sopenharmony_ci return 0; 77141cc406Sopenharmony_ci (new->next = head->next)->prev = new; 78141cc406Sopenharmony_ci (new->prev = head)->next = new; 79141cc406Sopenharmony_ci return new->buf; 80141cc406Sopenharmony_ci} 81141cc406Sopenharmony_ci 82141cc406Sopenharmony_civoid * 83141cc406Sopenharmony_cisanei_hp_allocz (size_t sz) 84141cc406Sopenharmony_ci{ 85141cc406Sopenharmony_ci void * new = sanei_hp_alloc(sz); 86141cc406Sopenharmony_ci 87141cc406Sopenharmony_ci if (!new) 88141cc406Sopenharmony_ci return 0; 89141cc406Sopenharmony_ci memset(new, 0, sz); 90141cc406Sopenharmony_ci return new; 91141cc406Sopenharmony_ci} 92141cc406Sopenharmony_ci 93141cc406Sopenharmony_civoid * 94141cc406Sopenharmony_cisanei_hp_memdup (const void * src, size_t sz) 95141cc406Sopenharmony_ci{ 96141cc406Sopenharmony_ci char * new = sanei_hp_alloc(sz); 97141cc406Sopenharmony_ci if (!new) 98141cc406Sopenharmony_ci return 0; 99141cc406Sopenharmony_ci return memcpy(new, src, sz); 100141cc406Sopenharmony_ci} 101141cc406Sopenharmony_ci 102141cc406Sopenharmony_cichar * 103141cc406Sopenharmony_cisanei_hp_strdup (const char * str) 104141cc406Sopenharmony_ci{ 105141cc406Sopenharmony_ci return sanei_hp_memdup(str, strlen(str) + 1); 106141cc406Sopenharmony_ci} 107141cc406Sopenharmony_ci 108141cc406Sopenharmony_civoid * 109141cc406Sopenharmony_cisanei_hp_realloc (void * ptr, size_t sz) 110141cc406Sopenharmony_ci{ 111141cc406Sopenharmony_ci if (ptr) 112141cc406Sopenharmony_ci { 113141cc406Sopenharmony_ci Alloc * old = VOID_TO_ALLOCP(ptr); 114141cc406Sopenharmony_ci Alloc copy = *old; 115141cc406Sopenharmony_ci Alloc * new = realloc(old, ALLOCSIZE(sz)); 116141cc406Sopenharmony_ci if (!new) 117141cc406Sopenharmony_ci return 0; 118141cc406Sopenharmony_ci if (new != old) 119141cc406Sopenharmony_ci (new->prev = copy.prev)->next = (new->next = copy.next)->prev = new; 120141cc406Sopenharmony_ci return new->buf; 121141cc406Sopenharmony_ci } 122141cc406Sopenharmony_ci else 123141cc406Sopenharmony_ci return sanei_hp_alloc(sz); 124141cc406Sopenharmony_ci} 125141cc406Sopenharmony_ci 126141cc406Sopenharmony_civoid 127141cc406Sopenharmony_cisanei_hp_free (void * ptr) 128141cc406Sopenharmony_ci{ 129141cc406Sopenharmony_ci Alloc * old = VOID_TO_ALLOCP(ptr); 130141cc406Sopenharmony_ci 131141cc406Sopenharmony_ci assert(old && old != head); 132141cc406Sopenharmony_ci (old->next->prev = old->prev)->next = old->next; 133141cc406Sopenharmony_ci old->next = old->prev = 0; /* so we can puke on multiple free's */ 134141cc406Sopenharmony_ci free(old); 135141cc406Sopenharmony_ci} 136141cc406Sopenharmony_ci 137141cc406Sopenharmony_civoid 138141cc406Sopenharmony_cisanei_hp_free_all (void) 139141cc406Sopenharmony_ci{ 140141cc406Sopenharmony_ci Alloc * ptr; 141141cc406Sopenharmony_ci Alloc * next; 142141cc406Sopenharmony_ci 143141cc406Sopenharmony_ci for (ptr = head->next; ptr != head; ptr = next) 144141cc406Sopenharmony_ci { 145141cc406Sopenharmony_ci next = ptr->next; 146141cc406Sopenharmony_ci free(ptr); 147141cc406Sopenharmony_ci } 148141cc406Sopenharmony_ci head->next = head->prev = head; 149141cc406Sopenharmony_ci} 150