| 1 | /* |
| 2 | * Copyright (C) 2006 Steve Ratcliffe |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * |
| 14 | * Author: Steve Ratcliffe |
| 15 | * Create date: 07-Dec-2006 |
| 16 | */ |
| 17 | package uk.me.parabola.imgfmt.app; |
| 18 | |
| 19 | import java.io.Closeable; |
| 20 | import java.util.concurrent.atomic.AtomicInteger; |
| 21 | |
| 22 | import uk.me.parabola.imgfmt.ReadFailedException; |
| 23 | |
| 24 | /** |
| 25 | * For reading subfiles from the img. The focus of mkgmap is on writing, |
| 26 | * but some limited reading is needed for several operations. |
| 27 | * |
| 28 | * @author Steve Ratcliffe |
| 29 | */ |
| 30 | public interface ImgFileReader extends Closeable { |
| 31 | |
| 32 | /** |
| 33 | * Get the position. Needed because may not be reflected in the underlying |
| 34 | * file if being buffered. |
| 35 | * |
| 36 | * @return The logical position within the file. |
| 37 | */ |
| 38 | public long position(); |
| 39 | |
| 40 | /** |
| 41 | * Set the position of the file. |
| 42 | * @param pos The new position in the file. |
| 43 | */ |
| 44 | public void position(long pos); |
| 45 | |
| 46 | /** |
| 47 | * Read in a single byte. |
| 48 | * Should not be used for reading numbers, use get1s/u instead. |
| 49 | * @return The byte that was read. |
| 50 | */ |
| 51 | public byte get() throws ReadFailedException; |
| 52 | |
| 53 | /** |
| 54 | * Read in a single byte. |
| 55 | * @return int sign-extended value that was read. |
| 56 | */ |
| 57 | public int get1s() throws ReadFailedException; |
| 58 | |
| 59 | /** |
| 60 | * Read in two bytes in little endian byte order. |
| 61 | * @return int sign-extended value that was read. |
| 62 | */ |
| 63 | public int get2s() throws ReadFailedException; |
| 64 | |
| 65 | /** |
| 66 | * Read in three bytes in little endian byte order. |
| 67 | * @return int sign-extended value that was read. |
| 68 | */ |
| 69 | public int get3s() throws ReadFailedException; |
| 70 | |
| 71 | public int get1u() throws ReadFailedException; |
| 72 | |
| 73 | public int get2u() throws ReadFailedException; |
| 74 | |
| 75 | public int get3u() throws ReadFailedException; |
| 76 | |
| 77 | /** |
| 78 | * Read a variable sized integer. The size is given. |
| 79 | * @param nBytes The size of the integer to read. Must be 1 to 4. |
| 80 | * @return The integer which will not be sign extended. |
| 81 | */ |
| 82 | public int getNu(int nBytes) throws ReadFailedException; |
| 83 | |
| 84 | /** |
| 85 | * Read in a 4 byte signed value. |
| 86 | * @return A 4 byte integer. |
| 87 | */ |
| 88 | public int get4() throws ReadFailedException; |
| 89 | |
| 90 | /** |
| 91 | * Read in an arbitrary length sequence of bytes. |
| 92 | * |
| 93 | * @param len The number of bytes to read. |
| 94 | */ |
| 95 | public byte[] get(int len) throws ReadFailedException; |
| 96 | |
| 97 | /** |
| 98 | * Read a zero terminated string from the file. |
| 99 | * @return A string |
| 100 | * @throws ReadFailedException For failures. |
| 101 | */ |
| 102 | public byte[] getZString() throws ReadFailedException; |
| 103 | |
| 104 | /** |
| 105 | * Read in a string of digits in the compressed base 11 format that is used |
| 106 | * for phone numbers in the POI section. |
| 107 | * @param delimiter This will replace all digit 11 characters. Usually a |
| 108 | * '-' to separate numbers in a telephone. No doubt there is a different |
| 109 | * standard in each country. |
| 110 | * @return A phone number possibly containing the delimiter character. |
| 111 | */ |
| 112 | public String getBase11str(byte firstChar, char delimiter); |
| 113 | |
| 114 | /** |
| 115 | * Get the offset in the GMP file. |
| 116 | * @return the offset, 0 if not a GMP file |
| 117 | */ |
| 118 | default int getGMPOffset() { |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Read varying length integer where first byte also gives number of following bytes. |
| 124 | * See also imgfmt/app/mdr/MdrUtils.writeVarLength() and ./MdrDisplay.printSect17() |
| 125 | * |
| 126 | * @return the length |
| 127 | */ |
| 128 | default int readVarLength(AtomicInteger varLength) { |
| 129 | int peek = get1u(); |
| 130 | if ((peek & 0x1) == 0x1) { |
| 131 | varLength.set(1); |
| 132 | return peek >> 1; |
| 133 | } else if ((peek & 0x3) == 0x2) { |
| 134 | varLength.set(2); |
| 135 | return peek >> 2 | get1u() << 6; |
| 136 | } else if ((peek & 0x7) == 0x4) { |
| 137 | varLength.set(3); |
| 138 | return peek >> 3 | get2u() << 5; |
| 139 | } else { // bottom 3 bits clear so assume: |
| 140 | varLength.set(4); |
| 141 | return peek >> 4 | get3u() << 4; // mkgmap writeVarLength does this |
| 142 | } |
| 143 | } |
| 144 | } |